gpt4 book ai didi

aws-lambda - 在 CloudFormation 中,如何在 Events::Rule 中定位 Lambda 别名

转载 作者:行者123 更新时间:2023-12-03 07:13:33 25 4
gpt4 key购买 nike

我正在尝试按计划触发 Lambda:alias(别名是此处的关键)。以下代码错误

"SampleLambdaLiveAlias is not valid. Reason: Provided Arn is not incorrect format. (Service: AmazonCloudWatchEvents; Status Code: 400;Error Code: ValidationException;"

如何在 CloudFormation 中正确定位 lambda:alias?我尝试过 !Ref!Sub 以及逻辑名称。

我检索最新 lambda 版本的自定义资源方法似乎是设置“live”别名的必要之恶,因为 AWS 会维护旧的 lambda 版本,即使在您删除 lambda 和堆栈并且需要有效版本之后也是如此。一个新的别名。如果有人知道解决该问题的更优雅的方法,请参阅:how-to-use-sam-deploy-to-get-a-lambda-with-autopublishalias-and-additional-alises

SampleLambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: SampleLambda
AutoPublishAlias: staging
CodeUri: src/
Handler: SampleLambda.handler
MemorySize: 512
Runtime: nodejs12.x
Role: !GetAtt SampleLambdaRole.Arn
SampleLambdaLiveAlias:
Type: AWS::Lambda::Alias
Properties:
FunctionName: !Ref SampleLambdaFunction
FunctionVersion: !GetAtt SampleLambdaGetMaxVersionFunction.version
Name: live
SampleLambdaFunctionScheduledEvent:
Type: AWS::Events::Rule
Properties:
State: ENABLED
ScheduleExpression: rate(1 minute) # same as cron(0/1 * * * ? *)
Description: Run SampleLambdaFunction once every 5 minutes.
Targets:
- Id: EventSampleLambda
Arn: SampleLambdaLiveAlias

最佳答案

您的错误位于您共享的配置片段的最后一行。为了获取资源 ARN,您需要使用 Ref 内部函数,例如 !Ref SampleLambdaLiveAlias:

SampleLambdaFunctionScheduledEvent:
Type: AWS::Events::Rule
Properties:
State: ENABLED
ScheduleExpression: rate(1 minute) # same as cron(0/1 * * * ? *)
Description: Run SampleLambdaFunction once every 5 minutes.
Targets:
- Id: EventSampleLambda
Arn: !Ref SampleLambdaLiveAlias

请注意,Ref 内部函数可能会针对不同类型的资源返回不同的内容。对于 Lambda 别名,它返回 ARN,正是您所需要的。

您可以查看 official documentation了解更多详情。

关于aws-lambda - 在 CloudFormation 中,如何在 Events::Rule 中定位 Lambda 别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63821180/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com