

# Create an Amazon EC2 instance for CodeDeploy (Amazon CloudFormation template)
<a name="instances-ec2-create-cloudformation-template"></a>

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](instances.md).

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

**Topics**
+ [Before you begin](#instances-ec2-create-cloudformation-template-before)
+ [Launch an Amazon EC2 instance with the Amazon CloudFormation template (console)](#instances-ec2-create-cloudformation-template-console)
+ [Launch an Amazon EC2 instance with the Amazon CloudFormation template (Amazon CLI)](#instances-ec2-create-cloudformation-template-cli)

## Before you begin
<a name="instances-ec2-create-cloudformation-template-before"></a>

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 created an administrative user, as described in [Step 1: Setting up](getting-started-setting-up.md). Double-check that the user has the following minimum permissions and add any that are not present:
   + cloudformation:\$1
   + codedeploy:\$1
   + ec2:\$1
   + iam:AddRoleToInstanceProfile
   + iam:CreateInstanceProfile
   + iam:CreateRole
   + iam:DeleteInstanceProfile
   + iam:DeleteRole
   + iam:DeleteRolePolicy
   + iam:GetRole
   + iam:DeleteRolePolicy
   + iam:PutRolePolicy
   + iam:RemoveRoleFromInstanceProfile

1. 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](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](https://docs.amazonaws.cn/AWSEC2/latest/UserGuide/ec2-key-pairs.html#having-ec2-create-your-key-pair). Be sure the key pair is created in one of the regions listed in [Region and endpoints](https://docs.amazonaws.cn/general/latest/gr/rande.html#codedeploy_region) in *Amazon Web Services General Reference*. Otherwise, you can't use the instance key pair with CodeDeploy.

## Launch an Amazon EC2 instance with the Amazon CloudFormation template (console)
<a name="instances-ec2-create-cloudformation-template-console"></a>

1. Sign in to the Amazon Web Services Management Console and open the Amazon CloudFormation console at [https://console.amazonaws.cn/cloudformation](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](getting-started-codedeploy.md). On the navigation bar, in the region selector, choose one of the regions listed in [Region and endpoints](https://docs.amazonaws.cn/general/latest/gr/rande.html#codedeploy_region) in *Amazon Web Services General Reference*. CodeDeploy supports these regions only.

1. Choose **Create Stack**.

1. 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)

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

1. 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**).

1. 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.

1. 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\$1COMPLETE** 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](codedeploy-agent-operations.md), and then proceed to [Create an application with CodeDeploy](applications-create.md).

## Launch an Amazon EC2 instance with the Amazon CloudFormation template (Amazon CLI)
<a name="instances-ec2-create-cloudformation-template-cli"></a>

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.

1. 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](codedeploy-agent-operations.md), and then proceed to [Create an application with CodeDeploy](applications-create.md).