Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference Arn and name of AWS lambda function created with Serverless Framework

I am using the Serverless Framework to create a lambda function and would like to be able to cross-reference its Arn and name in other parts of serverless.yml.

I'm surprised how difficult I'm finding this as !GetAtt and !Ref do not seem to work as I would expect if the lambda was created via vanilla CloudFormation. (AWS::Lambda::Function returns Ref and Fn::GetAtt which would make this easy!)

I have found a few posts, that allude to solutions, but nothing that states in plain English how to achieve this.

SETUP

serverless.yml

...
functions:
  - ${file(functions/sendEmail.yml)}
...

sendEmail.yml

sendEmail:
  handler: lambda-functions/send-email.handler
...

ATTEMPTS TO REFERENCE

Arn

In another part of the template I have attempted:

...    
LambdaFunctionArn: !GetAtt sendEmail.Arn

but, when I deploy, I get:

Error: The CloudFormation template is invalid: Template error: instance of Fn::GetAtt references undefined resource sendEmail

I notice that the final CloudFormation template has converted sendEmail to sendEmailLambdaFunction, so I then tried:

LambdaFunctionArn: !GetAtt sendEmailLambdaFunction.Arn

but received a similar error.

Name

I also would like to be able to reference the name, but sadly

!Ref sendEmail

causes the error:

Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [sendEmail] in the Resources block of the template

Any assistance with regards to precise changes I need to make, in order to achieve grabbing the lambda's Arn and name, would be greatly appreciated!

Thanks in advance! I

like image 583
HapiDaze Avatar asked Jun 12 '26 05:06

HapiDaze


1 Answers

I am not sure of the best practice, but the below works for me.

Provide functionName in the serverless function and when you want the arn, you can form it.

test.yaml

TestFunction:
  Type: AWS::Serverless::Function
  Properties:
    Handler: index.handler
    FunctionName: myFunction

When referencing,

LambdaArn: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:myFunction
like image 141
Dinush Avatar answered Jun 15 '26 15:06

Dinush



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!