Thursday 1 August 2024

AWS CodeDeploy

 



The (Continuous Delivery) goal is to have seamless and automated iterations of:
  • writing the code (updates)
  • testing
  • releasing/deploying to production - across multiple EC2 instances

Amazon CodeDeploy:

  • fully managed deployment service provided by Amazon Web Services (AWS)
  • automates the process of deploying applications to various compute services such as:
    • Amazon EC2 instances
    • AWS Lambda functions
    • on-premises servers
  • helps developers deploy code quickly and efficiently, while avoiding downtime during application updates and maintaining the integrity of the application environment. The same setup is used to release new code to:
    • dev instances for debugging
    • staging instances for testing
    • production - for release to customers
  • helps in maintaining high availability and reliability of applications while simplifying the deployment process, making it an essential tool for DevOps practices and continuous delivery in the AWS ecosystem


Key features and benefits of Amazon CodeDeploy:

  • Automated Deployments: Automates the process of deploying applications, making it easier and faster to release new features and updates.
  • Scalability: Can deploy to a single instance or thousands of instances, allowing it to scale according to the needs of the application.
  • Flexibility: Supports a wide range of deployment types, including:
    • in-place deployments
    • blue/green deployments
  • Minimized Downtime: Ensures minimal disruption to services during deployments by allowing for rolling updates and automated rollback on failure.
  • Monitoring and Reporting: Provides detailed monitoring and reporting of deployment status, enabling developers to track the progress and health of deployments.
  • Integration with CI/CD Pipelines: Easily integrates with other AWS services and third-party tools to create a comprehensive continuous integration and continuous delivery (CI/CD) pipeline.
  • Support for Various Application Types: Can be used to deploy a variety of application types, including serverless applications, containerized applications, and traditional server-based applications.
  • On-Premises Support: Allows for deployments not only to AWS resources but also to on-premises servers, enabling hybrid cloud deployments.


The benefits of using CodeDeploy are:

  • All process is automated, no need to keep track what's been deployed to which instance.
  • The same revision is deployed to all instances in all environments consistently. 
  • Application is kept highly available while performing rolling updates across all EC2 instances. 
  • It prevents downtime.

CodeDeploy >> Deploy >> Applications >> Create application




Application configuration:
  • Application name e.g. echo
  • Compute platform
    • EC2/On-premises - we'll select this one
    • AWS Lambda
    • Amazon ECS


Once application is created, we need to create deployment group(s) in order to be able to deploy it.
We can create multiple deployment groups e.g. for development, staging, production.




Before this, we need to create a role which allows CodeDeploy to access AWS resources. To do this, we need to go IAM >> Access Management >> Roles >> Create role, select CodeDeploy, then select one of 3 use cases:
  • CodeDeploy. Allows CodeDeploy to call AWS services such as Auto Scaling on our behalf
  • CodeDeploy - ECS. Allows CodeDeploy to read S3 objects, invoke Lambda functions, publish to SNS topics and update ECS services on your behalf
  • CodeDeploy for Lambda. Allows CodeDeploy to route traffic to a new version of an AWS Lambda function version on your behalf 



Next, we need to select permissions.



We can filter out only those policies related to CodeDeploy.

For EC2/On-Premises deployments, we need to attach the AWSCodeDeployRole policy. It provides the permissions for your service role to:
  • Read the tags on your instances or identify your Amazon EC2 instances by Amazon EC2 Auto Scaling group names.
  • Read, create, update, and delete Amazon EC2 Auto Scaling groups, lifecycle hooks, and scaling policies.
  • Publish information to Amazon SNS topics.
  • Retrieve information about CloudWatch alarms.
  • Read and update Elastic Load Balancing.
AWSCodeDeployRole:
  • Allows EC2 instances to call AWS services on your behalf.
  • Provides CodeDeploy service access to expand tags and interact with Auto Scaling on your behalf



We can then add tags and review created role where we can set role name e.g. MyCodeDeployRole.





We can now proceed with creating a deployment group:

CodeDeploy >> Applications >> select our application (e.g. echo) >> in Deployment groups tab click on Create deployment group

  • Name: e.g. Dev, Beta, Prod
  • Service role. Choose a service role with CodeDeploy permissions that grants AWS CodeDeploy access to your target instances. We need to type in Service role ARN which can be copied from role's page in IAM e.g. arn:aws:iam::231993119338:role/MyCodeDeployRole
  • Deployment type. Choose how to deploy your application:
    • In-place. Updates the instances in the deployment group with the latest application revisions. During a deployment, each instance will be briefly taken offline for its update
    • Blue/green. Replaces the instances in the deployment group with new instances and deploys the latest application revision to them. After instances in the replacement environment are registered with a load balancer, instances from the original environment are deregistered and can be terminated. 
  • Environment configuration. Select any combination of Amazon EC2 Auto Scaling groups, Amazon EC2 instances and on-premises instances to add to this deployment. 
    • Amazon EC2 Auto Scaling groups. You can select up to 10 Amazon EC2 Auto Scaling groups to deploy your application revision to.
    • Amazon EC2 instances. You can add up to three groups of tags for EC2 instances to this deployment group. We can specify tags and values associated with those EC2 instances we want to be included. e.g. Key = Name and Value = BetaBox or Key = Environment and Value = Beta. 
    • On-premises instances. You can add up to three groups of tags for EC2 instances to this deployment group.

  • Agent configuration with AWS Systems Manager. AWS Systems Manager will install the CodeDeploy Agent on all instances and update it based on the configured frequency. Install AWS CodeDeploy Agent
    • Never
    • Only once
    • Now and schedule updates e.g. every 14 days
  • Deployment settings - Deployment configuration. Choose from a list of default and custom deployment configurations. A deployment configuration is a set of rules that determines how fast an application will be deployed and the success or failure conditions for a deployment. 
    • CodeDeployDefault.AllAtOnce
    • CodeDeployDefault.HalfAtATime
    • CodeDeployDefault.OneAtTime
Here is a more comprehensive list of all Deployment Configurations:




  • Load balancer. Select a load balancer to manage incoming traffic during the deployment process. The load balancer blocks traffic from each instance while it's being deployed to and allows traffic to it again after the deployment succeeds.
    • Enable load balancing - check box. 


When clicking on Create deployment group, you might get the following error:


To fix this:






Now we need to create a deployment.

TBC...

 

CodeDeploy agent

 
 
CodeDeploy agent is an application that needs to be installed and running on the EC2 instance onto which CodeDeploy service deploys our application. 

This agent can already be present on the custom AMI we build or, we can add command which installs it, to user data configuration before launching the EC2 instance.
 

Application specification (AppSpec) file

 
 
During the deployment CodeDeploy agent will unpack the archive and copy its content to the root 

We can control what gets executed during the deployment. 

https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent.html
https://stackoverflow.com/questions/53103139/the-codedeploy-agent-did-not-find-an-appspec-file-within-the-unpacked-revision-d
https://cloudacademy.com/blog/how-to-deploy-application-code-from-s3-using-aws-codedeploy/
https://stackoverflow.com/questions/42000069/deployment-getting-failed-in-aws-code-deploy-before-install
https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html
https://docs.aws.amazon.com/codedeploy/latest/userguide/application-specification-files.html
https://stackoverflow.com/questions/47931381/why-wont-my-aws-codedeploy-run-my-script-after-deployment



Resources:


No comments: