Create a pipeline and AMI using CodePipeline and HashiCorp Packer - AWS Prescriptive Guidance

Create a pipeline and AMI using CodePipeline and HashiCorp Packer

Created by Akash Kumar (AWS)

Environment: PoC or pilot

Source: DevOps

Target: Amazon Machine Images(AMI)

R Type: Rehost

Workload: All other workloads

Technologies: DevOps; Modernization; Web & mobile apps

Summary

This pattern provides code samples and steps to create both a pipeline in the Amazon Web Services (AWS) Cloud by using AWS CodePipeline and an Amazon Machine Image (AMI) by using HashiCorp Packer. The pattern is based on the continuous integration practice, which 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, create a project and configure your source code by using AWS CodeBuild. Finally, create an AMI that gets committed to your repository.

Prerequisites and limitations

Prerequisites

  • An active AWS account

  • An Amazon Linux AMI for launching Amazon Elastic Compute Cloud (Amazon EC2) instances

  • HashiCorp Packer 0.12.3 or later

  • Amazon CloudWatch Events (optional)

  • Amazon CloudWatch Logs (optional)

Architecture

The following diagram shows an example of application code that automates the creation of an AMI by using this pattern’s architecture.

Automate creation of an AMI.

The diagram shows the following workflow:

  1. The developer commits code changes to a private CodeCommit Git repository. Then, CodePipeline uses CodeBuild to initiate the build and add new artifacts that are ready for deployment to the Amazon Simple Storage Service (Amazon S3) bucket.

  2. CodeBuild uses Packer to bundle and package the AMI based on a JSON template. If enabled, CloudWatch Events can automatically start the pipeline when a change occurs in the source code.

Technology stack

  • CodeBuild

  • CodeCommit

  • CodePipeline

  • CloudWatch Events (optional)

Tools

  • AWS 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.

  • AWS CodeCommit – AWS CodeCommit is a version control service that enables you to privately store and manage Git repositories in the AWS Cloud. CodeCommit eliminates the need for you to manage your own source control system or worry about scaling its infrastructure.

  • AWS CodePipeline – AWS CodePipeline is a continuous delivery service that you can use to model, visualize, and automate the steps required to release your software.

  • HashiCorp Packer – HashiCorp Packer is an open-source tool for automating the creation of identical machine images from a single source configuration. Packer is lightweight, runs on every major operating system, and creates machine images for multiple platforms in parallel.

Code

This pattern includes the following attachments:

  • buildspec.yml – This file uses CodeBuild to build and create an artifact for deployment.

  • amazon-linux_packer-template.json – This file uses Packer to create an Amazon Linux AMI.

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 amazon-linux_packer-template.json 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 Ubuntu.

  7. For RunTime(s), choose Standard.

  8. For Image, choose aws/codebuild/standard:4.0.

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

  10. For Environment, choose Linux.

  11. Choose the Privileged check box.

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

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

  14. (Optional) For Type in the Artifacts section, choose No artifacts.

  15. (Recommended) To upload build output logs to CloudWatch Logs, choose CloudWatch logs.

  16. (Optional) To upload build output logs to Amazon S3, choose the S3 logs check box.

  17. Choose Create build project.

App developer, AWS systems administrator
TaskDescriptionSkills required

Pipeline name

  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) to start the pipeline or AWS CodePipeline to periodically check for changes. 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 epic.

  11. Choose your build options and then choose Next.

  12. Choose Skip deploy stage.

  13. Choose Create pipeline.

App developer, AWS systems administrator

Related resources

Attachments

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