Mahbub Rahman
Mahbub Rahman

Reputation: 1343

AWS Cloudformation : Order of Execution among ConfigSets, UserData, Cloud-Init

I have created a template where I created EC2 instance and I used cfn-init to process the configsets, and in the Userdata section of the instance, I wrote some commands to be executed by cloud-init and some commands to be executed without cloud-init.

I am not sure which commands runs in which sequence? What I mean is, in which order the commands are executed? For example:

  1. Commands in the configsets
  2. Commands in the cloud-init section of the userdata
  3. Commands in the Userdata

Part of my code is below:

UserData:
        Fn::If:
          - DatadogAgentEnabled
          -
            Fn::Base64: !Sub |
              #!/bin/bash -xe
              yum update -y
              yum update -y aws-cfn-bootstrap
              /opt/aws/bin/cfn-init --stack ${AWS::StackName} --resource GhostServer --configsets prepare_instance_with_datadog --region ${AWS::Region}
              /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource GhostServer --region ${AWS::Region}

              #cloud-config <----cloud-init section inside the Userdata
              runcmd:
                - [ sh, -c, "sed 's/api_key:.*/api_key: {DatadogAPIKey}/' /etc/datadog-agent/datadog.yaml.example > /etc/datadog-agent/datadog.yaml" ]
                - systemctl restart datadog-agent

Upvotes: 2

Views: 2291

Answers (1)

SRJ
SRJ

Reputation: 2806

From the AWS documentation

UserData : The user data to make available to the instance. For more information, see Running commands on your Linux instance at launch (Linux) and Adding User Data (Windows). If you are using a command line tool, base64-encoding is performed for you, and you can load the text from a file. Otherwise, you must provide base64-encoded text. User data is limited to 16 KB.

So Basically we define UserData to execute some commands at the launch of our EC2 instance.

Reference : https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html

To Answer your question first of all UserData commands will be executed and all the commands specified in this section will execute sequentially so in your example you invoked cfn helper scripts first and then cloud init thus configsets will be applied first and then cloud init commands will be called.

Inside that UserData section we are invoking cfn helper scripts which reads cloudformation template metadata and executes all the configsets defined under AWS::CloudFormation::Init:

From the AWS Documentation :

The cfn-init helper script reads template metadata from the AWS::CloudFormation::Init key and acts accordingly to:

Fetch and parse metadata from AWS CloudFormation

Install packages

Write files to disk

Enable/disable and start/stop services

Reference : https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html

Upvotes: 2

Related Questions