Home > Software design >  How to run command line commands in the .ebextensions folder for elastic beanstalk?
How to run command line commands in the .ebextensions folder for elastic beanstalk?

Time:12-10

I have a few commands that I need to run on start-up of a new Elastic Beanstalk instance. How do I structure the .config file so that the commands run on boot-up of a new instance. These are the comamnds that I need ran:

INSTANCE_ID=$(ec2-metadata -i)
words=( $INSTANCE_ID )
EC2_ID="${words[1]}"

aws ec2 associate-address --instance-id $EC2_ID --allocation-id eipalloc-16c1ecdcesrxx

How do I run this in my file called setup.config inside the .ebextensions folder? I figure something like this:

container_commands:
  00bootup:
    command: "INSTANCE_ID=$(ec2-metadata -i)"
    command: "words=( $INSTANCE_ID )"
    command: "EC2_ID="${words[1]}""
    command: "aws ec2 associate-address --instance-id $EC2_ID --allocation-id eipalloc-16c1ecdcesrxx"

However, this is not setting the elastic IP as expected.

Update: I tried this:

--- 
container_commands: 
  00setup: 
    command: |-
        export AWS_DEFAULT_REGION=US-EAST-1    
        export AWS_ACCESS_KEY_ID=MY_KEY
        export AWS_SECRET_ACCESS_KEY=MY_SECRET_KEY
        INSTANCE_ID=$(ec2-metadata -i)
        words=( $INSTANCE_ID )
        EC2_ID="${words[1]}"
        aws ec2 associate-address --instance-id $EC2_ID --allocation-id eipalloc-06cXXXXXXXXXX

And when I deployed the environment, it fails. It will not deploy, but if I do not use the .config file then it deploys successfully. Here is the log:

2021/12/09 11:55:01.043643 [INFO] Executing instruction: configure X-Ray
2021/12/09 11:55:01.043652 [INFO] X-Ray is not enabled.
2021/12/09 11:55:01.043657 [INFO] Executing instruction: configure proxy Nginx
2021/12/09 11:55:01.070732 [INFO] Executing instruction: configure healthd specific proxy conf
2021/12/09 11:55:01.073824 [INFO] Running command /bin/sh -c systemctl show -p PartOf healthd.service
2021/12/09 11:55:01.400439 [INFO] Running command /bin/sh -c systemctl daemon-reload
2021/12/09 11:55:02.065983 [INFO] Running command /bin/sh -c systemctl reset-failed
2021/12/09 11:55:02.070668 [INFO] Running command /bin/sh -c systemctl is-enabled aws-eb.target
2021/12/09 11:55:02.090250 [INFO] Running command /bin/sh -c systemctl enable aws-eb.target
2021/12/09 11:55:02.166046 [INFO] Running command /bin/sh -c systemctl start aws-eb.target
2021/12/09 11:55:02.184403 [INFO] Running command /bin/sh -c systemctl enable healthd.service
2021/12/09 11:55:02.258453 [INFO] Running command /bin/sh -c systemctl show -p PartOf healthd.service
2021/12/09 11:55:02.270566 [INFO] Running command /bin/sh -c systemctl is-active healthd.service
2021/12/09 11:55:02.274378 [INFO] Running command /bin/sh -c systemctl start healthd.service
2021/12/09 11:55:02.320050 [INFO] Copying file /opt/elasticbeanstalk/config/private/healthd/healthd_logformat.conf to /var/proxy/staging/nginx/conf.d/healthd_logformat.conf
2021/12/09 11:55:02.322053 [INFO] Copying file /opt/elasticbeanstalk/config/private/healthd/healthd_nginx.conf to /var/proxy/staging/nginx/conf.d/elasticbeanstalk/healthd.conf
2021/12/09 11:55:02.325036 [INFO] Executing instruction: configure log streaming
2021/12/09 11:55:02.325048 [INFO] log streaming is not enabled
2021/12/09 11:55:02.325052 [INFO] disable log stream
2021/12/09 11:55:02.325066 [INFO] Running command /bin/sh -c systemctl show -p PartOf amazon-cloudwatch-agent.service
2021/12/09 11:55:02.340721 [INFO] Running command /bin/sh -c systemctl stop amazon-cloudwatch-agent.service
2021/12/09 11:55:02.345794 [INFO] Executing instruction: GetToggleForceRotate
2021/12/09 11:55:02.345807 [INFO] Checking if logs need forced rotation
2021/12/09 11:55:02.345844 [INFO] Running command /bin/sh -c /opt/aws/bin/cfn-get-metadata -s arn:aws:cloudformation:us-east-1:796071762232:stack/awseb-e-8eps8ub4a3-stack/1f9bcf70-58e6-11ec-8466-127df4d38a9f -r AWSEBAutoScalingGroup --region us-east-1
2021/12/09 11:55:04.975344 [INFO] Running command /bin/sh -c /opt/aws/bin/cfn-get-metadata -s arn:aws:cloudformation:us-east-1:796071762232:stack/awseb-e-8eps8ub4a3-stack/1f9bcf70-58e6-11ec-8466-127df4d38a9f -r AWSEBBeanstalkMetadata --region us-east-1
2021/12/09 11:55:05.364102 [INFO] Procfile not found. Generating default rsyslog config
2021/12/09 11:55:05.366813 [INFO] Running command /bin/sh -c systemctl restart rsyslog.service
2021/12/09 11:55:06.345757 [INFO] Executing instruction: Configure Docker Logging
2021/12/09 11:55:06.365153 [INFO] Executing instruction: PostBuildEbExtension
2021/12/09 11:55:06.365179 [INFO] Starting executing the config set Infra-EmbeddedPostBuild.
2021/12/09 11:55:06.365193 [INFO] Running command /bin/sh -c /opt/aws/bin/cfn-init -s arn:aws:cloudformation:us-east-1:796071762232:stack/awseb-e-8eps8ub4a3-stack/1f9bcf70-58e6-11ec-8466-127df4d38a9f -r AWSEBAutoScalingGroup --region us-east-1 --configsets Infra-EmbeddedPostBuild
2021/12/09 11:55:20.596390 [ERROR] An error occurred during execution of command [app-deploy] - [PostBuildEbExtension]. Stop running the command. Error: Container commands build failed. Please refer to /var/log/cfn-init.log for more details. 

