Welcome to our comprehensive guide on setting up a Continuous Integration and Continuous Deployment (CI/CD) pipeline using AWS CodeCommit for version control and Amazon S3 for website hosting. This blog will walk you through each step, from creating the necessary IAM roles to deploying your application in different environments using S3 buckets. By the end of this blog, you’ll be equipped with the knowledge to efficiently manage your web application’s lifecycle on AWS.
Introduction
In today’s fast-paced development world, automating the build, test, and deployment processes is crucial for efficient and error-free application delivery. AWS offers a suite of services to facilitate this, including CodeCommit for version control, S3 for hosting static websites, and various CI/CD tools for automation.
Prerequisites
Before we dive into the steps, ensure you have the following:
- An active AWS account with permissions to create IAM roles, S3 buckets, CodeCommit repositories, and CodePipeline pipelines.
- Basic understanding of IAM (Identity and Access Management).
- A public repository on GitHub with the sample code.
Step 1: Create Required IAM user and required credentials.
- Navigate to the IAM Management Console in your AWS Dashboard and attached required Permissions or AdministratorAccess
- Go to User’s Security credentials Tab and Generate Access keys required for AWS CLI login.
Access keys for AWS CLI
HTTPS Git credentials for AWS CodeCommit. for Git Login. - AWS CodeCommit will automatically create AWS Service Role for CodeCommit.
Step 2: Create S3 Buckets for Test and Production Environments
- Open the S3 Management Console: From your AWS Dashboard, go to the S3 service.
- Create New Bucket: Click “Create bucket”.
Name your bucket (must be unique globally), select the region, and keep the default settings. Repeat this step to create two buckets: one for the test environment and another for production.
Buckets Name: “examnative-test and examnative-prod”.
Step 3: Enable S3 Bucket for Hosting Public Website
- Select Your Bucket: From the S3 dashboard, click on the bucket you created for the test environment.
- Enable Static Website Hosting: Navigate to the “Properties” tab and click on “Static website hosting”. Choose “Use this bucket to host a website” and enter the index document (e.g.,
index.html
). Repeat for the production bucket. - Set Bucket Policy: Go to the “Permissions” tab and edit the bucket policy to allow public access to your website content, and make sure to update your bucket name in given policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}
Step 4: Set Up AWS CodeCommit and Import Code from GitHub
Download Application Code to Local System
- Initialize your local folder with git using git init.
- Clone your GitHub repository using
git clone <GitHub_Repo_URL>
. ExamNative/UseCases (github.com) - Navigate into your repository directory.
Upload Code to AWS CodeCommit
- Set up Git credentials for CodeCommit in the IAM dashboard under your user’s “Security credentials” tab. (IAM -> User -> Security Credentials -> HTTPS Git credentials for AWS CodeCommit -> Generate credentials -> Download credentials)
- Change the remote repository URL to your CodeCommit repository’s HTTPS URL using
git remote set-url origin <CodeCommit_Repo_URL>
.
# git remote add https://git-codecommit.us-east-1.amazonaws.com/v1/repos/examnative-codecommit-repo
#git remote set-url origin https://git-codecommit.us-east-1.amazonaws.com/v1/repos/examnative-codecommit-repo
- Push your code to CodeCommit using,
git push --set-upstream origin ma
in.
Step 5: Set Up AWS CI/CD Pipeline
Source Stage: AWS CodeCommit
Create a Pipeline: Go to the AWS CodePipeline dashboard, click “Create pipeline”, name your pipeline, with default setup.
And Next Add storage stage: Set the source provider as CodeCommit, select your repository and the branch.
Skip Build Phase
When configuring your pipeline, you can skip the build phase by selecting “Skip build stage” since we are deploying a static website.
Deploy Application on S3 Test Environment
Add Deploy Stage: Choose “Amazon S3” as the deploy provider, select the test environment bucket, and enable “Extract file before deploy”.
Review all the settings and click on Create Pipeline. The pipeline has been created and automatically executed for our test environment.
Your test environment setup is ready, test your website by hitting on test bucket website URL.
From here we will start set-up for Prod Environment.
Manual Intervention and SNS Configuration
Note: SNS Topic and Subscribe with Email (Already Done)
- Now edit the current pipeline (Developer Tools -> CodePipeline ->Pipelines -> pipeline -> Edit).
- Add Manual Approval Stage: Before the production deployment, add a manual approval stage, action and configure SNS to notify you when approval is needed.
Deploy Application to Production Environment
Add one more stage after Manual Approval Stage: Deployment-Prod.
Deployment-Prod (New Stage) -> Add Action Group ->Edit Action
Enter the required details for the production environment, ensuring you select the production bucket this time. And we also provide the URL for Review (URL of Test Environment)
As Our test environment is up and running and we want to execute same code to Prod Environment.
We need to click button “Release changes.” Pipeline will wait for Approver for Approval.
After successfully reviewed and approved, it will start deploying to Production environment.
Conclusion
By following these steps, you’ve successfully automated the deployment of your web application with AWS CI/CD tools, leveraging CodeCommit for version control and S3 for hosting. This setup not only simplifies your development workflow but also ensures a reliable and efficient delivery process. Happy coding!