Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Politica Lambda:
- {
- "Version": "2012-10-17",
- "Statement": [
- {
- "Effect": "Allow",
- "Action": [
- "logs:CreateLogGroup",
- "logs:CreateLogStream",
- "logs:PutLogEvents"
- ],
- "Resource": "arn:aws:logs:*:*:*"
- },
- {
- "Effect": "Allow",
- "Action": [
- "ec2:DescribeInstances",
- "ec2:Start*",
- "ec2:Stop*"
- ],
- "Resource": "*"
- }
- ]
- }
- #Funcion lambda python3
- import boto3
- region = 'us-east-1'
- ec2_client = boto3.client('ec2', region_name=region)
- def lambda_handler(event, context):
- lambda_response = [] # Used for response of the lambda function
- instance_list = [] # Used for storing filtered instances
- # Get a list of all instances filterd by tag
- response = ec2_client.describe_instances(Filters=[
- {
- 'Name': 'tag:Env',
- 'Values': ['dev']
- }
- ],
- )
- # add found intances to instance_list
- for Reservation in response['Reservations']:
- for instance in Reservation['Instances']:
- print('Found ', instance['InstanceId'])
- instance_list.append(instance['InstanceId'])
- # Turn off found instaces
- if len(instance_list) > 0:
- lambda_response.append(ec2_client.stop_instances(InstanceIds=instance_list))
- #pass
- else:
- lambda_response.append('No running instances found with given tag(s)')
- return lambda_response
Advertisement
Add Comment
Please, Sign In to add comment