1

I am trying to get Tags to work in a C# AWS CDK project but I can't seem to get anything to work except for the Depreciated syntax. For example in the following code:

using Amazon.CDK;
using Amazon.CDK.AWS.S3;

namespace HelloCdk
{
    public class HelloCdkStack : Stack
    {
        internal HelloCdkStack(Construct scope, string id, IStackProps props = null) : base(scope, id, props)  
        {
            // The code that defines your stack goes here
            var g_bucket = new Bucket(this, "GameContent", new BucketProps
            {
                Versioned = false, 
                PublicReadAccess = true,
                AutoDeleteObjects = true,              //delete and 
                RemovalPolicy = RemovalPolicy.DESTROY, //destroy bucket when CF Stack deleted.
                WebsiteIndexDocument = "index.html",
                WebsiteErrorDocument = "index.html"
            });

            Tag.Add(g_bucket, "key", "value");
        }
    }
}

The above code will succeed when I issue the cdk synth command. However, I get a warning message that says:

warning CS0618: 'Tag.Add(Construct, string, string, ITagProps?)' is obsolete: 'use "Tags.of(scope).add()"'

But, when I try to use "Tags.of...", the cdk synth command throws the following error:

error CS1061: 'TagManager' does not contain a definition for 'of' and no accessible extension method 'of'...

What do I need to change to get the recommended tagging approach to work?

  • This answer may be helpful: https://stackoverflow.com/a/64587781/14072498 – Roar S. Jan 18 '21 at 09:48
  • Thanks Roar, but the code in the question that you linked to is not structured the same as that which is created when you use the `cdk init app --language csharp` command. That command creates a structure like that posted in my original question. – user3242873 Jan 18 '21 at 16:09

2 Answers2

3

Your source code with Tags.of should not compile as there is no such a method for .NET defined.

Documentation specifies Tags.Of - https://docs.aws.amazon.com/cdk/latest/guide/tagging.html

Tags.Of(myConstruct).Add("key", "value");

(!) Notice that the stack object has the Tags property, so make sure you do not access the Tags property of the stack, but Tags class.

enter image description here

Milan Gatyás
  • 2,509
  • 1
  • 17
  • 23
  • 1
    Sorry, that doesn't work either. Still get the following error: `error CS1061: 'TagManager' does not contain a definition for 'Of' and no accessible extension method 'of'...` – user3242873 Jan 19 '21 at 04:27
  • Okay, I tried to replicate your issue and probably see the root cause. As the Stack has the Tags property, you might unintentionally access the property instead of the Tags class. I updated my answer. – Milan Gatyás Jan 19 '21 at 08:01
  • Thanks for taking the time to replicate the issue and for the updated response. Makes sense now - things are working as expected. – user3242873 Jan 19 '21 at 16:06
0

Make sure you're using the correct Tags class in Amazon.CDK namespace, and not the Stack.Tags property. Here's how you can get to it directly:

Amazon.CDK.Tags.Of(CONSTRUCT).Add(KEY, VALUE);
Armin Sadeghi
  • 766
  • 12
  • 19