AWS Now in UAE Region

Effortless Code Releases: How AWS CodePipeline Can Help

You can use Amazon CodePipeline, a fully managed continuous delivery service, to automate your release pipelines for dependable and speedy application and infrastructure modifications.

Using CodePipeline, you can develop, visualize, and test the procedures required to distribute your software. When you make changes to your code, you may set up automatic triggers to start the release process.

CodeCommit, CodeBuild, and CodeDeploy are just a few of the AWS services that CodePipeline integrates with, allowing you to automatically build, test, and deploy code as part of your release process. You may also integrate CodePipeline with third-party tools like GitHub, Jenkins, and others.

In today’s post, you’ll learn how to set up AWS Code Pipeline using CodeCommit, CodeBuild, and CodeDeploy.

There are a few resources you should have ready before utilizing this tutorial to develop your CD pipeline. The following are the prerequisites for getting started.

Prerequisites:

  1. A source control repository (in our example, CodeCommit) containing your Dockerfile and application source code.
  2. A Docker image repository (in our example, Amazon ECR) that stores an image built from your Dockerfile and application source.
  3. A task definition for Amazon ECS that employs a Docker image from your image repository.
  4. If you have met these requirements, you can proceed to the course and begin building your CD pipeline.

Step 1: Build Specification File Creation and Source Repository Addition

CodeBuild is used in this post to develop your Docker image and upload it to Amazon ECR. Add a buildspec.yml file to your source code root repository so CodeBuild knows what to do.

version: 0.2

phases:
 install:
   runtime-versions:
     nodejs: 16
   commands:
     - npm install
 pre_build:
   commands:
     - echo Logging in to Amazon ECR...
     - aws --version
     - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin account_id.dkr.ecr.account_region.amazonaws.com
     - REPOSITORY_URI=account_id.dkr.ecr.account_region.amazonaws.com/repo_name
     - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
     - IMAGE_TAG=${COMMIT_HASH:=latest}
 build:
   commands:
     - echo Building the Docker image...
     - docker build -t $REPOSITORY_URI:latest .
     - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
 post_build:
   commands:
     - echo Pushing the Docker images...
     - docker push $REPOSITORY_URI:latest
     - docker push $REPOSITORY_URI:$IMAGE_TAG
     - echo Writing image definitions file...
     - printf '[{"name":"service_name","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
   files: imagedefinitions.json

The build specification was based on the sample task definition given by the Amazon ECS service for this tutorial. The ECR’s image repository is named repo_name. The value at the end.

of the file corresponds to the container name in the service’s job definition, and the REPOSITORY_URI value represents the image repository.

Step 2: Make Your Continuous Deployment Pipeline

Using the CodePipeline wizard, create your pipeline stages and connect your source repository to your ECS service.

  1. To access the CodePipeline console, go to https://console.aws.amazon.com/codepipeline/.
  2. On the Welcome page, select Create pipeline.


Create a pipeline on the Welcome page

  1. Fill in the Pipeline name area with the name of your pipeline. Leave everything in its default settings. In this stage, new Service roles will be created for the pipeline. After entering the pipeline name, click next.


    Create a new pipeline role
  2. In Step 2, select AWS CodeCommit as your source provider. Choose the name of the CodeCommit repository that will act as the pipeline’s Repository source location. After selecting the Branch name, choose the relevant branch and then click Next.
    Select the source provider
  3. Add a build stage page, choose AWS CodeBuild as the build provider, and then press the Create project button. As the project name, choose a unique name for your building project. This sample build project is called my-first-build-project.


Select the build provider

  1. For the Environment image, choose a Managed image. Choose Ubuntu for your operating system. Runtime should be set to Standard (s). For Image, use aws/codebuild/standard:6.0. Enable this setting if you want to produce Docker images or if you want your builds to have more rights. In the Buildspec stage, paste the buildspec.yml file.

    Create build project

    Select Environment Image

Select BuildSpec

  1. To proceed, select CodePipeline and then Next. The CodeBuild service role codebuild-build-project-name-service-role is established for your build project by the wizard. Keep this role name in mind when you apply Amazon ECR privileges to it later.
  2. In Step 4, choose Amazon ECS as your deployment provider. As the Cluster name, provide the Amazon ECS cluster where your service is running. For this example, the cluster is my_first_cluster.

Under the Service name, select the service you want to update, then click Next. This tutorial’s service name is my-first-service. In the Image definitions file area, enter imagedefinitions.json.

Create Deploy Stage

  1. On the Review screen, review your pipeline setup before selecting Create pipeline to begin building your pipeline.

Step 3: Give the Amazon ECR Permissions to CodeBuild Role

Because the buildspec.yml file makes calls to Amazon ECR API operations, the role must have a policy that allows rights to make these Amazon ECR calls. Using the approach outlined below, you can provide the role with the necessary permissions.

  1. To access the IAM console, go to https://console.aws.amazon.com/iam/.
  2. Roles can be found on the left navigation pane.
  3. Enter codebuild- into the search bar, then select the role created by the CodePipeline wizard.
  1. On the Summary page, click Attach policies.
  2. After selecting the box to the left of the AmazonEC2ContainerRegistryPowerUser policy, select Attach policy from the menu.

Step 4: Test your pipeline

Everything required for a complete native AWS continuous deployment should be in your pipeline. You can now test the functionality of your code by publishing it to your source repository.

  1. Make a code change, commit it, and push it to your preferred source repository.
  2. To access the CodePipeline console, go to https://console.aws.amazon.com/codepipeline/.
  3. Choose the desired pipeline from the list.
  4. Track the pipeline as it progresses through each level. Your pipeline should be complete, and your Amazon ECS service should begin using the Docker image you modified in your code.

Conclusion:

Using CodeCommit, CodeBuild, and CodeDeploy, we have learned how to build up AWS Codepipeline in this article. Thank you for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *