sreedath
sreedath

Reputation: 63

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?

Upvotes: 4

Views: 6703

Answers (2)

Jason
Jason

Reputation: 2105

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

Upvotes: 12

Evgeniy Kuzmin
Evgeniy Kuzmin

Reputation: 2462

Put "files" to "upload" section as

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

Upvotes: 0

Related Questions