20) Deployment with AWS Lesson

Part 2: AWS CodeDeploy

14 min to complete · By Ryan Desmond, Jared Larsen

In the previous lesson, you were introduced to the concept of deployment groups with AWS CodeDeploy, but there was some preparation to be done before actually creating one. In Part 2 of this AWS deployment tutorial, you'll now create a deployment group and proceed to use it as a target for application deployment.

Create Deployment Group

Back in the AWS CodeDeploy console, on your Application detail page, click the orange Create deployment group button.

First, give your deployment group a name that coincides with the tag you added to your instance, then follow the steps below.

Service Role

Select the IAM service role that you created previously (CodeDeployServiceRole), which grants CodeDeploy access to other AWS services on your behalf.

Deployment Type

Here, you have two options.

  • In-place - replaces an existing deployment live on the server. This will cause the deployment to go offline for a short time while the process takes place and the new deployment boots up.

  • Blue/green - when using a load balancer in a production environment, this registers a new set of instances with the latest build before taking the previous instances offline. This can offer a more seamless experience for users.

As blue/green is a much more advanced option, you will be selecting in-place here.

Environment Configuration

In this section you'll reference your target instance. In this case, simply check the box Amazon EC2 instances, then choose the tag you added to your instance earlier. Drop-down boxes should automatically appear to help you choose.

Colorful illustration of a light bulb

Tip: After choosing your tag, you should see "1 unique matched instance" below.

Agent Configuration with AWS Systems Manager

To perform its automated functions, CodeDeploy requires a special software service called CodeDeploy Agent to be installed on your instance.

Leave the default option selected here: Now and schedule updates. This will install AWS CodeDeploy Agent right away and also keep it updated.

Illustration of a lighthouse

Note: A bug exists that might cause the automatic installation and scheduled updates of CodeDeploy agent to fail on Ubuntu. You'll learn how to check if it was installed and how to manually install it using AWS Systems Manager just ahead.

Deployment Settings

The default settings can be used here.

Load Balancer

In the future you will utilize this option as a load balancer brings many benefits to your deployment, but for now you can uncheck this option.

After clicking Create deployment group, the deployment group will be created, and you'll be taken to its summary page. Great job! You're just about ready for your first deployment.

Appspec File

Before proceeding, you'll need to create an appspec.yml file at the root of your project. This is an essential piece of the CodeDeploy puzzle, as (just like the buildspec.yml file before) it contains important instructions. This time, they determine how your application is to be deployed.

This file will generally specify various lifecycle hooks which are used to execute shell scripts to perform duties related to the event. Common hooks include ApplicationStop, ApplicationStart, BeforeInstall, AfterInstall, etc. You can use them to do anything from copying/moving files to stopping/starting services, to testing local endpoints with curl, just about anything necessary to support your deployment.

Luckily, since your demo deployment is currently using Tomcat to handle most of the heavy lifting - the appspec file is super simple. It names a single file ( ROOT.war, from your artifact), and the destination where it should be placed. It also specifies that the file should be owned by your instance user ubuntu.

version: 0.0
os: linux
files:
  - source: target/ROOT.war
    destination: /var/lib/tomcat9/webapps
permissions:
  - object: /
    pattern: "**"
    owner: ubuntu
    group: ubuntu

Just like buildspec files, these files are capable of much more. You can find out more information via the excellent AppSpec file reference.

Create this file in your project and push it to GitHub now. Afterward, open the CodeBuild console in a new tab and start your DemoBuild project. It will pull the latest commit from your repo and ensures that your artifact will now include the appspec.yml file.

Create Deployment

Now that your application has a deployment group and an appspec file, it's time to create a deployment! On your application detail page, click the Create deployment button.

codedeploy-create deployment

Deployment Settings

  • Deployment group - The deployment group that you previously created.

CodeDeploy supports code stored in Amazon S3 buckets or GitHub repos. Since you're already building your project with CodeBuild and storing artifacts in S3, you'll pull them from there.

  • Revision type - My application is stored in Amazon S3

You'll now need the address of your artifact (zip file) stored in S3. It will be something simple like:

s3://my-demo-bucket-name/DemoBuild.zip

To grab the exact URI, open Amazon S3 in a new tab and navigate to your build bucket. Click on DemoBuild.zip, then on the detail page click the Copy S3 URI button, and head back to your CodeDeploy tab.

  • Revision location - Paste from your S3 bucket.
  • Revision file type - Zip

Additional Deployment Behavior Settings

  • Content options - Overwrite the content

This will ensure that the existing ROOT.war file on the instance can be replaced by the deployment process.

Click Create deployment.

The deployment will begin immediately. Wait and watch - this page will automatically refresh as the process runs. After a few minutes, you'll receive a status of either Succeeded or Failed.

  • Succeeded - The AWS CodeDeploy agent has been automatically installed and the deployment was able to proceed successfully.

Since you've already run CodeBuild, CodeDeploy successfully located your artifact and deployed ROOT.war to the appropriate folder on your EC2 instance. Open an SSH session and verify that the file /var/lib/tomcat9/webapps/ROOT.war was indeed updated (use ls -al and check out the date). Nice!

  • Failed - This is most likely due to a failure in installing the CodeDeploy agent. Below are instructions on how to install it manually, after which you can retry the deployment.

AWS CodeDeploy Agent

As mentioned earlier, CodeDeploy relies on the CodeDeploy agent to be installed on target instances. Under normal circumstances, the agent is automatically installed during your first deployment (as specified when creating your deployment group). When this automated process fails, you'll most likely receive an error similar to this:

codedeploy failed

This means CodeDeploy was unable to communicate with the agent.

Installation Verification

You can easily verify if the agent is installed on your instance by issuing the following command via SSH session:

$ sudo systemctl status codedeploy-agent

If you get a status of "active (running)", the agent is installed correctly and there must be some other issue causing the deployment to fail. However, if the following message is displayed - you'll need to install the CodeDeploy agent manually.

Unit codedeploy-agent.service could not be found.

Install AWS CodeDeploy Agent

AWS recommends installing CodeDeploy agent using AWS Systems Manager. You can install a single software package or multiple, you can even automatically install specific packages on new instances based on tags. To get started, navigate to the Systems Manager console.

In the sidebar, click on Distributor.

On the Distributor page, select AWSCodeDeployAgent.

Click Install on a schedule.

A new State Manager window opens to configure the installation schedule.

  • Name - use something like "CodeDeploy-Agent-Package"
  • Document - leave defaults selected in this section
  • Action - Install
  • Installation type - Uninstall and reinstall
  • Target selection - Choose instances manually (then select your demo instance)
  • Specify schedule - On schedule, then Rate schedule builder and 14 days

Click Create association.

On the Associations page, click the radio next to your new association and click Apply association now.

Once CodeDeploy Agent is installed and running on your instance, attempt the deployment again by locating it in your "Deployment history", and clicking the Retry deployment button.

Colorful illustration of a light bulb

Notice: At this point, unless your database password is hard-coded in your application-deploy.properties file on GitHub (hopefully not!!), the application will be deployed but ultimately fail to boot - this is normal, and you will learn how to properly resolve using AWS Parameter Store coming up.

Summary: AWS CodeDeploy Part 2 - Deployment Groups, Appspec, Agent

You're now able to build and deploy your application, each with the click of a button. Sweet! But this is not exactly automated, nor does it look like continuous delivery. So how does this all come together?

Now, the moment you've been working towards - it's time to truly automate these actions by introducing another AWS tool called CodePipeline.