Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export the Lambda ARN

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.

like image 398
Jerome Avatar asked Dec 21 '18 01:12

Jerome


People also ask

How do you get lambda ARN in CloudFormation?

You can use the intrinsic functions, specifically Fn::GetAtt to get the lambda function's arn.

How do I export a value from CloudFormation?

To export a stack's output value, use the Export field in the Output section of the stack's template. To import those values, use the Fn::ImportValue function in the template for the other stacks. For a walkthrough and sample templates, see Walkthrough: Refer to resource outputs in another AWS CloudFormation stack.

How do you clone a Lambda function?

When editing a lambda function, you can go Actions > Export Function > Download deployment package. This downloads a . zip file of the function. This duplicates the code and npm modules etc...


4 Answers

Try

resources:
  Outputs:
    ServiceLambdaFunctionQualifiedArn:
      Export:
        Name: MyServiceARN
      Value:
        Fn::GetAtt: ServiceLambdaFunction.Arn
like image 179
cementblocks Avatar answered Nov 08 '22 12:11

cementblocks


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.

like image 31
Mike M Avatar answered Nov 08 '22 13:11

Mike M


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
like image 42
zrkl Avatar answered Nov 08 '22 11:11

zrkl


In short:

!GetAtt MyCreatedResource.Arn

E.g.

Outputs: 
  FluentBitVerifyRole:
    Description: ARN of the verification role
    Value: !GetAtt FluentBitVerifyRole.Arn
like image 36
Mukla.C Avatar answered Nov 08 '22 12:11

Mukla.C