Skip to content

CDKHelpers

tag_all(stack, env, application)

Apply standardized organizational tags to a CDK stack and all its resources.

This function applies a consistent set of tags to a CDK stack, which will be inherited by all resources within that stack. The tags follow organizational standards for resource identification, cost allocation, and governance.

The following tags are applied: - Project: The project identifier - Organization: The organization name - Environment: The deployment environment (dev, prod, test, etc.) - Technology: The technology stack identifier - Owner: The project or team owner

Parameters:

Name Type Description Default
stack Construct

The CDK stack or construct to apply tags to. All child resources will inherit these tags automatically.

required
env str

The deployment environment name (e.g., 'dev', 'prod', 'test', 'staging'). This should match the environment context used throughout the application.

required
application Dict[str, Any]

Dictionary containing application metadata with the following required keys: - 'project': Project identifier - 'organization': Organization name
- 'technology': Technology stack identifier - 'owner': Project or team owner - 'repository': Repository identifier in CodeCommit of DevOps account - 'govis.seqnum': Govis Id of the project - 'govis.name': Govis Name of the project

required

Returns:

Name Type Description
None None

This function modifies the stack in-place and does not return a value.

Raises:

Type Description
KeyError

If any required keys are missing from the application dictionary.

AttributeError

If the stack parameter is not a valid CDK construct.

Note

Tags applied at the stack level are automatically inherited by all resources created within that stack. This provides a consistent tagging strategy without requiring manual tagging of individual resources.

Source code in mare_aws_common_lib/helpers/cdk_helpers.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def tag_all(stack : Stack, env, application) -> None:
    """
    Apply standardized organizational tags to a CDK stack and all its resources.

    This function applies a consistent set of tags to a CDK stack, which will be
    inherited by all resources within that stack. The tags follow organizational
    standards for resource identification, cost allocation, and governance.

    The following tags are applied:
    - Project: The project identifier
    - Organization: The organization name
    - Environment: The deployment environment (dev, prod, test, etc.)
    - Technology: The technology stack identifier
    - Owner: The project or team owner

    Args:
        stack (Construct): The CDK stack or construct to apply tags to. All child
            resources will inherit these tags automatically.
        env (str): The deployment environment name (e.g., 'dev', 'prod', 'test', 'staging').
            This should match the environment context used throughout the application.
        application (Dict[str, Any]): Dictionary containing application metadata with
            the following required keys:
            - 'project': Project identifier
            - 'organization': Organization name  
            - 'technology': Technology stack identifier
            - 'owner': Project or team owner
            - 'repository': Repository identifier in CodeCommit of DevOps account
            - 'govis.seqnum': Govis Id of the project
            - 'govis.name': Govis Name of the project

    Returns:
        None: This function modifies the stack in-place and does not return a value.

    Raises:
        KeyError: If any required keys are missing from the application dictionary.
        AttributeError: If the stack parameter is not a valid CDK construct.


    Note:
        Tags applied at the stack level are automatically inherited by all resources
        created within that stack. This provides a consistent tagging strategy
        without requiring manual tagging of individual resources.
    """
    Tags.of(stack).add("Project", application["project"])
    Tags.of(stack).add("Organization", application["organization"])
    Tags.of(stack).add("Environment", env)
    Tags.of(stack).add("Technology", application["technology"])
    Tags.of(stack).add("Owner", application["owner"])
    Tags.of(stack).add("Source", stack.stack_name)
    if not application["repository"]:
        Tags.of(stack).add("Repository", "Not specified in the cdk.json")
    else:
        Tags.of(stack).add("Repository", application["repository"])

    # tags requested by DIGIT
    # https://webgate.ec.europa.eu/fpfis/wikis/pages/viewpage.action?pageId=2312213698&spaceKey=CVTF&title=AWS%2BResource%2BTagging%2BPolicy%2Band%2BEnforcement%2BMechanism
    if not application["govis"]:
        Tags.of(stack).add("ec.GovIS2SeqNum", "N/A")
        Tags.of(stack).add("ec.GovIS2Name", "N/A")
    else:
        Tags.of(stack).add("ec.GovIS2SeqNum", application["govis"]["seqnum"])
        Tags.of(stack).add("ec.GovIS2Name", application["govis"]["name"])

    Tags.of(stack).add("ec.Application.Name", application["project"])
    Tags.of(stack).add("ec.CreatedBy", application["owner"])
    Tags.of(stack).add("ec.EnvironmentType", env)