Interview Prep/Python for DevOps/boto3 & AWS automation

How do you work with tags in boto3?

IntermediateConceptboto3 & AWS automation
Answer

Most services represent tags as a list of {"Key": ..., "Value": ...} dicts; convert to a dict for lookups.

tags = {t["Key"]: t["Value"] for t in inst.get("Tags", [])}
ec2.create_tags(Resources=[iid], Tags=[{"Key": "Owner", "Value": "platform"}])
ec2.describe_instances(Filters=[{"Name": "tag:Environment", "Values": ["prod"]},
                                {"Name": "tag-key", "Values": ["Owner"]}])

Some services differ (S3 uses TagSet, Lambda uses a plain dict). The Resource Groups Tagging API (resourcegroupstaggingapi.get_resources) searches tags across services in one place.

All Python for DevOps questions Roadmap: Python for automation

More on boto3 & AWS automation