首页 > 解决方案 > 在 AWS cloudformation 模板中使用条件时如何管理资源依赖关系?

问题描述

我有一个 cloudformation 模板,它为 Stream 创建表以及 EventSourceMapping。我在表创建中使用条件,但它抱怨 EventSourceMapping 的依赖关系,因为我的 EventSourceMapping 依赖于表创建。我想要一些关于如何管理依赖的建议。这是我的示例代码:

参数:TableName: 描述:DynamoDb 表的名称类型:String

条件:TableCreationCondition: !Equals [ !Ref TableName, "" ]

资源:

 DynamoDBTable:
    #Condition: TableCreationCondition
    Type: "AWS::DynamoDB::Table"
    DeletionPolicy: Retain
    Properties:
      AttributeDefinitions:
      - AttributeName: !Ref HashKeyElementName
        AttributeType: !Ref HashKeyElementType
      KeySchema:
      - AttributeName: !Ref HashKeyElementName
        KeyType: HASH
      TableName: !Ref TableName
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES
      ProvisionedThroughput:
        ReadCapacityUnits: !Ref ReadCapacityUnits
        WriteCapacityUnits: !Ref WriteCapacityUnits
      SSESpecification:
          SSEEnabled: true

  DynamoDBTableStream:
    Type: AWS::Lambda::EventSourceMapping
    Properties:
      BatchSize: 1 #trigger one lambda per document
      Enabled: True
      EventSourceArn: 
        Fn::GetAtt:
          - DynamoDBTable
          - StreamArn 
      FunctionName: 
        Fn::GetAtt: 
          - MyLambdaFunction
          - Arn 
      StartingPosition: LATEST

标签: amazon-web-servicesstreamamazon-dynamodb

解决方案


您可以在 DynamoDB 表上设置此属性:

DependsOn: !Ref DynamoDBTableStream

推荐阅读