"Accelerating Your DevOps Workflow: A Comprehensive Guide to AWS CodeCommit, CodeBuild, CodeDeploy, and CodePipeline"

"Accelerating Your DevOps Workflow: A Comprehensive Guide to AWS CodeCommit, CodeBuild, CodeDeploy, and CodePipeline"

·

10 min read

1-What are Code - Commit, Build, Deploy, and Pipeline in AWS:

AWS CodeCommit is a fully-managed source control service that makes it easy for teams to collaborate on code in a secure and highly scalable environment. AWS CodeBuild is a fully-managed build service that compiles source code, runs tests, and produces software packages that are ready to deploy. AWS CodeDeploy is a fully-managed deployment service that automates software deployments to a variety of computing services such as Amazon EC2, AWS Lambda, and on-premises servers. Finally, AWS CodePipeline is a fully-managed continuous delivery service that helps you automate your release pipelines for fast and reliable application and infrastructure updates.

Together, these services provide a complete end-to-end solution for managing the development, testing, and deployment of software applications in the AWS cloud.

Let us Start with Building our Project:

2- Code - Commit:

Logged into the AWS Management Console.

Navigate to the AWS CodeCommit console by searching for it in the services search bar.

Once you're in the CodeCommit console, click the "Create repository" button.

In the "Create repository" page, give your repository a name and description, and select the region you want to create the repository in. Then, click the "Create" button.

Create a New IAM User:

Navigate to the AWS IAM console by searching for it in the services search bar.

Click on "Users" in the left navigation pane and then click the "Add user" button

In the "Add user" page, give your user a name.

Click "Next: Review" to review your settings.

In the "Review" page, review your settings and click the "Create user" button.

Retrieve the IAM User's Access Key and Secret Access Key:

On the confirmation page, you will see your IAM user's Access key ID and Secret access key. You will need these to configure your Git client to access CodeCommit.

Go to - security credentials-scroll down to - HTTPS for git credentials for code commit in AWS - Generate credentials- download credentials.

Clone the Repository and Make Changes:

In the CodeCommit console, click the "Clone URL" button to get the repository's Git URL.

Configure Git Client:

Install a Git client on your local machine if you haven't already.

Open a terminal and configure Git with the Access key ID and Secret access key of the IAM user you just created:

Use the Git client to clone the repository to your local machine

Error - unable to access because our USER does not access permission.

In the "Set permissions" page, select "Attach existing policies directly" search for "AWSCodeCommitFullAccess" Select the policy and click "Next: Tags"

Again, Use the Git client to clone the repository to your local machine

Give the Access key and Secret key for the USER to access.

Create an "Index.html" file. As we are going to use Nginx, When a user accesses a website or application hosted on an Nginx server, Nginx looks for the index.html file in the specified document root directory and returns it to the user's browser.

After making changes to the files in the repository, stage and commit your changes using Git commands such as "git add" and "git commit".

the file is being pushed to the code commit repository

Let's make some changes to the HTML file by creating another branch as "dev" and git add and commit and push to the repository - (it's not necessary to create another branch we are doing this just for practice)

We can merge the changes by "dev" to the "Master" branch.

Select - Pull Requests from the left menu bar - add master and dev branch to destination and source - create a pull request

And Merge. - Changes from dev merge to master branch.

2- Code - Build:

Go to the AWS Management Console and navigate to the CodeBuild service. Click "Create Project"

Enter a name for your project

In the "Source" section, select "AWS CodeCommit" as the source provider and choose your repository and branch.

In the "Environment" section, select a computing type (OS) and image.

select "Use a buildspec file".

Create a buildspec yaml file in your Local system in VS Code or using Terminal.

The buildspec.yml file is used in AWS CodeBuild to define the build steps and configuration for your project. It is a YAML file that provides instructions on how to build, test, and package your application or code.

And Git -add and commit and push the file to the code commit repository

back to the code build console, select "type" as no artifacts and logs are optional, and Select - "Create Build project".

As "Project is created", Now click - "Start build".

The CodeBuild project will automatically trigger a build based on the buildspec.yml file in your repository.

The build is started and we can check the status of the build - view status or details - phase details

Now, go to the build project section and go to "Edit" and select artifacts to store the file in the S3 bucket. But before we need to create an S3 bucket

Create an S3 bucket to store the built artifacts:

Go to the AWS Management Console and navigate to the S3 service. Click "Create Bucket" and enter a unique name for your bucket. Configure the bucket settings as desired and click "Create Bucket".

