
Security News
The Changelog Podcast: Practical Steps to Stay Safe on npm
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.
token-injectable-docker-builder
Advanced tools
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.
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.
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. The TokenInjectableDockerBuilder allows injecting CDK tokens as build-time arguments into Docker-based assets, enabling more dynamic dependency relationships.
For example, a Next.js frontend Docker image may require an API Gateway URL as an argument to create a reference from the UI to the associated API in a given deployment. With this construct, you can deploy the API Gateway first, then pass its URL as a build-time argument to the Next.js Docker image. As a result, your Next.js frontend can dynamically fetch data from the API Gateway without hardcoding the URL or needing multiple separate stacks.
install and pre_build phases of the CodeBuild build process.completenessQueryInterval property (defaults to 30 seconds).Install the construct using NPM:
npm install token-injectable-docker-builder
Install the construct using pip:
pip install token-injectable-docker-builder
TokenInjectableDockerBuilderscope: The construct's parent scope.id: The construct ID.props: Configuration properties.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. These are transformed into --build-arg flags. To use in Dockerfile, leverage the ARG keyword. For more details, please see the official Docker docs. |
dockerLoginSecretArn | string | No | ARN of an AWS Secrets Manager secret for Docker credentials. Skips login if not provided. |
vpc | IVpc | No | The VPC in which the CodeBuild project will be deployed. If provided, the CodeBuild project will be launched within the specified VPC. |
securityGroups | ISecurityGroup[] | No | The security groups to attach to the CodeBuild project. These should define the network access rules for the CodeBuild project. |
subnetSelection | SubnetSelection | No | The subnet selection to specify which subnets to use within the VPC. Allows the user to select private, public, or isolated subnets. |
installCommands | string[] | No | Custom commands to run during the install phase of the CodeBuild build process. Will be executed before the Docker image is built. Useful for installing necessary dependencies for running pre-build scripts. |
preBuildCommands | string[] | No | Custom commands to run during the pre_build phase of the CodeBuild build process. Will be executed before the Docker image is built. Useful for running pre-build scripts, such as fetching configs. |
kmsEncryption | boolean | No | Whether to enable KMS encryption for the ECR repository. If true, a KMS key will be created for encrypting ECR images; otherwise, AES-256 encryption is used. Defaults to false. |
completenessQueryInterval | Duration | No | The query interval for checking if the CodeBuild project has completed. This determines how frequently the custom resource polls for build completion. Defaults to Duration.seconds(30). |
exclude | string[] | No | A list of file paths in the Docker directory to exclude from the S3 asset bundle. If a .dockerignore file is present in the source directory, its contents will be used if this prop is not set. Defaults to an empty list or .dockerignore contents. |
This example demonstrates the basic usage of the TokenInjectableDockerBuilder, where a Next.js frontend Docker image requires an API Gateway URL as a build argument to create a reference from the UI to the associated API in a given deployment.
import * as cdk from 'aws-cdk-lib';
import { TokenInjectableDockerBuilder } from 'token-injectable-docker-builder';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
export class SimpleStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create your API Gateway
const api = new apigateway.RestApi(this, 'MyApiGateway', {
restApiName: 'MyService',
});
// Create the Docker builder
const dockerBuilder = new TokenInjectableDockerBuilder(this, 'SimpleDockerBuilder', {
path: './nextjs-app', // Path to your Next.js app Docker context
buildArgs: {
API_URL: api.url, // Pass the API Gateway URL as a build argument
},
// Optionally override the default completeness query interval:
// completenessQueryInterval: cdk.Duration.seconds(45),
});
// Use in ECS
const cluster = new ecs.Cluster(this, 'EcsCluster', {
vpc: new ec2.Vpc(this, 'Vpc'),
});
const service = new ecs.FargateService(this, 'FargateService', {
cluster,
taskDefinition: new ecs.FargateTaskDefinition(this, 'TaskDef', {
cpu: 512,
memoryLimitMiB: 1024,
}).addContainer('Container', {
image: dockerBuilder.containerImage,
logging: ecs.LogDriver.awsLogs({ streamPrefix: 'MyApp' }),
}),
});
service.node.addDependency(dockerBuilder);
}
}
from aws_cdk import (
aws_ecs as ecs,
aws_ec2 as ec2,
aws_apigateway as apigateway,
Duration,
core as cdk,
)
from token_injectable_docker_builder import TokenInjectableDockerBuilder
class SimpleStack(cdk.Stack):
def __init__(self, scope: cdk.App, id: str, **kwargs):
super().__init__(scope, id, **kwargs)
# Create your API Gateway
api = apigateway.RestApi(self, "MyApiGateway",
rest_api_name="MyService",
)
# Create the Docker builder
docker_builder = TokenInjectableDockerBuilder(self, "SimpleDockerBuilder",
path="./nextjs-app", # Path to your Next.js app Docker context
build_args={
"API_URL": api.url, # Pass the API Gateway URL as a build argument
},
# Optionally override the default completeness query interval:
# completeness_query_interval=Duration.seconds(45)
)
# Use in ECS
vpc = ec2.Vpc(self, "Vpc")
cluster = ecs.Cluster(self, "EcsCluster", vpc=vpc)
task_definition = ecs.FargateTaskDefinition(self, "TaskDef",
cpu=512,
memory_limit_mib=1024,
)
task_definition.node.add_dependency(docker_builder)
task_definition.add_container("Container",
image=docker_builder.container_image,
logging=ecs.LogDriver.aws_logs(stream_prefix="MyApp"),
)
ecs.FargateService(self, "FargateService",
cluster=cluster,
task_definition=task_definition,
)
Building on the previous example, this advanced usage demonstrates how to include additional configurations, such as fetching private API endpoints and configuration files during the build process.
import * as cdk from 'aws-cdk-lib';
import { TokenInjectableDockerBuilder } from 'token-injectable-docker-builder';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
export class AdvancedStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create your API Gateway
const api = new apigateway.RestApi(this, 'MyApiGateway', {
restApiName: 'MyService',
});
// VPC and Security Group for CodeBuild
const vpc = new ec2.Vpc(this, 'MyVpc');
const securityGroup = new ec2.SecurityGroup(this, 'MySecurityGroup', {
vpc,
});
// Create the Docker builder with additional pre-build commands
const dockerBuilder = new TokenInjectableDockerBuilder(this, 'AdvancedDockerBuilder', {
path: './nextjs-app',
buildArgs: {
API_URL: api.url,
},
vpc,
securityGroups: [securityGroup],
subnetSelection: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
installCommands: [
'echo "Updating package lists..."',
'apt-get update -y',
'echo "Installing necessary packages..."',
'apt-get install -y curl',
],
preBuildCommands: [
'echo "Fetching private API configuration..."',
// Replace with your actual command to fetch configs
'curl -o config.json https://internal-api.example.com/config',
],
// Optionally override the default completeness query interval:
// completenessQueryInterval: cdk.Duration.seconds(45),
});
// Use in ECS
const cluster = new ecs.Cluster(this, 'EcsCluster', { vpc });
const service = new ecs.FargateService(this, 'FargateService', {
cluster,
taskDefinition: new ecs.FargateTaskDefinition(this, 'TaskDef', {
cpu: 512,
memoryLimitMiB: 1024,
}).addContainer('Container', {
image: dockerBuilder.containerImage,
logging: ecs.LogDriver.awsLogs({ streamPrefix: 'MyApp' }),
}),
});
service.node.addDependency(dockerBuilder);
}
}
from aws_cdk import (
aws_ecs as ecs,
aws_ec2 as ec2,
aws_apigateway as apigateway,
Duration,
core as cdk,
)
from token_injectable_docker_builder import TokenInjectableDockerBuilder
class AdvancedStack(cdk.Stack):
def __init__(self, scope: cdk.App, id: str, **kwargs):
super().__init__(scope, id, **kwargs)
# Create your API Gateway
api = apigateway.RestApi(self, "MyApiGateway",
rest_api_name="MyService",
)
# VPC and Security Group for CodeBuild
vpc = ec2.Vpc(self, "MyVpc")
security_group = ec2.SecurityGroup(self, "MySecurityGroup", vpc=vpc)
# Create the Docker builder with additional pre-build commands
docker_builder = TokenInjectableDockerBuilder(self, "AdvancedDockerBuilder",
path="./nextjs-app",
build_args={
"API_URL": api.url,
},
vpc=vpc,
security_groups=[security_group],
subnet_selection=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PRIVATE_WITH_EGRESS),
install_commands=[
'echo "Updating package lists..."',
'apt-get update -y',
'echo "Installing necessary packages..."',
'apt-get install -y curl',
],
pre_build_commands=[
'echo "Fetching private API configuration..."',
# Replace with your actual command to fetch configs
'curl -o config.json https://internal-api.example.com/config',
],
# Optionally override the default completeness query interval:
# completeness_query_interval=Duration.seconds(45)
)
# Use in ECS
cluster = ecs.Cluster(self, "EcsCluster", vpc=vpc)
task_definition = ecs.FargateTaskDefinition(self, "TaskDef",
cpu=512,
memory_limit_mib=1024,
)
task_definition.node.add_dependency(docker_builder)
task_definition.add_container("Container",
image=docker_builder.container_image,
logging=ecs.LogDriver.aws_logs(stream_prefix="MyApp"),
)
ecs.FargateService(self, "FargateService",
cluster=cluster,
task_definition=task_definition,
)
In this advanced example:
installCommands and preBuildCommands properties are used to install necessary packages and fetch configuration files from a private API before building the Docker image.Docker Source: Packages the source code or Dockerfile specified in the path property as an S3 asset.
CodeBuild Project:
buildArgs to build the Docker image.installCommands and preBuildCommands during the build process.Custom Resource:
onEvent).isComplete) which polls at the interval specified by completenessQueryInterval (defaulting to 30 seconds if not provided).Outputs:
.containerImage: Returns the Docker image for ECS..dockerImageCode: Returns the Docker image code for Lambda.The construct automatically grants permissions for:
CodeBuild:
dockerLoginSecretArn is provided.Lambda Functions:
buildArgs as --build-arg flags. CDK tokens can be used to inject dynamic values resolved at deployment time.installCommands and preBuildCommands to run custom shell commands during the build process. This can be useful for installing dependencies or fetching configuration files.completenessQueryInterval property.onEvent and isComplete Lambda function logs in CloudWatch Logs.For issues or feature requests, please open an issue on GitHub.
This project is licensed under the terms of the MIT license.
Feel free to reach out if you have any questions or need further assistance!
FAQs
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.
We found that token-injectable-docker-builder demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Learn the essential steps every developer should take to stay secure on npm and reduce exposure to supply chain attacks.

Security News
Experts push back on new claims about AI-driven ransomware, warning that hype and sponsored research are distorting how the threat is understood.

Security News
Ruby's creator Matz assumes control of RubyGems and Bundler repositories while former maintainers agree to step back and transfer all rights to end the dispute.