Skip to content

Instantly share code, notes, and snippets.

@AngelChaidez
Created May 19, 2023 19:19
Show Gist options
  • Save AngelChaidez/c6433e551ff30b237f2a039bde8b0f2b to your computer and use it in GitHub Desktop.
Save AngelChaidez/c6433e551ff30b237f2a039bde8b0f2b to your computer and use it in GitHub Desktop.
import json
#!/usr/bin/env python3.11
# this script will start ec2 instances based off of tags
import boto3
import sys
import os
def lambda_handler(event, context):
ec2 = boto3.client('ec2')
regions = [region['RegionName']
for region in ec2.describe_regions()['Regions']]
# start instances in us-east-1 with Key labeled 'tag' and Value labeled 'swarm_node'
filters = [{
'Name': 'tag:swarm_node',
'Values': ['true']
}
]
for region in regions:
ec2 = boto3.resource('ec2', region_name=region)
instances = ec2.instances.filter(Filters=filters)
for instance in instances:
instance.start()
print(f"started instance: {instance.id} in {region}")
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment