How can I be sure that the AWS CodeDeploy agent is installed and running on Amazon Elastic Compute Cloud (Amazon EC2) instances when I'm provisioning my infrastructure and CodeDeploy resources using AWS CloudFormation?

If you create an AWS::EC2::Instance resource or an AWS::AutoScaling::AutoScalingGroup resource as the target of a deployment with CodeDeploy, then use the cfn-init and cfn-signal helper scripts. Before you provision your infrastructure deployment, you can use the helper scripts to fully provision your target instances with the CodeDeploy agent.

To use the cfn-init and cfn-signal helper scripts to deploy the CodeDeploy agent, complete the following steps to correctly configure the AWS CloudFormation template.

1.    In a code editor, create a JSON or YAML file to use as the AWS CloudFormation template for your resources.

2.    For the Metadata key, use the AWS::CloudFormation::Init type to include metadata for your instance or launch configuration. See the following example:  

"SampleLinuxInstance" : {
            "Type" : "AWS::EC2::Instance",
            "Metadata" : {
                "AWS::CloudFormation::Init" : {
                    "config" : {
                        "packages" : {
                            "yum" : {
                                "ruby" : []
                            }
                        },
                        "files" : {
                            "/home/ec2-user/install" : {
                                "source" : {"Fn::Join" : ["", ["https://aws-codedeploy-", {"Ref" : "AWS::Region"}, ".s3.amazonaws.com/latest/install"]]},
                                "mode" : "000755"
                            }
                        },
                        "commands" : {
                            "00-install-agent" : {
                                "command" : "./install auto",
                                "cwd" : "/home/ec2-user/"
                            },
                            "01-cfn-signal" : {
                                "command" : {"Fn::Join" : ["", ["/opt/aws/bin/cfn-signal -e 0 --stack ", { "Ref": "AWS::StackName" }, " --resource SampleLinuxInstance --region ", { "Ref" : "AWS::Region" }]]}
                            }
                        }
                    }
                }
            },
            "Properties" : {
                ...
            }
        }

Note: The AWS::CloudFormation::Init type includes installation steps for the CodeDeploy agent and the command to signal back to the AWS CloudFormation stack after you complete the following steps.

3.    Set the UserData property to invoke the cfn-init script. See the following example:

"Properties" : {
                "InstanceType" : "t2.micro",
                "ImageId" : {"Ref" : "AMI"},
                "IamInstanceProfile" : {"Ref" : "SampleInstanceProfile"},
                "KeyName" : {"Ref" : "SSHKey"},
                "UserData" :  {"Fn::Base64" : {
                    "Fn::Join" : ["", [
                        "#!/bin/bash\n",
                        "yum -y update\n",
                        "yum -y install cfn-bootstrap\n",
                        "/opt/aws/bin/cfn-init -v",
                        " --stack ", { "Ref" : "AWS::StackName" },
                        " --resource SampleLinuxInstance",
                        " --region ", { "Ref" : "AWS::Region" }, 
                        "\n"
                    ]]
                }},
                "Tags" : [
                    {
                        "Key" : {"Ref" : "InstanceTagKey"},
                        "Value" : {"Ref" : "InstanceTagValue"}
                    }
                ]
            }

4.    For the instance resource, use the CreationPolicy attribute to require the Amazon EC2 instance or Amazon EC2 Auto Scaling group to send success signals to AWS CloudFormation within a specified time period. See the following example:

"SampleLinuxInstance" : {
            "Type" : "AWS::EC2::Instance",
            "CreationPolicy" : {
                "ResourceSignal" : {
                    "Count" : "1",
                    "Timeout" : "PT5M"
                }
            },

5.    To create an explicit dependency for your deployment group resource on the Amazon EC2 instance or Amazon EC2 Auto Scaling group, use the DependsOn attribute. See the following example:

"SampleLinuxCDDeploymentGroup" : {
            "Type" : "AWS::CodeDeploy::DeploymentGroup",
            "DependsOn" : "SampleLinuxInstance",
            "Properties" : {
                "ApplicationName" : {"Ref" : "SampleCDApp"},
                "ServiceRoleArn" : {"Ref" : "ServiceRole"},
                "Ec2TagFilters" : [
                    {
                        "Key" : {"Ref" : "InstanceTagKey"},
                        "Value" : {"Ref" : "InstanceTagValue"},
                        "Type" : "KEY_AND_VALUE"
                    }
                ],
                "Deployment" : {
                    "Revision" : {
                        "RevisionType" : "S3",
                        "S3Location" : {
                            "Bucket" : {"Ref" : "BucketName"},
                            "BundleType" : {"Ref" : "BundleType"},
                            "Key" : {"Ref" : "BundleKey"}
                        }
                    }
                }
            }
        }

6.    To complete the AWS CloudFormation template, include any other additional resources, properties, or outputs that you require for your environment.

7.    Use the completed template to create an AWS CloudFormation stack.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2018-12-26