# CI/CD Using AWS

**Introduction**

CI/CD is a software development approach that focuses on automating the process of integrating code changes, testing them, and delivering them to production environments. AWS provides various services and tools that can help facilitate CI/CD pipelines.

Here’s a typical CI/CD workflow using AWS:

1.  Version Control: Developers use a version control system like Git to manage their codebase. They commit changes to a central repository.
2.  Build Stage: AWS CodeBuild is a fully managed build service that compiles source code, runs tests, and produces deployable artifacts. You can configure CodeBuild to trigger automatically whenever changes are pushed to the repository.
3.  Deploy Stage: AWS CodeDeploy is a deployment service that automates the process of deploying applications to various environments, such as EC2 instances, ECS clusters, or Lambda functions. It allows you to define deployment strategies and rollbacks in case of failures.
4.  Infrastructure as Code: AWS CloudFormation or AWS CDK (Cloud Development Kit) can be used to define infrastructure resources in a declarative manner. This enables version-controlled and automated provisioning of infrastructure as part of the CI/CD pipeline.
5.  Continuous Delivery: AWS CodePipeline is a fully managed continuous delivery service that helps orchestrate the different stages of your CI/CD pipeline. It integrates with other AWS services and third-party tools, allowing you to customize and automate your pipeline according to your requirements.

Setups to Create Ci/CD using AWS

Setup 1:  
Install the below file:  
`https://static.us-east-1.prod.workshops.aws/public/8c4076ba-a416-424d-acc7-06e5cc2de102//static/20-infrastructure/InfrastructureApp.tgz`

And Navigate into it. Install CDK application dependencies and build the CDK project.

npm install  
npm run build

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080629462/fb8071be-d8fd-46b4-8440-ac052e14c1e3.png)

Bootstrap the CDK into your account and region. This is a one-time operation that creates the resources necessary to allow CDK deployments in your AWS account to a given region.

cdk bootstrap

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080631157/2db4e493-2565-4562-8f5c-b103f00122c6.png)

Deploy the CDK application to your account and region.

cd ~/environment/InfrastructureApp  
cdk deploy

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080632621/96aff807-6486-4d30-b1ee-b9b79ab965bd.png)

Step 2:  
Create a Git repository using CodeCommit.

1.  Go to the AWS Management Console, select **services** then select **CodeCommit** under Developer Tools
2.  Select **create repository**
3.  Enter `DemoApp` in the **repository name** field
4.  Select **Create**

You should see a message at the top of the page that says **repository successfully created.**

Clone the repository URL by selecting the **clone URL** button and then selecting **clone HTTPS.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080634270/f5d92e28-9e1c-4f01-82b7-905b120636a7.png)

Populate your new, empty, repository with the demo Java application.

https://static.us-east-1.prod.workshops.aws/public/8c4076ba-a416-424d-acc7-06e5cc2de102//static/30-source/DemoApp.tgz | tar -xzv  
cd DemoApp

Commit your changes to the local Git repository and push your changes to the CodeCommit-hosted repository.

git add .  
git commit \-m "Initial commit"  
git push

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080635825/253c5cf6-b167-459d-bb98-30c827897bb8.png)

Setup 3:  
Create Build Project  
Create a build project using CodeBuild.  
Go to the AWS Management Console, select **services** then select **CodeBuild** under Developer Tools

1.  Select **create build project**

In the Project Configuration panel:

1.  Enter `DemoApp` in the **project name** field

In the Source panel:

1.  Make sure **source provider** is **AWS CodeCommit**
2.  Select **DemoApp** in the **repository** field
3.  Make sure **reference type** is **branch**
4.  Select **master** in the **branch** field

In the Environment panel:

