Search code examples
amazon-web-servicesaws-lambdaserverlessaws-serverless

How can I deploy a specific AWS Lambda function to a specific Stage


I have two AWS Lambda functions. I have 3 stacks dev, test, and PROD.

I want a deploy a specific the Lambda function to only dev and test but not prod.

I want the trial Lambda function to be only in test and dev stages but not in PROD stage.

How can I achieve that? Here is my serverless.yml:

service:
  name: demo-app

# Add the serverless-webpack plugin
plugins:
  - serverless-webpack
  - serverless-offline

provider:
  name: aws
  runtime: nodejs12.x
  timeout: 30
  stage: dev
  region: us-west-2
  profile: serverless-admin

custom:
  region: ${self:provider.region}
  stage: ${opt:stage, self:provider.stage}
  prefix: ${self:service}-${self:custom.stage}
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

functions:
  toggle:
    handler: src/functions/unleash-toggle/handler.main
    timeout: 900
    events:
      - http:
          path: /toggle
          method: POST
  trial:
    handler: src/functions/city/handler.main
    timeout: 900
    events:
      - http:
          path: /trial
          method: POST

resources:
  Resources:
    taskTokenTable: 
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: ${self:service}-${self:custom.stage}-tokenTable
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

Solution

  • After doing some research I found out this can be done.

    This can be done using serverless plugin: serverless-plugin-ifelse and defining your conditions under custom block.

    you can see the same on Serverless Docs. The plugin is available on npm

    custom:
      serverlessIfElse:
        - If: '"${self:custom.stage}" == "prod"'
          Exclude:
            - functions.<functionName>
    

    Complete serverless.yml file

    service:
      name: demo-app
    
    # Add the serverless-webpack plugin
    plugins:
      - serverless-webpack
      - serverless-plugin-ifelse
      - serverless-offline
    
    provider:
      name: aws
      runtime: nodejs12.x
      timeout: 30
      stage: dev
      region: us-west-2
      profile: serverless-admin
    
    custom:
      region: ${self:provider.region}
      stage: ${opt:stage, self:provider.stage}
      prefix: ${self:service}-${self:custom.stage}
      webpack:
        webpackConfig: ./webpack.config.js
        includeModules: true
      serverlessIfElse:
        - If: '"${self:custom.stage}" == "prod"'
          Exclude:
            - functions.trail
    
    functions:
      toggle:
        handler: src/functions/unleash-toggle/handler.main
        timeout: 900
        events:
          - http:
              path: /toggle
              method: POST
      trial:
        handler: src/functions/city/handler.main
        timeout: 900
        events:
          - http:
              path: /trial
              method: POST
    

    The same thing can be achieved by another plugin serverless-plugin-conditional-functions