Building containers images for AWS Marketplace containers-based product with AWS CodePipeline
By: Date: 26/10/2021 Categories: AWS Tags:

AWS Marketplace simplifies the way customers find, subscribe, and deploy software that runs on AWS. At Re:invent 2018, AWS Marketplace for Containers was launched for customers to discover and buy container products in AWS Marketplace. The product selection spans categories such as high-performance computing, security, and developer tools. In December 2020, AWS Marketplace announced an easier self-service experience for independent software vendors (ISVs) to add versions and new information for their container products. With this release, ISVs could easily publish new container images and Helm charts by hosting dedicated Amazon Elastic Container Registry (Amazon ECR) repositories for their container products.

Solution Overview

In this post, I show you how to build container images automatically to an Amazon ECR repository every time code changes occur in AWS CodeCommit. You use an AWS CloudFormation template to build a Continuous Integration/Continuous Delivery (CI/CD) pipeline to orchestrate the deployment process and launch the necessary services automatically. After you create a CI/CD pipeline, it automatically triggers a pipeline to release the latest version of your source code to build and push the container image to Amazon ECR’s repository. From then on, every time you make a change in your source code, the pipeline is triggered. The following diagram illustrates the architectural overview of the solution.

  1. Engineer develops and pushes their code into AWS CodeCommit repository.
  2. The action releases the latest version of the code to AWS CodeBuild.
  3. AWS CodeBuild starts the containers image build process.
  4. Once the containers image is built successfully, the image is automatically pushed into the AWS Marketplace ECR repository.
CI/CD pipeline to build container images for AWS Marketplace

Prerequisites

  • A container product already published in the limited state in AWS Marketplace. Products in the limited state are visible only to your seller AWS account and test accounts you specify. All new products are first published into the limited state for testing before they are made publicly available in AWS Marketplace. For more information on submitting your product, see Getting started with container products.
  • An AWS Cloud9 environmentAWS Cloud9 comes prepackaged and configured with AWS Command Line Interface (AWS CLI) to help easily start the deployment.

Solution walkthrough

This solution walkthrough has the following steps:

  1. Create an AWS CodeCommit repository.
  2. Push a sample application to your AWS CodeCommit repository.
  3. Create an Amazon ECR repository.
  4. Deploy AWS CodePipeline.

Step 1: Create an AWS CodeCommit repository

Open the AWS Cloud9 integrated development environment (IDE) via the AWS Console. In the AWS Cloud9 IDE console, run the following command to create an AWS CodeCommit repository. Replace repository-name with the repository name you choose and save to use later, in Step 3.

aws codecommit create-repository --repository-name example-repo --repository-description "Example Demo"

Bash

You should receive output similar to the following:

{
    "repositoryMetadata": {
        "repositoryName": "example-repo", 
        "cloneUrlSsh": "ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/example-repo", 
        "lastModifiedDate": 1625695942.77, 
        "repositoryDescription": "Example Repository", 
        "cloneUrlHttp": "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/example-repo", 
        "creationDate": 1625695942.77, 
        "repositoryId": "c8867d6b-xxxx-xxxx-xxxx-xxxxxxxxxxxx", 
        "Arn": "arn:aws:codecommit:us-east-1:xxxxxxxxxxxx:awsmp-demo-repo", 
        "accountId": "xxxxxxxxxxxx"
    }
}

JSON

Step 2: Push a sample application to your AWS CodeCommit repository

To create a Hello World sample application and Dockerfile, run the following commands in your AWS Cloud9 IDE. This builds a container image to push to the AWS CodeCommit repository you created Step 1:

  1. Clone the repository

In your AWS Cloud9 IDE console, run the following commands:

git clone < cloneUrlHttp-in-step-1>

cd awsmp-demo-repo

Bash

  1. Create a sample index.html file

 In your AWS Cloud9 IDE console, run the following command:

cat << EOF > index.html
<head>
<title>Hello World</title>
</head>
<div class="info">
<h>Hello World!</h>
</div>
EOF

Bash

  1. Create a sample hello.conf file

In your AWS Cloud9 IDE console, run the following command:

cat << EOF > hello.conf
server {
    listen 80;

    root /usr/share/nginx/html;
    try_files /index.html =404;

    expires -1;
}
EOF

Bash

  1. Create a Dockerfile

In your AWS Cloud9 IDE console, run the following command:

cat << EOF > Dockerfile
FROM public.ecr.aws/nginx/nginx
RUN rm /etc/nginx/conf.d/*
ADD hello.conf /etc/nginx/conf.d/
ADD index.html /usr/share/nginx/html/
EOF

Bash

  1. Push all of the files to AWS CodeCommit repository

In your AWS Cloud9 IDE console, run the following commands:

git add --all
git commit -a -m "first commit"
git push

Bash

Step 3: Create an Amazon ECR repository

Create an Amazon ECR repository to store the container image. For your container products already published in limited state in AWS Marketplace, the container image must be hosted on an Amazon ECR repository managed by AWS Marketplace.

Use the AWS Marketplace Catalog API (CAPI) to create a repository. To invoke CAPI using AWS CLI, run the following command, replacing the Identifier value with your product ID and the RepositoryName you created in Step 1.

aws marketplace-catalog start-change-set \
--catalog "AWSMarketplace" \
--change-set '[
  {
    "ChangeType": "AddRepositories",
    "Entity": {
      "Identifier": "Product-ID",
      "Type": "[email protected]"
    },
    "Details": "{\"Repositories\":[{\"RepositoryName\":\"example-repo\",\"RepositoryType\":\"ECR\"}]}"
  }
]'

Bash

Monitor the progress via the AWS Marketplace management portal (AMMP) request page. The following screenshot shows the Requests history page with two requests with the status of Succeeded.

To view the details of the request and get the Amazon ECR repository name to use in Step 4, on the Requests history page Request name column, select the request to see the request details page. On the request details page, you can find the repository name under Repository 1. The repository name is the value after 709825985650.dkr.ecr.us-east-1.amazon.com. The Amazon ECR repository name should be in this format <seller display name>/<repository name>.

Step 4: Deploy AWS CodePipeline

  1. To deploy the pipeline to build and push the container image to the Amazon ECR repository, choose the following Launch Stack button.
  1. On the AWS CloudFormation create stack console, enter the repository name you created in Step 1 and AmazonECR’s repository name you created in Step 3.
  2. Select Next until you can select “I acknowledge that AWS CloudFormation might create IAM resources” and then select Create Stack. When the AWS CloudFormation stack creation completes, the pipeline builds the container image.
  3. Navigate to the AWS CodePipeline console and select the created pipeline to monitor the build progress.
  4. When the build completes successfully, to verify the pipeline built and pushed the container image to the Amazon ECR repository, run the following command using the AWS Cloud9 IDE. Replace repository-name value with the repository name you created in Step 1.
aws ecr list-images --registry-id 709825985650 --repository-name amazon-web-services/repository-name --region us-east-1

Bash

Output should appear similar to the following:

{
    "imageIds": [
        {
            "imageTag": "35dac292", 
            "imageDigest": "sha256:51344a34145bc52af............"
        }
    ]
}

JSON

Your container image is now successfully pushed into the right place and it is ready to be published in AWS Marketplace. You can follow described in Getting started with container products to test and publish your containers product.

Cleaning up

To clean up the deployment, delete the AWS CodeCommit repositorydelete the AWS Cloud9 environment and delete the AWS Cloudformation stack deployed as part of this blog tutorial.