How to use myApplication inside cdk? (Tag value doesn't resolve and applies literally) #30868
-
I'd like to tie myApplication into my cdk application to help monitor it vs everything else inside my account. (See which stack is creating costs, get security alerts, etc.). The problem is I can't figure out how to add the 'awsApplication' tag to the stack I want to manage without hitting an error. You can get it working by not tagging, and then going into the console and have the myApplication gui apply the tag, but the problem is since cdk isn't applying the tag, I'm guessing it won't get applied to new resources added to the cdk stack later, right? I tried a few routes, and this is the one I got the farthest in. It gets blocked because it tries to apply the token literally as a value, instead of resolving the token. (It tries to apply from aws_cdk import (
Stack,
Tags,
aws_servicecatalogappregistry as appregistry,
)
from constructs import Construct
class MyApplicationStack(Stack):
def __init__(
self,
scope: Construct,
construct_id: str,
application_id: str,
container_name_id: str,
tag_stacks: list[Stack],
**kwargs
) -> None:
super().__init__(scope, construct_id, **kwargs)
# https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_servicecatalogappregistry.CfnApplication.html
self.my_application = appregistry.CfnApplication(
self,
"CfnApplication",
name=application_id,
description=f"Core logic for managing {container_name_id} automatically",
)
## For each stack they pass in, add it to this application
for stack in tag_stacks:
# MUST be in the same region as this stack!!
assert stack.region == self.region, f"Stack '{stack.stack_name}' ({stack.region}) must be in the same region as this stack '{self.stack_name}' ({self.region})"
## Adds the 'awsApplication' tag:
Tags.of(stack).add(self.my_application.attr_application_tag_key, self.my_application.attr_application_tag_value)
## TODO: I saw a couple of other tags in the AWS myApplication GUI, add those here too.
## Add the Stack to myApplication:
# https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_servicecatalogappregistry.CfnResourceAssociation.html
stack_resource_association = appregistry.CfnResourceAssociation(
self,
"CfnResourceAssociation",
application=self.my_application.name,
# resource=stack.stack_id,
resource=stack.stack_name,
resource_type="CFN_STACK",
)
# I think this is only required because these are Cfn objects?:
stack_resource_association.add_dependency(self.my_application) Error: Having this in a stack that you apply last, to the previous stack that you want to manage, gets around a circular import dependency with tags.
Since I have to use the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
The issue you're facing is with how you're trying to apply the awsApplication tag to the stacks you want to manage. Here's a breakdown of the problem and the solution: Problem: You're using .attr_application_tag_key and .attr_application_tag_value from the CfnApplication construct directly. Use the Tags helper functions to add tags: This approach uses the Tags.of(stack) to access the tag manager for the specific stack you want to tag. self.my_application.application_arn is a token that resolves to the actual application ARN when the CDK generates the CloudFormation template. You don't need CfnResourceAssociation in this case. The Tags approach automatically tags the stack resources. |
Beta Was this translation helpful? Give feedback.
-
Hello, All i can think of is Double-Check Installation: Ensure you've installed the required v3 client libraries correctly: npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presigner Verify the installation paths in node_modules. Review Codemod Output: Carefully examine the migrated files generated by aws-sdk-js-codemod. Look for incomplete changes or potential errors. myApplication and CDK Stack Dependencies: Circular Dependency: The circular dependency arises because you're trying to add a dependency between stacks where they both need information from each other during deployment. Solution: Break the Circular Dependency: Separate Tagging Logic: Create a separate construct responsible solely for tagging stacks with the awsApplication tag. This construct can depend on both MyApplicationStack and the stacks you want to tag.
This approach ensures MyApplicationStack and the tagged stacks are deployed independently, breaking the circular dependency. CloudFormation Outputs: If the information needed for tagging exists as outputs from the stack that needs tagging, you can reference those outputs directly in the MyApplicationStack to construct the awsApplication tag value. Consult the official CDK documentation for aws_servicecatalogappregistry: https://docs.aws.amazon.com/cloudformation/ |
Beta Was this translation helpful? Give feedback.
-
Stack-level tag values cannot be tokens, they have to be known at synth-time. Refer to the explanation here: #16342 (comment) |
Beta Was this translation helpful? Give feedback.
Stack-level tag values cannot be tokens, they have to be known at synth-time. Refer to the explanation here: #16342 (comment)