Created
July 8, 2026 08:56
-
-
Save Dirga36/da75582c2ee364432684d267038f7dc9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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