Create an Amazon EC2 instance for CodeDeploy (Amazon CloudFormation template) - Amazon CodeDeploy
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China.

Create an Amazon EC2 instance for CodeDeploy (Amazon CloudFormation template)

You can use our Amazon CloudFormation template to quickly launch an Amazon EC2 instance running Amazon Linux or Windows Server. You can use the Amazon CLI, the CodeDeploy console, or the Amazon APIs to launch the instance with the template. In addition to launching the instance, the template does the following:

  • Instructs Amazon CloudFormation to give the instance permission to participate in CodeDeploy deployments.

  • Tags the instance so CodeDeploy can find it during a deployment.

  • Installs and runs the CodeDeploy agent on the instance.

You don't have to use our Amazon CloudFormation to set up an Amazon EC2 instance. For alternatives, see Working with instances for CodeDeploy.

We do not provide an Amazon CloudFormation template for Amazon EC2 instances running Ubuntu Server or Red Hat Enterprise Linux (RHEL).

Before you begin

Before you can use the Amazon CloudFormation template to launch Amazon EC2 instances, make sure you complete the following steps.

  1. Make sure you have completed the steps in Step 3: Provision an IAM user. Double-check that the policy assigned to the IAM user has the following minimum permissions and add any that are not present:

    • cloudformation:*

    • codedeploy:*

    • ec2:*

    • iam:AddRoleToInstanceProfile

    • iam:CreateInstanceProfile

    • iam:CreateRole

    • iam:DeleteInstanceProfile

    • iam:DeleteRole

    • iam:DeleteRolePolicy

    • iam:GetRole

    • iam:DeleteRolePolicy

    • iam:PutRolePolicy

    • iam:RemoveRoleFromInstanceProfile

  2. Make sure you have an instance key pair to enable SSH access to the Amazon EC2 instance running Amazon Linux or RDP access to the instance running Windows Server.

    To find a key pair name, open the Amazon EC2 console at https://console.amazonaws.cn/ec2. In the navigation pane, under Network & Security, choose Key Pairs, and note the key pair name in the list.

    To generate a new key pair, see Creating your key pair using Amazon EC2. Be sure the key pair is created in one of the regions listed in Region and endpoints in Amazon General Reference. Otherwise, you can't use the instance key pair with CodeDeploy.

Launch an Amazon EC2 instance with the Amazon CloudFormation template (console)

  1. Sign in to the Amazon Web Services Management Console and open the Amazon CloudFormation console at https://console.amazonaws.cn/cloudformation.

    Important

    Sign in to the Amazon Web Services Management Console with the same account you used in Getting started with CodeDeploy. On the navigation bar, in the region selector, choose one of the regions listed in Region and endpoints in Amazon General Reference. CodeDeploy supports these regions only.

  2. Choose Create Stack.

  3. In Choose a template, choose Specify an Amazon S3 template URL. In the box, type the location of the Amazon CloudFormation template for your region, and then choose Next.

    For the China (Beijing) Region:

    • https://s3.cn-north-1.amazonaws.com.cn/aws-codedeploy-cn-north-1/templates/latest/CodeDeploy_SampleCF_Template.json (for the China (Beijing) region)

  4. In the Stack name box, type a name for the stack (for example, CodeDeployDemoStack).

  5. In Parameters, type the following, and then choose Next.

    • For InstanceCount, type the number of instances you want to launch. (We recommend you leave the default of 1.)

    • For InstanceType, type the instance type you want to launch (or leave the default of t1.micro).

    • For KeyPairName, type the instance key pair name. Type the key pair name only, not the key pair file extension.

    • For OperatingSystem box, type Windows to launch instances running Windows Server (or leave the default of Linux).

    • For SSHLocation, type the IP address range to use for connecting to the instance with SSH or RDP (or leave the default of 0.0.0.0/0).

      Important

      The default of 0.0.0.0/0 is provided for demonstration purposes only. CodeDeploy does not require Amazon EC2 instances to have unrestricted access to ports. As a best practice, we recommend restricting access to SSH (and HTTP) ports.

    • For TagKey, type the instance tag key CodeDeploy will use to identify the instances during deployment (or leave the default of Name).

    • For TagValue, type the instance tag value CodeDeploy will use to identify the instances during deployment (or leave the default of CodeDeployDemo).

  6. On the Options page, leave the option boxes blank, and choose Next.

    Important

    Amazon CloudFormation tags are different from CodeDeploy tags. Amazon CloudFormation uses tags to simplify administration of your infrastructure. CodeDeploy uses tags to identify Amazon EC2 instances. You specified CodeDeploy tags on the Specify Parameters page.

  7. On the Review page, in Capabilities, select the I acknowledge that Amazon CloudFormation might create IAM resources box, and then choose Create.

    After Amazon CloudFormation has created the stack and launched the Amazon EC2 instances, in the Amazon CloudFormation console, CREATE_COMPLETE will be displayed in the Status column. This process can take several minutes.

