Introduction
AWS CodeDeploy is a fully managed deployment service that automates software deployments to a variety of compute services such as Amazon EC2, AWS Fargate, AWS Lambda, and on-premises servers. CodeDeploy makes it easier to rapidly release new features, helps avoid downtime during application deployment, and handles the complexity of updating your applications.
Key Concepts
-
Deployment Types:
- In-place Deployment: Updates the application on the same set of instances.
- Blue/Green Deployment: Deploys the new version on a separate set of instances and then switches traffic to the new instances.
-
Deployment Configurations:
- OneAtATime: Deploys the application to one instance at a time.
- HalfAtATime: Deploys the application to half of the instances at a time.
- AllAtOnce: Deploys the application to all instances simultaneously.
-
Application Specification File (AppSpec File):
- Defines the deployment actions.
- Contains hooks for lifecycle events.
-
Lifecycle Events:
- BeforeInstall: Tasks to be performed before the application revision is installed.
- AfterInstall: Tasks to be performed after the application revision is installed.
- ApplicationStart: Tasks to be performed after the application revision is started.
- ValidateService: Tasks to be performed to validate the service is running correctly.
Setting Up AWS CodeDeploy
Step 1: Create an Application
- Open the AWS Management Console.
- Navigate to the CodeDeploy service.
- Click on "Create application".
- Enter a name for your application.
- Choose the compute platform (EC2/On-premises, AWS Lambda, or Amazon ECS).
- Click "Create application".
Step 2: Create a Deployment Group
- In the CodeDeploy console, select your application.
- Click on "Create deployment group".
- Enter a name for the deployment group.
- Choose a service role that grants CodeDeploy permissions to access your resources.
- Select the deployment type (In-place or Blue/Green).
- Configure the deployment settings (e.g., load balancer, deployment configuration).
- Click "Create deployment group".
Step 3: Create an AppSpec File
The AppSpec file is a YAML or JSON file that defines the deployment actions. Below is an example of a YAML AppSpec file for an EC2/On-premises deployment:
version: 0.0 os: linux files: - source: /src destination: /var/www/html hooks: BeforeInstall: - location: scripts/install_dependencies.sh timeout: 300 runas: root AfterInstall: - location: scripts/start_server.sh timeout: 300 runas: root ApplicationStart: - location: scripts/verify_service.sh timeout: 300 runas: root
Step 4: Create a Deployment
- In the CodeDeploy console, select your application.
- Click on "Create deployment".
- Choose the deployment group.
- Specify the revision location (e.g., S3 bucket, GitHub repository).
- Click "Create deployment".
Practical Example
Example: Deploying a Simple Web Application
-
Create an Application:
- Name:
MyWebApp
- Compute Platform:
EC2/On-premises
- Name:
-
Create a Deployment Group:
- Name:
MyWebAppDeploymentGroup
- Service Role:
CodeDeployServiceRole
- Deployment Type:
In-place
- Deployment Configuration:
CodeDeployDefault.OneAtATime
- Name:
-
AppSpec File:
version: 0.0 os: linux files: - source: /src destination: /var/www/html hooks: BeforeInstall: - location: scripts/install_dependencies.sh timeout: 300 runas: root AfterInstall: - location: scripts/start_server.sh timeout: 300 runas: root ApplicationStart: - location: scripts/verify_service.sh timeout: 300 runas: root
-
Create a Deployment:
- Application Name:
MyWebApp
- Deployment Group:
MyWebAppDeploymentGroup
- Revision Location:
S3://mybucket/mywebapp.zip
- Application Name:
Scripts
-
install_dependencies.sh:
#!/bin/bash yum install -y httpd
-
start_server.sh:
#!/bin/bash service httpd start
-
verify_service.sh:
#!/bin/bash curl -I http://localhost
Exercises
Exercise 1: Create a CodeDeploy Application
- Create a new CodeDeploy application named
MyFirstApp
. - Choose the compute platform as
EC2/On-premises
.
Exercise 2: Create a Deployment Group
- Create a deployment group named
MyFirstAppDeploymentGroup
for theMyFirstApp
application. - Use the
CodeDeployDefault.OneAtATime
deployment configuration.
Exercise 3: Write an AppSpec File
- Write an AppSpec file that copies files from
/src
to/var/www/html
. - Include hooks for
BeforeInstall
,AfterInstall
, andApplicationStart
.
Exercise 4: Create a Deployment
- Create a deployment for the
MyFirstApp
application using theMyFirstAppDeploymentGroup
. - Use an S3 bucket to store the application revision.
Solutions
Solution to Exercise 1
- Open the AWS Management Console.
- Navigate to the CodeDeploy service.
- Click on "Create application".
- Enter
MyFirstApp
as the application name. - Choose
EC2/On-premises
as the compute platform. - Click "Create application".
Solution to Exercise 2
- In the CodeDeploy console, select
MyFirstApp
. - Click on "Create deployment group".
- Enter
MyFirstAppDeploymentGroup
as the deployment group name. - Choose a service role.
- Select
In-place
as the deployment type. - Choose
CodeDeployDefault.OneAtATime
as the deployment configuration. - Click "Create deployment group".
Solution to Exercise 3
version: 0.0 os: linux files: - source: /src destination: /var/www/html hooks: BeforeInstall: - location: scripts/install_dependencies.sh timeout: 300 runas: root AfterInstall: - location: scripts/start_server.sh timeout: 300 runas: root ApplicationStart: - location: scripts/verify_service.sh timeout: 300 runas: root
Solution to Exercise 4
- In the CodeDeploy console, select
MyFirstApp
. - Click on "Create deployment".
- Choose
MyFirstAppDeploymentGroup
as the deployment group. - Specify the revision location (e.g.,
S3://mybucket/myfirstapp.zip
). - Click "Create deployment".
Conclusion
In this section, you learned about AWS CodeDeploy, its key concepts, and how to set up and use it for deploying applications. You also practiced creating a CodeDeploy application, deployment group, and deployment, and writing an AppSpec file. This knowledge will help you automate and manage your application deployments efficiently. In the next module, we will explore AWS CodePipeline, which integrates with CodeDeploy to provide a complete CI/CD solution.