Day 51: CI/CD pipeline on AWS - Part 2 ๐Ÿš€

Day 51: CI/CD pipeline on AWS - Part 2 ๐Ÿš€

ยท

7 min read

In the Upcoming next 4 days, I will be making an CI/CD pipeline on AWS with these tools.

  • CodeCommit

  • CodeBuild

  • CodeDeploy

  • CodePipeline

  • S3 Bucket

What is CodeBuild?

  • AWS CodeBuild is a fully managed build service in the cloud. CodeBuild compiles your source code, runs unit tests, and produces artifacts that are ready to deploy. CodeBuild eliminates the need to provision, manage, and scale your own build servers. It provides prepackaged build environments for popular programming languages and builds tools such as Apache Maven, Gradle, and more. You can also customize build environments in CodeBuild to use your own build tools. CodeBuild scales automatically to meet peak build requests.

  • CodeBuild provides these benefits:

  1. Fully managed โ€“ CodeBuild eliminates the need to set up, patch, update, and manage your own build servers.

  2. On-demand โ€“ CodeBuild scales on demand to meet your build needs. You pay only for the number of build minutes you consume.

  3. Out of the box โ€“ CodeBuild provides preconfigured build environments for the most popular programming languages. All you need to do is point to your build script to start your first build.

Why we use AWS CodeBuild:

  • Automated build process

  • Scalable for any workload size

  • Cost-effective pay-as-you-go pricing

  • Seamless integration with AWS services and third-party tools

  • Customizable build environments

  • Secure build and testing environment

  • Supports multiple programming languages and platforms

  • Detailed logs and monitoring capabilities

Today I am sharing a Blog on AWS CodeCommit &AWS CodeBuild. I hope you will like it.

  • First, we will make AWS Commit and then move to AWS CodeBuild but if have already watched my previous blog then you can skip this part.

AWS CodeCommit

Let's setup an IAM User for AWS CodeCommit.

  • Step-01: Go to the IAM service in AWS Console and click on Users and then click on Add User.

Screenshot from 2023-08-05 10-01-56

  • Step-02: Click on Next:Permissions and then click on Attach existing policies directly and then select AWSCodeCommitFullAccess then click on Next:Tags.

Screenshot from 2023-08-05 10-03-43

  • Step-03: Click on Next:Review and then click on Create User and thus we have created an IAM User for AWS CodeCommit.

Screenshot from 2023-08-05 10-04-24


Now we will see how to add HTTP GitCredentials in your AWS IAM.

  • Step-01: Go to IAM > Users > Security Credentials.

Screenshot from 2023-08-05 11-32-30

  • Step-02: Scroll down to HTTPS Git credentials for AWS CodeCommit and then click on Generate credentials

  • HTTPS Git credentials for AWS CodeCommit > Generate > Download Credentials.

Screenshot from 2023-08-05 11-32-50

  • Thus we have Generated Credentials for our IAM User saved it in a safe place it will be used in the future.

Screenshot from 2023-08-02 23-29-40


Now we will see how to create a repository in AWS CodeCommit.

  • Step-01: Go to CodeCommit service in AWS Console and click on Create repository.

Screenshot from 2023-08-05 11-34-53

  • Step-02: Enter the Repository name and then click on Create repository.

Screenshot from 2023-08-05 11-35-57

  • Thus we have created a Repository in AWS CodeCommit.

Screenshot from 2023-08-05 11-36-25


Let's create an EC2 Instance that will clone our CodeCommit Repository.

  • Step-01: Go to EC2 service AWS Console and click on Launch Instance and then select Ubuntu Server 22.04 and then click on Select.

Screenshot from 2023-08-05 11-38-36

  • Step-02: Select t2.micro and then use existing key-pair or create new key-pair.

Screenshot from 2023-08-05 11-38-54

  • Step-03: Click on Network Settings.

Screenshot from 2023-08-05 11-39-10

  • Now log in to the EC2 instance via SSH and open Terminal.

Screenshot from 2023-08-05 11-41-31


Now Before moving forward Go to CodeCommit and click on Clone URL and copy the HTTPS URL.

imageedit_3_7964211194


  • **Step-01:**Now we will clone the CodeCommit Repository in our EC2 Instance.
git clone <HTTPS URL>
# The URL which is copied from CodeCommit
# Enter the UserName and Password which we downloaded for HTTP and git credentials.

Screenshot from 2023-08-05 11-51-10

  • After this, we can see that the repository is cloned in our local machine.
ls
cd <repository which we have cloned>

