๐ Tired of deploying your app manually every time you push code to GitHub?
What if I told you that with just a few clicks, you can automate the entire process โ from a GitHub commit to your app running live on an EC2 instance.
In this tutorial, Iโll walk you through how AWS Developer Tools โ CodeBuild, CodeDeploy, and CodePipeline โ work together to create a seamless deployment pipeline. No more manual SSH into EC2, no more missed steps โ just commit, build, deploy, repeat.
๐งPrerequisites
Before we jump into AWS, hereโs what youโll need:
- Source Control: GitHub (you can also use CodeCommit, but here weโll stick with GitHub).
- Compute: EC2 instance.
- CI/CD Tools: CodePipeline, CodeBuild, CodeDeploy.
- IAM Roles: Weโll create service roles for CodePipeline, CodeBuild, and CodeDeploy, and instance profile for EC2.
- Sample Application: This works with any stack โ Node.js, Python, Java, or even a static website.
- ๐ If you donโt have one, feel free to fork my GitHub repo and use that as your sample app.
- Link to my Github repo: https://github.com/Imash24/AWS-CI-CD-DEMO
Step 1: Create an EC2 Instance:
- Go to the EC2 Console โ Launch a new instance.
- Pick Amazon Linux 2 (free-tier eligible).
- Create/attach a security group that allows:
- Port 22 (SSH) โ so you can connect to it.
- Port 3000 (or whatever port your app runs on) โ so you can actually access your app in the browser.
Once your Instance is up, you can use ssh to connect to your Instance or easiest way is to use EC2 Instance Connect.

After Connecting to your Console , We need to install the node.js as well the Codedeploy agent.
๐ But wait, whatโs this CodeDeploy Agent thing?
Think of it as a messenger that lives inside your EC2 instance. When AWS CodeDeploy pushes a deployment, the agent is the one that:
- Listens for instructions from the CodeDeploy service.
- Pulls the application files (artifacts) from S3 or GitHub.
- Runs the scripts you define in appspec.yml (like install dependencies, restart the app, etc.).
Without the agent, your EC2 has no idea that CodeDeploy even exists โ so itโs a must-have.
Now Lets install the Node.js and CodeDeploy agent.
# Update packages
sudo yum update -y
# Install Node.js and npm
sudo yum install -y nodejs npm
# Install CodeDeploy Agent
sudo yum install -y ruby wget
cd /home/ec2-user
wget https://aws-codedeploy-<your-region>.s3.<your-region>.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
sudo service codedeploy-agent start
- Make sure to replace {your-region} to match your region, in my case it is ap-south-1.
โ Verify the CodeDeploy Agent
Now that weโve installed the CodeDeploy agent, letโs make sure itโs actually running.
Run this command:
sudo systemctl status codedeploy-agent.service
Step 2: Setting up CodeDeploy Application
Now We have created an Ec2 instance and also setup the CodeDeploy agent on EC2, Lets set up the CodeDeploy Application.
A CodeDeploy Application = just a container that represents your app in AWS.

1๏ธโฃ Create a CodeDeploy Application
- Go to the AWS CodeDeploy Console โ Applications โ Create application.
- Give it a name (e.g., cicd-demo-app).
- Choose Compute platform โ EC2/On-premises.
2๏ธโฃ Create a Deployment Group:
Inside your application, click Create deployment group.
Give it a name (e.g., cicd-app-deployment-grp).
Service role:
Youโll need a service role for CodeDeploy (something like CodeDeployServiceRole).
Attach the managed policy: AWSCodeDeployRole.
This allows CodeDeploy to talk to your EC2.
Lets Create an Service role for CodeDeploy,
- Go to the IAM Console โ Roles โ Create role.
- For Trusted entity type, choose AWS service.
- For Use case, select CodeDeploy โ CodeDeploy.
- Click Next and attach the managed policy:
- AWSCodeDeployRole (this gives CodeDeploy the permissions it needs).
- Give the role a name (e.g., CodeDeployServiceRole).
- Click Create role.
Environment configuration:
- Choose Amazon EC2 instances.
- Pick your EC2 using Tags, My case i tagged as server.
Deployment settings:
Choose โOne at a timeโ for simplicity (safe deployment).
Once done, CodeDeploy knows:
๐ โThis application gets deployed to this EC2 instance with these rules.โ
Step 3: Create a CodeBuild Project:
Now We Created and also setup the CodeDeploy that actually knows where to deploy our application (EC2).
1๏ธโฃ Go to CodeBuild Console
- Click Create build project.
- Give it a name (e.g., cicd-demo-build).
2๏ธโฃ Configure Source
Source provider: GitHub (connect your repo or forked repo). Or simply you can select the public repository option and paste my GitHub repository. link.

3๏ธโฃ Environment
- Environment image: Managed image.
- Operating system: Amazon Linux 2.
- Runtime: Standard.
- Service role: Create a new role (CodeBuild will do this automatically).
4๏ธโฃ Buildspec
The buildspec.yml file gives main instructions to ** CodeBuild.** It tells AWS exactly how to build your application โ whether thatโs installing dependencies, running tests, or packaging files.
Iโve already pushed my own buildspec.yml file into my GitHub repo, so CodeBuild will automatically pick it up during the build stage.
๐ In short, this file is where we define:
- How to install dependencies
- How to run build/test steps
- What files should be bundled as artifacts for deployment
โ
CodeBuild Completed Successfully!
Weโve set up and completed our CodeBuild project, and our application can now be built automatically using the instructions we defined in the buildspec.yml file. (The YAML handles dependency installation, build, and artifact creation).

