Export the Lambda ARN

Viewed 10957

I would like to export my Lambda ARN that I created using the serverless framework, as I need to use this ARN in other CF template

I defined Outputs and Export in my serverless.yml file.

resources:
  Outputs:
    ServiceLambdaFunctionQualifiedArn:
      Export:
        Name: MyServiceARN

It's working fine.

However, the export contains the lambda version ARN (i.e arn:aws:lambda:region:12345:function:servicename:2).

This is causing issue as this ARN is used by other CF thus cannot be updated.

"Export MyServiceArn cannot be updated as it is in use by xyz"

Is there a way to get the ARN without the version number ?

Thanks for your help.

5 Answers

Try

resources:
  Outputs:
    ServiceLambdaFunctionQualifiedArn:
      Export:
        Name: MyServiceARN
      Value:
        Fn::GetAtt: ServiceLambdaFunction.Arn

UPDATE

In response to the first comment, I'd like to give some context to this answer: the author asked how to export the ARN without the version number?

The actual problem lies in the Key that was used to export the function ARN: ServiceLambdaFunctionQualifiedArn

resources:
  Outputs:
    ServiceLambdaFunctionQualifiedArn: # <- CloudFormation Key
      Export:
        Name: MyServiceARN

The Serverless Framework, by default, exports each Lambda function with their Qualified Function ARN (i.e., ARN plus version suffix) as CloudFormation Stack Output. That means, the Lambda function hello will be exported as HelloLambdaFunctionQualifiedArn, even if you don't define any outputs in the resources section. Just check the CloudFormation Stack output of a deployed Serverless service:

enter image description here

That means, Serverless will overwrite your output declaration ServiceLambdaFunctionQualifiedArn with their own declaration during deployment. By the way, that is also the reason that it was working even though no actual Value was specified.

In order to export the ARN without version number, you must manually declare an additional output with a different Key, for example HelloLambdaFunctionArn or ServiceLambdaFunctionArn (remove Qualified from the string), and reference the ARN as Value:

resources:
  Outputs:
    ServiceLambdaFunctionArn: # <- CloudFormation Key
      Value: !GetAtt ServiceLambdaFunction.Arn # <- CloudFormation Value
      Export: 
        Name: ${self:service}-${self:provider.stage}-MyServiceARN

This has worked for me:

Export in first serverless stack with a unique export name <stack>-<stage>-TransformDataLambdaArn:

service: first-service

provider:
  name: aws
  runtime: nodejs12.x
  region: eu-west-1
  stage: ${opt:stage, 'dev'}

functions:
  myLambda:
    handler: src/handlers/myLambda.handle

resources:
  Outputs:
    MyLambdaFunctionArn:
      Description: 'ARN will be imported by other stacks'
      Value: !GetAtt MyLambdaFunction.Arn
      Export:
        Name: ${self:service}-${self:provider.stage}-MyLambdaArn

And import in the second stack with the same stage like this:

Fn::ImportValue: first-service-${self:custom.stage}-MyLambdaArn

Update 07-2020

This question is still valid, and the accepted answer did not work for me with serverless 1.74.1. My use case was to export a custom authorizer lambda (as a separate service) to other services.

Here is what worked for me:

resources:
  Outputs:
    ServiceLambdaFunctionQualifiedArn:
      Export:
        Name: MyServiceARN

You don't need a reference to value, the arn should be exported with just the name.

Then to use the Export in another service:

authorizer:
  name: MyServiceARN
  arn:
    Fn::ImportValue: MyServiceARN

The "name" added to this ended up being required for the custom authorizer use case.

In short:

!GetAtt MyCreatedResource.Arn

E.g.

Outputs: 
  FluentBitVerifyRole:
    Description: ARN of the verification role
    Value: !GetAtt FluentBitVerifyRole.Arn

nah , just add versionFunctions: false inside your serverless.yml inside the provider

like this

  name: aws
  region: eu-west-1
  runtime: nodejs10.x
  versionFunctions: false
  stage: ${opt:stage, 'dev'}
Related