How to put Tags on API Gateway V2 Resources using a YAML CloudFormation Template

Viewed 1198

How to put Tags on the following Resources using a CloudFormation Template:

  • AWS::ApiGatewayV2::Api
  • AWS::ApiGatewayV2::DomainName
  • AWS::ApiGatewayV2::Stage

For a generic AWS::ApiGatewayV2::Api Resource I have tried the following in the Resources section of the CloudFormation Template:

MyApi:
  Type: 'AWS::ApiGatewayV2::Api'
  Properties:
    Name: MyApi
    ProtocolType: WEBSOCKET
    RouteSelectionExpression: $request.body.action
    ApiKeySelectionExpression: $request.header.x-api-key
    Tags:
      - Key: TagKey1
        Value: MyFirstTag
      - Key: TagKey2
        Value: !Ref MySecondTagAsParameter

In the CloudFormation Events view of Amazon Management Console, The Resource failed with the following reason:

Property validation failure: [Value of property {/Tags} does not match type {Map}]

I looked up the Type, which appeared to be Json in the documentation:

Tags
  The collection of tags. Each tag element is associated with a given resource.
  Required: No
  Type: Json
  Update requires: No interruption
  Required: No

Which made me try the following:

 Tags: !Sub "{ \"TagKey1\" : \"MyFirstTag\", \"TagKey2\" : \"${MySecondTagAsParameter}\"}"

That also did not work, prompting me to try YAML literals:

Tags: !Sub |
  {
    "TagKey1": "MyFirstTag",
    "TagKey2": "${MySecondTagAsParameter}"
  }

That did not work either.

3 Answers

The following did the trick:

Tags:
  TagKey1: MyFirstTag
  TagKey2: !Ref MySecondTagAsParameter

You were very close to the json-like solution:

      Tags: { "TagKey1": "MyFirstTag",
              "TagKey2": !Ref MySecondTagAsParameter}

For me the following syntax worked:

Tags: 
  - 
    Key: "keyname1"
    Value: "value1"
  - 
    Key: "keyname2"
    Value: "value2"

Source: AWS Documentation

Related