How to Set Up CI/CD on AWS

4.25K 0 0 0 0

📘 Chapter 1: Creating a Source Repository and IAM Roles

🔍 Overview

The first step to building a CI/CD pipeline on AWS is to establish a reliable source repository for your code and configure IAM (Identity and Access Management) roles for secure automation. This chapter walks you through creating and configuring a source code repository using AWS CodeCommit or third-party options like GitHub and GitLab. You’ll also set up IAM roles and policies needed for AWS CodePipeline, CodeBuild, and CodeDeploy to operate securely and effectively.


🧱 1. Choosing a Source Code Repository

Before setting up your pipeline, your code must be stored in a version control system (VCS).

Popular Options

Repository Type

AWS Integration Level

Use Case

CodeCommit

Native integration

Fully AWS-managed solution

GitHub

First-class support

Widely used, social coding

GitLab/Bitbucket

Moderate integration

Third-party DevOps pipelines


🔧 Creating a CodeCommit Repository (AWS CLI)

bash

 

aws codecommit create-repository \

  --repository-name MyWebApp \

  --repository-description "Code repo for web app CI/CD"

You can also create one via the AWS Management Console:

  • Go to CodeCommit
  • Click Create Repository
  • Name it (e.g., MyWebAppRepo)
  • Add a description and tags (optional)

🔐 Cloning the Repository (HTTPS or SSH)

To clone it locally:

bash

 

git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyWebApp

For SSH, set up SSH keys via the AWS IAM Console → Security credentialsSSH keys for AWS CodeCommit.


🔑 2. Setting Up IAM Roles and Policies

IAM roles define who can do what in your AWS account. For CI/CD, we need IAM roles for:

  1. CodePipeline – orchestrator of the pipeline
  2. CodeBuild – builder and tester of source code
  3. CodeDeploy – deployer of artifacts to EC2, ECS, or Lambda

📌 IAM Role Creation Strategy

Service

Role Name Example

Policy Required

CodePipeline

CodePipelineServiceRole

AWSCodePipelineFullAccess

CodeBuild

CodeBuildServiceRole

AWSCodeBuildDeveloperAccess + S3 access

CodeDeploy

CodeDeployServiceRole

AWSCodeDeployRole or custom deployment policy


🛠️ 3. Create an IAM Role for CodePipeline

Using AWS CLI:

bash

 

aws iam create-role \

  --role-name CodePipelineServiceRole \

  --assume-role-policy-document file://codepipeline-trust.json

codepipeline-trust.json:

json

 

{

  "Version": "2012-10-17",

  "Statement": [

    {

      "Effect": "Allow",

      "Principal": {

        "Service": "codepipeline.amazonaws.com"

      },

      "Action": "sts:AssumeRole"

    }

  ]

}

Then attach a policy:

bash

 

aws iam attach-role-policy \

  --role-name CodePipelineServiceRole \

  --policy-arn arn:aws:iam::aws:policy/AWSCodePipelineFullAccess


🛠️ 4. Create an IAM Role for CodeBuild

Trust Policy (codebuild-trust.json)

json

 

{

  "Version": "2012-10-17",

  "Statement": [

    {

      "Effect": "Allow",

      "Principal": {

        "Service": "codebuild.amazonaws.com"

      },

      "Action": "sts:AssumeRole"

    }

  ]

}

bash

 

aws iam create-role \

  --role-name CodeBuildServiceRole \

  --assume-role-policy-document file://codebuild-trust.json

Attach permissions:

bash

 

aws iam attach-role-policy \

  --role-name CodeBuildServiceRole \

  --policy-arn arn:aws:iam::aws:policy/AWSCodeBuildDeveloperAccess

🔐 Add S3 permissions for artifact upload/download.


🛠️ 5. Create an IAM Role for CodeDeploy

Use predefined managed policy:

bash

 

aws iam create-role \

  --role-name CodeDeployServiceRole \

  --assume-role-policy-document file://codedeploy-trust.json

codedeploy-trust.json:

json

 

