aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

Tags not applied to Cognito UserPools

Open johncrn opened this issue 7 years ago • 5 comments

:bug: Bug Report

What is the problem?

In short, Cognito User Pool resources generated by the CDK do not have their parent Stack's Tags applied to them.

It's perhaps a bit of a stretch to call this a bug with the CDK. The problem appears to be that the CloudFormation syntax for defining tags on a Cognito User Pool resource differs from the norm. This may be breaking whatever convention the CDK uses to apply tags to resources.

For User Pools, Tags must be supplied as a property called UserPoolTags and follow a JSON structure, see more here: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpool.html#cfn-cognito-userpool-userpooltags

Reproduction Steps

class TestStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    // just to confirm tags are propagating, create a resource where the stack level tags are applied.
    new dynamodb.Table(this, "TablesGetTagsNoProblems", {
      partitionKey: { name: "pk", type: AttributeType.STRING },
      sortKey: { name: "sk", type: AttributeType.STRING },
      billingMode: BillingMode.PAY_PER_REQUEST
    });

    const testUserPool = new cognito.UserPool(this, "TestUserPool");

    testUserPool;

    // Here's a workaround
    /*
    const cfnPool = testUserPool.node.defaultChild as cognito.CfnUserPool;

    cfnPool.userPoolTags = {
      TagAppliedDirect: "TestTagValue",
      AnotherTagAppliedDirect: "TestTagValue2"
    };
    */
  }
}

const testStack = new TestStack(app, "TestStack");

Tag.add(testStack, "TestTag", "TestTagValue"); // this is applied to the DDB Table but not the Cognito User Pool

Verbose Log

Here's the CloudFormation output when I use the workaround above, note the differences in how tags are applied to the resources

Resources:
  TablesGetTagsNoProblems170A1452:
    Type: AWS::DynamoDB::Table
    Properties:
      KeySchema:
        - AttributeName: pk
          KeyType: HASH
        - AttributeName: sk
          KeyType: RANGE
      AttributeDefinitions:
        - AttributeName: pk
          AttributeType: S
        - AttributeName: sk
          AttributeType: S
      BillingMode: PAY_PER_REQUEST
      Tags:
        - Key: TestTag
          Value: TestTagValue
    UpdateReplacePolicy: Retain
    DeletionPolicy: Retain
    Metadata:
      aws:cdk:path: TestStack/TablesGetTagsNoProblems/Resource
  TestUserPool83C2ABD0:
    Type: AWS::Cognito::UserPool
    Properties:
      LambdaConfig: {}
      UserPoolTags:
        TagAppliedDirect: TestTagValue
        AnotherTagAppliedDirect: TestTagValue2
    Metadata:
      aws:cdk:path: TestStack/TestUserPool/Resource
  CDKMetadata:
    Type: AWS::CDK::Metadata
    Properties:
      Modules: aws-cdk=1.6.1,@aws-cdk/assets=1.6.1,@aws-cdk/aws-apigateway=1.6.1,@aws-cdk/aws-applicationautoscaling=1.6.1,@aws-cdk/aws-autoscaling-common=1.6.1,@aws-cdk/aws-cloudwatch=1.6.1,@aws-cdk/aws-cognito=1.6.1,@aws-cdk/aws-dynamodb=1.6.1,@aws-cdk/aws-ec2=1.6.1,@aws-cdk/aws-events=1.6.1,@aws-cdk/aws-iam=1.6.1,@aws-cdk/aws-kms=1.6.1,@aws-cdk/aws-lambda=1.6.1,@aws-cdk/aws-lambda-event-sources=1.6.1,@aws-cdk/aws-logs=1.6.1,@aws-cdk/aws-s3=1.6.1,@aws-cdk/aws-s3-assets=1.6.1,@aws-cdk/aws-s3-notifications=1.6.1,@aws-cdk/aws-sns=1.6.1,@aws-cdk/aws-sns-subscriptions=1.6.1,@aws-cdk/aws-sqs=1.6.1,@aws-cdk/aws-ssm=1.6.1,@aws-cdk/core=1.6.1,@aws-cdk/cx-api=1.6.1,@aws-cdk/region-info=1.6.1,jsii-runtime=node.js/v10.16.0

Environment

  • CDK CLI Version: 1.6.1
  • Module Version: 1.6.1
  • OS: all
  • Language: TypeScript

Other information

I brought this up in the Gitter channel and it was suggested I raise an issue for it to be investigated: https://gitter.im/awslabs/aws-cdk?at=5d68b4392efeca1df51cb710

johncrn avatar Aug 30 '19 07:08 johncrn

from an initial glance, I don't believe we're classifying this resource as taggable since it doesn't have an attribute called Tags, but rather has one called UserPoolTags.

The aspect to apply tags to a construct and all it's children isn't affecting the resource since it's not being deemed as eligible to be tagged.

shivlaks avatar Aug 30 '19 07:08 shivlaks

from an initial glance, I don't believe we're classifying this resource as taggable since it doesn't have an attribute called Tags, but rather has one called UserPoolTags. @shivlaks I definitely missed this tag format. I can try to pick this up next week.

moofish32 avatar Sep 18 '19 17:09 moofish32

@eladb - I dug into this a little. In order to implement this my first design thought is that I will need to break the constructor of TagManager. The change is the additional input of tag property name. This is the first time the property is not named Tags. If we are going to change it the signature is a bit long already:

  constructor(tagType: TagType, resourceTypeName: string, tagStructure?: any) { ... }

I was thinking of moving to props for a single arg like the below?

export interface TagManagerProps {
  /**
   * The CloudFormation Resource Name
   */
  resourceTypeName: string;

  /**
   * The name of the property in CloudFormation for these tags
   *
   * Normally this is Tags, but Cognito UserPool uses UserPoolTags
   */
  tagPropertyName: string;

  /**
   * The type or format of the tags
   */
  tagType: TagType;

  /**
   * The initial tags from the CloudFormation properites
   */
  tagStructure?: any;
}

If we don't want to change that constructor there are two other options:

  1. add a member and a setter function for it and immediately set it
  2. code up the resource type to tag property name map

For the second one, I didn't like it because I was also trying to move any knowledge of the property name back into the schema definitions and out of codegen.ts (If you look there are a few places the naked string Tags can be found; I was trying to kill that leak).

moofish32 avatar Sep 23 '19 05:09 moofish32

Is this fixed yet? This is still not working for me.

phuctm97 avatar Apr 03 '21 15:04 phuctm97

2024 and still not fixed!

ajishab avatar Aug 14 '24 21:08 ajishab