In build project - Edit - Artifacts - type- S3 - select bucket name - "bucket folder name" same as in S3 bucket

The path is optional but you can just add the path URL from the S3 bucket folder

Start the building project Again - now, the artifacts will be uploaded to the S3 bucket in the folder.

what are artifacts - Artifacts files are the output files generated as a result of the build process. These files represent the final product of the build process, which may be a compiled software application, a set of configuration files, or any other type of deliverable that is created as part of the build process.

In our case, it's the Index.html and build spec files.

3- Code - Deploy:

Navigate to Code Deploy - Applications - Select "Create application"

Provide the Application name and Compute platform and select "create application"

Application is created and now, the next step is to - Create deployment group

provide group name and Next go to IAM to attach necessary permissions

In IAM - Roles - select an existing role or create a new one and add permissions to the roles.

Give policies access to role

Now, Back to code deploy - deployment group - Select - Service role that you attach permission to.

Open a new tab with AWS and launch a free tier Ec2 instance.

Again back to - deployment group - In environment - Select - Amazon EC2 instance and Select - "key" as Name and in "value" give instance name

For AWS code deploy agent Install - select "never" because it has a version issue. So we install the agent manually in the instance. And "Create"

In Deployment settings - "Disable" load balancing and "Create Deployment group"

Deployment group created.

Now. connect to your EC2 instance and create an vim install.sh file for Code Deploy agent install.

Copy the agent code from trainwithshubham website in the Blog section and paste it into install.sh file

Link: https://www.trainwithshubham.com/blog/setting-up-aws-codedeploy-agent-on-ubuntu-ec2

Why are we separately installing agents in Instance because there may be an Agent version issue with AWS

Run the Install.sh file in the instance to set up an agent

Create an appspec.yml file and add folder name scripts and add two shell scripts for installing nginx and starting nginx

The appspec.yml file is used in AWS CodeDeploy to define the deployment and configuration details for your application. It is a YAML file that specifies how CodeDeploy should deploy your application to the target instances.

And push all code to the code commit repository like before.

Codes have been pushed to the repository.

Now, Go to Code Build And Select - start build - So that the update code be uploaded to S3

Do some changes in the "artifact file path - In buildspec file" so that every file is pushed to S3 not just the index.html file. Change path from /var/www/html/index.html to '**/*' . So that every file push to S3.

In my case, I upload a zip file from my local system to S3 because the zip was not getting created and uploaded to the S3 bucket even after building the project because I didn't change the path in buildspec file. But later in the pipeline, I fixed it.

From S3 - Copy the uploaded code "S3 URI"

Now, In Code Deploy - Application - Create Deployment - paste the S3 URI to the Code Deploy in - Revision Location - now "create deployment". But the status will be - in progress and "pending". We have to give IAM permission.

Again go to IAM - and Add new Roles - name the role and proceed to attach policies - additional access or permission for EC2 instance and S3 for Code Deploy

Now, Attach the Role to the EC2 instance - click Actions - select security - select - modify IAM roles.

I already added the role so it's shown on the image.

In EC2 in Install.sh paste the syntax for installing code deploy agent

Run the command cat Install.sh - copy the restart the code deploy agent service and rub the restart command

sudo service codedeploy-agent restart

In Code Deployment - deployment will start to deploy and the status will change to succeeded after getting the required permissions.

Copy the public IP from the Instance And open it in the New tab.

5- Code - Pipeline:

Go to the Code Pipeline And create pipeline

Choose a name for the pipeline and other options to be default And Next

In Source - Add AWS code commit repository and branch

Click Next

In the build stage - choose - aws code build and code build project and build type "single"

In the code deploy stage - choose - aws code deploy and code deploy application name and deployment group. And Next

Review All settings

And Click "Create Pipeline"

the pipeline started and is in progress.

Deployment failed in the Deploy stage - why let's check

As I mentioned earlier I forgot to change the location path in buildspec yaml file - now we update the artifacts files' location path.

And git pushed it to the code commits repository.

"Retry build" in the code pipeline.

Pipeline deployment is successful and we can check by Opening Public IP in a new tab.

Let's edit the index.html file to see the code pipeline triggered by any change in code.

And pushed the updated file to the repository.

pipeline gets triggered with the change and starts a new build.

Refresh the web page and can see the change.

That's it we successfully deployed the Pipeline project

Thank you for taking the time to read my blog! I appreciate your interest and hope you found the information valuable. Your support means a lot to me, and I'm grateful for the opportunity to share my knowledge and insights with you.