2021/12/09 11:55:20.596410 [INFO] Executing cleanup logic
2021/12/09 11:55:20.596519 [INFO] CommandService Response: {"status":"FAILURE","api_version":"1.0","results":[{"status":"FAILURE","msg":"Engine execution has encountered an error.","returncode":1,"events":[{"msg":"Instance deployment failed. For details, see 'eb-engine.log'.","timestamp":1639050920,"severity":"ERROR"}]}]}

2021/12/09 11:55:20.597712 [INFO] Platform Engine finished execution on command: app-deploy

2021/12/09 11:56:44.993323 [INFO] Starting...
2021/12/09 11:56:44.993370 [INFO] Starting EBPlatform-PlatformEngine
2021/12/09 11:56:44.994105 [INFO] reading event message file
2021/12/09 11:56:45.002909 [INFO] no eb envtier info file found, skip loading env tier info.
2021/12/09 11:56:45.002987 [INFO] Engine received EB command cfn-hup-exec

2021/12/09 11:56:45.109117 [INFO] Running command /bin/sh -c /opt/aws/bin/cfn-get-metadata -s arn:aws:cloudformation:us-east-1:796071762232:stack/awseb-e-8eps8ub4a3-stack/1f9bcf70-58e6-11ec-8466-127df4d38a9f -r AWSEBAutoScalingGroup --region us-east-1
2021/12/09 11:56:45.596582 [INFO] Running command /bin/sh -c /opt/aws/bin/cfn-get-metadata -s arn:aws:cloudformation:us-east-1:796071762232:stack/awseb-e-8eps8ub4a3-stack/1f9bcf70-58e6-11ec-8466-127df4d38a9f -r AWSEBBeanstalkMetadata --region us-east-1
2021/12/09 11:56:45.956533 [INFO] checking whether command tail-log is applicable to this instance...
2021/12/09 11:56:45.956550 [INFO] this command is applicable to the instance, thus instance should execute command
2021/12/09 11:56:45.956555 [INFO] Engine command: (tail-log)

2021/12/09 11:56:45.957863 [INFO] Executing instruction: GetTailLogs
2021/12/09 11:56:45.957872 [INFO] Tail Logs...
2021/12/09 11:56:45.963685 [INFO] Running command /bin/sh -c tail -n 100 /var/log/eb-engine.log

What is wrong with the format of my setup.config file. I have it inside the .ebextensions folder.

CodePudding user response:

To run those multiple commands as one, you can do the following:

container_commands:
  00bootup:
    command: |
       INSTANCE_ID=$(ec2-metadata -i)
       words=( $INSTANCE_ID )
       EC2_ID="${words[1]}"
       aws ec2 associate-address --instance-id $EC2_ID --allocation-id eipalloc-16c1ecdcesrxx
  • Related