amazon-web-servicesaws-lambdaserverless-frameworkamazon-vpc

Serverless Lambda creation with vpc returns Variables/vpc: expected type: String, found: JSONObject


im trying to deploy a simple lambda inside a vpc with serverless framework using this .yml

useDotenv: true

provider:
  name: aws
  runtime: nodejs18.x
  stage: prod

functions:
  bot:
    handler: src/index.run
    environment:
      vpc:
        securityGroupIds:
          - ${env:SECURITY_GROUP_ID}
        subnetIds:
          - ${env:SUBNET_ID}
    events:
      # Invoke Lambda function every 35 minutes
      - schedule: rate(35 minutes)

and this is returned

Error:
CREATE_FAILED: BotLambdaFunction (AWS::Lambda::Function)
Properties validation failed for resource BotLambdaFunction with message:
[#/Environment/Variables/vpc: expected type: String, found: JSONObject]

Solution

  • Check that you have the vpc property nested on environment. So I guess that is why we see the error message expecting a string rather than a JSON object.

    Apparently you dont have any environment variable, so you could just remove that key and move the vpc to be nested directly on the function:

    useDotenv: true
    
    provider:
      name: aws
      runtime: nodejs18.x
      stage: prod
    
    functions:
      bot:
        handler: src/index.run
        vpc:
          securityGroupIds:
            - ${env:SECURITY_GROUP_ID}
          subnetIds:
            - ${env:SUBNET_ID}
        events:
          # Invoke Lambda function every 35 minutes
          - schedule: rate(35 minutes)
    

    Here are some examples of a lambda function being set with VPC: https://www.serverless.com/framework/docs-providers-aws-guide-functions

    The link below show info on the environment property:

    https://www.serverless.com/framework/docs-providers-aws-guide-variables