'Conditional tagging in cloudformation template

I have a cloudformation template and I would like to know if it is possible to apply conditional tagging according to the environment (dev/test/prod).

AWSTemplateFormatVersion: "2010-09-09"
Description: Some description
Conditions:
  CreateProdResources: !Equals [ !Ref Environment, "prod" ]
  CreateDevResources: !Equals [ !Ref Environment, "dev" ]
  CreateTestResources: !Equals [ !Ref Environment, "test"]
Parameters:
  Environment:
    Type: String
    Description: Environment Used
    ConstraintDescription: Must be either dev, test, or prod
    AllowedValues:
      - dev
      - test
      - prod
  ApplicationDeploymentDev:
    Type: String
    Description: 'Enter ApplicationDeployment Tag'
    Default: Some default dev value 
  ApplicationDeploymentTest:
    Type: String
    Description: 'Enter ApplicationDeployment Tag'
    Default: Some default test value 
  ApplicationDeploymentProd:
    Type: String
    Description: 'Enter ApplicationDeployment Tag'
    Default: Some default prod value 
  Tags:

In the Tags section I'd like to have each key-value tag pair in sync with the environment I am choosing while deploying. So for example if environment = dev then

Tags:
  Key: ApplicationDeploymentDev
  Value: Some default dev value 

Has anyone achieved doing something similar?



Solution 1:[1]

You can nest If:

        Tags:
          !If
            - CreateDevResources
            - [{Key: ApplicationDeploymentDev, Value: Some default dev value }]
            - !If
              - CreateProdResources
              - [{Key: ApplicationDeploymentProd, Value: Some default Prod value }]
              - [{Key: ApplicationDeploymentTest, Value: Some default Test value }]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin