我正在使用无服务器框架创建lambda函数,并希望能够在serverless.yml的其他部分中交叉引用其Arn和名称。
令我惊讶的是,我很难找到!GetAtt和!Ref,因为lambda是通过香草CloudFormation创建的,我似乎并不希望这样做。 (AWS :: Lambda :: Function返回Ref和Fn :: GetAtt,这很容易做到!)
我发现了几篇文章,这些文章都提到了解决方案,但是没有任何内容用简单的英语陈述如何实现这一目标。
...
functions:
- ${file(functions/sendEmail.yml)}
...
sendEmail:
handler: lambda-functions/send-email.handler
...
在模板的另一部分中,我尝试过:
...
LambdaFunctionArn: !GetAtt sendEmail.Arn
但是,当我部署时,我得到:
Error: The CloudFormation template is invalid: Template error: instance of Fn::GetAtt references undefined resource sendEmail
我注意到最终的CloudFormation模板已将sendEmail转换为sendEmailLambdaFunction,因此我尝试了:
LambdaFunctionArn: !GetAtt sendEmailLambdaFunction.Arn
但收到类似的错误。
我也希望能够引用这个名字,但不幸的是
!Ref sendEmail
导致错误:
Error: The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [sendEmail] in the Resources block of the template
为获得lambda的Arn和名称,我需要进行任何有关精确更改的帮助,将不胜感激!
提前谢谢! 我
答案 0 :(得分:2)
我不确定最佳做法,但以下内容对我有用。
在无服务器函数中提供functionName,当您需要arn时,可以形成它。
test.yaml
TestFunction:
Type: AWS::Serverless::Function
Properties:
Handler: index.handler
FunctionName: myFunction
引用时,
LambdaArn: !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:function:myFunction
答案 1 :(得分:0)
好的,感谢Dinush使我步入正轨,我实现了如下功能解决方案:
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}
注意:表示法:
#{AWS::AccountId}
需要插件:serverless-pseudo-parameters。
sendEmail:
...
name: ${self:custom.myContact.lambdaFuncName}
...
LambdaFunctionArn: ${self:custom.myContact.lambdaFuncArn}
我仍然认为我不能简单地使用Ref和Fn :: GetAtt(这是很遗憾的,因为如果lambda是通过原始CloudFormation创建的,AWS :: Lambda :: Function,resource,我将能够做到),但这暂时有效。
希望这对其他人有帮助。再次感谢Dinush为我设置了这门课程!