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

aws-lambda: VpcConfig is not configured for Lambda Function without specifying Vpc prop #26508

Closed
longtv2222 opened this issue Jul 26, 2023 · 3 comments · Fixed by #26528
Closed
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@longtv2222
Copy link
Contributor

longtv2222 commented Jul 26, 2023

Describe the bug

Specifying securityGroups without specifying vpc prop in Lambda Function leads to VpcConfig not added to the generated CloudFormation Template.

Expected Behavior

VpcConfig is added to Lambda Function:

  "functionF19B1A04": {
   "Type": "AWS::Lambda::Function",
   "Properties": {
    "Code": {
     "ZipFile": "..."
    },
    "Handler": "index.handler",
    "Role": {
     "Fn::GetAtt": [
      "functionServiceRoleEF216095",
      "Arn"
     ]
    },
    "Runtime": "nodejs18.x",
    "VpcConfig": { // VpcConfig is added here
        ...
    }
   },
   "DependsOn": [
    ...
   ]

Current Behavior

VpcConfig is not in Lambda Function

  "functionF19B1A04": {
   "Type": "AWS::Lambda::Function",
   "Properties": {
    "Code": {
     "ZipFile": "..."
    },
    "Handler": "index.handler",
    "Role": {
     "Fn::GetAtt": [
      "functionServiceRoleEF216095",
      "Arn"
     ]
    },
    "Runtime": "nodejs18.x"
   },
   "DependsOn": [
    "functionServiceRoleEF216095"
   ],
   "Metadata": {
    "aws:cdk:path": "TestStack/function/Resource"
   }
  },

Reproduction Steps

The following code does not add VpcConfig to CloudFormation template for Lambda function:

import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as ec2 from 'aws-cdk-lib/aws-ec2'
import * as lambda from 'aws-cdk-lib/aws-lambda';

export class TestStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const vpc = new ec2.Vpc(this, "vpc");
    const sg = new ec2.SecurityGroup(this, "sg", {
      vpc
    });

    const fn = new lambda.Function(this, 'function', {
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline("..."),
      securityGroups:[sg],
    });
  }
}

However, by adding vpc to props, now VpcConfig is added to the CloudFormation template

    const fn = new lambda.Function(this, 'function', {
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline("..."),
      securityGroups:[sg],
      vpc, // Add vpc to prop
    });

Possible Solution

securityGroup (no s) is deprecated and CDK is not throwing error if securityGroups is specified and vpc is not specified

if ((props.securityGroup || props.allowAllOutbound !== undefined) && !props.vpc) {
throw new Error('Cannot configure \'securityGroup\' or \'allowAllOutbound\' without configuring a VPC');
}

Additional Information/Context

No response

CDK CLI Version

2.87.0

Framework Version

No response

Node.js Version

18

OS

Window

Language

Typescript

Language Version

No response

Other information

No response

@longtv2222 longtv2222 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jul 26, 2023
@github-actions github-actions bot added the @aws-cdk/aws-lambda Related to AWS Lambda label Jul 26, 2023
@longtv2222 longtv2222 changed the title aws-lambda: VpcConfig is not configured for Lambda Function without Vpc aws-lambda: VpcConfig is not configured for Lambda Function without specifying Vpc prop Jul 26, 2023
@pahud
Copy link
Contributor

pahud commented Jul 26, 2023

Agree. configureVpc() will only be executed if props.vpc is provided. We need a check when props.securityGroup is provided and vpc undefined.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jul 26, 2023
lpizzinidev added a commit to lpizzinidev/aws-cdk that referenced this issue Aug 18, 2023
rix0rrr added a commit to lpizzinidev/aws-cdk that referenced this issue Aug 22, 2023
mergify bot added a commit to lpizzinidev/aws-cdk that referenced this issue Aug 22, 2023
@mergify mergify bot closed this as completed in #26528 Aug 22, 2023
mergify bot pushed a commit that referenced this issue Aug 22, 2023
Specifying the `securityGroups` property requires a `vpc`.

This fix adds validation for the case when a `vpc` is not specified, but `securityGroups` is.
It also adds validation for the case when both `securityGroups` and `allowAllOutbound` are specified (`allowAllOutbound` should be configured in the SGs).

**Question for the reviewers**
How should we handle the case of an empty list in `securityGroups`? (eg `securityGroups: []`)

Closes #26508.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@mbhaalalhhaexchange
Copy link

When we are using the Security group without VPC for @aws-cdk/aws-lambda package it was working well but when we have migrate it to the aws-cdk-lib/aws-lambda package we are getting following error message,
Cannot configure 'securityGroups' without configuring a VPC

Kindly suggest the possible solution for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda Related to AWS Lambda bug This issue is a bug. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants