Skip to content

Instantly share code, notes, and snippets.

@Dirga36
Created July 8, 2026 08:56
Show Gist options
  • Select an option

  • Save Dirga36/da75582c2ee364432684d267038f7dc9 to your computer and use it in GitHub Desktop.

Select an option

Save Dirga36/da75582c2ee364432684d267038f7dc9 to your computer and use it in GitHub Desktop.
import boto3
REQUIRED_TAGS = {"team", "environment", "project"}
def lambda_handler(event, context):
ec2 = boto3.client("ec2")
detail = event["detail"]
if detail["eventName"] != "RunInstances":
return
instance_ids = [
item["instanceId"]
for item in detail["responseElements"]["instancesSet"]["items"]
]
for instance_id in instance_ids:
tags_response = ec2.describe_tags(
Filters=[{"Name": "resource-id", "Values": [instance_id]}]
)
existing_tags = {tag["Key"] for tag in tags_response["Tags"]}
missing = REQUIRED_TAGS - existing_tags
if missing:
ec2.create_tags(
Resources=[instance_id],
Tags=[{"Key": "cost-compliance", "Value": "missing-tags"}],
)
print(f"Instance {instance_id} missing tags: {missing}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment