AWS CodeDeploy | DevOps With AWS Part 3
Image from Google Images

AWS CodeDeploy | DevOps With AWS Part 3

As Developer, we write codes, but it is often required for a developer to release changes in production, or may have the knowledge to deploy the changes in production.

There is a known saying: "The Value of such developers is much higher, who can just not only code but also able to deploy their code efficiently than those who can't".

If you have never deployed codes anywhere yet, let's use this opportunity to learn an efficient and widely used deployment system offered by AWS i.e. AWS CodeDelpoy. In this article, I have covered this service in greater depths.

What is AWS CodeDeploy:

AWS CodeDeploy is a fully managed deployment service that automates software deployments to a variety of computing services such as Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises servers. AWS CodeDeploy makes it easier for you to rapidly release new features, helps you avoid downtime during application deployment, and handles the complexity of updating your applications. You can use AWS CodeDeploy to automate software deployments, eliminating the need for error-prone manual operations. The service scales to match your deployment needs.



Why We Should Use CodeDeploy?


Automated instance deployments

Repeatable deployments

You can easily repeat an application deployment across different groups of instances with AWS CodeDeploy. CodeDeploy uses a file and command-based install model, which enables it to deploy any application and reuse existing setup code. The same setup code can be used to consistently deploy and test updates across your deployment, test, and production release stages for Amazon EC2 instances. Eliminating manual steps from deployments increases both the speed and reliability of your software delivery process.

Automatic scaling

AWS CodeDeploy allows you to integrate software deployment and scaling activities in order to keep your application up-to-date in a dynamic production environment. For Amazon EC2 instances, CodeDeploy integrates with Auto Scaling. Auto Scaling allows you to scale EC2 capacity according to conditions you define such as spikes in traffic. CodeDeploy is notified whenever a new instance launches into an Auto Scaling group and will automatically perform an application deployment on the new instance before it is added to an Elastic Load Balancing load balancer.

On-premises deployments

You can use AWS CodeDeploy to automate software deployments across your development, test, and production environments running on any instance including instances in your own data centers (your instances will need to be able to connect to AWS public endpoints). This enables you to use a single service to consistently deploy applications across hybrid architectures.

Minimize downtime

Rolling and Blue/Green updates

Applications do not require downtime when they’re being upgraded to a new revision with AWS CodeDeploy. AWS CodeDeploy can perform blue/green deployments to Amazon EC2 instances, an Amazon ECS service (both EC2 and AWS Fargate launch type), or an AWS Lambda function. With a blue/green deployment, the new version of your application is launched alongside the old version. Once the new revision is tested and declared ready, CodeDeploy can shift the traffic from your prior version to your new version according to your specifications.

CodeDeploy can also perform a rolling update across a group of Amazon EC2 instances where only a fraction of the instances are taken offline at any one time. CodeDeploy progressively works its way across the instances allowing applications to remain available and continue serving traffic. For AWS Lambda functions, incoming traffic is gradually routed from the old version to the new one.

Deployment health tracking

Deployment Health Tracking works in conjunction with rolling updates to keep applications highly available during deployments. Unexpected downtime can occur if bad updates are deployed. AWS CodeDeploy monitors your deployment and will stop deployment if there are too many failed updates.

Stop and rollback

You can stop an application deployment that is in process at any time using the AWS Management Console, the AWS CLI, or any of the AWS SDKs. You can simply re-deploy that revision if you want to continue the stopped deployment at a later time. You can also immediately rollback by redeploying the previous revision.

Centralized control

Monitoring and control

You can launch, control, and monitor deployments of your software directly from the AWS Management Console or by using the AWS CLI, SDKs, or APIs. In the case of a failure, you can pinpoint the script experiencing failure. You can also set push notifications that allow you to monitor the status of your deployments via SMS or email messages through Amazon Simple Notification Service.

Deployment groups

One application can be deployed to multiple deployment groups. Deployment groups are used to match configurations to specific environments, such as staging or production environments. You can test a revision in staging and then deploy that same code with the same deployment instructions to production once you are satisfied.

Deployment history

AWS CodeDeploy tracks and stores the recent history of your deployments. You can view which application versions are currently deployed to each of your target deployment groups. You can inspect the change history and success rates of past deployments to specific deployment groups. You can also investigate a timeline of past deployments for a detailed view of your deployment successes and errors.

Easy to adopt

Language and architecture agnostic

