1

I have a PHP website configured within Elastic Beanstalk on two environments, a production environment and a stage environment. Within each environment I have an APPLICATION_ENV environment variable set which identifies to the code which environment it's running on. I don't want my stage website to be accessible to the public and I wanted to place a htpasswd on this environment but not on the production one.

To try to achieve this I have created a configuration within .ebextensions. The entire .ebextensions folder has three files:

  1. 01stage.config

    container_commands:
      command-01:
        command: "/bin/bash .ebextensions/set_stage_htpasswd.sh"
        leader_only: true
    
  2. set_stage_htpasswd.sh

    #!/bin/bash
    if [ "$APPLICATION_ENV" == "stage" ]; then
      cp /var/app/current/.ebextensions/.htpasswd /var/app/current/ 
    fi
    
  3. .htpasswd

    my_username:my_password
    

I want Elastic Beanstalk to run the config file 01stage.config which will in turn run the shell script set_stage_htpasswd.sh to copy the .htpasswd file to a specific location.

However when I try to deploy this:

  1. I get an error message in the Events log:

    Instance: i-2f795c6e Module: AWSEBAutoScalingGroup ConfigSet: null Command failed on instance. Return code: 1 Output: Error occurred during build: Command command-01 failed .

  2. Elastic Beanstalk informs me that it has deployed the application and that it's running the new version but it hasn't actually deployed the new code and is still running the previous one.

Is this is right way to try and achieve this or is there a better alternative.

Adrian Walls
  • 852
  • 3
  • 19
  • 31

1 Answers1

1

I think the quotes on your command: line is giving you problems. I wouldn't be surprised if the shell is looking for a very strange command... But even if that's ok, it's probably easier to use the test: directive to conditionally execute the file copy operations you want. Try something along the lines:

container_commands:
    command-01: cp .ebextensions/.htpasswd /var/app/current
    test: "[ .$APPLICATION_ENV. = .stage. ]"

(Environment variables are available to container_commands:, but I'm not sure -- and you'll need to verify -- that the they are also available in the test: part of the section.)

Community
  • 1
  • 1
joker
  • 746
  • 4
  • 11