如何引用Arn和使用无服务器框架创建的AWS lambda函数的名称

问题描述:

我正在使用无服务器框架创建lambda函数,并希望能够在serverless.yml的其他部分中交叉引用其Arn和名称.

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.

令我惊讶的是,我很难找到!GetAtt和!Ref,因为lambda是通过香草CloudFormation创建的,所以我似乎并不希望这样做.(AWS :: Lambda :: Function返回Ref和Fn :: GetAtt,这很容易做到!)

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.

...
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

我注意到最终的CloudFormation模板已将sendEmail转换为sendEmailLambdaFunction,因此我尝试了:

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

LambdaFunctionArn: !GetAtt sendEmailLambdaFunction.Arn

但收到类似的错误.

我也希望能够引用该名称,但很遗憾

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

!Ref sendEmail

导致错误:

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

为获得lambda的Arn和名称,我需要做的任何与精确更改有关的帮助,将不胜感激!

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!

提前谢谢!我

我不确定最佳做法,但以下内容对我有用.

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

在无服务器功能中提供functionName,当您需要arn时,就可以形成它.

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

引用时,

LambdaArn: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:myFunction