Skip to content

Instantly share code, notes, and snippets.

@bblanchon
Last active March 8, 2021 14:02
Show Gist options
  • Save bblanchon/88243641b9339c3639d9564bd1f0c4c6 to your computer and use it in GitHub Desktop.
Save bblanchon/88243641b9339c3639d9564bd1f0c4c6 to your computer and use it in GitHub Desktop.
AWS Lambda: start EC2 instances
import boto3
region = 'eu-west-3'
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
instances = event['instances']
ec2.start_instances(InstanceIds=instances)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*"
],
"Resource": "*"
}
]
}
@bblanchon
Copy link
Author

To be called with the following params:

{"instances":["i-0123456789abcdef"]}

@bblanchon
Copy link
Author

Replace start_instances with stop_instances to stop the EC2 instances.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment