gpt4 book ai didi

amazon-web-services - Cloudformation 初始化 - 安装 docker

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

我正在尝试使用 AWS Cloudformation init 在某些 ec2 实例上安装 docker 和 docker-compose。

Before trying this I have just used user data successfully

我正在使用这个模板

# Use public Systems Manager Parameter
Parameters:
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
Resources:
host1:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
configSets:
ec2_bootstrap:
- install_docker
# - install_compose
install_docker:
packages:
yum:
docker: []
services:
sysvinit:
docker:
enabled: "true"
ensureRunning: "true"
commands:
docker_for_ec2_user:
command: usermod -G docker ec2-user
# install_compose:
# commands:
# compose_for_ec2_user:
# command:
# - curl -L https://github.com/docker/compose/releases/download/1.20.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# - chmod +x /usr/local/bin/docker-compose
# - ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
CreationPolicy:
ResourceSignal:
Timeout: PT5M
Properties:
InstanceType: t2.micro
KeyName: NVirginia
# Note we use the pseudo parameter AWS::Region
ImageId: !Ref LatestAmiId
AvailabilityZone: !Sub ${AWS::Region}a
Tags:
- Key: "Name"
Value: "host1"
SecurityGroupIds:
- !GetAtt SSHSecurityGroupStack.Outputs.SSHGroupId
UserData:
"Fn::Base64":
!Sub |

#!/bin/bash -xe
# Get the latest CloudFormation package
yum update -y aws-cfn-bootstrap
# Start cfn-init
/opt/aws/bin/cfn-init -s ${AWS::StackId} -r host1 --region ${AWS::Region}a || error_exit 'Failed to run cfn-init'
# Start up the cfn-hup daemon to listen for changes to the EC2 instance metadata
/opt/aws/bin/cfn-hup || error_exit 'Failed to start cfn-hup'
# All done so signal success
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource host1 --region ${AWS::Region}a

## change the hostname
sudo hostname host1
## restart docker
sudo service docker restart


SSHSecurityGroupStack:
Type: AWS::CloudFormation::Stack
Properties:
TemplateURL: https://cloudformation-bruvio-templates.s3.amazonaws.com/ssh-security-group.yaml
TimeoutInMinutes: 5

该模板应该创建一个安全组,其中打开几个端口,然后创建一个 ec2 实例,并使用 init 来安装 docker 和 docker-compose

我实际上从 this post 复制了 init 部分

尝试创建实例时堆栈失败,因为未收到成功信号

我想知道出了什么问题?

最佳答案

您的模板中存在许多问题。其中一些是:

  • 缺少-configsets
  • 区域错误${AWS::StackId}
  • 未定义 hup
  • 缩进问题
  • 缺少error_exit

修复了所有这些问题,现在应该可以工作了:

# Use public Systems Manager Parameter
Parameters:
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
Resources:
host1:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
configSets:
ec2_bootstrap:
- install_docker
# - install_compose
install_docker:
packages:
yum:
docker: []
services:
sysvinit:
docker:
enabled: "true"
ensureRunning: "true"
commands:
docker_for_ec2_user:
command: usermod -G docker ec2-user
# install_compose:
# commands:
# compose_for_ec2_user:
# command:
# - curl -L https://github.com/docker/compose/releases/download/1.20.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
# - chmod +x /usr/local/bin/docker-compose
# - ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
CreationPolicy:
ResourceSignal:
Timeout: PT5M
Properties:
InstanceType: t2.micro
KeyName: NVirginia
# Note we use the pseudo parameter AWS::Region
ImageId: !Ref LatestAmiId
AvailabilityZone: !Sub ${AWS::Region}a
Tags:
- Key: "Name"
Value: "host1"
SecurityGroupIds:
- !GetAtt SSHSecurityGroupStack.Outputs.SSHGroupId
UserData:
"Fn::Base64":
!Sub |
#!/bin/bash -xe
# Get the latest CloudFormation package
yum update -y aws-cfn-bootstrap
# Start cfn-init
/opt/aws/bin/cfn-init -s ${AWS::StackId} -r host1 --configsets ec2_bootstrap --region ${AWS::Region}
# All done so signal success
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource host1 --region ${AWS::Region}




关于amazon-web-services - Cloudformation 初始化 - 安装 docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66285318/

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