gpt4 book ai didi

amazon-web-services - AWS 云形成 : EventBridge rule still enabled when 'Enabled: false' is set

转载 作者:行者123 更新时间:2023-12-03 07:39:22 30 4
gpt4 key购买 nike

我有一个 AWS SAM 应用程序,其函数包含 schedule 事件,并且我希望有条件地启用/禁用根据参数值为事件生成的 EventBridge 规则。

为此,我在 template.yaml 中执行了以下操作 -

  1. 添加了 FunctionEnabled 参数,该参数仅允许 [ 'true', 'false' ] 作为其值。
  2. 添加了 EnableFunction 条件,用于检查是否 !Equals [ !Ref FunctionEnabled, 'true' ]
  3. 使用 !If [ EnableFunction, true, false ] 设置 schedule 事件的 Enabled 属性值。

但是,当我构建模板并部署它时,无论 FunctionEnabled 参数的值如何,EventBridge 规则始终处于启用状态。

foo@bar:~/sam-project$ sam build -u
foo@bar:~/sam-project$ sam deploy --parameter-overrides FunctionEnabled=true
foo@bar:~/sam-project$ sam deploy --parameter-overrides FunctionEnabled=false

请注意,FunctionEnabled 输出根据参数的值正确显示 truefalse,这表明正在使用正确的 bool 值传递给 Enabled 属性。

那么如何根据参数的值启用/禁用 EventBridge 规则?


更新 1

我想知道 !If 函数是否可能返回字符串而不是 bool 值,因此我尝试使用以下条件函数作为 Enabled 的值schedule 事件的属性。

  • !条件启用函数
  • !等于 [ !Ref FunctionEnabled, 'true' ]

但是,两种情况下的结果仍然相同,因此 EventBridge 规则仍然始终启用。

更新2

当我将 schedule 事件的 Enabled 属性值硬编码为 false 时,EventBridge 规则将按预期禁用。

这是否意味着所有条件函数都返回字符串而不是 bool 值?


SAM 应用程序

代码/app.py

def lambda_handler(event, context) -> dict:
return {
'statusCode': 200,
'headers': {
'AWS-Request-ID': context.aws_request_id,
'Content-Type': 'application/json'
},
'body': '{"message": "Hello World!!"}'
}

samconfig.toml

version = 0.1

[default.deploy.parameters]
capabilities = "CAPABILITY_IAM"
confirm_changeset = true

模板.yaml

AWSTemplateFormatVersion: 2010-09-09
Transform: AWS::Serverless-2016-10-31

Parameters:
FunctionEnabled:
Description: Whether the EventBridge rule is enabled.
Type: String
AllowedValues: [ 'true', 'false' ]
Schedule:
Description: The schedule on which the EventBridge rule triggers the Function.
Type: String
Default: cron(0 3 1 * ? *)

Conditions:
EnableFunction: !Equals [ !Ref FunctionEnabled, 'true' ]

Globals:
Function:
Architectures:
- x86_64
Handler: app.lambda_handler
Runtime: python3.8
Timeout: 1

Resources:
FunctionLogGroup:
Type: AWS::Logs::LogGroup
Properties:
LogGroupName: !Sub "/aws/lambda/${Function}"
RetentionInDays: 7

FunctionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: CloudWatch
PolicyDocument:
Version: 2012-10-17
Statement:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: '*'

Function:
Type: AWS::Serverless::Function
Properties:
CodeUri: code/.
Events:
ScheduleEvent:
Type: Schedule
Properties:
Enabled: !If [ EnableFunction, true, false ]
Schedule: !Ref Schedule
RetryPolicy:
MaximumEventAgeInSeconds: 100
MaximumRetryAttempts: 3
Role: !GetAtt FunctionRole.Arn

Outputs:
FunctionEnabled:
Description: The value of the 'Resources.Function.Properties.Events.ScheduleEvent.Properties.Enabled'.
Value: !If [ EnableFunction, true, false ]

最佳答案

这完全没有记录,但不是使用 Enabled: true|false ,可以简单地使用 CloudFormation AWS::Events::Rule 属性 State: ENABLED|DISABLED .

SAM 似乎忽略了内部函数,因此在转换为 CF 时,它看到的值为 Enabled假设为 true ,从而启用该规则。请注意,此行为本身似乎与 documentation 中描述的不同。 .

If this property is set to true then AWS SAM passes ENABLED, otherwise it passes DISABLED.

由于我在这里没有得到任何牵引力,我还开了一个GitHub问题。

关于amazon-web-services - AWS 云形成 : EventBridge rule still enabled when 'Enabled: false' is set,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73554947/

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