Build a CI/CD Pipeline on AWS
What is a CI/CD Pipeline?
CI and CD stand for continuous integration and continuous delivery/continuous deployment. A CI/CD pipeline automates the process of software delivery. It builds code, runs tests, and helps you to safely deploy a new version of the software. By automating the process, the objective is to minimize human error and maintain a consistent process for how software is released.
The goal of this article is to guide you through a practical example of what it looks like when you’re building, testing, and deploying applications with AWS CodePipeline.
OBJECTIVE
Your team has asked you to create a way to automate the deployment of an app for your developers. Currently, your developers have to go through the build process manually to test each new update to their code. You’ll need to provide the static site URL to the developers and also make a modification to the code in the GitHub repo to verify the pipeline is working.
LETS GET STARTED!
STEP 1: Create S3 Bucket
Sign-in into AWS Management console with your credentials and navigate to S3 from the search bar.
To create S3 Bucket, click on “Create bucket” as shown below:
Enter a “Bucket name”, choose the “Region” where you want to create the bucket. In the “Block Public Access settings for this bucket section”, uncheck the box for Block all public access. Leave the other settings as and create the bucket by clicking on “Create bucket”.
STEP 2: Configure static website hosting for the S3 bucket
Navigate to the newly created bucket and then click on the “Properties” tab.
Scroll to the bottom and choose edit on the “Static website hosting” section
Select “Enable” and fill in the “index.html” and “error.html” fields and click on “Save changes” as shown below:
STEP 3: Grant Permissions
Navigate to the “Permissions” tab for the S3 Bucket.
Under Bucket Policy, click on “Edit”, next click on “Policy Generator” and find the Action for “GetObject”, and Click “Generate Policy” and the following code will be generated:
{
"Id": "Policy1659540315262",
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1659540307023",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::new-bucket-cicd-july22/*",
"Principal": "*"
}
]
}
Click the “Permissions” tab again and erase the current policy and replace it with the code from above. *Note: if you’re following along with this project, replace the ARN with the name of your bucket. Do not forget to add the /* after your bucket name! Click on “Save changes”
You’ll see that the bucket is now publicly accessible/has public access. We can move on to setting up our pipeline!
STEP 4: Connect to GitHub:
Sign in to the AWS Management Console, and open the Developer Tools console at https://console.aws.amazon.com/codesuite/settings/connections.
To create a connection to a GitHub, under Select a provider, choose “GitHub”
Under GitHub connection settings, your connection name appears in the Connection name. Choose “Connect to GitHub”. The access request page appears as the following:
STEP 5: Create CI/CD Pipeline
Navigate to “CodePipeline” in the AWS management console. Click on “Create pipeline”, type the name of your new pipeline, and click “Next” as shown below:
Under Source provider, select “GitHub (Version 2)”. Select your existing GitHub connection in the dropdown menu. Select the “Repository name” in your GitHub account. Select the “Branch name” and click on “Next” as shown below.
Select “AWS CodeBuild” as the build provider and click on “Create project”.
Under Project creation, name your project, under environment choose “Managed Image”, choose runtime as “standard”, choose the latest version of the image, and choose a “new service role”. Under “BuildSpec” choose the default setting and then continue to pipeline.
Click on “Next” to proceed as shown below:
For the deploy stage, select your S3 bucket as shown in the following image:
Click “Next” to review your pipeline and lastly, click “Create pipeline”.
STEP 6: Verify that the static website is accessible
Navigate to S3 in a new browser tab. Click on the S3 bucket name, “new-bucket-cicd-july22”. Click on “Properties”. Scroll to the bottom of the page and under “Static website hosting”, click the Bucket website endpoint URL.
You should see the following screen when you click the URL:
STEP 7: Verify that the CodePipeline is triggered when you make changes to the source code
Now is the time to test our CI/CD Pipeline. Changes should be auto-deployed by making changes to any of the file. For this, navigate to your GitHub and edit the index.html.
Commit & Push code back to your GitHub Repository
Verify that the CodePipeline is triggered and updates your static website!
And Congratulations! You’ve just built your first CI/CD pipeline using GitHub and CodePipeline.