What is AWS LAMBDA?
AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no charge when your code is not running.
With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.
WHY LAMBDA?
NO SERVERS TO MANAGE
AWS Lambda automatically runs your code without requiring you to provision or manage servers. Just write the code and upload it to Lambda.
CONTINUOUS SCALING
AWS Lambda automatically scales your application by running code in response to each trigger. Your code runs in parallel and processes each trigger individually, scaling precisely with the size of the workload.
SUBSECOND METERING
With AWS Lambda, you are charged for every 100ms your code executes and the number of times your code is triggered. You don't pay anything when your code isn't running.
import boto3 # Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g.; 'us-east-1' region = 'XX-XXXXX-X' # Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX'] instances = ['X-XXXXXXXX'] def lambda_handler(event, context): ec2 = boto3.client('ec2', region_name=region) ec2.start_instances(InstanceIds=instances) print 'started your instances: ' + str(instances)
Please find the Details Step By Step for Ec2 Start Stop Through lambda https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
Lambda Function for Stop Ec2 Instance
boto3 # Enter the region your instances are in. Include only the region without specifying Availability Zone; e.g., 'us-east-1' region = 'XX-XXXXX-X' # Enter your instances here: ex. ['X-XXXXXXXX', 'X-XXXXXXXX'] instances = ['X-XXXXXXXX'] def lambda_handler(event, context): ec2 = boto3.client('ec2', region_name=region) ec2.stop_instances(InstanceIds=instances) print 'stopped your instances: ' + str(instances)