To verify the CodeDeploy agent is running on the Amazon EC2 instances, see Managing CodeDeploy agent operations, and then proceed to Create an application with CodeDeploy.

Launch an Amazon EC2 instance with the Amazon CloudFormation template (Amazon CLI)

  1. Use our Amazon CloudFormation template in a call to the create-stack command. This stack will launch a new Amazon EC2 instance with the CodeDeploy agent installed.

    To launch an Amazon EC2 instance running Amazon Linux:

    aws cloudformation create-stack \ --stack-name CodeDeployDemoStack \ --template-url templateURL \ --parameters ParameterKey=InstanceCount,ParameterValue=1 ParameterKey=InstanceType,ParameterValue=t1.micro \ ParameterKey=KeyPairName,ParameterValue=keyName ParameterKey=OperatingSystem,ParameterValue=Linux \ ParameterKey=SSHLocation,ParameterValue=0.0.0.0/0 ParameterKey=TagKey,ParameterValue=Name \ ParameterKey=TagValue,ParameterValue=CodeDeployDemo \ --capabilities CAPABILITY_IAM

    To launch an Amazon EC2 instance running Windows Server:

    aws cloudformation create-stack --stack-name CodeDeployDemoStack --template-url template-url --parameters ParameterKey=InstanceCount,ParameterValue=1 ParameterKey=InstanceType,ParameterValue=t1.micro ParameterKey=KeyPairName,ParameterValue=keyName ParameterKey=OperatingSystem,ParameterValue=Windows ParameterKey=SSHLocation,ParameterValue=0.0.0.0/0 ParameterKey=TagKey,ParameterValue=Name ParameterKey=TagValue,ParameterValue=CodeDeployDemo --capabilities CAPABILITY_IAM

    keyName is the instance key pair name. Type the key pair name only, not the key pair file extension.

    template-url is the location of the Amazon CloudFormation template for your region:

    For the China (Beijing) Region:

    • https://s3.cn-north-1.amazonaws.com.cn/aws-codedeploy-cn-north-1/templates/latest/CodeDeploy_SampleCF_Template.json (for the China (Beijing) region)

    This command creates an Amazon CloudFormation stack named CodeDeployDemoStack, using the Amazon CloudFormation template in the specified Amazon S3 bucket. The Amazon EC2 instance is based on the t1.micro instance type, but you can use any type. It is tagged with the value CodeDeployDemo, but you can tag it with any value. It has the specified instance key pair applied.

  2. Call the describe-stacks command to verify the Amazon CloudFormation stack named CodeDeployDemoStack was successfully created:

    aws cloudformation describe-stacks --stack-name CodeDeployDemoStack --query "Stacks[0].StackStatus" --output text

    Do not proceed until the value CREATE_COMPLETE is returned.

To verify the CodeDeploy agent is running on the Amazon EC2 instance, see Managing CodeDeploy agent operations, and then proceed to Create an application with CodeDeploy.