AWS SAM模板获取具有内在函数的规则名称



假设我有一个在SAM模板中声明的调度函数。yaml

myScheduledFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./bin
Handler: myScheduledFunction
Policies:
- AWSLambdaBasicExecutionRole
Events:
CloudwatchEvents:
Type: Schedule
Properties:
Schedule: rate(1 minute)
Enabled: true

然后我有另一个功能,可以启用/禁用计划规则

myFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./bin
Handler: myFunction
Environment:
Variables:
RULE_NAME: !Ref MyRuleName
Policies:
- AWSLambdaBasicExecutionRole
- EventBridgePutEventsPolicy:
EventBusName: default
Events:
SomeEvent: ...

现在,如何引用环境变量RULE_NAME: !Ref MyRuleName中的规则名称?是否可以在SAM中进行?也许使用类似!GetAtt myScheduledFunction.RuleName的东西?我找不到任何关于这方面的信息,我知道在Cloudformation中有一种方法可以做到这一点,但我知道在SAM中是否也可以,谢谢。

我认为这不可能用编写的模板进行检索。解决方法是将CloudWatch规则创建为顶级资源,而不是在Events属性中创建它。

例如:

myScheduledFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./bin
Handler: myScheduledFunction
Policies:
- AWSLambdaBasicExecutionRole
myRule:
Type: AWS::Events::Rule
Properties: 
Description: "ScheduledRule"
ScheduleExpression: "rate(1 minutes)"
State: "ENABLED"
Targets: 
- Arn: 
Fn::GetAtt: 
- "myScheduledFunction"
- "Arn"
PermissionForEventsToInvokeLambda: 
Type: AWS::Lambda::Permission
Properties: 
FunctionName: 
Ref: "myScheduledFunction"
Action: "lambda:InvokeFunction"
Principal: "events.amazonaws.com"
SourceArn: 
Fn::GetAtt: 
- "myRule"
- "Arn"
myFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./bin
Handler: myFunction
Environment:
Variables:
RULE_NAME: !Ref myRule
Policies:
- AWSLambdaBasicExecutionRole
- EventBridgePutEventsPolicy:
EventBusName: default
Events:
SomeEvent: ...

此规则/权限的代码片段取自CloudwatchRule云信息文档。

最新更新