Skip to content

Commit

Permalink
fix(lambda): unable to access SingletonFunction vpc connections (aws#…
Browse files Browse the repository at this point in the history
…14533)

fixes aws#6261

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
yattoni authored and hollanddd committed Aug 26, 2021
1 parent 931b536 commit 3a42614
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/@aws-cdk/aws-lambda/lib/singleton-lambda.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
Expand Down Expand Up @@ -63,6 +64,20 @@ export class SingletonFunction extends FunctionBase {
this.canCreatePermissions = true; // Doesn't matter, addPermission is overriden anyway
}

/**
* @inheritdoc
*/
public get isBoundToVpc(): boolean {
return this.lambdaFunction.isBoundToVpc;
}

/**
* @inheritdoc
*/
public get connections(): ec2.Connections {
return this.lambdaFunction.connections;
}

/**
* Returns a `lambda.Version` which represents the current version of this
* singleton Lambda function. A new version will be created every time the
Expand Down
24 changes: 24 additions & 0 deletions packages/@aws-cdk/aws-lambda/test/singleton-lambda.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import '@aws-cdk/assert-internal/jest';
import { ResourcePart } from '@aws-cdk/assert-internal';
import * as ec2 from '@aws-cdk/aws-ec2';
import * as iam from '@aws-cdk/aws-iam';
import * as cdk from '@aws-cdk/core';
import * as lambda from '../lib';
Expand Down Expand Up @@ -174,4 +175,27 @@ describe('singleton lambda', () => {
},
});
});

test('bind to vpc and access connections', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'VPC', { maxAzs: 2 });
const securityGroup = new ec2.SecurityGroup(stack, 'SecurityGroup', {
vpc: vpc,
});

// WHEN
const singleton = new lambda.SingletonFunction(stack, 'Singleton', {
uuid: '84c0de93-353f-4217-9b0b-45b6c993251a',
code: new lambda.InlineCode('foo'),
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'index.handler',
securityGroups: [securityGroup],
vpc: vpc,
});

// THEN
expect(singleton.isBoundToVpc).toBeTruthy();
expect(singleton.connections).toEqual(new ec2.Connections({ securityGroups: [securityGroup] }));
});
});

0 comments on commit 3a42614

Please sign in to comment.