CI/CD for a web application Using AWS CodePipeline

Sanjay Saravanan
4 min readMay 4, 2021
Continuous Integration and Deployment

Build and deploy any application using AWS code pipeline.

  • AWS CodePipeline is a CI/CD service similar to Jenkins.
  • Build can be done in CodeBuild which is part of AWS Code Pipeline.
  • CodeBuild uses buildspec.yml which should be in the root directory of the repository.
  • buildspec.yml has different phases, each phase with a defined set of commands to execute the build process.
  • Deployment can also be done in the code pipeline, the application can be deployed in any of the available AWS services.

Here the code pipeline does the following,

  1. Check out the source code from SCM.
  2. Build an image of the Node application.
  3. Push it to AWS ECR.
  4. Deploy an instance of the application in Elastic Beanstalk.

Requirements

Source Code:

Application Source code within a repository with the following files in the root directory,

  1. Dockerfile for the application. Containerize and deploy an application in Elastic Beanstalk.
  2. buildspec.yml with the following code, https://github.com/sanjaysaravanan/build-spec-test/blob/master/buildspec.yml
    the above file does the following,
    Phases:
    PRE_BUILD:
    Login to AWS-CLI, create variables for image-URI, commit-hash, image-tag.
    BUILD: Build the docker image from the Dockerfile and tag the image as the latest and image-tag created.
    POST_BUILD: Push the image to the ECR. Create a file called Dockerrun.aws.json which will be used to deploy the application in Elastic Beanstalk.
    Artifacts:
    Output the file Dockerrun.aws.json for the deployment stage.

Deployment:

Deployment needs an Elastic Beanstalk ENV and Application created using a custom image of the application. Containerize and deploy an application in Elastic Beanstalk.

AWS CodePipeline

A pipeline is similar to a job in Jenkins.

In the AWS console navigate to CodePipeline and create a new pipeline with the following stages.

Pipeline and Roles:

Source Stage
  • Enter an appropriate name for the pipeline.
  • Choose an existing role if you have a role that has CodePipeline access policy, else choose New Service Role

Source Stage:

  • Select the source as Github( Version 2 ). Click on connect to GitHub and follow the instructions to authenticate the CodePipeline to access your GitHub repository.
  • Select the appropriate repository & default branch where the application code exists.
  • Check the Start the pipeline on source code change. This automatically triggers the pipeline on a new change in the selected branch. Leave the rest all to default.

Build Stage:

  • Select the build provider as AWS CodeBuild. Region default value.
  • If you already have a build project with appropriate config choose that or else create a new one like below,

Project Configuration:

  • Name the project and provide a description if needed.

Environment:

  • Select Managed Image. ( Choose Custom Image and specify an image if you want to run the build in a custom env)
  • Choose,
    Operating System: Ubuntu or Linux 2
    Runtime: Standard
    Image: aws/codebuild/standard:5.0
    Image Version: Always use the latest image for this runtime version
    Environment Type: Linux
    Privileged: True( Check the box)
  • Role should have the AWSEC2ContainerRegistryFullAccess since an image will be built during the process. So, create a new role and add AWSEC2ContainerRegistryFullAccess policy to it.

BuildSpec File:

  • Select Use buildspec file.
  • Specify the file name if any other file name is used other than buildspec.yml.

Logs:

Check the CloudWatch logs if needed. Then click on Continue to CodePipeline.

Deploy Stage:

Here, the previously created Elastic Beanstalk env and application will be used.

  • Choose Deploy Provider as AWS Elastic Beanstalk.
  • Choose the region where the Elastic Beanstalk env and application is created previously in the Deployment requirement.
  • Choose the correct Env and application. Then, click on next.

Preview the Pipeline, then create the pipeline. Automatically, the pipeline will be triggered.

Now, the pipeline is created successfully. So whenever a code change is pushed into the selected repository, and the selected branch pipeline will be triggered and changes will be deployed in the Elastic Beanstalk application.

Hope you got to know about the CI/CD Using AWS Code Pipeline. This way we can create a Continuous Integration and Deployment setup for any application.

Hope you liked it, Thanks.

--

--

Sanjay Saravanan
0 Followers

A budding software engineer, curious about learning the end to end software development.