创建应用程序自动缩放目标时如何在资源 ID 中提及 AWS DynamoDB 实例

How to mention a instance of AWS DynamoDB in the resource ID while creating application auto scaling target

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

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

 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
        

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

  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 之前创建,因此云形成部署失败。

请帮忙。

如果您使用 !Ref!Sub 引用 DynamoDB table 资源的名称,它将输出 table.

的名称

当使用 !Ref!Sub 并引用其他资源的名称时,Cloudformation 可以检测资源是否属于另一个资源定义的一部分。它将正确地对资源的创建进行排序,而不需要 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