npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

token-injectable-docker-builder

v1.0.5

Published

The TokenInjectableDockerBuilder is a flexible AWS CDK construct that enables the usage of AWS CDK tokens in the building, pushing, and deployment of Docker images to Amazon Elastic Container Registry (ECR). It leverages AWS CodeBuild and Lambda custom re

Downloads

538

Readme

TokenInjectableDockerBuilder

The TokenInjectableDockerBuilder is a flexible AWS CDK construct that enables the usage of AWS CDK tokens in the building, pushing, and deployment of Docker images to Amazon Elastic Container Registry (ECR). It leverages AWS CodeBuild and Lambda custom resources.

Why?

AWS CDK already provides mechanisms for creating deployable assets using Docker, such as DockerImageAsset and DockerImageCode, but these Constructs are limited because they cannot accept CDK tokens as build-args. With the TokenInjectableDockerBuilder, one can inject CDK tokens as build-time args into their Docker-based assets to satisfy a much larger range of dependency relationships.

For example, imagine a NextJS frontend Docker image that calls an API Gateway endpoint. Logically, one would first deploy the API Gateway, then deploy the NextJS frontend such that it has reference to the API Gateway endpoint through a build-time environment variable. In this case, building the Docker-based asset before deployment time doesn't work since it is dependent on the deployment of the API Gateway.

Features

  • Automatically builds and pushes Docker images to ECR.
  • Supports custom build arguments for Docker builds.
  • Provides Lambda functions to handle onEvent and isComplete lifecycle events for custom resources.
  • Retrieves the latest Docker image from ECR for use in ECS or Lambda.

Installation

First, install the construct using NPM:

npm install token-injectable-docker-builder

Constructor

TokenInjectableDockerBuilder

Parameters

  • scope: The construct's parent scope.
  • id: The construct ID.
  • props: Configuration properties.

Properties in TokenInjectableDockerBuilderProps

| Property | Type | Required | Description | |----------------|-----------------------------------|----------|------------------------------------------------------------| | path | string | Yes | The file path to the Dockerfile or source code directory. | | buildArgs | { [key: string]: string } | No | Build arguments to pass to the Docker build process. |


Usage Example

Here is an example of how to use the TokenInjectableDockerBuilder in your AWS CDK application:

import * as cdk from 'aws-cdk-lib';
import { TokenInjectableDockerBuilder } from 'token-injectable-docker-builder';

export class MyStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Create a TokenInjectableDockerBuilder construct
    const dockerBuilder = new TokenInjectableDockerBuilder(this, 'MyDockerBuilder', {
      path: './docker', // Path to the directory containing your Dockerfile
      buildArgs: {
        TOKEN: 'my-secret-token', // Example of a build argument
        ENV: 'production',
      },
    });

    // Retrieve the container image for ECS
    const containerImage = dockerBuilder.getContainerImage();

    // Retrieve the Docker image code for Lambda
    const dockerImageCode = dockerBuilder.getDockerImageCode();

    // Example: Use the container image in an ECS service
    new ecs.FargateTaskDefinition(this, 'TaskDefinition', {
      containerImage,
    });

    // Example: Use the Docker image code in a Lambda function
    new lambda.Function(this, 'DockerLambdaFunction', {
      runtime: lambda.Runtime.FROM_IMAGE,
      code: dockerImageCode,
      handler: lambda.Handler.FROM_IMAGE,
    });
  }
}

How It Works

  1. Docker Source: The construct packages the source code or Dockerfile specified in the path property as an S3 asset.
  2. CodeBuild Project:
    • Uses the packaged asset and build arguments to build the Docker image.
    • Pushes the image to an ECR repository.
  3. Custom Resource:
    • Triggers the build process using a Lambda function (onEvent).
    • Monitors the build status using another Lambda function (isComplete).
  4. Outputs:
    • Provides the Docker image via getContainerImage() for ECS use.
    • Provides the Docker image code via getDockerImageCode() for Lambda.

Methods

getContainerImage()

Returns a ContainerImage object that can be used in ECS services.

const containerImage = dockerBuilder.getContainerImage();

getDockerImageCode()

Returns a DockerImageCode object that can be used in Lambda functions.

const dockerImageCode = dockerBuilder.getDockerImageCode();

IAM Permissions

This construct automatically grants the required IAM permissions for:

  • CodeBuild to pull and push images to ECR.
  • CodeBuild to write logs to CloudWatch.
  • Lambda functions to monitor the build status and retrieve logs.

Notes

  • Build Arguments: Use the buildArgs property to pass custom arguments to the Docker build process. These are transformed into --build-arg flags.
  • ECR Repository: A new ECR repository is created automatically.
  • Custom Resources: Custom resources are used to handle lifecycle events and ensure the build is completed successfully.

Prerequisites

Ensure you have the following:

  1. Docker installed locally if you're testing builds.
  2. AWS CDK CLI installed (npm install -g aws-cdk).
  3. An AWS account and configured credentials.

Troubleshooting

  1. Build Errors: Check the AWS CodeBuild logs in CloudWatch.
  2. Lambda Function Errors: Check the onEvent and isComplete Lambda logs in CloudWatch.
  3. Permissions: Ensure the IAM role for CodeBuild has the required permissions to interact with ECR and CloudWatch.

Support

Open an issue on GitHub :)


Reference Links