Skip to content

Commit

Permalink
feat: support CodeBuild on VPC for ContainerImageBuild (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmokmss authored Jan 25, 2025
1 parent c2d1940 commit d0e97c4
Show file tree
Hide file tree
Showing 8 changed files with 3,550 additions and 1,563 deletions.
16 changes: 16 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/container-image-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { existsSync, readFileSync } from 'fs';
import { join } from 'path';
import { CfnResource, CustomResource, Duration, RemovalPolicy } from 'aws-cdk-lib';
import { BuildSpec, LinuxBuildImage } from 'aws-cdk-lib/aws-codebuild';
import { IVpc } from 'aws-cdk-lib/aws-ec2';
import { IRepository, Repository } from 'aws-cdk-lib/aws-ecr';
import { DockerImageAssetProps } from 'aws-cdk-lib/aws-ecr-assets';
import { ContainerImage } from 'aws-cdk-lib/aws-ecs';
Expand Down Expand Up @@ -31,6 +32,15 @@ export interface ContainerImageBuildProps extends DockerImageAssetProps {
* @default false
*/
readonly zstdCompression?: boolean;

/**
* The VPC where your build job will be deployed.
* This VPC must have private subnets with NAT Gateways.
*
* Use this property when you want to control the outbound IP addresses that base images are pulled from.
* @default No VPC used.
*/
readonly vpc?: IVpc;
}

/**
Expand Down Expand Up @@ -74,6 +84,7 @@ export class ContainerImageBuild extends Construct implements IGrantable {
buildImage: buildImage,
privileged: true,
},
vpc: props.vpc,
buildSpec: BuildSpec.fromObject({
version: '0.2',
phases: {
Expand Down
17 changes: 14 additions & 3 deletions src/singleton-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class SingletonProject extends Construct {
}

private ensureProject(props: SingletonProjectProps): Project {
const constructName = (props.projectPurpose ?? 'SingletonProject') + this.slugify(props.uuid);
const constructName = (props.projectPurpose ?? 'SingletonProject') + this.slugify(props.uuid, this.propsToAdditionalString(props));
const existing = Stack.of(this).node.tryFindChild(constructName);
if (existing) {
return existing as Project;
Expand All @@ -41,7 +41,18 @@ export class SingletonProject extends Construct {
return new Project(Stack.of(this), constructName, props);
}

private slugify(x: string): string {
return x.replace(/[^a-zA-Z0-9]/g, '');
private propsToAdditionalString(props: SingletonProjectProps) {
// This string must be stable to avoid from replacement.
// Things that can be added to the slug later (we have to create a new project per these properties):
// * vpc addr
// * instance type
// But actually, replacement will not cause any disruption because of its stateless nature.
let slug = '';
slug += props.vpc?.node.addr ?? '';
return slug;
}

private slugify(x: string, additionalString?: string): string {
return `${x}${additionalString ?? ''}`.replace(/[^a-zA-Z0-9]/g, '');
}
}
Loading

0 comments on commit d0e97c4

Please sign in to comment.