1.  Make sure **environment image** is **managed image**
2.  Select **Ubuntu** in the **operating system** field
3.  Select **standard** in the **runtime(s)** field
4.  Select **aws/codebuild/standard:4.0** in the **image** field
5.  Make sure **image version** is **always use the latest image for this runtime version**
6.  Make sure **environment type** is **Linux**
7.  Do not select the checkbox beside the **privileged** label
8.  Select **existing service role** in the **service role** field
9.  Select the role created by your InfrastructureStack in the **role ARN** field. You will find the role ARN in the output of the CDK application deployment with the key **InfrastructureStack.BuildRoleArn**.
10.  Make sure **allow AWS CodeBuild to modify this service role so it can be used with this build project** is checked

In the Buildspec panel:

1.  Make sure the **build specifications** is **use a buildspec file**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080637403/c336145e-6357-482a-9e94-6f41b6649a62.png)

In the Artifacts panel:

1.  Select **Amazon S3** in the **type** field
2.  Select the S3 bucket created by your InfrastructureStack in the **bucket name** field. You will find the bucket name in the output of the CDK application deployment with the key **InfrastructureStack.BucketName**
3.  Enter `WebAppOutputArtifact.zip` in the **name** field
4.  Select **zip** in the **artifacts packaging** field

Do not change the defaults in the Logs panel.

Select **create build project**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080639170/ef6bc513-2baf-45ca-b439-eef2527c510d.png)

Create file name buildspec.yml and paste below code and commit and push to repo

version: 0.2  
  
phases:  
  install:  
    runtime-versions:  
      java: corretto8  
  build:  
    commands:  
      \- mvn install  
artifacts:  
  files:  
    \- target/javawebdemo.war  
    \- appspec.yml  
    \- scripts/\*\*/\*  
  discard-paths: no

Verify your build project and specification by initiating a build using the CodeBuild console.

1.  Go to the AWS Management Console, select **services** then select **CodeBuild** under Developer Tools
2.  Select your **DemoApp** build project
3.  Select **start build**
4.  Review the default options and select **start build**
5.  Verify that the status proceeds from **in progress** to **succeeded**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080640552/5da1b474-c5eb-4765-88e1-222074de9017.png)

Setup 4:  
Create an application and deployment group using CodeDeploy.

1.  Go to the AWS Management Console, select **services** then select **CodeDeploy** under Developer Tools
2.  Select **create application**

In the Application Configuration panel:

1.  Enter `DemoApp` in the **application name** field
2.  Select **EC2/on-premises** in the **compute platform** field
3.  Select **create application**
4.  Go to the AWS Management Console, select **services** then select **CodeDeploy** under Developer Tools
5.  Select **applications**
6.  Select **DemoApp**
7.  Select **create deployment group**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080642083/fe29c300-9f44-485a-916e-c5b0b87029bf.png)

In the Deployment Group Name panel:

1.  Enter `Development` in the **deployment group name** field

In the Service Role panel:

1.  Select the role created by your InfrastructureStack in the **service role** field. You will find the role ARN in the output of the CDK application deployment with the key **InfrastructureStack.DeployRoleArn**.

In the Deployment Type panel:

1.  Select **in place**

In the Environment Configuration panel:

1.  Select **Amazon EC2 instances**
2.  In **tag group 1** enter `App` as the **key** and `DemoApp` as the **value**
3.  Add a tag group
4.  In **tag group 2** enter `Env` as the **key** and `DEV` as the **value**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080643885/8cfdcc31-65c2-4d90-b021-ad8ff17f85fe.png)

In the Deployment Settings panel:

1.  Leave the default **deployment configuration** as **CodeDeployDefault.AllAtOnce**

In the Load Balancer panel:

1.  Uncheck the **enable load balancing** checkbox

Select **create deployment group.**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080645399/087dbd8c-040c-41b3-93a4-078afc270bac.png)

Create file name appspec.yml and paste below code and commit and push to repo

version: 0.0  
os: linux  
files:  
  \- source: /target/javawebdemo.war  
    destination: /tmp/codedeploy-deployment-staging-area/  
  \- source: /scripts/configure\_http\_port.xsl  
    destination: /tmp/codedeploy-deployment-staging-area/  
