aefuen1
aefuen1

Reputation: 83

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

Upvotes: 2

Views: 2218

Answers (2)

kumar harshit
kumar harshit

Reputation: 672

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

Upvotes: 2

oieduardorabelo
oieduardorabelo

Reputation: 2985

if you check the serverless.yml reference, there's no support for "conditions" key in the lambda

Serverless Framework definitions ARE NOT a 1:1 to CloudFormation

you can override the AWS CloudFormation resource generated by Serverless, to apply your own options, link here

which more or less would look like this:

functions:
  startXtractUniversalInstance:
  ...

resources:
  extensions:
    StartXtractUniversalInstanceFunction:
      Condition: ...

make sure to double check the name generated to your function, the above StartXtractUniversalInstanceFunction could be wrong

Upvotes: 1

Related Questions