Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 591
andy mccullough Avatar asked Dec 08 '17 16:12

andy mccullough


People also ask

What is SLS command?

The sls deploy command deploys your entire service via CloudFormation. Run this command when you have made infrastructure changes (i.e., you edited serverless. yml ).


Video Answer


2 Answers

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}).

like image 170
Noel Llevares Avatar answered Oct 10 '22 19:10

Noel Llevares


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
like image 37
naribo Avatar answered Oct 10 '22 19:10

naribo