hooks:  
  ApplicationStop:  
    \- location: scripts/stop\_application  
      timeout: 300  
  BeforeInstall:  
    \- location: scripts/install\_dependencies  
      timeout: 300  
  ApplicationStart:  
    \- location: scripts/write\_codedeploy\_config.sh  
    \- location: scripts/start\_application  
      timeout: 300  
  ValidateService:  
    \- location: scripts/basic\_health\_check.sh

Trigger a new build of your application so that the deployment artifact stored in S3 will contain your appspec.yml file when you are ready to test your deploy.

1.  Go to the AWS Management Console, select **services** then select **CodeBuild** under Developer Tools
2.  Select your **DemoApp** build project
3.  Select **start build**
4.  Review the default options and select **start build**
5.  Verify that the status proceeds from **in progress** to **succeeded**

Verify your application and specification by initiating a deployment using the CodeDeploy console.

1.  Go to the AWS Management Console, select **services** then select **CodeDeploy** under Developer Tools
2.  Select **applications**, then select **DemoApp** application, then select your **development** deployment group
3.  Select **create deployment**

In the Deployment Settings panel:

1.  Enter `s3://<your bucket name>/WebAppOutputArtifact.zip` in the **revision location** field. You will find your bucket name in the output of the CDK application deployment

Review the remaining default options and select **create deployment**.

Verify that the status proceeds from **in progress** to **succeeded**

Verify your deployment by loading the web application in your browser with the associated EC2 Instance IP address.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080646869/0a17e428-92f0-4f57-8b6c-04ae6dd57aad.png)

Setup 5:  
Create a pipeline using CodePipeline.

1.  Go to the AWS Management Console, select **services** then select **CodePipeline** under Developer Tools
2.  Select **create pipeline**

In the Pipeline Settings panel:

1.  Enter `DemoApp` in the **pipeline name** field
2.  Select **new service role** in the **service role** field
3.  Do not change the **role name**
4.  Ensure **allow AWS CodePipeline to create a service role so it can be used with this new pipeline** is checked
5.  Select **next**

In the Source panel:

1.  Select **AWS CodeCommit** in the **source provider** field
2.  Select **DemoApp** in the **repository name** field
3.  Select **master** in the **branch name** field
4.  Ensure **Amazon CloudWatch Events** is selected in the **change detection options** field
5.  Select **next**

In the Build panel:

1.  Select **AWS CodeBuild** in the **build provider** field
2.  Do not change the **region** selection
3.  Select **DemoApp** in the **project name** field
4.  Select **next**

In the Deploy panel:

1.  Select **AWS CodeDeploy** in the **deploy provider** field
2.  Do not change the **region** selection
3.  Select **DemoApp** in the **application name** field
4.  Enter **Development** in the **deployment group** field
5.  Select **next**

Review your entries and select **create pipeline**.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080648656/adbe4904-e93e-4028-b5e6-bec7f852f2a8.png)

Your pipeline will begin executing and, after several minutes, you should see each stage succeed.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1762080650563/ea86c7c7-6fff-47da-908b-ba630f561b8f.png)

Verify your deployment by loading the web application in your browser. You will find your development server location in the output of the CDK application deployment

**Please do remember to Destroy your Resources !!!!**

**Follow me :**

**Linkedin**: [https://www.linkedin.com/in/harshaljethwa/](https://www.linkedin.com/in/harshaljethwa/)

**GitHub**: [https://github.com/HARSHALJETHWA19/](https://github.com/HARSHALJETHWA19/)

**Twitter**: [https://twitter.com/harshaljethwaa](https://twitter.com/harshaljethwaa)

**Hashnode**: [https://harshaljethwa.hashnode.dev/](https://harshaljethwa.hashnode.dev/)

Thank You!!!
