A Serverless plugin to use a function version within an environment variable.
There are circumstances where you may need to reference a function's version within an environment variable. For example, when using CodeDeploy, the pre/post-traffic Lambdas need to know which version of a function to test.
Serverless produces a AWS::Lambda::Version
resource - which contains the Lambda version reference - using
a dynamically generated name. This makes it impossible to reference this resource at configuration time
within the Serverless config file.
This plugin solves the problem by finding the AWS::Lambda::Version
resource after the CloudFormation template
has been generated, then including a reference to the resource as the variable-value.
npm install --save-dev serverless-function-version
Add the plugin to serverless.yml:
plugins:
- serverless-function-version
Note: Node 10.x or higher runtime required.
Inside your Serverless config, include this plugin.
plugins:
- serverless-function-version
Then you can use the following syntax to get the function version:
${functionVersionArn:...}
is replaced with the full ARN of the function, including the version number${functionVersion:...}
is replaced with just the version number
# serverless.yml
functions:
getPhone: {...}, # This is the function we want to reference
getPhonePreDeployHook:
handler: 'src/getPhone/hooks.pre'
environment:
FUNCTION_TO_TEST: '${functionVersionArn:GetPhoneLambdaFunction}', # This is converted to the ARN including the VERSION at deployment-time
FUNCTION_VERSION_ONLY: '${functionVersion:GetPhoneLambdaFunction}'
NOTE: The ${functionVersion*}
variables can only be used within the provider.environment
section,
or within a function's environment
section.