Referencing Lambda version in other variables

I noticed Serverless will auto-increment the Lambda version, which is great for safely deploying updates. However, I’m wondering how to reference that version number in serverless.yml. For example, in a stepFunctions definition:

              - StartAt: Transcode
                    Type: Task
                    Resource: arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:${self:service}-${self:custom.stage}-transcode
                    End: true

I’d instead like the source to end with :${currentVersion} for example. Is this possible? Or can I somehow just use the fully qualified Lambda ARN?

A few thoughts:

In AWS, there’s a variable for Lambda version called $LATEST:

but I wonder if you really want to do latest version - usually you’ll want to work with stage and/or version aliases.

No, I definitely don’t want the $LATEST version. I’m looking to use the version of the function that was just auto-created by Serverless. I don’t think it’s possible, which is a bummer. It means I’ll have to have two separate YAML files/CloudFormation stacks, and one will use the outputs to get the Lambda ARNs (with version numbers).

This can be achieved with the [serverless-scriptable-plugin]( As an example, I put a placeholder for the reference in my serverless.yml file


The scriptable plugin uses a hook

           before:aws:package:finalize:saveServiceState: scripts/completeLambdaAssociation.js

The script completeLambdaAssociation.js updates the LambdaFunctionARN by searching for the AWS::Lambda::Version resource.

// serverless injected by serverless-scriptable-plugin
// noinspection JSUnresolvedVariable
const sls = serverless;

// noinspection JSUnresolvedVariable
const resources = sls.service.provider.compiledCloudFormationTemplate.Resources;
const resourceType = 'AWS::Lambda::Version';
const prefix = 'RewriteUriLambdaVersion';
const resourceNames = Object.keys(resources)
    .filter(name => resources[name].Type === resourceType && name.startsWith(prefix))
if (resourceNames.length !== 1) {
    throw Error(`Must have exactly 1 resource of type ${resourceType} and prefix ${prefix}, found ${resourceNames}`);
const distConfig = resources['CFDistribution']['Properties']['DistributionConfig'];
distConfig['DefaultCacheBehavior']['LambdaFunctionAssociations'][0]['LambdaFunctionARN'] = {Ref: resourceNames[0]};
console.log(`[${__filename}] Updated LambdaFunctionARN on first LambdaFunctionAssociation on DefaultCacheBehavior`);
1 Like