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): cannot create lambda in public subnets #9468

Merged
merged 6 commits into from
Aug 11, 2020

Conversation

flemjame-at-amazon
Copy link
Contributor


Closes #8935

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@nija-at nija-at changed the title fix(lambda): Cannot create lambda in public subnets fix(lambda): cannot create lambda in public subnets Aug 6, 2020
nija-at
nija-at previously requested changes Aug 6, 2020
packages/@aws-cdk/aws-lambda/lib/function.ts Show resolved Hide resolved
@mergify mergify bot dismissed nija-at’s stale review August 6, 2020 14:03

Pull request has been modified.

Copy link
Contributor

@nija-at nija-at left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making these changes. The main set of code changes look good.

Some comments below around messaging, documentation and tests.

if (publicSubnetIds.has(subnetId)) {
throw new Error('Not possible to place Lambda Functions in a Public subnet');
if (publicSubnetIds.has(subnetId) && !allowPublicSubnet) {
throw new Error('Lambda Functions in a Public subnet won\'t have internet access. If you need to do this, set `allowPublicSubnet` to true');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error('Lambda Functions in a Public subnet won\'t have internet access. If you need to do this, set `allowPublicSubnet` to true');
throw new Error('Lambda Functions in a public subnet can NOT access the internet. ' +
'If you are aware of this limitation and would still like to place the function int a public subnet, set `allowPublicSubnet` to true');

Comment on lines 304 to 307
* Whether to override the error when trying to place a Function into a public subnet. Lambda functions in Public
* subnets cannot access the internet, so only do this if you need to.
*
* @default - false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Whether to override the error when trying to place a Function into a public subnet. Lambda functions in Public
* subnets cannot access the internet, so only do this if you need to.
*
* @default - false
* Lambda Functions in a public subnet can NOT access the internet.
* Use this property to acknowledge this limitation and still place the function in a public subnet.
* @see https://stackoverflow.com/questions/52992085/why-cant-an-aws-lambda-function-inside-a-public-subnet-in-a-vpc-connect-to-the/52994841#52994841
*
* @default false

test.done();
},

'picking public subnet type is not allowed if not overriding allowPublicSubnet'(test: Test) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'picking public subnet type is not allowed if not overriding allowPublicSubnet'(test: Test) {
'picking public subnet type is not allowed by default'(test: Test) {

vpc,
vpcSubnets: { subnetType: ec2.SubnetType.PUBLIC },
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that the error is the one that we actually expect, and not some other validation/error.

Suggested change
});
}, /Lambda Functions in a public subnet/);

Comment on lines 247 to 265
test.doesNotThrow(() => {
new lambda.Function(stack, 'PrivateLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_10_X,
vpc,
vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE },
});
});

test.doesNotThrow(() => {
new lambda.Function(stack, 'IsolatedLambda', {
code: new lambda.InlineCode('foo'),
handler: 'index.handler',
runtime: lambda.Runtime.NODEJS_10_X,
vpc,
vpcSubnets: { subnetType: ec2.SubnetType.ISOLATED },
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions should be replaced with assertions that the CloudFormation template is correctly configured.

This will probably need to be split into separate tests for each subnet type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These assertions should be replaced with assertions that the CloudFormation template is correctly configured.

You mean like a hasResourceLike assertion?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

@mergify mergify bot dismissed nija-at’s stale review August 10, 2020 14:02

Pull request has been modified.

@mergify
Copy link
Contributor

mergify bot commented Aug 11, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildProject6AEA49D1-qxepHUsryhcu
  • Commit ID: c4ef6f9
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify
Copy link
Contributor

mergify bot commented Aug 11, 2020

Thank you for contributing! Your pull request will be updated from master and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit b46fdc9 into aws:master Aug 11, 2020
mergify bot pushed a commit that referenced this pull request Sep 1, 2020
…10022)

Fixes #10018.
Fixes #10027.

#9468 added a flag (`allowPublicSubnet`) to `FunctionProps`, but `PythonFunction` and `NodejsFunction` props derive from `FunctionOptions`. This renders these derived function constructs unable to bypass the public subnet check that occurs in the base `Function` construct. We can resolve this issue by moving `allowPublicSubnet` to `FunctionOptions`.

I also moved `filesystem` up to `FunctionOptions` while I was here.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@flemjame-at-amazon flemjame-at-amazon deleted the lambda-public-subnet branch September 2, 2020 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[aws-lambda] Allow placing Lambda Functions in a Public subnet
3 participants