Screenshot from 2023-08-05 11-52-21

  • Let's create a file in our repository and then push it to CodeCommit.
vim index.html

Screenshot from 2023-08-05 12-04-47

  • Thus we have created a index.html file in our repository or you can use your own.

Screenshot from 2023-08-05 12-05-40

  • Now we will push our code to AWS CodeCommit.
git add .
git commit -m "Added index.html file"

Screenshot from 2023-08-05 12-08-05

git push origin master

Screenshot from 2023-08-05 12-09-44

  • Thus we have pushed our code to AWS CodeCommit.

Screenshot from 2023-08-05 12-10-55


  • (So this is what we have learned on Day 50 of #90DaysofDevOps. Now we will see how to use AWS CodeBuild.)

Task-01 :

Read about the Buildspec file for Codebuild it can be written in YAML or JSON.

  • version: Specifies the version of the BuildSpec file. The current version is 0.2.

  • phases: This section defines the different phases of the build process, such as install, pre_build, build, post_build, and finalize. Each phase can have a series of commands will be executed in order.

  • artifacts: In this section, you specify the files and directories that should be packaged and made available as build artifacts after the build process is complete. These artifacts can be used in the subsequent deployment stages.

  • cache: If you want to speed up the build process by caching certain dependencies or build artifacts, you can specify caching settings in this section.

  • environment: Here, you can define environment variables that will be available during the build process. You can set variables specific to each build phase or shared across all phases.

  • source: Defines the source code location for the build. You specify the repository type (e.g., GitHub, Bitbucket), the source code location, and any authentication credentials if needed.

  • logs: Specifies the configuration for storing build logs, such as the AWS S3 bucket optional encryption settings.

Task-02 :

Let's create a buildspec.yml file in our repository for our index.html file.

version: 0.2

phases:
  install:
    commands:
      - echo Installing NGINX
      - sudo apt-get update
      - sudo apt-get install nginx -y
  build:
    commands:
      - echo Build started on 'date'
      - cp index.html /var/www/html/
  post_build:
    commands:
      - echo Configuring NGINX

artifacts:
  files:
    - /var/www/html/index.html
  • Create vim buildspec.yml a file in your repository.

Screenshot from 2023-08-05 13-55-17

  • Now we will push this file to our repository.
git add .
git commit -m "Added buildspec.yml file"

Screenshot from 2023-08-05 13-56-29

git push origin master

Screenshot from 2023-08-05 13-59-35


  • Now let's create a CodeBuild project in AWS Console.

Screenshot from 2023-08-05 14-00-40

  • Step-01: Create a CodeBuild Project.

Screenshot from 2023-08-05 14-01-58

  • Step-02: Give a name to your CodeBuild Project.

Screenshot from 2023-08-05 14-04-36

  • Step-03: Select your source provider and repository.

Screenshot from 2023-08-05 14-04-59

  • Step-04: Select your environment and operating system.

Screenshot from 2023-08-05 14-07-28

  • Thus we have created our CodeBuild Project.

  • Now we will start the build so click on Start Build.

Screenshot from 2023-08-05 14-08-28

  • Wait for Untill the Build is finished.

Screenshot from 2023-08-05 14-27-27


Now we will create an AWS S3 bucket to add our build artifacts.

  • Step-01: Go to AWS S3 and create a bucket.

Screenshot from 2023-08-05 14-45-08

  • Step-02: Give a name to your bucket and create it.

Screenshot from 2023-08-05 14-45-51

  • Thus we have created our S3 bucket.

Screenshot from 2023-08-05 14-46-36


Now go to CodeBuild and edit your CodeBuild Project and edit artifacts.

  • Step-01: Go to CodeBuild and edit your CodeBuild Project and edit artifacts.

Screenshot from 2023-08-05 14-49-00

  • Step-02: Select your S3 bucket and give a name to your build artifacts.

Screenshot from 2023-08-05 14-51-47

  • Step-03: Save your changes and start the build again so that the artifacts are uploaded to your S3 bucket.

Screenshot from 2023-08-05 14-58-36

  • Step-04: Wait until the build is finished and after this check build is finished go to your S3 bucket and check if the artifacts are uploaded or not.

Screenshot from 2023-08-05 14-59-09

  • Step-05: Now go inside your artifacts and then /var/www/html/ and check if the index.html the file is present or not.

imageedit_6_9485105158

  • Step-06: Click on the open button and check if the index.html the file is present or not.

imageedit_8_5023200972


Happy Learning :)

Did you find this article valuable?

Support DevOps by becoming a sponsor. Any amount is appreciated!

ย