Skip to content

Commit

Permalink
Merge branch 'master' into ec2-instance-type-g5
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Nov 15, 2021
2 parents 7b991bc + 0fe090c commit cb28c23
Show file tree
Hide file tree
Showing 32 changed files with 596 additions and 273 deletions.
6 changes: 2 additions & 4 deletions packages/@aws-cdk/aws-ec2/lib/nat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,8 @@ class NatGatewayProvider extends NatProvider {
// Create the NAT gateways
let i = 0;
for (const sub of options.natSubnets) {
const gateway = sub.addNatGateway();
if (this.props.eipAllocationIds) {
gateway.allocationId = pickN(i, this.props.eipAllocationIds);
}
const eipAllocationId = this.props.eipAllocationIds ? pickN(i, this.props.eipAllocationIds) : undefined;
const gateway = sub.addNatGateway(eipAllocationId);
this.gateways.add(sub.availabilityZone, gateway.ref);
i++;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1840,11 +1840,11 @@ export class PublicSubnet extends Subnet implements IPublicSubnet {
* Also adds the EIP for the managed NAT.
* @returns A ref to the the NAT Gateway ID
*/
public addNatGateway() {
public addNatGateway(eipAllocationId?: string) {
// Create a NAT Gateway in this public subnet
const ngw = new CfnNatGateway(this, 'NATGateway', {
subnetId: this.subnetId,
allocationId: new CfnEIP(this, 'EIP', {
allocationId: eipAllocationId ?? new CfnEIP(this, 'EIP', {
domain: 'vpc',
}).attrAllocationId,
});
Expand Down
25 changes: 25 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/vpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,31 @@ describe('vpc', () => {

});

test('EIP passed with NAT gateway does not create duplicate EIP', () => {
const stack = getTestStack();
new Vpc(stack, 'VPC', {
cidr: '10.0.0.0/16',
subnetConfiguration: [
{
cidrMask: 24,
name: 'ingress',
subnetType: SubnetType.PUBLIC,
},
{
cidrMask: 24,
name: 'application',
subnetType: SubnetType.PRIVATE_WITH_NAT,
},
],
natGatewayProvider: NatProvider.gateway({ eipAllocationIds: ['b'] }),
natGateways: 1,
});
expect(stack).toCountResources('AWS::EC2::EIP', 0);
expect(stack).toHaveResource('AWS::EC2::NatGateway', {
AllocationId: 'b',
});
});

test('with mis-matched nat and subnet configs it throws', () => {
const stack = getTestStack();
expect(() => new Vpc(stack, 'VPC', {
Expand Down
71 changes: 66 additions & 5 deletions packages/@aws-cdk/aws-ecr-assets/lib/image-asset.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as fs from 'fs';
import * as path from 'path';
import * as ecr from '@aws-cdk/aws-ecr';
import { Annotations, AssetStaging, FeatureFlags, FileFingerprintOptions, IgnoreMode, Stack, SymlinkFollowMode, Token, Stage } from '@aws-cdk/core';
import { Annotations, AssetStaging, FeatureFlags, FileFingerprintOptions, IgnoreMode, Stack, SymlinkFollowMode, Token, Stage, CfnResource } from '@aws-cdk/core';
import * as cxapi from '@aws-cdk/cx-api';
import { Construct } from 'constructs';

Expand Down Expand Up @@ -147,6 +147,30 @@ export class DockerImageAsset extends CoreConstruct implements IAsset {
*/
public readonly assetHash: string;

/**
* The path to the asset, relative to the current Cloud Assembly
*
* If asset staging is disabled, this will just be the original path.
*
* If asset staging is enabled it will be the staged path.
*/
private readonly assetPath: string;

/**
* The path to the Dockerfile, relative to the assetPath
*/
private readonly dockerfilePath?: string;

/**
* Build args to pass to the `docker build` command.
*/
private readonly dockerBuildArgs?: { [key: string]: string };

/**
* Docker target to build to
*/
private readonly dockerBuildTarget?: string;

constructor(scope: Construct, id: string, props: DockerImageAssetProps) {
super(scope, id);

Expand All @@ -160,7 +184,8 @@ export class DockerImageAsset extends CoreConstruct implements IAsset {
}

// validate the docker file exists
const file = path.join(dir, props.file || 'Dockerfile');
this.dockerfilePath = props.file || 'Dockerfile';
const file = path.join(dir, this.dockerfilePath);
if (!fs.existsSync(file)) {
throw new Error(`Cannot find file at ${file}`);
}
Expand Down Expand Up @@ -223,17 +248,53 @@ export class DockerImageAsset extends CoreConstruct implements IAsset {
this.assetHash = staging.assetHash;

const stack = Stack.of(this);
this.assetPath = staging.relativeStagedPath(stack);
this.dockerBuildArgs = props.buildArgs;
this.dockerBuildTarget = props.target;

const location = stack.synthesizer.addDockerImageAsset({
directoryName: staging.relativeStagedPath(stack),
dockerBuildArgs: props.buildArgs,
dockerBuildTarget: props.target,
directoryName: this.assetPath,
dockerBuildArgs: this.dockerBuildArgs,
dockerBuildTarget: this.dockerBuildTarget,
dockerFile: props.file,
sourceHash: staging.assetHash,
});

this.repository = ecr.Repository.fromRepositoryName(this, 'Repository', location.repositoryName);
this.imageUri = location.imageUri;
}

/**
* Adds CloudFormation template metadata to the specified resource with
* information that indicates which resource property is mapped to this local
* asset. This can be used by tools such as SAM CLI to provide local
* experience such as local invocation and debugging of Lambda functions.
*
* Asset metadata will only be included if the stack is synthesized with the
* "aws:cdk:enable-asset-metadata" context key defined, which is the default
* behavior when synthesizing via the CDK Toolkit.
*
* @see https://github.com/aws/aws-cdk/issues/1432
*
* @param resource The CloudFormation resource which is using this asset [disable-awslint:ref-via-interface]
* @param resourceProperty The property name where this asset is referenced
*/
public addResourceMetadata(resource: CfnResource, resourceProperty: string) {
if (!this.node.tryGetContext(cxapi.ASSET_RESOURCE_METADATA_ENABLED_CONTEXT)) {
return; // not enabled
}

// tell tools such as SAM CLI that the resourceProperty of this resource
// points to a local path and include the path to de dockerfile, docker build args, and target,
// in order to enable local invocation of this function.
resource.cfnOptions.metadata = resource.cfnOptions.metadata || { };
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_PATH_KEY] = this.assetPath;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKERFILE_PATH_KEY] = this.dockerfilePath;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_ARGS_KEY] = this.dockerBuildArgs;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_DOCKER_BUILD_TARGET_KEY] = this.dockerBuildTarget;
resource.cfnOptions.metadata[cxapi.ASSET_RESOURCE_METADATA_PROPERTY_KEY] = resourceProperty;
}

}

function validateProps(props: DockerImageAssetProps) {
Expand Down
Loading

0 comments on commit cb28c23

Please sign in to comment.