Start Stop EC2 with Lambda Python
Вставка
- Опубліковано 20 лип 2024
- In this video I will show you how to start and stop EC2 instances with lambda python. We will also use Amazon EventBridge to setup rules that will trigger the AWS Lambda that starts or stops EC2 instances.
🔔 Subscribe and never miss new videos: ua-cam.com/users/nktstudios?su...
🔴 AWS Cost Management Playlist: • AWS Cost Management Se...
🔴 AWS Lambda Series: • AWS Lambda Series
✔️ Lambda and Test Events Source Code: gist.github.com/nktstudios/51...
⏱️TIMESTAMPS⏱️
0:00 Introduction to Starting and Stopping EC2 Instances with AWS Lambda Python
0:38 Setting up EC2 Instances
2:27 Setting up AWS Lambda in Python to Start and Stop EC2 Instances
2:58 Create AWS Lambda Role to Start - Stop EC2 Instances
5:10 Writing Lambda Code to Start - Stop EC2 Instances
9:28 Testing our Lambda that Starts and Stops EC2 Instances
10:44 Testing Stopping EC2 Instances with Lambda
11:28 Testing Starting EC2 Instances with Lambda
11:51 Using Amazon EventBridge to trigger Lambda
12:17 Create an Amazon EventBridge rule with Cron Expression to Start - Stop EC2 Instances
🔹 My Courses: nktstudios.net/courses
⚡ My Gear
Windows Laptop: amzn.to/3d4xaK6
Recording Mic (Blue Snowball): amzn.to/30K0PTP
Redragon Headset: amzn.to/33AMR8H
✅ Let's connect:
Website: nktstudios.net
💬 Twitter - @nktstudios
Facebook: / nktstudioz
DISCLOSURE: Some of the links on this page are affiliate links, meaning that, at no additional cost to you, I may earn a commission if you click through and make a purchase and/or subscribe. Affiliate commissions help us to continue making videos for you.
❤️ Thanks for watching.
great video
Man, your videos are so easy to follow! Keep it up!
Your videos are very informative.. please create a video on AWS Glue
Thanks - very clear.
Thank you for this helpful example
Just subscribed. I had no choice... your videos are so easy to follow and they work! Thanks!
Thank you very much for this helpful video 👍
Thank you
thank you so-so very much, please make more videos on python configuration with lambda function for various aws services, i am non programmer, and this video is such a great help,
thank you again
@RAMNIK JAIN, thanks for the kind words and we will surely make more videos!
Can you help share the way how to start/stop lightsail ?
i have "errorType": "EndpointConnectionError" , how can i solved this?
Hi, Can you please share the video link which you have discussed in the last minutes of this video that how to stop and start the ec2 instances via TAGS. Thanks
hi, new to AWS .Can we also terminate the stopped instances using scripting ? does lambda function cost ?
hey, when I try to run the tests the response is still { "statusCode" : 200, "body" : "\"Hello from Lambda!\""} , it's like I run the default lambda function , like I didn't modify the function. Can you help me out please ?
Hi @Daniel Pop, are you still having problems?
@@NKTStudios no, i have figured it out, i needed to press the deploy button for the function to update from the default one
@@MyDaniel128 thanks i was also facing the same issue and wondered where I went wrong.After clicking deploy its working all fine.
Hi bro ur video is very nice, thanks and please help me RDS also same it would be great helpful to me
import json
import boto3
region = 'ap-south-1' # Dont use the specific, like instead of us-east-1d just write us-east-1
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
Instances = event["instances"].split(',')
action = event["action"]
if action == 'Start':
print("Starting your instances: " + str(instances))
ec2.start_instances(InstanceIds=instances)
response = "Successfully Started instances: " + str(instances)
elif action == 'Stop':
print("Stopping your instances: " + str(instances))
ec2.stop_instances(InstanceIds=instances)
response = "Successfully Stopped instances: " + str(instances)
return {
'statusCode': 200,
'body': json.dumps(response)
}
## Anything wrong with this code currently getting this error:-Response
{
"errorMessage": "name 'instances' is not defined",
"errorType": "NameError",
"requestId": "e6c27fb6-315f-4c5c-b6a1-e270adbedefa",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 12, in lambda_handler
print(\"Starting your instances: \" + str(instances))
"
]
}
please use instances instead Instances in this line Instances = event["instances"].split(',')