andy mccullough
andy mccullough

Reputation: 9571

Exclude Lambda function from deploy to a particular stage

I am trying to exclude a Lambda function from being deployed via serverless to my prod stage within AWS.

A snippet from my serverless yaml looks something like -

functions:
  some-prod-function:
    handler: prodFunction.handler
    events:
      - http:
          path: /prod-function
          method: post
  some-dev-function:
    handler: devFunction.handler
    events:
      - http:
          path: /dev-function
          method: post

Is there a way to exclude some-dev-function from being deployed to prod?

Upvotes: 9

Views: 2911

Answers (4)

CTN
CTN

Reputation: 334

Create a file for example

env-functions.yml

and add content as below

prod:
    some-prod-function:
      handler: prodFunction.handler
      events:
        - http:
            path: /prod-function
            method: post
dev:
    some-dev-function:
      handler: devFunction.handler
      events:
        - http:
            path: /dev-function
            method: post

After this in serverless.yml set

functions: ${file(env-functions.yml):${opt:stage, self:provider.stage}}

Upvotes: 0

kartick shaw
kartick shaw

Reputation: 1013

If you are using Serverless framework you could use serverless plugin

serverless-plugin-ifelse

Then

plugins:
  - serverless-plugin-ifelse 

If you want to exclude say func1

functions:
  func1:
    name: Function 1
    handler: lambda.func1
    events:
      - http:
          path: "path1"
          method: "post"
          authorizer:
            arn: arn:aws:cognito-idp:us-east-1:123456789012:userpool/us-east-1_0acCDefgh

  func2:
    name: Function 2
    handler: lambda.func2
    events:
      - http:
           path: "path2"
           method: "post"



func3:
    name: Function 3
    handler: lambda.func2
    events:
      - http:
           path: "path3"
           method: "post"

for us-east-1 . Then use below code snippet

- If: '"${self:provider.region}" == "us-east-1"'
        Exclude:
          - functions.func1

Upvotes: 1

naribo
naribo

Reputation: 710

I'm using SLS 1.32.0

I wasn't able to get functions: ${self:environment-functions.${opt:stage}} to work. (Not sure why)

It returns the following:

A valid service attribute to satisfy the declaration 'self:environment-functions.dev' could not be found.

However, using the same logic in dashmug's answer, file worked for me:

serverless.yml:

functions: ${file(serverless-${opt:stage}.yml)}

serverless-dev.yml:

some-dev-function:
  handler: devFunction.handler
  events:
    - http:
        path: /dev-function
        method: post

serverless-prod.yml:

some-prod-function:
  handler: prodFunction.handler
  events:
    - http:
        path: /prod-function
        method: post

Upvotes: 1

Noel Llevares
Noel Llevares

Reputation: 16037

You can put those definitions on a different property and use variables in order to choose which definitions to use.

environment-functions:
  prod:
    some-prod-function:
      handler: prodFunction.handler
      events:
        - http:
            path: /prod-function
            method: post
  dev:
    some-dev-function:
      handler: devFunction.handler
      events:
        - http:
            path: /dev-function
            method: post


functions: ${self:environment-functions.${opt:stage}}      

You may need to change this depending on how you specify your stage on deployment (${opt:stage} or ${env:stage}).

Upvotes: 8

Related Questions