Skip to content

Commit 531b7d9

Browse files
authored
Merge branch 'master' into image-lambda-metadata
2 parents eb67bcd + 050f6fa commit 531b7d9

5 files changed

Lines changed: 64 additions & 9 deletions

File tree

packages/@aws-cdk/aws-ec2/lib/nat.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,8 @@ class NatGatewayProvider extends NatProvider {
233233
// Create the NAT gateways
234234
let i = 0;
235235
for (const sub of options.natSubnets) {
236-
const gateway = sub.addNatGateway();
237-
if (this.props.eipAllocationIds) {
238-
gateway.allocationId = pickN(i, this.props.eipAllocationIds);
239-
}
236+
const eipAllocationId = this.props.eipAllocationIds ? pickN(i, this.props.eipAllocationIds) : undefined;
237+
const gateway = sub.addNatGateway(eipAllocationId);
240238
this.gateways.add(sub.availabilityZone, gateway.ref);
241239
i++;
242240
}

packages/@aws-cdk/aws-ec2/lib/vpc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,11 +1840,11 @@ export class PublicSubnet extends Subnet implements IPublicSubnet {
18401840
* Also adds the EIP for the managed NAT.
18411841
* @returns A ref to the the NAT Gateway ID
18421842
*/
1843-
public addNatGateway() {
1843+
public addNatGateway(eipAllocationId?: string) {
18441844
// Create a NAT Gateway in this public subnet
18451845
const ngw = new CfnNatGateway(this, 'NATGateway', {
18461846
subnetId: this.subnetId,
1847-
allocationId: new CfnEIP(this, 'EIP', {
1847+
allocationId: eipAllocationId ?? new CfnEIP(this, 'EIP', {
18481848
domain: 'vpc',
18491849
}).attrAllocationId,
18501850
});

packages/@aws-cdk/aws-ec2/test/vpc.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,31 @@ describe('vpc', () => {
585585

586586
});
587587

588+
test('EIP passed with NAT gateway does not create duplicate EIP', () => {
589+
const stack = getTestStack();
590+
new Vpc(stack, 'VPC', {
591+
cidr: '10.0.0.0/16',
592+
subnetConfiguration: [
593+
{
594+
cidrMask: 24,
595+
name: 'ingress',
596+
subnetType: SubnetType.PUBLIC,
597+
},
598+
{
599+
cidrMask: 24,
600+
name: 'application',
601+
subnetType: SubnetType.PRIVATE_WITH_NAT,
602+
},
603+
],
604+
natGatewayProvider: NatProvider.gateway({ eipAllocationIds: ['b'] }),
605+
natGateways: 1,
606+
});
607+
expect(stack).toCountResources('AWS::EC2::EIP', 0);
608+
expect(stack).toHaveResource('AWS::EC2::NatGateway', {
609+
AllocationId: 'b',
610+
});
611+
});
612+
588613
test('with mis-matched nat and subnet configs it throws', () => {
589614
const stack = getTestStack();
590615
expect(() => new Vpc(stack, 'VPC', {

packages/@aws-cdk/aws-lambda-event-sources/lib/kafka.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ISecurityGroup, IVpc, SubnetSelection } from '@aws-cdk/aws-ec2';
33
import * as iam from '@aws-cdk/aws-iam';
44
import * as lambda from '@aws-cdk/aws-lambda';
55
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
6-
import { Stack } from '@aws-cdk/core';
6+
import { Stack, Names } from '@aws-cdk/core';
77
import { StreamEventSource, StreamEventSourceProps } from './stream';
88

99
// keep this import separate from other imports to reduce chance for merge conflicts with v2-main
@@ -101,15 +101,16 @@ export interface SelfManagedKafkaEventSourceProps extends KafkaEventSourceProps
101101
export class ManagedKafkaEventSource extends StreamEventSource {
102102
// This is to work around JSII inheritance problems
103103
private innerProps: ManagedKafkaEventSourceProps;
104+
private _eventSourceMappingId?: string = undefined;
104105

105106
constructor(props: ManagedKafkaEventSourceProps) {
106107
super(props);
107108
this.innerProps = props;
108109
}
109110

110111
public bind(target: lambda.IFunction) {
111-
target.addEventSourceMapping(
112-
`KafkaEventSource:${this.innerProps.clusterArn}${this.innerProps.topic}`,
112+
const eventSourceMapping = target.addEventSourceMapping(
113+
`KafkaEventSource:${Names.nodeUniqueId(target.node)}${this.innerProps.topic}`,
113114
this.enrichMappingOptions({
114115
eventSourceArn: this.innerProps.clusterArn,
115116
startingPosition: this.innerProps.startingPosition,
@@ -118,6 +119,8 @@ export class ManagedKafkaEventSource extends StreamEventSource {
118119
}),
119120
);
120121

122+
this._eventSourceMappingId = eventSourceMapping.eventSourceMappingId;
123+
121124
if (this.innerProps.secret !== undefined) {
122125
this.innerProps.secret.grantRead(target);
123126
}
@@ -146,6 +149,16 @@ export class ManagedKafkaEventSource extends StreamEventSource {
146149
? undefined
147150
: sourceAccessConfigurations;
148151
}
152+
153+
/**
154+
* The identifier for this EventSourceMapping
155+
*/
156+
public get eventSourceMappingId(): string {
157+
if (!this._eventSourceMappingId) {
158+
throw new Error('KafkaEventSource is not yet bound to an event source mapping');
159+
}
160+
return this._eventSourceMappingId;
161+
}
149162
}
150163

151164
/**

packages/@aws-cdk/aws-lambda-event-sources/test/kafka.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,5 +488,24 @@ describe('KafkaEventSource', () => {
488488
]),
489489
});
490490
});
491+
492+
test('ManagedKafkaEventSource name conforms to construct id rules', () => {
493+
// GIVEN
494+
const stack = new cdk.Stack();
495+
const fn = new TestFunction(stack, 'Fn');
496+
const clusterArn = 'some-arn';
497+
const kafkaTopic = 'some-topic';
498+
499+
const mskEventMapping = new sources.ManagedKafkaEventSource(
500+
{
501+
clusterArn,
502+
topic: kafkaTopic,
503+
startingPosition: lambda.StartingPosition.TRIM_HORIZON,
504+
});
505+
506+
// WHEN
507+
fn.addEventSource(mskEventMapping);
508+
expect(mskEventMapping.eventSourceMappingId).toBeDefined();
509+
});
491510
});
492511
});

0 commit comments

Comments
 (0)