Skip to content

Commit

Permalink
Merge branch 'master' into nija-at/apigw-integration-hash
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Nov 29, 2021
2 parents f01f73e + a1685c6 commit 2b45903
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 8 deletions.
20 changes: 19 additions & 1 deletion packages/@aws-cdk/aws-ec2/lib/vpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,13 @@ export interface SubnetConfiguration {
* @default false
*/
readonly reserved?: boolean;

/**
* Controls if a public IP is associated to an instance at launch
*
* @default true in Subnet.Public, false in Subnet.Private or Subnet.Isolated.
*/
readonly mapPublicIpOnLaunch?: boolean;
}

/**
Expand Down Expand Up @@ -1452,12 +1459,23 @@ export class Vpc extends VpcBase {
return;
}

// mapPublicIpOnLaunch true in Subnet.Public, false in Subnet.Private or Subnet.Isolated.
let mapPublicIpOnLaunch = false;
if (subnetConfig.subnetType !== SubnetType.PUBLIC && subnetConfig.mapPublicIpOnLaunch !== undefined) {
throw new Error(`${subnetConfig.subnetType} subnet cannot include mapPublicIpOnLaunch parameter`);
}
if (subnetConfig.subnetType === SubnetType.PUBLIC) {
mapPublicIpOnLaunch = (subnetConfig.mapPublicIpOnLaunch !== undefined)
? subnetConfig.mapPublicIpOnLaunch
: true;
}

const name = subnetId(subnetConfig.name, index);
const subnetProps: SubnetProps = {
availabilityZone: zone,
vpcId: this.vpcId,
cidrBlock: this.networkBuilder.addSubnet(cidrMask),
mapPublicIpOnLaunch: (subnetConfig.subnetType === SubnetType.PUBLIC),
mapPublicIpOnLaunch: mapPublicIpOnLaunch,
};

let subnet: Subnet;
Expand Down
78 changes: 77 additions & 1 deletion packages/@aws-cdk/aws-ec2/test/vpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,82 @@ describe('vpc', () => {

});

test('with public subnets MapPublicIpOnLaunch is true if parameter mapPublicIpOnLaunch is true', () => {
const stack = getTestStack();
new Vpc(stack, 'VPC', {
maxAzs: 1,
subnetConfiguration: [
{
cidrMask: 24,
name: 'ingress',
subnetType: SubnetType.PUBLIC,
mapPublicIpOnLaunch: true,
},
],
});
expect(stack).toCountResources('AWS::EC2::Subnet', 1);
expect(stack).not.toHaveResource('AWS::EC2::NatGateway');
expect(stack).toHaveResource('AWS::EC2::Subnet', {
MapPublicIpOnLaunch: true,
});
});
test('with public subnets MapPublicIpOnLaunch is false if parameter mapPublicIpOnLaunch is false', () => {
const stack = getTestStack();
new Vpc(stack, 'VPC', {
maxAzs: 1,
subnetConfiguration: [
{
cidrMask: 24,
name: 'ingress',
subnetType: SubnetType.PUBLIC,
mapPublicIpOnLaunch: false,
},
],
});
expect(stack).toCountResources('AWS::EC2::Subnet', 1);
expect(stack).not.toHaveResource('AWS::EC2::NatGateway');
expect(stack).toHaveResource('AWS::EC2::Subnet', {
MapPublicIpOnLaunch: false,
});
});
test('with private subnets throw exception if parameter mapPublicIpOnLaunch is defined', () => {
const stack = getTestStack();
expect(() => {
new Vpc(stack, 'VPC', {
maxAzs: 1,
subnetConfiguration: [
{
name: 'public',
subnetType: SubnetType.PUBLIC,
},
{
name: 'private',
subnetType: SubnetType.PRIVATE_WITH_NAT,
mapPublicIpOnLaunch: true,
},
],
});
}).toThrow(/subnet cannot include mapPublicIpOnLaunch parameter/);
});
test('with isolated subnets throw exception if parameter mapPublicIpOnLaunch is defined', () => {
const stack = getTestStack();
expect(() => {
new Vpc(stack, 'VPC', {
maxAzs: 1,
subnetConfiguration: [
{
name: 'public',
subnetType: SubnetType.PUBLIC,
},
{
name: 'private',
subnetType: SubnetType.PRIVATE_ISOLATED,
mapPublicIpOnLaunch: true,
},
],
});
}).toThrow(/subnet cannot include mapPublicIpOnLaunch parameter/);
});
test('maxAZs defaults to 3 if unset', () => {
const stack = getTestStack();
new Vpc(stack, 'VPC');
Expand Down Expand Up @@ -1817,4 +1893,4 @@ function hasTags(expectedTags: Array<{Key: string, Value: string}>): (props: any
throw e;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,6 @@
"LBListenerTargetsGroup76EF81E8": {
"Type": "AWS::ElasticLoadBalancingV2::TargetGroup",
"Properties": {
"TargetGroupAttributes": [
{
"Key": "stickiness.enabled",
"Value": "false"
}
],
"Targets": [
{
"Id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ export class ApplicationTargetGroup extends TargetGroupBase implements IApplicat
const result = target.attachToApplicationTargetGroup(this);
this.addLoadBalancerTarget(result);
}

if (this.targetType === TargetType.LAMBDA) {
this.setAttribute('stickiness.enabled', undefined);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,33 @@ describe('tests', () => {
}).toThrow(/'vpc' is required for a non-Lambda TargetGroup/);
});

test('Lambda target should not have stickiness.enabled set', () => {
const app = new cdk.App();
const stack = new cdk.Stack(app, 'Stack');

new elbv2.ApplicationTargetGroup(stack, 'TG', {
targetType: elbv2.TargetType.LAMBDA,
});

const tg = new elbv2.ApplicationTargetGroup(stack, 'TG2');
tg.addTarget({
attachToApplicationTargetGroup(_targetGroup: elbv2.IApplicationTargetGroup): elbv2.LoadBalancerTargetProps {
return {
targetType: elbv2.TargetType.LAMBDA,
targetJson: { id: 'arn:aws:lambda:eu-west-1:123456789012:function:myFn' },
};
},
});

expect(stack).not.toHaveResourceLike('AWS::ElasticLoadBalancingV2::TargetGroup', {
TargetGroupAttributes: [
{
Key: 'stickiness.enabled',
},
],
});
});

test('Can add self-registering target to imported TargetGroup', () => {
// GIVEN
const app = new cdk.App();
Expand Down

0 comments on commit 2b45903

Please sign in to comment.