- The next logical step is to orchestrate the entire CI/CD flow โ and thatโs exactly where AWS CodePipeline comes in.
๐ What is CodePipeline?
AWS CodePipeline is a fully managed CI/CD orchestration service that automates the software release process. It connects different stages of delivery โ Source โ Build โ Test โ Deploy โ into a single continuous pipeline.
- With CodePipeline, every time you push code to your repository:
- The pipeline automatically detects the change (via webhook or polling).
- It triggers CodeBuild to build and test the application.
- Once artifacts are ready, it hands them over to CodeDeploy (or another deploy service).
- The application is deployed to the target environment (EC2, ECS, Lambda, etc.).
1๏ธโฃ Go to CodePipeline in AWS Console
Open the AWS Management Console โ Search for CodePipeline โ Click Create pipeline and select Build a custom pipeline.

2๏ธโฃ Pipeline Settings
Pipeline name โ cicd-demo-pipeline (you can name it anything you want).
Service role โ Select โNew service roleโ (AWS will automatically create a role for CodePipeline with the right trust relationships).
Leave advanced settings as default โ Click Next.

3๏ธโฃAdd Source Stage
This is where our code comes from (GitHub in this case).
- Source provider โ GitHub (v2).
- Connect to GitHub โ Authorize your GitHub account with AWS.
- Repository โ Select your repo.
- Branch โ main (or whatever branch you want to deploy).
- Change detection โ Leave as default (CodePipeline uses webhooks to detect new commits).
- Click Next.
4๏ธโฃ Add Build Stage
- Now we connect the CodeBuild project we created earlier.
- Build provider โ AWS CodeBuild.
- Project name โ Select the project you just created.
- Click Next.
5๏ธโฃ Add Deploy Stage
This is where the built artifacts are deployed to your EC2 instance using CodeDeploy.
- Deploy provider โ AWS CodeDeploy.
- Application name โ Choose the CodeDeploy application you created.
- Deployment group โ Select the deployment group.
- Click Next.
6๏ธโฃ Review & Create
- Double-check everything โ Click Create pipeline.
- AWS will immediately trigger the pipeline for the first time.
- Youโll see stages: Source โ Build โ Deploy with live status updates.
๐จ Something Failed !!! Dont panic expected one๐
This is expected if your EC2 instance doesnโt yet have the correct IAM Instance Profile attached.
Why does this happen?
Because the CodeDeploy agent running inside your EC2 needs permissions to talk to CodeDeploy and pull artifacts from S3. Without that IAM role attached, the agent has no credentials โ so the deployment fails.

๐ Fix: Go to your EC2 instance โ attach an IAM role (Instance Profile) with permissions like AmazonS3ReadOnlyAccess and AWSCodeDeployRole. Once added, rerun the deployment and it should succeed.

- We have attached AmazonS3ReadonlyAccess and AWSCodeDeployRole.
โ
And there we go โ Success! ๐
After attaching the correct IAM Instance Profile to our EC2 and rerunning the pipeline, everything works smoothly.
- Source Stage โ grabbed the code from GitHub
- Build Stage โ CodeBuild installed dependencies and packaged the app
- Deploy Stage โ CodeDeploy agent pulled the artifact and deployed it to EC2.
๐ Accessing Our Application
Now that our pipeline has successfully deployed, letโs confirm everything is working.
- I opened my EC2 public IP in the browser at port 3000 โ and boom, our Node.js application is up and running! ๐
- The best part? I didnโt have to SSH into the server or manually copy files. The whole thing was automated through CodePipeline โ CodeBuild โ CodeDeploy.
๐ Next, letโs test the real magic of CI/CD: weโll make a small manual change in GitHub, push the code, and watch the pipeline automatically pick it up and deploy Version 2 to EC2.
After making a small change in my code, I pushed it to the main branch on GitHub.
Within seconds, CodePipeline detected the update, automatically triggered a new run, and started executing the stages again โ Source โ Build โ Deploy.
๐ Version 2 Deployed Successfully
After the pipeline finished its run, I opened the application in the browser again at port 3000, and the changes from Version 2 were live!
This proves that our CI/CD pipeline is fully functional: every time you push a commit to GitHub, CodePipeline detects it, CodeBuild packages it, and CodeDeploy deploys it โ all automatically. No manual intervention, no missed steps.
โ Conclusion / Key Takeaways
Automation is powerful: Once set up, your pipeline handles every commit, build, and deployment.
CodePipeline = orchestrator: It connects Source โ Build โ Deploy seamlessly.
CodeBuild = builder: Packages and prepares your app artifacts.
CodeDeploy = delivery agent: Pushes the code to EC2 instances and runs lifecycle scripts.
IAM Roles Matter: Both the CodeDeploy service role and EC2 instance profile are critical for permissions.












Top comments (0)