Tagging AWS Reserved Instances

What has always bugged me about using AWS’s Cost Explorer was how they handled Reserved Instances. Every month there is a huge spike for reserved instances and there is no was to tag them to allocate them to a cost category or some other pivot… or is there?

Tagging of reservations is not exposed in the AWS Console, but is possible using the AWS CLI. Tagging is done via the ec2 create-tags (documentation) command and you specify the reserved instances ID as the resource ID.

To find the reserved instances ID:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ aws ec2 describe-reserved-instances --filters Name=state,Values=active

{
    "ReservedInstances": [
        {
            "Duration": 31536000,
            "End": "2021-10-14T21:27:24+00:00",
            "FixedPrice": 0.0,
            "InstanceCount": 2,
            "InstanceType": "m5.large",
            "ProductDescription": "Linux/UNIX",
            "ReservedInstancesId": "f5f692dd-c94d-4149-8ec9-5f8a6bf560e9",
            "Start": "2020-10-14T21:27:25.452000+00:00",
            "State": "active",
            "UsagePrice": 0.0,
            "CurrencyCode": "USD",
            "InstanceTenancy": "default",
            "OfferingClass": "convertible",
            "OfferingType": "No Upfront",
            "RecurringCharges": [
                {
                    "Amount": 0.071,
                    "Frequency": "Hourly"
                }
            ],
            "Scope": "Region"
        }
    ]
}

The ID in the above case is f5f692dd-c94d-4149-8ec9-5f8a6bf560e9. To tag this reservation with the cost_category=cogs,environment=prod,role=kubernetes-worker tags we would use the following command:

1
$ aws ec2 create-tags --resources f5f692dd-c94d-4149-8ec9-5f8a6bf560e9 --tags Key=environment,Value=prod Key=cost_category,Value=cogs Key=role,Value=kubernetes-worker

We can confirm this worked by running the describe-reserved-instances command again and we should see that the tags were indeed added:

 1
 2
 3
 4
 5
 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
$ aws ec2 describe-reserved-instances --filters Name=state,Values=active

{
    "ReservedInstances": [
        {
            "Duration": 31536000,
            "End": "2021-10-14T21:27:24+00:00",
            "FixedPrice": 0.0,
            "InstanceCount": 2,
            "InstanceType": "m5.large",
            "ProductDescription": "Linux/UNIX",
            "ReservedInstancesId": "f5f692dd-c94d-4149-8ec9-5f8a6bf560e9",
            "Start": "2020-10-14T21:27:25.452000+00:00",
            "State": "active",
            "UsagePrice": 0.0,
            "CurrencyCode": "USD",
            "InstanceTenancy": "default",
            "OfferingClass": "convertible",
            "OfferingType": "No Upfront",
            "RecurringCharges": [
                {
                    "Amount": 0.071,
                    "Frequency": "Hourly"
                }
            ],
            "Scope": "Region",
            "Tags": [
                {
                    "Key": "environment",
                    "Value": "prod"
                },
                {
                    "Key": "role",
                    "Value": "kubernetes-worker"
                },
                {
                    "Key": "cost_category",
                    "Value": "cogs"
                }
            ]
        }
    ]
}