首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在SAM模板中使用!If内在函数

在SAM模板中使用!If内在函数
EN

Stack Overflow用户
提问于 2022-09-15 17:54:52
回答 1查看 43关注 0票数 0

我认为您可以使用SAM为状态机执行这样的操作:

代码语言:javascript
复制
SM:
    Type: AWS::Serverless::StateMachine
    Properties:
      Name: !Sub "${StageName}-State-Machine"
      DefinitionUri: statemachine/dddd.asl.json
      Events:
        Schedule:
          !If
            - IsPrimaryRegion
            -
              Type: Schedule
              Properties:
                Description: Schedule to run the twilio number state machine
                Enabled: !Ref ScheduleEnabled
                Schedule: "rate(1 hour)"
            - 
              Type: Schedule
              Properties:
                Description: Schedule to run the twilio number state machine
                Enabled: !Ref ScheduleEnabled
                Schedule: "rate(1 hour)"

但是SAM验证失败了。如果我使用State而不是在属性中启用它,它也会失败。最终,我希望有一种方法动态地设置启用,使用参数或任何其他方式。但与国家的验证失败了。

也尝试过这样做:

代码语言:javascript
复制
Enabled: !If [IsPrimaryRegion, true, false]
EN

回答 1

Stack Overflow用户

发布于 2022-09-16 21:32:17

您需要定义条件块,然后可以在模板中使用这些条件,如下所示:

代码语言:javascript
复制
Parameters:
  PrimaryRegion:
    Type: String
    Description: Primary region

Conditions:
  IsPrimaryRegion: !Equals [ !Ref PrimaryRegion, !Ref "AWS::Region" ]

Resources:
  SM:
    Type: AWS::Serverless::StateMachine
      Properties:
        Name: !Sub "${StageName}-State-Machine"
        DefinitionUri: statemachine/dddd.asl.json
        Events:
          Schedule:
            Type: Schedule
            Properties:
              Description: Schedule to run the twilio number state machine
              Enabled: !If [ IsPrimaryRegion, true, false ]
              Schedule: "rate(1 hour)"

最终:

代码语言:javascript
复制
Parameters:
  PrimaryRegion:
    Type: String
    Description: Primary region

Conditions:
  IsPrimaryRegion: !Equals [ !Ref PrimaryRegion, !Ref "AWS::Region" ]
  IsNotPrimaryRegion: !Not [ !Equals [ !Ref PrimaryRegion, !Ref "AWS::Region" ] ]

Resources:
  SMPrimary:
    Type: AWS::Serverless::StateMachine
      Condition: IsPrimaryRegion
      Properties:
        Name: !Sub "${StageName}-State-Machine"
        ...
              Enabled: true
  SM:
    Type: AWS::Serverless::StateMachine
      Condition: IsNotPrimaryRegion
      Properties:
        Name: !Sub "${StageName}-State-Machine"
        ...
              Enabled: false
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73735525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档