CodeDeploy Agent Operations: A Guide with CodeDeploy, Appspec, and EC2.

DevFunOps With Pradeep
6 min readMar 18, 2024

--

In the fast-paced world of cloud computing, seamless application deployment is a cornerstone of operational success.
With AWS CodeDeploy, Amazon Web Services (AWS) offers a cutting-edge solution that revolutionises the deployment process onto EC2 instances, serverless Lambda functions, or Amazon ECS services.
This dynamic tool not only streamlines the deployment workflow but also enhances efficiency, enabling businesses to stay ahead in the ever-evolving digital landscape.

The AWS CodeDeploy agent, once installed and configured on an instance, enables the instance to take part in CodeDeploy deployments.

The AWS CodeDeploy Agent is a software tool that, when installed and configured on an instance, allows that instance to be used in CodeDeploy deployments.

Install the CodeDeploy agent

To use CodeDeploy on EC2 instances or on-premises servers, the CodeDeploy agent must be installed first.

“Login to the instance and execute the listed commands sequentially, or crafting a shell script for execution.”

OR

“When launching an Amazon EC2 instance, you can provide user data for automated configuration tasks and script execution. User data can be in the form of shell scripts or cloud-init directives.”

Please refer to this dedicated blog post to pass user data while creating the EC2 Instance.👉👉 [link]

“Replace ‘bucket-name’ with the name of the Amazon S3 bucket that contains the CodeDeploy Resource Kit files specific to your region. Similarly, replace ‘region-identifier’ with the identifier corresponding to your region.”

For a list of bucket names and region identifiers, see Resource kit bucket names by Region.

#!/bin/bash

sudo yum update -y
# Install the CodeDeploy agent
sudo yum install -y ruby wget
cd /home/ec2-user
wget https://aws-codedeploy-${AWS_REGION}.s3.${AWS_REGION}.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
# Start the CodeDeploy agent
sudo service codedeploy-agent start

Alternatively, you can install the CodeDeploy agent using AWS Systems Manager. For more insights on Systems Manager, check out “What is AWS Systems Manager.”

If the CodeDeploy agent is installed and operational, you’ll typically encounter a message indicating its status, such as “The AWS CodeDeploy agent is running.”

Once the CodeDeploy agent is installed, it creates a configuration file on the instance.

This file specifies directory paths and settings for CodeDeploy’s interactions with the instance.

The file details are as follows:

Amazon Linux, Ubuntu, and RHEL instances:

  • Name: ‘codedeployagent.yml
  • Location: ‘/etc/codedeploy-agent/conf

Windows Server instances:

  • Name: ‘conf.yml
  • Location: ‘C:\ProgramData\Amazon\CodeDeploy

Please refer to the screenshots below for the file details.

The CodeDeploy agent actively manages revisions, deployment history, and deployment scripts within its root directory on an instance.

The default directory locations are as follows:

  • Amazon Linux, Ubuntu : ‘/opt/codedeploy-agent/deployment-root’
  • For Windows Server instances: ‘C:\ProgramData\Amazon\CodeDeploy’

Please refer to the screenshots below for the file details.

AppSpec files on an EC2/on-premises compute platform

For applications utilizing the EC2/On-Premises compute platform, the AppSpec file should be a YAML-formatted document named appspec.yml. It must reside at the root of the directory structure within the application’s source code. Failure to adhere to this structure will result in deployment errors.

The AppSpec file plays a pivotal role in CodeDeploy by directing:

  • Content Installation: What content to install onto instances from the application revision stored in Amazon S3 or GitHub.
  • Lifecycle Event Hooks: Which lifecycle event hooks to trigger in response to deployment events.

Once you’ve finalized the AppSpec file, you package it together with the deployment content into an archive file, typically in zip, tar, or compressed tar format.

Once you have the bundled archive file, referred to as a revision in CodeDeploy, you upload it to an Amazon S3 bucket or Git repository. Subsequently, you utilize CodeDeploy to initiate the deployment of the revision.

Please refer to the below appspec.yml file and screenshot of directory structure within the application’s source code.

version: 0.0
os: linux
files:
- source: /
destination: /var/www/html/
hooks:
BeforeInstall:
- location: scripts/install_dependencies.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/change_permissions.sh
timeout: 300
runas: root
ApplicationStart:
- location: scripts/start_server.sh
- location: scripts/create_test_db.sh
timeout: 300
runas: root
ApplicationStop:
- location: scripts/stop_server.sh
timeout: 300
runas: root

How the CodeDeploy agent uses the AppSpec file

During deployment, the CodeDeploy agent checks the hooks section of the AppSpec file for the current event’s name. If found, it retrieves and runs the listed scripts in order. The agent logs each script’s status in its log file on the instance. If a script runs successfully, it returns an exit code of 0 (zero).

During the Install event, the CodeDeploy agent uses the mappings defined in the files section of the AppSpec file to determine which folders or files to copy from the revision to the instance.

Deployment fails if the CodeDeploy agent installed on the operating system doesn’t match what’s specified in the AppSpec file.

View log data for CodeDeploy EC2/On-Premises deployments

You can view the log data created by a CodeDeploy deployment by setting up the Amazon CloudWatch agent to view aggregated data in the CloudWatch console or by logging into an individual instance to review the log file.

When the Amazon CloudWatch agent is installed on an instance, deployment data for all deployments to that instance becomes available for viewing in the CloudWatch console. For simplicity, we recommend using CloudWatch to centrally monitor log files instead of viewing them instance by instance

To view deployment log data for an individual instance, you can sign in to the instance and browse for information about errors or other deployment events.

deployment logs are stored in the following location:

  • Amazon Linux, Ubuntu : ‘/opt/codedeploy-agent/deployment-root/deployment-logs/codedeploy-agent-deployments.log’
  • Windows Server instances: ‘C:\ProgramData\Amazon\CodeDeploy\log\codedeploy-agent-log.txt’

In the log files, you can review any errors encountered during deployment, facilitating troubleshooting and resolution of deployment issues.

Conclusion

In summary, the CodeDeploy agent serves as a vital component in the deployment process by adhering to instructions laid out in the AppSpec file. Throughout deployment, it executes scripts in response to specified events and transfers designated files from the revision to the instance. However, it’s crucial to note that any inconsistencies between the installed CodeDeploy agent and the specifications outlined in the AppSpec file can result in deployment failures. Therefore, maintaining alignment between the agent version and the AppSpec file is imperative for seamless deployment operations.

If you encounter any issues or need further assistance, feel free to ask for help. Your feedback and questions are always welcome in the comments section below. We’re here to support you on your AWS journey!🚀🚀

As we wrap up our journey through CodeDeploy Agent Operations, let’s remember: every step forward is a chance to expand our knowledge and capabilities. Embrace the challenges, for they are the stepping stones to greatness. With CodeDeploy Agent by our side, let’s fearlessly pursue our goals and unlock new levels of achievement. Keep pushing boundaries, keep striving for excellence, and let each deployment be a testament to your growth and determination. Happy coding, and may your journey be filled with endless discoveries and triumphs! 🚀🌟

--

--