Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I define tags as a parameter in parameters section AWS CloudFormation template

Can I define tags as a parameter in parameters section AWS CloudFormation template and use it in each created resource?

My current template:

AWSTemplateFormatVersion: 2010-09-09
Description: "Some information"
Parameters:
  Test1:
    Type: String
    Description: Test1
    Default: t1
  Test2:
    Type: String
    Description: Test2
    Default: t2

...

LBSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
  GroupDescription: "Enable access to app loadbalancer"
  Tags:
    - Key: Name
      Value: !Join
        - ""
        - - !Ref Test1
          - !Ref Test2
    - Key: Test1
      Value: !Ref Test1
    - Key: Test2
      Value: !Ref Test2

I use the same tags for any other resourses(AWS::ElasticLoadBalancing::LoadBalancer, AWS::AutoScaling::AutoScalingGroup etc.), and it seems like duplicated code (each time when I create a new resource). Can I craft something like this?:

AWSTemplateFormatVersion: 2010-09-09
Description: "Some information"
Parameters:
  Test1:
    Type: String
    Description: Test1
    Default: t1
  Test2:
    Type: String
    Description: Test2
    Default: t2
  Tag:
    #define all tags, which I created above 

...

LBSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
  GroupDescription: "Enable access to app loadbalancer"
  Tags: !Ref Tag

...

Unfortunately, I didn't find any information about it on https://docs.aws.amazon.com/

like image 534
Ihar Vauchok Avatar asked Mar 07 '18 10:03

Ihar Vauchok


People also ask

How do I specify tags in CloudFormation template?

The key name of the tag. You can specify a value that's 1 to 128 Unicode characters in length and can't be prefixed with aws: . You can use any of the following characters: the set of Unicode letters, digits, whitespace, _ , . , / , = , + , and - . The value for the tag.

Which of the following types is not allowed as a parameters type When defining a parameter in a CloudFormation template?

In addition, AWS CloudFormation does not support defining template parameters as SecureString Systems Manager parameter types. However, you can specify Secure Strings as parameter values for certain resources by using dynamic parameter patterns.

Which section of CloudFormation template does not allow for conditions?

According to the docs, Conditions should be used at the top level of the resource you want to conditionally create. Putting a Condition inside the Instance UserData section isn't supported. To use Conditions in your situation, you'd want separate Resources conditionally created based on the Parameter.

What are parameters which are predefined by CloudFormation called?

Pseudo parameters are parameters that are predefined by AWS CloudFormation. You don't declare them in your template. Use them the same way as you would a parameter, as the argument for the Ref function.


1 Answers

You could apply your tags to the stack instead and they will propagate to the resources.

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-resource-tags.html

All stack-level tags, including automatically created tags, are propagated to resources that AWS CloudFormation supports. Currently, tags are not propagated to Amazon EBS volumes that are created from block device mappings.

For example:

aws cloudformation create-stack --stack-name my-stack
                                --template-url <template>
                                --parameters ParameterKey=Param1,ParameterValue=Value1
                                --tags Key=Tag1,Value=Value1
like image 166
Rik Turnbull Avatar answered Oct 08 '22 08:10

Rik Turnbull