通过cft顺序初始化ec2

时间:2018-11-21 02:22:46

标签: amazon-ec2 ansible amazon-cloudformation aws-cli

我试图创建一个cft,由于某种原因,我想通过cft顺序初始化ec2实例。 甚至有可能吗?

1 个答案:

答案 0 :(得分:0)

您将需要使用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'