AWS Classroomnotes 05/Mar/2022

Exposing Lambda function over http

  • Lets create a simple lambda function with python runtime
  • Now lets integrate this Lambda function with API Gateway











  • Now as discussed in the class we have exposed lambda function using API Gateway
import json
import boto3

def stop_instances_by_tag(region, tag_name, tag_value):
    """
    This method will find the instances in a region
    """
    ec2 = boto3.client('ec2', region_name=region)
    response = ec2.describe_instances(Filters=[
        {
            'Name': f'tag:{tag_name}',
            'Values': [tag_value]
        }
    ])
    instances_found = response['Reservations'][0]['Instances']
    print(f"found {len(instances_found)} instances")
    #for instance in response['Reservations'][0]['Instances']:
    #    ec2.start_instances(InstanceIds=[instance['InstanceId']])
    instance_ids = [ instance['InstanceId'] for instance in response['Reservations'][0]['Instances']]
    response = ec2.stop_instances(InstanceIds=instance_ids)
    print(response)

def lambda_handler(event, context):
    tag_value = event['queryStringParameters']['tagvalue']
    tag_name  = event['queryStringParameters']['tagname']
    region = event['queryStringParameters']['region']
    stop_instances_by_tag(region, tag_name, tag_value)
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

  • Next Steps:
    • Message Queues using AWS Managed Services.
    • Event-based architecture using AWS Managed Services.
Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Animated Social Media Icons by Acurax Responsive Web Designing Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%