with AWS CodeDeploy
In this tutorial, you will learn how to deploy application code to a virtual machine on AWS. You will use AWS CodeDeploy, a service that automates code deployments to AWS or on-premises servers, to deploy code to virtual machines that you create and manage with Amazon EC2.
Everything done in this tutorial is free tier eligible.
Manage Your AWS Resources
Sign in to the ConsoleYou will need to create a key pair to access your virtual machine with Amazon EC2. If you already have a key pair, skip ahead to Step 2.
a. When you click here, the AWS Management Console will open in a new browser window, so you can keep this step-by-step guide open. Click Create Key Pair.
b. Name your key pair. For this tutorial, we will use MyFirstKey. Click Create.
Note: Amazon EC2 uses public-key cryptography to encrypt and decrypt login information. To learn more about key pairs, see Amazon EC2 Key Pairs.
You will need to launch an AWS virtual machine to deploy your code on. AWS virtual machines are known as Amazon EC2 instances, or just 'instances' for short. In this step, we will launch three EC2 instances using a pre-configured EC2 template.
You will configure your instance settings with the options below:
- Operating System: You can choose the OS of your EC2 Instance. For this tutorial, we will use Amazon Linux.
- Instance Type: For this tutorial, the t1.micro instance type has been selected as the default value to stay within the free tier. Amazon EC2 provides a wide selection of instance types optimized to fit different use cases. Instance types comprise varying combinations of CPU, memory, storage, and networking capacity and give you the flexibility to choose the appropriate mix of resources for your applications.
- Key Pair Name: From the drop-down list, choose the Amazon EC2 instance key pair you created in step 1, MyFirstKey, to connect to the Amazon EC2 instances. You can also choose a key pair you already have.
- Tag Key and Value: AWS CodeDeploy will use this tag key and value to locate the instances during deployments. You can leave the default values.
Click Launch Instances.
Note: This step may take several minutes to complete. As you wait, feel free to review this tutorial with the video below:
Adobe Flash Player or a modern browser is required to view videos on this site.
AWS CodeDeploy uses application names during code deployments to make sure it is referencing the correct deployment components, such as the deployment group, deployment configuration, and application revision.
b. Review information about your application revision, such as its location and description.
Note: You have the option to download the sample bundle. In this view, you can review information about the application revision you’d like to deploy to EC2. An application revision is an archive file containing source content—such as source code, web pages, executable files, and deployment scripts—along with an application specification file (AppSpec file). The AppSpec file helps CodeDeploy map the source files in your revision to their destinations and run scripts at various stages of the deployment.
Click Next Step
A deployment group is a set of individual EC2 instances that CodeDeploy deploys revisions to. A deployment group contains individually tagged instances, Amazon EC2 instances in Auto Scaling groups, or both.
In the Deployment Group Name box, leave the proposed deployment group name (DemoFleet) as is.
You will then specify the Amazon EC2 instances to deploy by entering the key-value pair in the Seach by Amazon EC2 Tags section:
- The Key and Value columns should be autopopulated with the values from Step 3.
- The Instances column displays the number of EC2 instances that you will deploy code to. For this tutorial, we have launched and pre-configured three EC2 instances and these instances have already been tagged together into a deployment group.
Choose Next Step.
In this step, you will grant AWS CodeDeploy permission to deploy to your instances. You create a role for an AWS service when you want to grant permissions to a service like Amazon EC2 or AWS CodeDeploy. These services can access AWS resources, so you create a role to determine what the service is allowed to do with those resources.
Service Role: Choose Create a new service role. If you already have a service role, you can choose Use an existing service role.
Role Name: You can accept default value of CodeDeploy_HelloWorld. If you are using an existing service role, choose it from the Role Name drop-down list.
Click Next Step.
In this step, we will select a deployment configuration and then initiate the deployment to our three EC2 instances. By the end of this step, we'll have successfully deployed a live and running website, which we can visit online.
a. The deployment configuration lets you determine how many instances to simultaneously deploy your application revisions to and describes the success and failure conditions for the deployment. For example, using the default configuration (“One at a Time”), if you deploy your application to 3 instances, this configuration will deploy to one instance at a time.
Accept the Default Deployment Configuration and click Next Step.
To avoid future charges, you must clean up the resources used in this tutorial. The EC2 instances you launched for this tutorial will keep running unless you terminate them.
You have successfully created and completed your very first code deployment to Amazon EC2 instances using AWS CodeDeploy. Using a provided template, you first launched three instances that were pre-configured with the proper tags and installed with the agent necessary to use AWS CodeDeploy. Finally, you prepared your application for deployment, granted CodeDeploy permission to deploy to your instances, and then successfully deployed your code.
Now that you have completed a sample deployment to Amazon EC2 instances using AWS CodeDeploy, you can begin learning how to use CodeDeploy in your own applications. Visit the CodeDeploy documentation to learn how to launch EC2 instances and then configure the instances so that CodeDeploy can deploy code to them. In our tutorial, we simplified those configuration steps to help you learn and experience how CodeDeploy works.