Skip to content

Instantly share code, notes, and snippets.

@amitavroy
Last active January 23, 2024 10:15
Show Gist options
  • Save amitavroy/26089061c7037e1ade77497504241618 to your computer and use it in GitHub Desktop.
Save amitavroy/26089061c7037e1ade77497504241618 to your computer and use it in GitHub Desktop.
AWS EC2 Start Stop Lambda
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:Start*",
"ec2:Stop*",
"ec2:DescribeInstanceStatus"
],
"Resource": "*"
},
{
"Sid": "VisualEditor1",
"Effect": "Allow",
"Action": [
"logs:CreateLogStream",
"logs:CreateLogGroup",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
}
]
}
56 3 ? * MON-SAT *
import boto3
region = 'us-east-1'
instances = ['i-001e2d04e37ccde3b']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
print('Starting instances')
ec2.start_instances(InstanceIds=instances)
import boto3
region = 'us-east-1'
instances = ['i-001e2d04e37ccde3b']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
print('Stopping instances')
ec2.stop_instances(InstanceIds=instances)
@shwanbekas
Copy link

thank you

@adboot
Copy link

adboot commented Mar 17, 2023

Thank you

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