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

refactor(lambda): missing Lambda VPC and security validation #26528

Merged
merged 6 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 15 additions & 6 deletions packages/aws-cdk-lib/aws-lambda/lib/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1222,11 +1222,20 @@ Environment variables can be marked for removal when used in Lambda@Edge by sett
* Lambda creation properties.
*/
private configureVpc(props: FunctionProps): CfnFunction.VpcConfigProperty | undefined {
if ((props.securityGroup || props.allowAllOutbound !== undefined) && !props.vpc) {
throw new Error('Cannot configure \'securityGroup\' or \'allowAllOutbound\' without configuring a VPC');
if (props.securityGroup && props.securityGroups) {
throw new Error('Only one of the function props, securityGroup or securityGroups, is allowed');
}

if (!props.vpc) {
if (props.allowAllOutbound !== undefined) {
throw new Error('Cannot configure \'allowAllOutbound\' without configuring a VPC');
}
if (props.securityGroup) {
throw new Error('Cannot configure \'securityGroup\' without configuring a VPC');
}
if (props.securityGroups) {
throw new Error('Cannot configure \'securityGroups\' without configuring a VPC');
}
if (props.vpcSubnets) {
throw new Error('Cannot configure \'vpcSubnets\' without configuring a VPC');
}
Expand All @@ -1237,12 +1246,12 @@ Environment variables can be marked for removal when used in Lambda@Edge by sett
throw new Error('Configure \'allowAllOutbound\' directly on the supplied SecurityGroup.');
}

let securityGroups: ec2.ISecurityGroup[];

if (props.securityGroup && props.securityGroups) {
throw new Error('Only one of the function props, securityGroup or securityGroups, is allowed');
if (props.securityGroups && props.allowAllOutbound !== undefined) {
throw new Error('Configure \'allowAllOutbound\' directly on the supplied SecurityGroups.');
}

let securityGroups: ec2.ISecurityGroup[];

if (props.securityGroups) {
securityGroups = props.securityGroups;
} else {
Expand Down
127 changes: 127 additions & 0 deletions packages/aws-cdk-lib/aws-lambda/test/function.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3246,6 +3246,133 @@ test('set SnapStart to desired value', () => {
});
});

describe('VPC configuration', () => {
test('with both securityGroup and securityGroups', () => {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc', {
maxAzs: 3,
natGateways: 1,
});
const securityGroup = new ec2.SecurityGroup(stack, 'LambdaSG', {
vpc,
allowAllOutbound: false,
});
expect(() => new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.PYTHON_3_9,
securityGroup,
securityGroups: [securityGroup],
})).toThrow(/Only one of the function props, securityGroup or securityGroups, is allowed/);
});

test('with allowAllOutbound and no VPC', () => {
const stack = new cdk.Stack();
expect(() => new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.PYTHON_3_9,
allowAllOutbound: true,
})).toThrow(/Cannot configure 'allowAllOutbound' without configuring a VPC/);
});

test('with allowAllOutbound and no VPC', () => {
const stack = new cdk.Stack();
expect(() => new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.PYTHON_3_9,
allowAllOutbound: true,
})).toThrow(/Cannot configure 'allowAllOutbound' without configuring a VPC/);
});

test('with securityGroup and no VPC', () => {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc', {
maxAzs: 3,
natGateways: 1,
});
const securityGroup = new ec2.SecurityGroup(stack, 'LambdaSG', {
vpc,
allowAllOutbound: false,
});
expect(() => new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.PYTHON_3_9,
securityGroup,
})).toThrow(/Cannot configure 'securityGroup' without configuring a VPC/);
});

test('with securityGroups and no VPC', () => {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc', {
maxAzs: 3,
natGateways: 1,
});
const securityGroup = new ec2.SecurityGroup(stack, 'LambdaSG', {
vpc,
allowAllOutbound: false,
});
expect(() => new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.PYTHON_3_9,
securityGroups: [securityGroup],
})).toThrow(/Cannot configure 'securityGroups' without configuring a VPC/);
});

test('with vpcSubnets and no VPC', () => {
const stack = new cdk.Stack();
expect(() => new lambda.Function(stack, 'MyLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.PYTHON_3_9,
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
})).toThrow(/Cannot configure 'vpcSubnets' without configuring a VPC/);
});

test('with securityGroup and allowAllOutbound', () => {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc', {
maxAzs: 3,
natGateways: 1,
});
const securityGroup = new ec2.SecurityGroup(stack, 'LambdaSG', {
vpc,
allowAllOutbound: false,
});
expect(() => new lambda.Function(stack, 'MyLambda', {
vpc,
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.PYTHON_3_9,
securityGroup,
allowAllOutbound: false,
})).toThrow(/Configure 'allowAllOutbound' directly on the supplied SecurityGroup./);
});

test('with securityGroups and allowAllOutbound', () => {
const stack = new cdk.Stack();
const vpc = new ec2.Vpc(stack, 'Vpc', {
maxAzs: 3,
natGateways: 1,
});
const securityGroup = new ec2.SecurityGroup(stack, 'LambdaSG', {
vpc,
allowAllOutbound: false,
});
expect(() => new lambda.Function(stack, 'MyLambda', {
vpc,
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.PYTHON_3_9,
securityGroups: [securityGroup],
allowAllOutbound: false,
})).toThrow(/Configure 'allowAllOutbound' directly on the supplied SecurityGroups./);
});
});

function newTestLambda(scope: constructs.Construct) {
return new lambda.Function(scope, 'MyLambda', {
code: new lambda.InlineCode('foo'),
Expand Down