0

I am setting up the creation of AWS resources in the AWS Cloud Formation template. I set up my dynamoDb table resource such that with every new stack, it creates a unique instance of the DynamoDb Table, and it's not a fixed name.

I used the following code in the cloud formation template:

 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
        

I am trying to set up an application auto-scaling target for this dynamo DB resource with this code. But i am not sure how to point it to the table created above.

  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

At deployment, the auto-scaling resource is getting created before dynamo DB and thus cloud formation deployment is failing.

Please help.

JD D
  • 7,398
  • 2
  • 34
  • 53
zsh_18
  • 1,012
  • 1
  • 11
  • 29
  • You control the order of creation with [DependsOn](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html). – jingx Sep 25 '20 at 04:07

1 Answers1

1

If you reference the name of the DynamoDB table resource using !Ref or !Sub it will output the name of the table.

Cloudformation can detect if a resource is part of another resource definition when !Ref and !Sub are used and reference the names of other resources. It will properly order the creation of the resources without requiring DependsOn to be explicitly defined.

Assuming this is a YAML Cloudformation template, the two resources in the template would look something like:

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
JD D
  • 7,398
  • 2
  • 34
  • 53