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.

КОМЕНТАРІ • 21

  • @irineashadevops
    @irineashadevops Рік тому

    great video

  • @DavidLopez-jb7tz
    @DavidLopez-jb7tz 3 роки тому +1

    Man, your videos are so easy to follow! Keep it up!

  • @AJ-0223
    @AJ-0223 Рік тому

    Your videos are very informative.. please create a video on AWS Glue

  • @ukmarkydaddy
    @ukmarkydaddy 3 роки тому

    Thanks - very clear.

  • @sushantpanda5064
    @sushantpanda5064 3 роки тому

    Thank you for this helpful example

  • @kesterbelgrove818
    @kesterbelgrove818 2 роки тому

    Just subscribed. I had no choice... your videos are so easy to follow and they work! Thanks!

  • @deepakkumarshrivastava6274
    @deepakkumarshrivastava6274 3 роки тому

    Thank you very much for this helpful video 👍

  • @srinivasareddy850
    @srinivasareddy850 2 роки тому

    Thank you

  • @ramnikjain1225
    @ramnikjain1225 3 роки тому +2

    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

    • @NKTStudios
      @NKTStudios  3 роки тому

      @RAMNIK JAIN, thanks for the kind words and we will surely make more videos!

  • @annx7305
    @annx7305 Рік тому

    Can you help share the way how to start/stop lightsail ?

  • @hananloogsi
    @hananloogsi 2 роки тому +1

    i have "errorType": "EndpointConnectionError" , how can i solved this?

  • @rehanmasood1522
    @rehanmasood1522 3 роки тому

    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

  • @nitinalety5326
    @nitinalety5326 3 роки тому

    hi, new to AWS .Can we also terminate the stopped instances using scripting ? does lambda function cost ?

  • @MyDaniel128
    @MyDaniel128 3 роки тому +1

    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 ?

    • @NKTStudios
      @NKTStudios  3 роки тому

      Hi @Daniel Pop, are you still having problems?

    • @MyDaniel128
      @MyDaniel128 3 роки тому +2

      @@NKTStudios no, i have figured it out, i needed to press the deploy button for the function to update from the default one

    • @rajarshichowdhuri815
      @rajarshichowdhuri815 3 роки тому +1

      @@MyDaniel128 thanks i was also facing the same issue and wondered where I went wrong.After clicking deploy its working all fine.

  • @mohansulamgunta7820
    @mohansulamgunta7820 2 роки тому

    Hi bro ur video is very nice, thanks and please help me RDS also same it would be great helpful to me

  • @abhisheksaha7391
    @abhisheksaha7391 2 роки тому

    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))
    "
    ]
    }

    • @irineashadevops
      @irineashadevops Рік тому

      please use instances instead Instances in this line Instances = event["instances"].split(',')