AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services.
What we are doing
Create IAM Role for AWS CodeDeploy
CodeDeploy needs Permission (IAM Role) to execute scripts and deploy your Application changes to EC2, or ECS, etc, we are creating for EC2 ,
because we are Creating a role for CodeDeploy which will deploy changes to EC2 That is why i am choosing first use case ,
Create AWS CodeDeploy Application, Deployment Group
Create AWS CodeDeploy Deployment Group
When We create Deployment the Source (Revision, Changes ) of the Application can be in S3 Bucket or Github, Output of CodeBuild, CodePipeline (also stored in Github), etc.
For this demo, let's set up an S3 bucket as the source for CodeDeploy. Later on, we'll explore how to modify CodeDeploy to use the output from CodeBuild as the source.
Create A S3 Bucket for AWS CodeDeploy
Creating a Deployment Package for AWS CodeDeploy: Zipping "source" Directory with "index.html"
CodeDeploy needs a source (Revision, Changes ) in one of the following extensions
tar,zip , tar.gz, tgz
I created a "source" directory, added an "index.html" file with the content "hello world," and then zipped the entire "source" directory into "source.zip."
and then upload
source.zip in the newly created S3 bucket
Create a First Deployment in AWS CodeDeploy
Now go back to the Previous Tab and create a deployment
It may remain in a progress state for 5 to 10 minutes as we haven't configured the CodeDeploy agent on our EC2 instance.
CodeDeploy agent was not able to receive the lifecycle event. Check the CodeDeploy agent logs on your host and make sure the agent is running and can connect to the CodeDeploy server.
The overall deployment failed because too many individual instances failed deployment, too few healthy instances are available for deployment, or some instances in your deployment group are experiencing problems.
Install CodeDeploy Agent in EC2 Ubuntu
sudo apt update && sudo apt install ruby-full wget cd /home/ubuntu # CHANGE region code if your EC2 is not in us-east-1 , you can see in instance # availibity zone wget https://aws-codedeploy-us-east-1.s3.us-east-1.amazonaws.com/latest/install chmod +x ./install sudo ./install auto sudo service codedeploy-agent status
After installing CodeDeploy you should have its status
Give codeDeploy Other Retry
InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Missing credentials - please check if this instance was started with an IAM instance profile
Attach EC2 Instance Profile (IAM Role ) for CodeDeploy
We need to attach a role to our EC2 machine,
Note: It takes some time for the attached role changes to reflect in EC2.
Attach IAM Role to EC2
Select your EC2 instance and attach IAM Role you just reated
wait for some time, if you are waiting please make sure to restart the code Deploy Agent in your EC2
sudo service codedeploy-agent restart
Create Another Deployment after adding IAM Role to EC2
if we check the logs of codedeploy-agent
tail -f /var/log/aws/codedeploy-agent/codedeploy-agent.log
CodeDeploy can talk to EC2 and EC2 also has permission to download code from s3 , when deploying through CodeDeploy we need to configure three parameters
CodeDeploy requires this specification for a deployment:
Revision - Specifies what to deploy.
Deployment group - Specifies where to deploy.
Deployment configuration - how to deploy. (Using Appspec.yml)
What is Appspec.yml
An AppSpec file, in YAML format, guides AWS CodeDeploy on file copying and script execution. Placed in the revision root directory, it comprises two sections:
Files Section: Identifies source files for copying and their destination on each instance.
Hooks Section: Defines script locations (relative paths from the revision root) for execution during deployment lifecycle events.
Create Another Deployment With an IAM Role to EC2, Appspec in S3
Create a deployment package
v2-source.zip containing an
appspec.yml , ndex.html file with deployment instructions. Upload this package to an S3 bucket and initiate a new deployment with AWS CodeDeploy. Adjust the S3 URI and CodeDeploy application/deployment group.
version: 0.0 os: linux files: - source: / # Destination in EC2 destination: /home/ubuntu/myproject
Create Another New Deployment
Deployment lifecycle and hooks
You can learn more about the deployment lifecycle and hooks