AWS CodeDeploy uses a file and command-based install model, which enables it to deploy any application and reuse existing setup code. CodeDeploy uses a single AppSpec configuration file that you can use to run actions, tests, or verifications at each lifecycle event (phase of deployment). The commands can be any code, such as a shell script, a custom program, or even a configuration management tool.

Toolchain integration

It is easy to integrate application deployments with your existing software delivery toolchain by using the AWS CodeDeploy APIs. AWS CodePipelineAWS CodeStar, and some AWS partners provide pre-built CodeDeploy integrations for continuous integration and continuous delivery services, making it simple to automatically deploy your updated application.

Receive Notifications

Review defined events

You can create notifications for events impacting your deployments. Notifications will come in the form of Amazon SNS notifications. Each notification includes a status message as well as a link to the resources whose event generated that notification.


AWS CodeDeploy pricing


For CodeDeploy on EC2/Lambda: There is no additional charge for code deployments to Amazon EC2 or AWS Lambda through AWS CodeDeploy.

For CodeDeploy On-Premises: You pay $0.02 per on-premises instance update using AWS CodeDeploy. There are no minimum fees and no upfront commitments. For example, a deployment to three instances equals three instance updates. You will only be charged if CodeDeploy performs an update to an instance. You will not be charged for any instances skipped during the deployment.

You pay for any other AWS resources (e.g. S3 buckets) you may use in conjunction with CodeDeploy to store and run your application. You only pay for what you use, as you use it; there are no minimum fees and no upfront commitments.


AWS CodeDeploy Concepts:

What is CodeDeploy Application?:

An application is simply a name or container (Unique Identifier) used by CodeDeploy to ensure the correct revision, deployment configuration, and deployment group is referenced during a deployment.

What is Compute Platform?

It's the target platform where the CodeDelpoy service will deploy the code i.e.

EC2

ECS

Lambda

On-premise servers


What is Deployment Configuration?

A deployment configuration is a set of rules and success and failure conditions used by CodeDeploy during a deployment. These rules and conditions are different, depending on whether you deploy to an EC2/On-Premises compute platform, AWS Lambda compute platform, or Amazon ECS compute platform.


What is the Deployment Group?

You can specify one or more deployment groups for a CodeDeploy application. Each application deployment uses one of its deployment groups. The deployment group contains settings and configurations used during the deployment. Most deployment group settings depending on the compute platform used by your application. Some settings, such as rollbacks, triggers, and alarms can be configured for deployment groups for any computing platform.

In short: Deployment Group contain contains individually tagged instances, Amazon EC2 instances in Amazon EC2 Auto Scaling groups, or both.

For ECS: In an Amazon ECS deployment, a deployment group specifies the Amazon ECS service, load balancer, optional test listener, and two target groups. It also specifies when to reroute traffic to the replacement task set and when to terminate the original task set and Amazon ECS application after a successful deployment.

For AWS Lambda: In an AWS Lambda deployment, a deployment group defines a set of CodeDeploy configurations for future deployments of an AWS Lambda function. For example, the deployment group specifies how to route traffic to a new version of a Lambda function. It also might specify alarms and rollbacks. A single deployment in an AWS Lambda deployment group can override one or more group configurations.

For EC2/On-Premises deployment:

In an EC2/On-Premises deployment, a deployment group is a set of individual instances targeted for deployment. A deployment group contains individually tagged instances, Amazon EC2 instances in Amazon EC2 Auto Scaling groups, or both.

In an in-place deployment, the instances in the deployment group are updated with the latest application revision.

In a blue/green deployment, traffic is rerouted from one set of instances to another by deregistering the original instances from a load balancer and registering a replacement set of instances that typically has the latest application revision already installed.

A Revision:

For AWS EC2 / On-premise instances:

In CodeDeploy, a revision contains a version of the source files CodeDeploy will deploy to your instances, or scripts CodeDeploy will run on your instances.

You plan the revision, add an AppSpec file to the revision, and then push the revision to Amazon S3 or GitHub. After you push the revision, you can deploy it.

For ECS:

It's a YML/JSON file that specifies the ECS task definition for deployment, usually consists of container-related information, port mapping for routing traffic, and other info as need

For Lambda:

It's a YML/JSON (App spec) file that contains information about what function to deploy, revision stored in S3.

What is a service role?

It's an IAM role that grants permissions to an AWS service so it can access AWS resources. The policies you attach to the service role determine which AWS resources the service can access and the actions it can perform with those resources. For CodeDeploy, a service role is used for the following:

  • To read either the tags applied to the instances or the Amazon EC2 Auto Scaling group names associated with the instances. This enables CodeDeploy to identify instances to which it can deploy applications.
  • To perform operations on instances, Amazon EC2 Auto Scaling groups, and Elastic Load Balancing load balancers.
  • To publish information to Amazon SNS topics so that notifications can be sent when specified deployment or instance events occur.
  • To retrieve information about CloudWatch alarms to set up alarm monitoring for deployments.


What is Target Version:

The most recent version of the application revision that you have uploaded to your repository and want to deploy to the instances in a deployment group. In other words, the application revision currently targeted for deployment. This is also the revision that is pulled for automatic deployments.

Check more key concepts in details here: https://docs.aws.amazon.com/codedeploy/latest/userguide/primary-components.html

What is CodeDeploy Agent:

The CodeDeploy agent is a software package that, when installed and configured on an instance, makes it possible for that instance to be used in CodeDeploy deployments.

Appspec.yml file, Deployment Lifecycle Events & Hooks:

The application specification file (AppSpec file) is a YAML-formatted or JSON-formatted file used by CodeDeploy to manage deployment. Keep the appspec.yml file in your project's root directory.

CodeDeploy deployment goes through a set of predefined stages, which called Deployment Lifecycle events, which allows us to run scrips/codes as part of the deployment:

1) ApplicationStop

2) DownloadBundle

3) BeforeInstall

4) Install

5) AfterInstall

6) ApplicationStart

7) ValidateService

Check the full list below:

No alt text provided for this image


Enough of the theories, I will say straight just theories never enough, just go ahead and get your hands dirty, watch the above video to gain some hands-on knowledge and try it yourself.

Sample appspec.yml file:

version: 0.0
os: linux
files:
  - source: /
    destination: /home/ubuntu/app
hooks:
  ApplicationStop:
    - location: scripts/stop_server.sh
      timeout: 300
      runas: root
      
  BeforeInstall:
    - location: scripts/before_install.sh
      timeout: 300
      runas: root


  AfterInstall:
    - location: scripts/after_install.sh
      timeout: 300
      runas: root


  ApplicationStart:
    - location: scripts/start_server.sh
      timeout: 300
      runas: root


  ValidateService:
    - location: scripts/validate_service.sh
      timeout: 300
  

I hope all this knowledge in this article, you are going to apply in your current and future projects and manage code more efficiently ??

Oh, btw, just so you know that it's not the only option you have to deploy your applications, you also have famous options for automatic code deployment, such as Jenkins, Ansible, Terraform, Puppet, and more, so look out for your use case and choose what best for your project, but in general, if your project needs deployment in AWS infra, AWS CodeDeployment will be undoubtedly one of the best options ??


References: AWS Official Site Documentation


The previous Article of "DevOps With AWS Series":

AWS CodeCommit | DevOps With AWS Part 1

AWS CodeBuild | DevOps With AWS Part 2


About the Author:

No alt text provided for this image

Sandip Das works as a Sr. Cloud Solutions Architect & DevOps Engineer for multiple tech product companies/start-ups, have AWS DevOps Engineer Professional certification, also holding the title of "AWS Container Hero",

He is always in "keep on learning" mode, enjoys sharing knowledge with others, and currently holds 5 AWS Certifications. Sandip finds blogging as a great way to share knowledge: he writes articles on Linkedin about Cloud, DevOps, Programming, and more. He also creates video tutorials on his YouTube channel.





Harshitha Harsh

?I help Businesses Upskill their Employees in DevOps | DevOps Mentor & Process Architect

1 年

Great read, Sandip! Thank you for sharing such valuable insights and training resources on AWS CodeDeploy. Looking forward to diving into this. #AlwaysLearning

回复
Lalit Khera

Founder & CEO at Aaptatt

1 年

The flexibility of AWS CodeDeploy is incredible. Whether you're deploying a simple web app or a complex microservices architecture, it handles it all

回复

How to modify appspec.yml for different environments? I have three environments: (1) Development (2) Staging (3) Production I want to change the destination folder per environment so the appspecy.yml becomes: .... files: ??destination: /var/www/{stage} .....

回复
Salman Kadaya

Senior DevOps Engineer

3 年

Hi Sandip Das, I need one suggestion. What if we have to deploy the same code into two different sever and deployment path and script is different than how we manage appspec file. Is it possible to manage with a single appspec file or do we have to create different appsepc files for each server?

DINESH KUMAR

Senior Data Engineer???? CACIB

3 年

My ecs deploy is in progress even after the changes updated into ecs service .

回复

要查看或添加评论,请登录

Sandip Das的更多文章

社区洞察

其他会员也浏览了