Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lambda): unable to access SingletonFunction vpc connections #14533

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] }));
});
});