Automate Start-Stop EC2 Instances Using Lambda - Step-by-Step Guide

In this informative video tutorial, we'll walk you through the process of automating the start and stop actions of your EC2 instances on Amazon Web Services (AWS) using AWS Lambda.
Throughout the video, we'll cover the following key points:
1. Introduction to AWS Lambda and its benefits in automating EC2 instance processes.
2. Setting up the AWS Management Console and creating a Lambda function.
3. Defining IAM roles and permissions for secure and seamless operations.
4. Configuring a scheduled event to trigger the Lambda function.
5. Writing the Lambda function code to start and stop EC2 instances.
6. Testing the function
By the end of this tutorial, you'll have a comprehensive understanding of how to leverage AWS Lambda to streamline your EC2 instance management and optimize your AWS resources efficiently.
Watch the video now and take control of your EC2 instances with automation powered by AWS Lambda. Subscribe to our channel for more AWS-related content and stay up-to-date with the latest cloud computing techniques!
Lambda code reference to stop EC2
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('stopped your instances: ' + str(instances))
Lambda code reference to start EC2
import boto3
region = 'us-west-1'
instances = ['i-12345cb6de4f78g9h', 'i-08ce9b2d7eccf6d26']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('started your instances: ' + str(instances))
reference document link
repost.aws/knowledge-center/s...

Пікірлер: 31

  • @mdiqbal5821
    @mdiqbal5821Күн бұрын

    Nice video man...please help us more

  • @Roneecol
    @Roneecol5 ай бұрын

    Many thanks.. It worked..

  • @InfoSecHindi
    @InfoSecHindiАй бұрын

    Thanks for explaining very nicely 🙏👍

  • @Guitar-Geek
    @Guitar-Geek10 ай бұрын

    many many many step-by-step and only tours worked perfectly fine.

  • @SaurabhGaikwadSSVlogs
    @SaurabhGaikwadSSVlogs10 ай бұрын

    Informative 👍

  • @ajaymadane8237
    @ajaymadane82374 ай бұрын

    very good explanation

  • @Shaan9987
    @Shaan998710 ай бұрын

    This is very informative . i tried and it works fine. thank you :)🙂

  • @user-su8wj4ey3x

    @user-su8wj4ey3x

    10 ай бұрын

    Hi Please can you give an insight on how you edited the Lambda function code?I can't copy and paste code. Showing i should use cloud9.

  • @Shaan9987

    @Shaan9987

    10 ай бұрын

    @@user-su8wj4ey3x Just copy from aws official document of python script for lambda function and try 1 st with deploy and then test

  • @CloudPaathshala

    @CloudPaathshala

    10 ай бұрын

    you can use lambda code generator from aws console itself

  • @gopalrathod262
    @gopalrathod2627 ай бұрын

    thanks bro

  • @saychaan7438
    @saychaan74386 ай бұрын

    nice video bro keep it up dont stop uploading

  • @CloudPaathshala

    @CloudPaathshala

    6 ай бұрын

    Thank you.

  • @user-su8wj4ey3x
    @user-su8wj4ey3x10 ай бұрын

    Hi Easy to follow and understand tutorial. Looks like one can't copy and paste code on Lambda anymore as you did on this tutorial. It showing i should use AWS cloud 9 to edit lambda function code.

  • @CloudPaathshala

    @CloudPaathshala

    10 ай бұрын

    you can use code generator for same and write required code

  • @gauravchoudhary3733
    @gauravchoudhary37332 ай бұрын

    instead of using cloudwatch, we can use eventbridge scheduler, by setting up the rate based timing schedule, to invoke the lambda function to turn on and off the EC2 instances right?

  • @user-mr5lx9hu7t

    @user-mr5lx9hu7t

    2 ай бұрын

    now cw rule now added into the EventBridge

  • @user-mr5lx9hu7t
    @user-mr5lx9hu7t2 ай бұрын

    Hi am trying to stop and start 3 ec2 its giving error of client error instance id is malformed. But its working for single ec2. Please how to solve this

  • @bhabanisanakrdas007
    @bhabanisanakrdas0075 ай бұрын

    Do you have to create 2 functions 2 rules for both start & stop ? Waiting for your valuable response..

  • @CloudPaathshala

    @CloudPaathshala

    4 ай бұрын

    Yes you can create 2 functions and 1 function is also enough where we can include other required rule as well.

  • @bhabanisanakrdas007

    @bhabanisanakrdas007

    4 ай бұрын

    @@CloudPaathshala it is good now

  • @karthiK-rg6fh
    @karthiK-rg6fh10 ай бұрын

    Hi how to monitor performance and availablity of ec2

  • @CloudPaathshala

    @CloudPaathshala

    9 ай бұрын

    by using cloudwatch, you can watch my video on cloudwatch to monitor ec2

  • @arunkumar-zz7tn
    @arunkumar-zz7tn8 ай бұрын

    For monthly cost should be??

  • @CloudPaathshala

    @CloudPaathshala

    6 ай бұрын

    monthly cost of any particular resource?

  • @SorowerHossan-kf6qp
    @SorowerHossan-kf6qp5 ай бұрын

    This is good stuff..

  • @sanketpawar9686
    @sanketpawar96862 ай бұрын

    when it start again

  • @KD-wp6cm
    @KD-wp6cm2 ай бұрын

    Thanks

  • @zdroweodzywianie9107
    @zdroweodzywianie91074 ай бұрын

    Hey I get an error: Response { "errorMessage": "2024-03-14T10:32:34.778Z c8640d39-6d77-4df5-8a9d-ea41a6db2300 Task timed out after 3.01 seconds" } what I could do wrong?

  • @toni9536

    @toni9536

    27 күн бұрын

    Try going to configuration of the lambda function and increasing the timeout. It worked for me

  • @travisjae1775
    @travisjae177522 күн бұрын

    For some reason it's not working for me, it gives me this message: Please Help [ERROR] NameError: name 'region_name' is not defined Traceback (most recent call last): File "/var/lang/lib/python3.12/importlib/__init__.py", line 90, in import_module