DEV Community

Udoh Deborah
Udoh Deborah

Posted on

Day 53: CI/CD pipeline on AWS pt 4

Prerequisites (quick check)
• You already have a CodeCommit repo containing: index.html, buildspec.yml, appspec.yml, and any scripts/ (e.g., scripts/restart_nginx.sh).
• An EC2 instance running (Amazon Linux 2 recommended), with nginx installed and CodeDeploy agent installed & running on it. If the agent is not installed, follow the agent install guide. 
• A CodeBuild project that builds your repo and produces an artifact (buildspec.yml must be correct). 
• An S3 bucket in the same region to be used as CodePipeline’s artifact store (you’ll create this below).
• IAM roles: CodePipeline service role (allows CodePipeline to interact with CodeBuild/CodeDeploy/S3), CodeBuild service role, and CodeDeploy service role (or use console auto-created roles). I’ll point to the IAM guidance below. 

Part A — Create a CodeDeploy Application + Deployment Group (console + CLI)

Console (recommended for first time)
1. Open AWS Console → CodeDeploy → Applications → Create application.
• Name: MyCodeDeployApp
• Compute platform: EC2/On-premises.
2. After app creation, go to Deployment groups tab → Create deployment group.
• Deployment group name: MyDeploymentGroup
• Service role: pick or create an IAM role that allows CodeDeploy to act on resources (console can auto-create).
• Environment configuration: choose how to identify EC2 targets:
• EC2 instance tags (recommended) — tag your EC2 instance(s), then supply tag filters here; OR
• Auto Scaling group or Amazon EC2 instance IDs.
• Leave other defaults (or set rollback/trigger options as needed) and Create deployment group.
3. You now have a deployment group that CodeDeploy can target.

CLI (example)

Tag your instance first (or use instance IDs). Example:

aws ec2 create-tags --resources i-0123456789abcdef0 --tags Key=CodeDeploy,Value=day53


Enter fullscreen mode Exit fullscreen mode

Create deployment group (replace ARNs and names):

aws deploy create-deployment-group \
  --application-name MyCodeDeployApp \
  --deployment-group-name MyDeploymentGroup \
  --service-role-arn arn:aws:iam::123456789012:role/CodeDeployServiceRole \
  --ec2-tag-filters Key=CodeDeploy,Value=day53,Type=KEY_AND_VALUE \
  --deployment-config-name CodeDeployDefault.OneAtATime \
  --auto-rollback-configuration enabled=true,events=DEPLOYMENT_FAILURE

Enter fullscreen mode Exit fullscreen mode

Part B — Verify appspec.yml and deploy scripts

Your appspec.yml (placed at repo root) should map files and define hooks. Example minimal appspec.yml for EC2/nginx:

version: 0.0
os: linux
files:
  - source: /index.html
    destination: /usr/share/nginx/html/

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

Enter fullscreen mode Exit fullscreen mode

And scripts/restart_nginx.sh:

#!/bin/bash
sudo systemctl restart nginx

Enter fullscreen mode Exit fullscreen mode

Make sure the scripts are executable (chmod +x scripts/*.sh) and pushed to CodeCommit. (AppSpec reference). 

Part C — Create the S3 artifact bucket for CodePipeline

CodePipeline needs an artifact store S3 bucket in the pipeline region:

aws s3 mb s3://my-pipeline-artifacts-123456789012 --region <your-region>

Enter fullscreen mode Exit fullscreen mode

Replace with a unique bucket name.

Part D — Create the CodePipeline (Console — easiest)
1. Open AWS Console → CodePipeline → Create pipeline.
2. Pipeline settings:
• Pipeline name: MyPipeline-Day53
• Service role: Let CodePipeline create a new service role (console will create a role named like AWS-CodePipeline-Service), or specify your role ARN. (Console auto-creation is easiest.) 
• Artifact store: choose the S3 bucket you created.
3. Add Source stage:
• Provider: AWS CodeCommit
• Repository: select your repo (e.g., MyDemoRepo)
• Branch: main (or your branch)
4. Add Build stage:
• Provider: AWS CodeBuild
• Project name: select the CodeBuild project you created earlier (it will use the buildspec.yml to build). 
• Ensure the CodeBuild project outputs an artifact (e.g., BuildOutput) if you need to pass files to CodeDeploy.
5. Add Deploy stage:
• Provider: AWS CodeDeploy
• Application Name: select MyCodeDeployApp
• Deployment Group: select MyDeploymentGroup
6. Create pipeline. CodePipeline will start and show the stages. Push a commit to the branch to trigger it.

Part E — Test the pipeline
1. Make a small change to index.html (e.g., change text) and push to CodeCommit:

git add index.html
git commit -m "Test pipeline - Day 53"
git push origin main

Enter fullscreen mode Exit fullscreen mode
  1. Go to CodePipeline console and watch the pipeline progress: Source → Build → Deploy.
    1. When deployment completes, open your EC2 public IP or load balancer URL; you should see the updated index.html. If not, check CodeDeploy deployment logs and CodeDeploy agent status on the EC2 instance. 

Quick troubleshooting (most common issues)
• Pipeline fails at Source: wrong repo/branch or CodePipeline role lacks permission. Check role and repository name. 
• CodeBuild fails: check buildspec.yml syntax and build logs in CodeBuild. 
• CodeDeploy fails: ensure the CodeDeploy agent is installed & running on EC2, appspec.yml paths are correct, and scripts are executable. Check /var/log/aws/codedeploy-agent/codedeploy-agent.log on the instance. 
• Permission/Role errors: confirm service roles exist (CodePipeline role, CodeBuild role, CodeDeploy role) and have necessary policies (console auto-creation simplifies this). 

Top comments (0)