AWS EventBridge Service - process AWS events

Поділитися
Вставка
  • Опубліковано 18 жов 2024
  • This video will explain the use of the AWS eventbridge service and how we can use it to control the resources that are created within the AWS account. We will perform a hands on exercise to simulate terminating a costly ec2 instance created by a naive user.
    Lambda python script to terminate ec2 instances - github.com/gau...
    Content:
    AWS EventBridge
    AWS Training
    automatically stop costly ec2 instances
    AWS Projects

КОМЕНТАРІ • 19

  • @indranilgoswami1500
    @indranilgoswami1500 2 місяці тому +1

    Thanks sir..very good explanation.

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

    Thank you for the detailed hands on video. I've been learning AWS recently and had trouble finding hands on guide on AWS EventBridge. This video have been really helpful to me

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

    👍🏻

  • @sudsrmsee
    @sudsrmsee 10 місяців тому +1

    super > keep it up

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

    Nice video sir, in this way we can automate lot of mannual things.Any documents for lamda we can go through.

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

      yeah, here is the aws doc - aws.amazon.com/lambda/ - Also i will be creating an entire course, so please feel free to subscribe to the channel.

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

    Thank you Gaurav, I liked this video, and subscribed your channel, am sure you will upload more informative video for AWS services. I have an query about EventBridge rule, you just created a t2. micro instance and that also got deleted by this rule, but t2.micro is free tier, so I'm little bit confused, I want delete instance those are above t2.large, can you please help for this?

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

      Thanks Sahab. I have keep the lambda code simple to reduce the complexity. The lambda code as of now is deleting any instance that got created. We can modify this code to ignore certain instance types. Let me know if you are looking for a modified lambda. I can provide you the same.

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

      I'm waiting for your response for above queries.

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

      @@sahabsinghlathwal5956 - this is the code that you can try (not tested, please test it out)
      import boto3
      def lambda_handler(event, context):
      # Initialize the EC2 client for the N. Virginia region (us-east-1)
      ec2_client = boto3.client('ec2', region_name='us-east-1')
      # Get a list of all EC2 instances in the N. Virginia region
      instances = ec2_client.describe_instances()
      # Define a list of instance types that are eligible for the AWS Free Tier in us-east-1
      free_tier_instance_types = ["t2.micro", "t3.micro", "t3.nano"]
      for reservation in instances['Reservations']:
      for instance in reservation['Instances']:
      # Check if the instance is in a running state
      if instance['State']['Name'] == 'running':
      instance_type = instance['InstanceType']
      instance_id = instance['InstanceId']
      # Check if the instance type is not in the free tier list
      if instance_type not in free_tier_instance_types:
      print(f"Terminating instance: {instance_id} (Instance Type: {instance_type})")
      ec2_client.terminate_instances(InstanceIds=[instance_id])
      else:
      print(f"Skipping instance in Free Tier: {instance_id} (Instance Type: {instance_type})")

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

      @@AWSTrainingByGauravAgrawal I tried to execute this code, but getting below error -
      {
      "errorMessage": "An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.",
      "errorType": "ClientError",
      "requestId": "d3493d54-903d-4852-bd0c-fc1164cc1acc",
      "stackTrace": [
      " File \"/var/task/lambda_function.py\", line 8, in lambda_handler
      instances = ec2_client.describe_instances()
      ",
      " File \"/var/lang/lib/python3.11/site-packages/botocore/client.py\", line 534, in _api_call
      return self._make_api_call(operation_name, kwargs)
      ",
      " File \"/var/lang/lib/python3.11/site-packages/botocore/client.py\", line 976, in _make_api_call
      raise error_class(parsed_response, operation_name)
      "
      ]
      }
      Function Logs
      START RequestId: d3493d54-903d-4852-bd0c-fc1164cc1acc Version: $LATEST
      [ERROR] ClientError: An error occurred (UnauthorizedOperation) when calling the DescribeInstances operation: You are not authorized to perform this operation.
      Traceback (most recent call last):
      File "/var/task/lambda_function.py", line 8, in lambda_handler
      instances = ec2_client.describe_instances()
      File "/var/lang/lib/python3.11/site-packages/botocore/client.py", line 534, in _api_call
      return self._make_api_call(operation_name, kwargs)
      File "/var/lang/lib/python3.11/site-packages/botocore/client.py", line 976, in _make_api_call
      raise error_class(parsed_response, operation_name)END RequestId: d3493d54-903d-4852-bd0c-fc1164cc1acc
      REPORT RequestId: d3493d54-903d-4852-bd0c-fc1164cc1acc Duration: 449.11 ms Billed Duration: 450 ms Memory Size: 128 MB Max Memory Used: 88 MB
      Request ID
      d3493d54-903d-4852-bd0c-fc1164cc1acc

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

      Can you plz fix this code and help for another which I asked you for tagging and security group?

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

    Can i use EventBridge to terminate and start my ec2 instances depending the working times

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

      yes, but insetad of using events, you will need to create a eventbridge scheduler. This scheduler can trigger a lambda function as per your schedule. That lambda will fetch all the ec2 instances in your account and stop or restart them as per your requirement.

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

      In the comments, i am not able to paste screenshot for the scheduler.

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

      I really need that scheduler... I want to terminate the instance at 1am and then start it at 6am.. I want to do it repeatedly... Hope you can help me with that

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

      @@tompatrickhankz2244 how comfortable r u with AWS ?? And with writing a AWS lambda ??

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

      @@AWSTrainingByGauravAgrawal Am not that comfortable but i can try.. With help of a video