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

青春壹個敷衍的年華 提交于 2021-01-05 07:22:08

问题


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


回答1:


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



回答2:


Ok, with thanks to Dinush for putting me on track, I achieved a functional solution as follows:

serverless.yml

custom:
  ...
  myEnvironment:
    assetPreName: ${self:service.name}-${self:provider.stage}
    arnRegionAndAccount: ${self:provider.region}:#{AWS::AccountId}
  ...
  myContact:
    ...
    lambdaFuncName: ${self:custom.myEnvironment.assetPreName}-sendEmail
    lambdaFuncArn: arn:aws:lambda:${self:custom.myEnvironment.arnRegionAndAccount}:function:${self:custom.myContact.lambdaFuncName}

Note: The notation:

#{AWS::AccountId}

requires the plugin: serverless-pseudo-parameters.

sendEmail.yml

sendEmail:
  ...
  name: ${self:custom.myContact.lambdaFuncName}
  ...

Elsewhere

LambdaFunctionArn: ${self:custom.myContact.lambdaFuncArn}

I still think it's a shame that I couldn't simply use Ref and Fn::GetAtt (as I would be able to if the lambda was created via a vanilla CloudFormation, AWS::Lambda::Function, resource), but this works for now.

Hope this helps someone else. Thanks again, Dinush for setting me on this course!



来源:https://stackoverflow.com/questions/60439849/how-to-reference-arn-and-name-of-aws-lambda-function-created-with-serverless-fra

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!