Create a pipeline and deploy artifact updates to on-premises EC2 instances using CodePipeline - AWS Prescriptive Guidance

Create a pipeline and deploy artifact updates to on-premises EC2 instances using CodePipeline

Created by Akash Kumar (AWS)

Environment: PoC or pilot

Source: DevOps

Target: Amazon EC2/On-Premises

R Type: Rehost

Technologies: DevOps; Modernization; Web & mobile apps

AWS services: AWS CodeBuild; AWS CodeCommit; AWS CodeDeploy; AWS CodePipeline

Summary

This pattern provides code samples and steps to create a pipeline in the Amazon Web Services (AWS) Cloud and deploy updated artifacts to on-premises Amazon Elastic Compute Cloud (Amazon EC2) instances in AWS CodePipeline. The pattern is based on the continuous integration practice. This practice automates the building and testing of code with a Git-based version control system. In this pattern, you create and clone a code repository by using AWS CodeCommit. Then, you create a project and configure your source code by using AWS CodeBuild. Finally, you create your application and configure its target environment for on-premises EC2 instances by using AWS CodeDeploy.

Prerequisites and limitations

Prerequisites

  • An active AWS account

  • User-defined tags to identify EC2 instances during deployment

  • CodeDeploy agent, installed on EC2 instances

  • Your required runtime software, installed on EC2 instances

  • Amazon Corretto 8 for the Java Development Kit

  • Apache Tomcat web server, installed

  • Amazon CloudWatch Events (optional)

  • A key pair to log in to the web server (optional)

  • An Apache Maven application project for a web application

Architecture

The following diagram shows an example Java web application that’s deployed to on-premises EC2 instances by using this pattern’s architecture.

Example Java web app deployed on EC2 instances

The diagram shows the following workflow:

  1. The developer commits code changes to a private CodeCommit Git repository.

  2. CodePipeline uses CodeBuild to initiate the build and add new artifacts that are ready for deployment in the Amazon Simple Storage Service (Amazon S3) bucket.

  3. CodePipeline uses the CodeDeploy agent to pre-install any dependencies required for the deployment artifact changes.

  4. CodePipeline uses the CodeDeploy agent to deploy the artifacts from the S3 bucket to target EC2 instances. If enabled, CloudWatch Events can automatically start the pipeline when a change occurs in the source code.

Technology stack

  • CodeBuild

  • CodeCommit

  • CodeDeploy

  • CodePipeline

  • CloudWatch Events (optional)

Tools

  • AWS CodeBuild is a fully managed build service that helps you compile source code, run unit tests, and produce artifacts that are ready to deploy. CodeBuild compiles your source code, runs unit tests, and produces artifacts that are ready to deploy.

  • AWS CodeCommit is a version control service that helps you privately store and manage Git repositories, without needing to manage your own source control system.

  • AWS CodeDeploy automates deployments to Amazon Elastic Compute Cloud (Amazon EC2) or on-premises instances, AWS Lambda functions, or Amazon Elastic Container Service (Amazon ECS) services.

  • AWS CodePipeline helps you quickly model and configure the different stages of a software release and automate the steps required to release software changes continuously.

Code

This pattern includes the following attachments:

  • buildspec.yml – This file specifies the actions that CodeBuild requires to build and create an artifact for deployment.

  • appspec.yml – This file specifies the actions that CodeDeploy requires to create an application and configure a target environment for on-premises EC2 instances.

  • install_dependencies.sh – This file installs dependencies for the Apache Tomcat web server.

  • start_server.sh – This file starts the Apache Tomcat web server.

  • stop_server.sh – This file stops the Apache Tomcat web server.

Epics

TaskDescriptionSkills required

Create the repository.

Create a CodeCommit repository.

AWS systems administrator

Clone the repository.

Connect to the CodeCommit repository by cloning the repository.

App developer

Push the source code to the remote repository.

  1. Create a commit to add the buildspec.yml and appspec.yml files to your local repository.

  2. Push the commit from your local repository to the remote CodeCommit repository.

App developer
TaskDescriptionSkills required

Create a build project.

  1. Sign in to the AWS Management console, open the AWS CodeBuild console, and then choose Create build project.

  2. For Project name, enter the name of your project.

  3. For Source provider, choose AWS CodeCommit.

  4. For Repository, choose the repository where you want to build the code pipeline.

  5. For Environment image, choose Managed image or Custom image.

  6. For Operating system, choose Amazon Linux 2.

  7. For RunTime(s), choose Standard.

  8. For Image, choose aws/codebuild/amazonlinux2-aarch64-standard:2.0.

  9. For Image version, choose Always use the latest image for this runtime version.

  10. For Service role, choose New service role or Existing service role.

  11. For Build specifications, choose Use a buildspec file or Insert build commands.

  12. (Optional) Choose Add artifact to configure artifacts.

  13. (Optional) To upload build output logs to Amazon CloudWatch, choose CloudWatch logs.

  14. Choose Create build project.

AWS administrator, App developer
TaskDescriptionSkills required

Create the application.

  1. Sign in to the AWS Management console, open the AWS CodeDeploy console, and then choose Create application.

  2. For Application name, enter a name for your application.

  3. For Compute platform, choose EC2/On-premises.

  4. Choose Create application and then choose Create deployment group.

  5. For Deployment group name, enter a name.

  6. Create a service role for CodeDeploy. Note: The service role must have permissions to grant CodeDeploy access to your target environment.

  7. For Service role, choose the service role that you created in step 6.

  8. For Deployment type, choose either In-place or Blue/green based on your business requirements.

  9. For Environment configuration, choose the options that meet your business requirements.

  10. (Optional) Create a target group for your load balancer separately in the Amazon EC2 console, and then go back to the Create deployment group page of the AWS CodeDeploy console to choose your load balancer and target group.

  11. Choose Create deployment group.

AWS systems administrator, App developer
TaskDescriptionSkills required

Create the pipeline.

  1. Sign in to the AWS Management console, open the AWS CodePipeline console, and then choose Create pipeline.

  2. For Pipeline name, enter a name for the pipeline.

  3. For Service role, choose New service role or Existing service role.

  4. For Role name, enter a name for your role.

  5. In the Advanced settings section, for Artifact store, choose Default location if you want Amazon S3 to create a bucket and store the artifacts in the bucket. To use an existing S3 bucket, choose Custom location. Choose Next.

  6. For Source provider, choose AWS CodeCommit.

  7. For Repository name, choose the repository that you cloned earlier. For Branch name, choose your source code branch.

  8. For Change detection options, choose Amazon CloudWatch Events (recommended) or AWS CodePipeline. Choose Next.

  9. For Build provider, choose AWS CodeBuild.

  10. For Project Name, choose the build project that you created in the Create a CodeBuild project for the application section of this pattern.

  11. Choose your build options and then choose Next.

  12. For Deploy provider, choose AWS CodeDeploy.

  13. Choose an application name and deployment group, and then choose Next.

  14. Choose Create pipeline.

AWS systems administrator, App developer

Related resources

Attachments

To access additional content that is associated with this document, unzip the following file: attachment.zip