I used the following code block to add Tags into Cloudformation yaml file.
%{~ if length(mytags) >0 ~}
Tags:
%{~ endif ~}
%{~ for tag_key, tag_value in mytags ~}
- Key: "${tag_key}"
Value: "${tag_value}"
%{~ endfor ~}
I had to put ~
before }
and after {
for each control lines to avoid having extra new lines for each tag key, value.
sample terraform code block:
variable "tags" {
description = "A list of tags to apply to fleet."
type = map(string)
default = {
"Tag1" = "value1"
"Tag2" = "value2"
"Tag3" = "value3"
}
}
resource "aws_cloudformation_stack" "appstream" {
count = var.enable_appstream == true ? 1 : 0
name = "appstream-${var.stack_name}"
tags = var.tags
parameters = {
StackName = var.stack_name
FleetName = var.fleet_name
FleetType = var.fleet_type
ImageName = var.appstream_image_name
InstanceType = var.instance_type
DesiredInstances = var.no_of_instance
SubnetIds = var.subnet_ids
SecurityGroupIds = var.security_group_ids
StreamView = var.stream_view
DisconnectTimeoutInSeconds = var.disconnect_timeout_in_seconds
IdleDisconnectTimeoutInSeconds = var.idle_disconnect_timeout_in_seconds
}
template_body = templatefile("${path.module}/cloudformation.yaml", { mytags = var.tags, stack_name = var.stack_name })
}
sample cloudformation.yaml
Resources:
AppStreamFleet:
Type: "AWS::AppStream::Fleet"
Properties:
Name: !Ref FleetName
Description: !Ref FleetName
ImageName: !Ref ImageName
InstanceType: !Ref InstanceType
FleetType: !Ref FleetType
StreamView: !Ref StreamView
ComputeCapacity:
DesiredInstances: !Ref DesiredInstances
VpcConfig:
SubnetIds: !Ref SubnetIds
SecurityGroupIds: !Ref SecurityGroupIds
MaxUserDurationInSeconds: "57600"
DisconnectTimeoutInSeconds: !Ref DisconnectTimeoutInSeconds
IdleDisconnectTimeoutInSeconds: !Ref IdleDisconnectTimeoutInSeconds
EnableDefaultInternetAccess: true
%{~ if length(mytags) >0 ~}
Tags:
%{~ endif ~}
%{~ for tag_key, tag_value in mytags ~}
- Key: "${tag_key}"
Value: "${tag_value}"
%{~ endfor ~}
CreationPolicy:
StartFleet: True
when you do terraform apply,the YAML content will be like the following:
IdleDisconnectTimeoutInSeconds: !Ref IdleDisconnectTimeoutInSeconds
EnableDefaultInternetAccess: true
Tags:
- Key: "Tag1"
Value: "value1"
- Key: "Tag2"
Value: "value2"
- Key: "Tag3"
Value: "value3"
CreationPolicy:
StartFleet: True
```