CodeDeploy is an AWS Service used to deploy applications. Application builds can be deployed to an EC2 instance, Lambda Function, or your servers running in a local data center. These are called deployment targets.
Applications need to be built before deployment. Applications can be built with either AWS CodeBuild or any other third party build tool like Jenkins.
There are a lot of concepts related to codedeploy, but to start we will discuss the below concepts,
- CodeDeploy Application
- CodeDeploy Deployment Group.
- Appspec file
- CodeDeploy Agent
- Application source and Instance Role
CodeDeploy Application is basically an identifier of the application and the deployment target. We select where we need to deploy the application (EC2, on-prem, lambda, or ECS)
CodeDeploy Deployment Group
A Deployment Group is all the configuration related to deployment.
A Deployment Group belongs to an application. It cannot be part of multiple applications, but an application can have multiple deployment groups based on environment (like Dev, Stage, or Prod) or any other factor.
Let's discuss the deployment Group for EC2/On-premise ,
The first thing is to create a CodeDeploy Service Role which consists of basic CodeDeploy permissions. Do not confuse this with the EC2 role. This is a role that needs to be created for CodeDeploy service and attach ‘AWSCodeDeployRole’ policy in it
The second is to select the Deployment type. CodeDeploy supports In-place and BlueGreen Deployment types. I have already published an article regarding Blue-Green Deployment which also talks about the difference between both deployment types. If interested you can click on the below link
Select In-Place if you do not want new servers to be created for each deployment.
The third is to select the Deployment instances. It is under ‘Environment configuration’. Here we need to select either the Autoscaling Group, or EC2/On-Prem Server (selected based on tags).
Fourth is to select the Deployment Configuration. There are three configurations,
- CodeDeployDefault.OneAtATime - As the name suggests, deployment is one by one linearly.
- CodeDeployDefault.HalfAtATime - This configuration deploys on half of the fleet at once. For example, there are 6 instances, deployment is carried on 3 instances first and then on the other 3.
- CodeDeployDefault.AllAtOnce - This configuration deploys on all of the instances at once.
Apart from these configurations, there is a very important file that is required to deploy the application. That is ‘appspec.yml
’. This file needs to be placed with the source code of the application.CodeDeploy deploys applications based on Appspec.yml
Appspec takes to source and destination for deployment on the server. Apart from these, there are different hooks that are executed during deployment. We can place our deployment scripts in these hooks based on when they need to be executed. These hooks vary based on the deployment target.
A sample appspec can be found in AWS Documentation. Click here
to see it.
A CodeDeploy agent should be running on the servers on which applications need to be deployed. An agent connects to the codedeploy service and maintains the state of versions from S3. We need to download the agent and start it on the server.
In the case of Blue-Green Deployment, the Golden image used by auto-scaling should have an agent installed.
For Amazon Linux and other RHEL based Linux server, steps to install agent can be found here
CodeDeploy supports either Github or Amazon S3 as an application source. This needs to be selected when creating deployment. The application needs to be bundled and stored in S3 for deployment.