Open In App

AWS CLI for Continuous Integration

Last Updated : 16 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Quick and efficient delivery of quality code is at the core of software development in the fast-paced arena. Practically, Continuous Integration (CI) has emerged as a lynchpin practice to this aim, where developers regularly integrate changes in the code into the shared repository. These integrations are automatically tested, built, and deployed to identify issues early in the development cycle.

The AWS Command Line Interface (CLI) is a very powerful tool that enables automation and has made CI quite easy. Using the AWS CLI, the developer can interact with most of the AWS services from a command line in an attempt to automate tasks like environment setup, code deployment, or resource management, not only for running the cycle of development fast but also for reducing the probability of error occurrence from the human end, making CI reliable and effective.

In this article, we will look at how to power your Continuous Integration workflows with the AWS CLI: installing the CLI in your CI environment, automating deployment, and monitoring. This guide will present you, whether a seasoned developer or new to Continuous Integration, with the knowledge and tools needed to efficiently integrate the AWS CLI into your development workflow.

Primary Terminologies

  • Continuous Integration: A practice in development that asks people to integrate new code into shared repositories rather frequently, with automated testing and building. CI lowers the probability of failures, increases the quality of the code, and thus often results in faster development.
  • AWS CLI: A widely used command line tool for calling and controlling AWS services directly through terminal commands, it supports a lot of AWS services and is most commonly used to perform automation tasks in CI/CD pipelines
  • CI Pipeline: A process pipeline that is triggered by a change in code, usually comprising processes such as fetching the code, building it, and running tests to its deployment.
  • IAM (Identity and Access Management): This is an AWS service for managing access to AWS services and resources in a secure way, we will utilize IAM to create roles and policies that allow necessary permissions to CI processes.
  • Environment Variables: Key-value pairs, for example, AWS credentials, to hold configuration data that may be used by the CI pipeline at runtime.

Step-by-Step Process for Using AWS CLI in Continuous Integration

Step 1: Set Up AWS CLI in Your CI Environment

  • To begin using the AWS CLI in your CI pipeline, you need to install and configure it in your CI environment.

Install AWS CLI

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

unzip awscliv2.zip

sudo ./aws/install

Install AWS CLI

AWS Configure

aws configure
AWS Configure

Step 2: Create an IAM Role for CodeBuild

  • An IAM role is required for CodeBuild to access other AWS services and resources.

Create the IAM Role

aws iam create-role --role-name CodeBuildRole --assume-role-policy-document file://trust-policy.json

Attach the Required Policies

aws iam attach-role-policy --role-name CodeBuildRole --policy-arn arn:aws:iam::aws:policy/AdministratorAccess

trust-policy.json example:

{

"Version": "2012-10-17",

"Statement": [

{

"Effect": "Allow",

" Principal": {

"Service": "codebuild.amazonaws.com"

},

"Action": "sts:AssumeRole"

}

]

}

trust-policy.json

Step 3: Create a CodeBuild Project

Define the Build Specifications:

  • Create a buildspec.yml file in the root of your project repository. This file defines the build commands and phases.

version: 0.2

phases:

install:

commands:

- echo Installing dependencies

build:

commands:

- echo Building the project

artifacts:

files:

- '**/*'

buildspec.yml

Create the Project Using AWS CLI

aws codebuild create-project --name myBuildProject \

--source type=GITHUB,location=https://github.com/your-username/your-repo.git \

--artifacts type=NO_ARTIFACTS \

--environment type=LINUX_CONTAINER,image=aws/codebuild/standard:5.0,computeType=BUILD_GENERAL1_MEDIUM \

--service-role arn:aws:iam::your-account-id:role/CodeBuildRole

Create the Project Using AWS CLI

Start a Build

  • Trigger a build using AWS CLI:
aws codebuild start-build --project-name myBuildProject
Trigger a Build Using AWS CLI

Monitor the Build

  • Check the build status through the AWS Management Console under CodeBuild.
  • Use AWS CLI to get build details:
aws codebuild batch-get-builds --ids <build-id>
Monitor the Build

Step 4: Create a CodePipeline

Define the Pipeline Configuration:

  • Create a pipeline.json file to define the pipeline stages.

{

"pipeline": {

"name": "myPipeline",

"roleArn": "arn:aws:iam::your-account-id:role/CodePipelineRole",

"artifactStore": {

"type": "S3",

"location": "my-pipeline-bucket"

},

"stages": [

{

"name": "Source",

"actions": [

{

"name": "SourceAction",

"actionTypeId": {

"category": "Source",

"owner": "ThirdParty",

"provider": "GitHub",

"version": "1"

},

"outputArtifacts": [

{

"name": "SourceOutput"

}

],

"configuration": {

"Owner": "your-username",

"Repo": "your-repo",

"Branch": "main",

"OAuthToken": "your-github-token"

}

}

]

},

{

"name": "Build",

"actions": [

{

"name": "BuildAction",

"actionTypeId": {

"category": "Build",

"owner": "AWS",

"provider": "CodeBuild",

"version": "1"

},

"inputArtifacts": [

{

"name": "SourceOutput"

}

],

"outputArtifacts": [

{

"name": "BuildOutput"

}

],

"configuration": {

"ProjectName": "myBuildProject"

}

}

]

}

]

}

}

pipeline.json

Create the Pipeline Using AWS CLI

aws codepipeline create-pipeline --cli-input-json file://pipeline.json
Create the Pipeline Using AWS CLI

Step 5: Delete the Project (If No Longer Needed)

aws codebuild delete-project --name myBuildProject
  • We can verify by using following command
aws codebuild list-projects
Delete the Project

Delete Pipeline

aws codepipeline delete-pipeline --name myPipeline
  • We can verify pipeline list
aws codepipeline list-pipelines
Delete Pipeline

Conclusion

The integration of the AWS CLI with Continuous Integration will take automation to a new level by making the majority of critical tasks run on their own without human intervention. The AWS CLI provides seamless integration of dozens of AWS services into your Continuous Integration pipeline, from building and testing code to deployment and monitoring.

By following through with this tutorial, you could create a very solid CI environment that would significantly speed up your development process, lower the chances of errors within your code, and make deployment more reliable. Scripting and automating interactions with AWS services give your team the power to code more and handle less hassle in infrastructure management.

This, therefore, makes your CI practices agile and responsive; hence, the development environment with software delivered will be of high quality and fast. The AWS CLI is a tool you will not stop using on your journey through the CI pipeline toward operations that are smooth and efficient.


Next Article
Article Tags :

Similar Reads