Search code examples
amazon-web-servicesaws-lambdaserverless-framework

How to deploy a AWS Lambda Function conditionally, using Serverless Framework, only to prd stage


thank you for taking the time to read this post. I’m trying to deploy an AWS Lambda Function conditionally, based on stage (only for “prd” stage).

This lambda has a Role, which deploys conditionally too. I already achieved this by using cloudformation conditions on the resources block, as shown below:

enter image description here

enter image description here

However, I don’t know how to make it work for the lambda function, as it is in the functions block I don’t have idea how to reference the condition. From the serverless.yml reference I decided to do what is shown below, and it doesn’t work:

enter image description here

Can someone help me to understand what am I doing wrong? And also what would be the solution to make this work? Thanks in advance


Solution

  • This can be achieved using the serverless if-else plugin https://www.serverless.com/plugins/serverless-plugin-ifelse

    You can use the plugin by adding them to your plugin section of the serverless.yml

    plugins:
     - serverless-plugin-ifelse 
    

    and set up conditions to update values in the serverless.yml for the functions and exclude them. The include option isn't available, so your condition would be something like -

    custom:
      currentStage: ${opt:stage, self:provider.stage}
      serverlessIfElse:
          - If: '"${self:provider.stage}" == "prd"'
            Set:
              functions.startXtractUniversalInstance.role: <custom role for prod>
            ElseExclude:
              - functions.startXtractUniversalInstance