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.