Start/stop EC2 instances using AWS Lambda and CloudWatch Events Scheduler

Поділитися
Вставка
  • Опубліковано 14 жов 2024
  • Task 1: Create one Windows/Linux instance.
    Task 2: Open the IAM service and create a role to access lambda services.
    Steps:
    • Select AWS services
    • Choose lambda use case.
    • Click on permission and use two policies: AWSLambdaBasicExecutionRole (allow to use cloudwatch) and AmazonEC2Full Access. Note: You can create your policies, or you can select existing policies
    • Provide a tag.
    • Provide Role name and role description.
    Task 3: Open Lambda services and create a new lambda function.
    Steps:
    • Provide function name.
    • Use Python as a runtime environment.
    • Go to permissions and use the created role in Task 2.
    • Now basic function code window will appear, and we need to write code for starting and stopping EC2 instances.
    Code:
    Start instance:
    import boto3
    region = 'us-east-2'
    instances = ['i-0f019ff0664238dcb']
    ec2 = boto3.client('ec2', region_name=region)
    def lambda_handler(event, context):
    ec2.start_instances(InstanceIds=instances)
    print('starting your instances: ' + str(instances))
    Stop Instance:
    import boto3
    region = 'us-east-2'
    instances = ['i-0f019ff0664238dcb']
    ec2 = boto3.client('ec2', region_name=region)
    def lambda_handler(event, context):
    ec2.stop_instances(InstanceIds=instances)
    print('stopped your instances: ' + str(instances))
    Task 4: Save the code. Select a test event and configure the test event.
    Task 5: Test the EC2 events by clicking on the startEC2 event and stopEC2 event.
    Task 6: Provide the execution details of the lambda function.
    Task 7: Select the event bridge and click on create a rule to trigger the created lambda function.
    Steps:
    • Provide a name and description for the rule.
    • Define pattern as an event by providing Cron expression.
    • Select targets as Lambda function, provide the name of the function, and copy the JSON text from the event window.
    • Create the rule now.

КОМЕНТАРІ • 8

  • @shaik-k4f
    @shaik-k4f 10 місяців тому +1

    Thanks a lot madam Suchi, The content which has shared by you is wonderful but it must require some more loud voice please don't think as negative....
    I hope you will do lots of useful videos...

  • @VarunBajaj-w8t
    @VarunBajaj-w8t 11 місяців тому

    how can we add multiple targets in scheduling?

    • @suchisingh9752
      @suchisingh9752  11 місяців тому

      To start or stop multiple instances, you can specify a list of instance IDs in a comma-separated form in the instances list.

  • @surajmishra4096
    @surajmishra4096 7 місяців тому

    how to create a instance at 10 am automatically and delete after 2-3 hours it is possible to create and delete the instance automatically

    • @suchisingh9752
      @suchisingh9752  7 місяців тому

      You can create two lambda functions or you can use if-else statement to start/stop EC2 instances. After creation of the lambda function, you can go to the event bridge schedular and in the schedule pattern, you can make a recurring pattern considering your specified time to start/stop your EC2 instance.

  • @tamilselvans3234
    @tamilselvans3234 Рік тому +1

    thanks mam 😀

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

    hello Madam
    volume is very low, please increase your volume madam....etc
    thanks

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

    volume bohat low hai mam....