AWS CDK construct for creating AMIs using HashiCorp Packer.
This construct enables you to define a Packer build environment as part of your CDK project and use the built instances.
import { resolve } from 'path';
import { Duration } from 'aws-cdk-lib';
import { Schedule } from 'aws-cdk-lib/aws-events';
import { AmiBuilder } from 'cdk-ami-builder';
// ... within a construct
const builder = new AmiBuilder(this, 'MyCoolImageBuilder', {
buildEnvDir: resolve(__dirname, '../my-image-build-env'),
packerFileName: 'build.pkr.hcl',
imagePrefix: 'my-cool-image',
// Optional schedule for automated builds
schedule: Schedule.rate(Duration.days(7)),
});
// This might be null if fhe first build hasn't completed yet
const myCoolImage = builder.latestImage;
if (myCoolImage) {
new Instance(this, 'MyCoolInstance', {
machineImage: myCoolImage,
});
}
By default, CDK will cache the AMI ID for builder.latestImage
in cdk.context.json
.
You can override this behavior by running this command before cdk deploy
:
cdk context -f --reset ami:*
This will force CDK to check for the latest image each time it synthesizes.
• buildEnvDir: string
The path of the directory containing the Packer file and any build assets
See example-build-env for an example
• Optional
buildEnvVars: Record<string, string | ISecret | IParameter>
Additional environment variables to expose to Packer. The values may be plain strings, Secrets Manager secrets, or SSM parameters.
• Optional
buildInstanceSubnet: ISubnet
The VPC subnet in which the Packer build instance should be launched
Default
No restriction on the subnet
• imagePrefix: string
A prefix string for the names of the built AMIs
• packerFileName: string
The name of the Packer file,
relative to buildEnvDir
See example.pkr.hcl for an example
• Optional
rootDeviceName: string
The name of the block device to which the root volume is mapped. In most cases this can be left unspecified.
Default
'/dev/sda1'
• Optional
schedule: Schedule
A schedule on which new image versions should be built automatically
Default
New versions are built only when the build definition changes