Reference a newly created lambda arn in serverless.yml

Written by Yaniv Kessler

How to reference my lambda arn in serverless.yml? Using the ${cf:...} object!


After some google searching and digging in serverless.com docs, this is what I came up with:

service: my-service-api

frameworkVersion: '2'

provider:
  memorySize: 128
  stage: ${opt:stage, 'dev'}
  name: aws
  runtime: nodejs12.x
  logRetentionInDays: 14
  region: us-east-1
  environment:
    STAGE: ${self:provider.stage}
    # this is where we use it:
    MY_LAMBDA_NAME: ${cf:my-service-api-${self:provider.stage}.MyDashfnLambdaFunctionQualifiedArn}

functions:
  my-fn:
    handler: index.myFn
  • ${cf: references the cloudformation object
  • then we add the name of the stack, which is made of the service name: my-service-api with the stage name connected by a dash -${self:provider.stage}, eg: my-service-api-dev
  • then a dot and the function key .MyDashfnLambdaFunctionQualifiedArn the MyDashfn part is the name of the lambda, as stated in functions section: my-fn. Mind that after the dash the letter case stays the same. The suffix LambdaFunctionQualifiedArn is fixed.

If you know of some better way to make this work, do tell! 😊