gpt4 book ai didi

amazon-ec2 - 通过cft依次初始化ec2

转载 作者:行者123 更新时间:2023-12-03 07:44:41 26 4
gpt4 key购买 nike

我正在尝试创建一个 cft,由于某种原因,我希望通过 cft 顺序初始化 ec2 实例。这可能吗?

最佳答案

您需要结合使用 cfn-init 和 cfn-signal 来协调这一过程。请按照下面的示例操作,它应该可以让您了解如何执行此操作。

Resources:    
Server1:
Type: AWS::EC2::Instance
Properties:
[...]
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
yum update -y aws-cfn-bootstrap # good practice - always do this.
/opt/aws/bin/cfn-init -v -c primary -s ${AWS::StackId} -r Server1 --region ${AWS::Region}
yum -y update
Metadata:
AWS::CloudFormation::Init:
configSets:
primary:
- InstallPreRequisites
- CreateCluster # will be run only in master node
secondary:
- InstallPreRequisites
- JoinCluster # will be run in each secondary node
InstallPreRequisites:
commands:
a-install-app:
command:
"install apps on each server"
b-signal-node-ready-join-cluster:
command: !Join
- ''
- - '/opt/aws/bin/cfn-signal -e 0 '
- Fn::Base64: !Ref AllNodesReadyToJoinClusterWaitHandle
CreateCluster:
commands:
a-create-cluster:
command:
"your commands to create the cluster"
b-signal-cluster-created:
command: !Join
- ''
- - '/opt/aws/bin/cfn-signal -e 0 '
- Fn::Base64: !Ref ClusterCreatedWaitHandle
JoinCluster:
a-wait-cluster-created:
command: !Sub >-
output=$(aws cloudformation describe-stack-resource
--region=${AWS::Region}
--stack-name=${AWS::StackName}
--logical-resource-id=ClusterCreatedWaitCondition
--output=text
--query=StackResourceDetail.ResourceStatus)

while [ "$output" != "CREATE_COMPLETE" ] && [ "$output" != "UPDATE_COMPLETE" ];
do
sleep 10
output=$(aws cloudformation describe-stack-resource
--region=${AWS::Region}
--stack-name=${AWS::StackName}
--logical-resource-id=ClusterCreatedWaitCondition
--output=text
--query=StackResourceDetail.ResourceStatus)
done
waitAfterCompletion: '0'
b-join-cluster:
command:
"your commands to join the cluster"
waitAfterCompletion: '0'

Server2:
Type: AWS::EC2::Instance
Properties:
[...]
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
yum update -y aws-cfn-bootstrap # good practice - always do this.
/opt/aws/bin/cfn-init -v -c secondary -s ${AWS::StackId} -r Server2 --region ${AWS::Region} # Resource was updated here
yum -y update
Metadata:
AWS::CloudFormation::Init:
[...] # exactly same as above... one will be primary, the other secondary, due to UserData cfn-init parameter

Server3:
Type: AWS::EC2::Instance
Properties:
[...]
UserData:
Fn::Base64:
!Sub |
#!/bin/bash
yum update -y aws-cfn-bootstrap # good practice - always do this.
/opt/aws/bin/cfn-init -v -c secondary -s ${AWS::StackId} -r Server3 --region ${AWS::Region} # Resource was updated here
yum -y update
Metadata:
AWS::CloudFormation::Init:
[...]


AllNodesReadyToJoinClusterWaitCondition:
Type: 'AWS::CloudFormation::WaitCondition'
Properties:
Handle: !Ref AllNodesReadyToJoinClusterWaitHandle
Count: 3 # this should match the number of nodes on your cluster
Timeout: '7200'
AllNodesReadyToJoinClusterWaitHandle:
Type: 'AWS::CloudFormation::WaitConditionHandle'

ClusterCreatedWaitCondition:
Type: 'AWS::CloudFormation::WaitCondition'
Properties:
Handle: !Ref ClusterCreatedWaitHandle
Count: 1 # only one node (master) will signal this
Timeout: '7200'
ClusterCreatedWaitHandle:
Type: 'AWS::CloudFormation::WaitConditionHandle'

关于amazon-ec2 - 通过cft依次初始化ec2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53404462/

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