Andrey Kalenyuk
Andrey Kalenyuk

Reputation: 93

Serverless error - The CloudFormation template is invalid - during deployment

When deploying lambdas with serverless, the following error occurs:

The CloudFormation template is invalid: Template format error: Output ServerlessDeploymentBucketName is malformed. The Name field of every Export member must be specified and consist only of alphanumeric characters, colons, or hyphens.

I don't understand what the problem is.

Serverless config file:

service: lambdas-${opt:region}

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  memorySize: 512
  timeout: 10
  lambdaHashingVersion: 20201221
  region: ${opt:region}
  stackName: lambdas-${opt:region}
  logRetentionInDays: 14
  deploymentBucket:
    name: lambdas-${opt:region}

plugins:
  - serverless-deployment-bucket

functions:
  function1:
    handler: function1/index.handler
    name: function1-${opt:stage}
    description: This function should call specific API on Backend server
    events:
      - schedule: cron(0 0 * * ? *)
    environment:
      ENV: ${opt:stage}
  function2:
    handler: function2/index.handler
    name: function2-${opt:stage}
    description: Function should be triggered by invocation from backend.
    environment:
      ENV: ${opt:stage}

Upvotes: 1

Views: 2985

Answers (2)

Luis Morales
Luis Morales

Reputation: 136

I ran into this same problem.

In the serverless.yml I changed service that I had it as lambda_function and put it as lambdaFunction

The error was solved and it deployed correctly.

Upvotes: 1

Gabriel Doty
Gabriel Doty

Reputation: 1786

Most likely your stage name contains an illegal character. Serverless auto-generates a name for your s3 bucket based on your stage name. If you look at the generated template file you will see the full export, which will look something like the following:

"ServerlessDeploymentBucketName": {
    "Value": "api-deployment",
    "Export": {
      "Name": "sls-api_stage-ServerlessDeploymentBucketName"
    }
  }

The way around this (assuming you don't want to change your stage name) is to explicitly set the output by adding something like this to your serverless config (in this case the illegal character was the underscore)

resources: {
      Outputs: {
          ServerlessDeploymentBucketName: {
              Export: {
                  Name: `sls-${stageKey.replace('api_', 'api-')}-ServerlessDeploymentBucketName`
              }
          }
      }
  }

Unfortunately this has to be done for every export... It is a better option to update your stage name to not include illegal characters

Upvotes: 0

Related Questions