Reputation: 83
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:
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:
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
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
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