0

我正在 AWS Cloud Formation 模板中设置 AWS 资源的创建。我设置了我的 dynamoDb 表资源,这样对于每个新堆栈,它都会创建 DynamoDb 表的唯一实例,并且它不是固定名称。

我在云形成模板中使用了以下代码:

 DynamoDBTable:
    Type: AWS::DynamoDB::Table
    # Retain the table when deleting from CF Stack!
    DeletionPolicy: Retain
    Properties:
      #TableName: !Sub "abc-${ClientVar}-DB-${EnvVar}" 
      #TableName: !Ref DbTableName
      TableName: TrackOreDB # This is just for the autoscaling group. I need to find a way to validate it in the autoscaling resource by making the name generic for multiple stacks.
      AttributeDefinitions:
        - AttributeName: Product_ID
          AttributeType: S
        
      KeySchema:
        - AttributeName: Product_ID
          KeyType: HASH
        

我正在尝试使用此代码为此发电机数据库资源设置应用程序自动缩放目标。但我不确定如何将其指向上面创建的表格。

  UserTableWriteCapacityScalableTarget: 
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties: 
      MaxCapacity: 100
      MinCapacity: 5   
      ResourceId: !Sub table/'abc-%s-DB-%s' % (client, env)
      RoleARN: !Sub arn:aws:iam::${AWS::AccountId}:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable
      ScalableDimension: dynamodb:table:WriteCapacityUnits
      ServiceNamespace: dynamodb

在部署时,自动缩放资源在 dynamo DB 之前创建,因此云形成部署失败。

请帮忙。

4

1 回答 1

1

如果您引用 DynamoDB 表资源的名称 using!Ref!Sub它将输出表的名称。

Cloudformation 可以检测资源是否是另一个资源定义的一部分,何时使用!Ref!Sub引用其他资源的名称。它将正确地对资源的创建进行排序,而无需DependsOn明确定义。

假设这是一个 YAML Cloudformation 模板,模板中的两个资源将类似于:

Resources:
  DynamoDBTable:
    Type: AWS::DynamoDB::Table
    DeletionPolicy: Retain
    Properties:
      TableName: !Sub "abc-${ClientVar}-DB-${EnvVar}" 
      AttributeDefinitions:
        - AttributeName: Product_ID
          AttributeType: S
      KeySchema:
        - AttributeName: Product_ID
          KeyType: HASH

  UserTableWriteCapacityScalableTarget: 
    Type: AWS::ApplicationAutoScaling::ScalableTarget
    Properties: 
      MaxCapacity: 100
      MinCapacity: 5   
      ResourceId: !Sub "table/${DynamoDBTable}"
      RoleARN: !Sub arn:aws:iam::${AWS::AccountId}:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable
      ScalableDimension: dynamodb:table:WriteCapacityUnits
      ServiceNamespace: dynamodb
于 2020-09-25T05:17:29.320 回答