{

  "Version": "2012-10-17",

  "Statement": [

    {

      "Effect": "Allow",

      "Principal": {

        "Service": "codedeploy.amazonaws.com"

      },

      "Action": "sts:AssumeRole"

    }

  ]

}

Attach policy:

bash

 

aws iam attach-role-policy \

  --role-name CodeDeployServiceRole \

  --policy-arn arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole


🔐 6. Best Practices for IAM and Source Repositories

  • Follow least privilege: Only allow minimum access needed for each service
  • Use managed policies: Faster to get started, and updated by AWS
  • Use environment variables: Don’t hardcode IAM credentials in pipelines
  • Rotate credentials: Especially SSH/Git keys used in CodeCommit
  • Tag your roles: For cost tracking and auditability

📋 Summary Table – Source & IAM Setup


Task

CLI Command / Tool

Create CodeCommit Repo

aws codecommit create-repository

Create CodePipeline Role

aws iam create-role + AWSCodePipelineFullAccess

Create CodeBuild Role

aws iam create-role + AWSCodeBuildDeveloperAccess

Create CodeDeploy Role

aws iam create-role + AWSCodeDeployRole

Clone Repo (HTTPS)

git clone https://git-codecommit...

Trust Policy Files

codepipeline-trust.json, codebuild-trust.json, etc.

Back

FAQs


❓1. What is CI/CD, and how does it work on AWS?

Answer:
CI/CD stands for Continuous Integration and Continuous Delivery/Deployment. On AWS, you can implement CI/CD using tools like CodeCommit (source control), CodeBuild (build & test), CodeDeploy (deployment), and CodePipeline (orchestration). These services automate the entire software delivery process from code changes to production releases.

❓2. Do I need to use AWS CodeCommit to set up CI/CD on AWS?

Answer:
No. You can integrate AWS CI/CD tools with external repositories like GitHub, GitLab, or Bitbucket. AWS CodePipeline and CodeBuild support webhook-based triggers and OAuth integrations with these platforms.

❓3. What is the difference between CodePipeline and CodeDeploy?

Answer:

  • CodePipeline is the orchestration tool that automates the flow from code to build to deployment.
  • CodeDeploy is specifically responsible for deploying your built application to compute targets like EC2, Lambda, or ECS.

❓4. How secure is the CI/CD process on AWS?

Answer:
Very secure—each service uses IAM roles with least privilege, encryption in transit and at rest, audit logging via CloudTrail, and VPC/private connections if needed. You can also integrate AWS Secrets Manager or Key Management Service (KMS) for secret management.

❓5. What kind of applications can I deploy using AWS CI/CD?

Answer:
You can deploy web apps, microservices, REST APIs, containerized apps (ECS/EKS), mobile backends, static sites, or serverless functions. AWS CI/CD supports Node.js, Python, Java, Go, Ruby, .NET, and more.

❓6. Is there a cost associated with AWS CI/CD tools?

Answer:
Yes, but the pricing is very granular:

  • CodePipeline: $1 per active pipeline/month
  • CodeBuild: Pay per build minute
  • CodeDeploy: Free for EC2 and Lambda (extra for on-premise)
  • CodeCommit: Free for up to 5 active users

❓7. Can I set up CI/CD for containerized applications?

Answer:
Absolutely. AWS CI/CD can build Docker images with CodeBuild, store them in Amazon ECR, and deploy them to ECS, EKS, or Fargate using CodePipeline and CodeDeploy integrations.

❓8. What is a buildspec.yml file?

Answer:
buildspec.yml is a YAML configuration file used by CodeBuild. It defines how to install dependencies, run tests, build code, and package artifacts during a CI/CD pipeline execution.

❓9. Can I use CodePipeline with GitHub Actions or Jenkins?

Answer:
Yes. You can trigger a CodePipeline from a GitHub webhook or use a CodePipeline source action for GitHub. Jenkins can also trigger CodePipeline stages via API or use AWS CLI commands in post-build steps.

Tutorials are for educational purposes only, with no guarantees of comprehensiveness or error-free content; TuteeHUB disclaims liability for outcomes from reliance on the materials, recommending verification with official sources for critical applications.