Skip to main content

Python CLI for tagging AWS resources based on a YAML configuration

Project description

Avatar

Build Status Security Status Published Version

Awstaga

Awstaga is a Python CLI for tagging AWS resources based on a YAML configuration.

This package is intended as a companion for AWS Tag Editor. While AWS Tag Editor is useful for tagging multiple resources in one go, it has no easy way to re-run the tagging since you have to use the AWS console UI, and its resource filtering capability is quite limited, making it hard to select resources with-more-than basic logic.

Using Awstaga, you can easily re-run the tagging by running the CLI again. And with its support of YAML configuration, it allows you to define multiple tagsets which you can reuse and mix and match with the resources, you can construct your own mapping between the resources and the relevant tags and tagsets. You can generate your own YAML configuration using Python scripts, or any other programming language, allowing you to construct a more complex filtering logic.

Installation

pip3 install awstaga

Usage

Create a configuration file, e.g. awstaga.yaml:

---
tagsets:
  - name: common
    tags:
      - key: CostCentre
        value: FIN-123
      - key: Organisation
        value: World Enterprise
      - key: Description
        value: AWS Resource
  - name: prod
    tags:
      - key: EnvType
        value: prod
      - key: Availability
        value: 24x7
  - name: nonprod
    tags:
      - key: EnvType
        value: non-prod
      - key: Availability
        value: on-demand
resources:
  - arn: 'arn:aws:ssm:ap-southeast-2:123456789012:document/high-avail'
    tags:
      - key: Description
        value: High availability SSM document
    tagsetnames:
      - common
      - prod
  - arn: 'arn:aws:s3:::world-enterprise/development/logo.jpg'
    tags:
      - key: Description
        value: World Enterprise logo
    tagsetnames:
      - common
      - nonprod

And then run awstaga CLI and pass the configuration file path:

awstaga --conf-file awstaga.yaml

It will write the log messages to stdout:

[awstaga] INFO Loading configuration file awstaga.yaml
[awstaga] INFO Loading 3 tagset(s)...
[awstaga] INFO Loading 2 resource(s)...
[awstaga] INFO Adding resource arn:aws:ssm:ap-southeast-2:123456789012:document/high-avail to a batch with tags {'CostCentre': 'FIN-123', 'Organisation': 'World Enterprise', 'Description': 'AWS Resource', 'EnvType': 'prod', 'Availability': '24x7', 'Description': 'High availability SSM document'}
[awstaga] INFO Adding resource arn:aws:s3:::world-enterprise/development/logo.jpg to a batch with tags {'CostCentre': 'FIN-123', 'Organisation': 'World Enterprise', 'Description': 'AWS Resource', 'EnvType': 'prod', 'Availability': '24x7', 'Description': 'World Enterprise logo'}

And if the tagging failed (e.g. due to rate exceeded), it will log the following error messages:

[awstaga] ERROR Failed to apply tags to 1 resource(s):
[awstaga] ERROR arn:aws:ssm:ap-southeast-2:123456789012:document/high-avail: 400 - Throttling - Rate exceeded
[awstaga] ERROR arn:aws:s3:::world-enterprise/development/logo.jpg: 400 - Throttling - Rate exceeded

Dry run

You can also run Awstaga in dry-run mode by adding --dry-run flag:

awstaga --conf-file awstaga.yaml --dry-run

During dry-run mode, Awstaga log messages will be labeled with [dry-run]:

[dry-run] [awstaga] INFO Loading configuration file awstaga.yaml
[dry-run] [awstaga] INFO Loading 3 tagset(s)...
[dry-run] [awstaga] INFO Loading 2 resource(s)...
[dry-run] [awstaga] INFO Adding resource arn:aws:ssm:ap-southeast-2:123456789012:document/high-avail to a batch with tags {'CostCentre': 'FIN-123', 'Organisation': 'World Enterprise', 'Description': 'AWS Resource', 'EnvType': 'prod', 'Availability': '24x7', 'Description': 'High availability SSM document'}

YAML includes

Awstaga supports YAML includes using , so you can split your configuration into multiple files:

---
tagsets:
  - !include include.d/tagset.yaml
resources: !include include.d/resources.yaml

Include files should be put under `include.d/`` folder relative to the configuration file.

The included tagset file include.d/tagset.yaml:

---
name: common
tags:
  - key: CostCentre
    value: FIN-123
  - key: Organisation
    value: World Enterprise
  - key: Description
    value: AWS Resource

The included resources file include.d/resources.yaml:

---
- arn: 'arn:aws:ssm:ap-southeast-2:123456789012:document/high-avail'
  tags:
    - key: Description
      value: High availability SSM document
  tagsetnames:
    - common
    - prod
- arn: 'arn:aws:s3:::world-enterprise/development/logo.jpg'
  tags:
    - key: Description
      value: World Enterprise logo
  tagsetnames:
    - common
    - nonprod

Batch size

In order to optimise the number of API calls, resources with identical tags are put into batches. By default, the batch size is 20. You can run Awstaga with a custom batch size --batch-size <number> flag:

awstaga --conf-file awstaga.yaml --batch-size 10

Delay

In order to avoid rate exceeded error, you can run Awstaga with a custom delay --delay <number> flag:

awstaga --conf-file awstaga.yaml --delay 5

By default, the delay is 2 seconds.

Configuration

These are the configuration properties that you can use with awstaga CLI. Some example configuration files are available on examples folder.

Property Type Description Example
tagsets[] Array A list of one or more tagsets. Any tagset can be associated with any resource, and the resource will include the tags specified in the tagset.
tagsets[].name String The name of the tagset. common
tagsets[].tags[] Array A list of one or more key-value pair tags within the tagset.
tagsets[].tags[].key String The tag key. CostCentre
tagsets[].tags[].value String The tag value. FIN-123
resources[] Array A list of one or more AWS resources. Each of the resource has a corresponding list of tags, along with the tags from tagsets.
resources[].arn String AWS resource ARN. arn:aws:s3:::world-enterprise/development/logo.jpg
resources[].tags[] Array A list of one or more key-value pair tags of the resource.
resources[].tags[].key String The tag key. Description
resources[].tags[].value String The tag value. Some description
resources[].tagsetnames[] Array A list of one or more tagset names. All tags within the tagsets specified are included in the resource.

Colophon

Developer's Guide

Build reports:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

awstaga-1.2.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

awstaga-1.2.0-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file awstaga-1.2.0.tar.gz.

File metadata

  • Download URL: awstaga-1.2.0.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Linux/6.2.0-1018-azure

File hashes

Hashes for awstaga-1.2.0.tar.gz
Algorithm Hash digest
SHA256 3dce3f22b87abfb28e0052c6acb8d593cfb7b8324eccf7168250a4555eab8d57
MD5 eb46cf9851a6a3153d3c88dd6014e587
BLAKE2b-256 bb37bd21c659c360dce7a480aef37eb5e638b2f0fb3ba28cc8c3b27a09392b39

See more details on using hashes here.

File details

Details for the file awstaga-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: awstaga-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.0 Linux/6.2.0-1018-azure

File hashes

Hashes for awstaga-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42c3aaa64ac8a06c209e028c6bf2422ce896beb5415d2325813c256626211af9
MD5 4173936d21612752a2ffb13547122e20
BLAKE2b-256 bc0bbaa41f4e952d09361592e9bf816991fcb55cf3bd06b719756781bb4e87ee

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page