如何在 .ebextensions 文件夹中为弹性 beantalk 运行命令行命令?

Posted

技术标签:

【中文标题】如何在 .ebextensions 文件夹中为弹性 beantalk 运行命令行命令?【英文标题】:How to run command line commands in the .ebextensions folder for elastic beanstalk? 【发布时间】:2022-01-13 19:34:05 【问题描述】:

我有一些命令需要在新 Elastic Beanstalk 实例启动时运行。 如何构建.config 文件,以便命令在新实例启动时运行。 这些是我需要运行的命令:

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

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

如何在 .ebextensions 文件夹内名为 setup.config 的文件中运行它? 我想是这样的:

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"

但是,这并没有按预期设置弹性 IP。

更新: 我试过这个:

--- 
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

当我部署环境时,它失败了。它不会部署,但如果我不使用.config 文件,那么它会成功部署。这是日志:

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

我的setup.config 文件的格式有什么问题。我在.ebextensions 文件夹中。

【问题讨论】:

Windows? Linux? 每次重新启动时,还是只是部署新的应用程序版本? 这是 Linux。并创建一个实例。当我使用命令“eb deploy project”部署新版本的实例时 【参考方案1】:

要将这多个命令作为一个运行,您可以执行以下操作:

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

【讨论】:

@mcool 不,它会在您的应用程序部署时运行。对于每次启动,您都必须编写自己的自定义 systemd 单元文件。 @mcool 未创建新实例。只有您在实例上的现有应用程序将被其新版本替换。在这种情况下,您的 .ebextensions 配置文件将运行。要重新启动实例,您必须实际重新启动它,方法与重新启动本地计算机相同。 @mcool 告诉我进展如何。 @mcool 是的,你可以重命名它。

以上是关于如何在 .ebextensions 文件夹中为弹性 beantalk 运行命令行命令?的主要内容,如果未能解决你的问题,请参考以下文章

尝试通过弹性 beantalk 运行救援时忽略 .ebextensions 文件

使用 ebextensions 配置在弹性 beantalk 上安装 ffmpeg

AWS 弹性豆茎。仅在新实例上或手动执行 .ebextensions 命令

如何在 .ebextensions 中获取负载均衡器的 ARN?

如何通过 ebextensions 配置文件添加 nginx 缓存控制标头?

使用配置文件 (.ebextensions) 在自定义 VPC 中为 TCP 直通配置 Elastic Beanstalk 环境的负载均衡器