Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a LaunchConfiguration using CloudFormation template which creates a config file?

I want to write a LaunchConfiguration for my AWS stack using CloudFormation template. I have written it like below.

"LaunchConfiguration": {
  "Type": "AWS::AutoScaling::LaunchConfiguration",
  "Metadata" : {
      "AWS::CloudFormation::Init" : {
          "files": {
              "/etc/test.conf": {
                  "content": { "Fn::Join": [ "", [
                                                  "user: root\n",
                                                  "password: password\n"
                  ]]},
                  "mode": "000400",
                  "user": "root",
                  "group": "root"
              }
          }
      }
  },
  "Properties": {
    "ImageId": "ami-*****",
    "InstanceType": "*****",
    "KeyName": "*****",
    "IamInstanceProfile": "*****",
    "InstanceMonitoring": "****",
    "SecurityGroups": [
      {
        "Ref": "SecurityGroup"
      }
    ]
  }
},

The file is not being created in the EC2 instances created. Can anyone help me on this?

like image 821
sreedath Avatar asked Oct 20 '25 20:10

sreedath


1 Answers

You're missing a couple things. First, you need invoke the cfn-init script from the LaunchConfiguration UserData. http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-helper-scripts-reference.html

"UserData" : { "Fn::Base64" : { "Fn::Join" : [ "", [
  "#!/bin/bash -ve\n",

  "# Run cfn-init\n",
  "/opt/aws/bin/cfn-init -v ",
  "         --stack ", { "Ref": "AWS::StackName" },
  "         --resource LaunchConfiguration ",
  "         --region ", { "Ref" : "AWS::Region" }, "\n",

  "# Signal success\n",
  "/opt/aws/bin/cfn-signal -e $? ",
  "         --stack ", { "Ref" : "AWS::StackName" },
  "         --resource AutoScalingGroup ",
  "         --region ", { "Ref" : "AWS::Region" }, "\n"
]]}}

This example also uses cfn-signal to signal success which notifies the Auto Scaling group that the instance bootstrapping was successful. To use this feature, you will also need to add the CreationPolicy to your AutoScalingGroup resource. http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-creationpolicy.html

  "CreationPolicy" : {
    "ResourceSignal" : {
      "Timeout" : "PT10M",
      "Count"   : "1"
    }
  }

Lastly, you are missing the default config wrapper around your Metadata.

  "Metadata" : {
    "AWS::CloudFormation::Init" : {
      "config" : {
        "files": {
          "/etc/test.conf" : {
            "content" : { "Fn::Join": [ "", [
              "user: root\n",
              "password: password\n"
            ]]},
            "mode" : "000400",
            "user" : "root",
            "group" : "root"
          }
        }
      }
    }
  }

You can use something other than config, but you then need to define the configSets attribute. http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-init.html#aws-resource-init-configsets

"AWS::CloudFormation::Init" : {
  "configSets" : {
      "default" : [
        "db-config",
        "app-config"
      ]
  },
  "db-config": {
    "files": {
      ...
    }
  },
  "app-config": {
    ...
  }
}

For more information, this is a detailed overview of bootstrapping instances using CloudFormation. https://s3.amazonaws.com/cloudformation-examples/BoostrappingApplicationsWithAWSCloudFormation.pdf

like image 192
Jason Avatar answered Oct 23 '25 11:10

Jason



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!