gpt4 book ai didi

bash - 让 CloudFormation 等待用户数据

转载 作者:行者123 更新时间:2023-12-03 23:47:31 25 4
gpt4 key购买 nike

我有一个cloudformation堆栈,它创建一个EC2实例并使用UserData在其中安装一些东西。创建基于 RedHat 的 EC2 实例后,Cloudformation 立即报告 CREATE_COMPLETE。但此时,该实例还不能真正使用,因为用户数据需要大约 40 分钟才能完成。我阅读了文档,甚至尝试了 cfn-signal 但无法成功执行它。

有人可以告诉我具体该怎么做吗?

EC2Instance:
Type: AWS::EC2::Instance
Properties:
CreditSpecification:
CPUCredits: standard
IamInstanceProfile:
Fn::ImportValue:
!Sub ${InstanceProfileStackName}-instanceProfile
ImageId: !Ref ImageId
InstanceInitiatedShutdownBehavior: stop
InstanceType: !Ref InstanceType
SubnetId: !Ref SubnetId
SecurityGroupIds:
- !Ref DefaultSecurityGroup
- !Ref WebSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash
set -e
yum update -y

以上是我的 Cloudformation 模板的截断部分。

更新

我的脚本包含以下行

source scl_source enable rh-python36

我的实例的默认值是 python2.7,但我必须使用 python3.6 安装我的 pip 包。我不确定这是否导致 cfn 信号失败。

脚本一直进行到最后一步,但似乎在那里失败了。我正在从 EC2 IP 创建记录集,但 Cloudformation 仍然认为 EC2 实例尚未完成并等待超时。

实例快照截图 This is the screenshot from the image snapshot from EC2 console

日志文件结尾如下 This the end of my log file

我的日志文件名为/var/log/cloud-init.log。该目录中没有 cloud-init-output.log。

最佳答案

您需要两个组件:

因此,您的模板可以针对 Redhat 8 进行如下修改:

EC2Instance:
Type: AWS::EC2::Instance

CreationPolicy: # <--- creation policy with timeout of 5 minutes
ResourceSignal:
Timeout: PT5M

Properties:
CreditSpecification:
CPUCredits: standard
IamInstanceProfile:
Fn::ImportValue:
!Sub ${InstanceProfileStackName}-instanceProfile
ImageId: !Ref ImageId
InstanceInitiatedShutdownBehavior: stop
InstanceType: !Ref InstanceType
SubnetId: !Ref SubnetId
SecurityGroupIds:
- !Ref DefaultSecurityGroup
- !Ref WebSecurityGroup
UserData:
Fn::Base64: !Sub |
#!/bin/bash -x

yum update -y

yum -y install python2-pip

pip2 install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz

python2 /usr/bin/cfn-signal -e $? \
--stack ${AWS::StackName} \
--resource EC2Instance \
--region ${AWS::Region}

对于调试,由于用户数据可能会出错,必须登录实例并检查/var/log/cloud-init-output.log文件

关于bash - 让 CloudFormation 等待用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61740024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com