Working with Lambda and EventBridge to test possibilities of scheduling invocations

Services Covered

  • lambda Lambda
  • CloudWatch CloudWatch
  • EventBridge EventBridge

Lab description

In this Lab I will create a Lambda function. Then I will configure EventBridge to create events that will trigger that Lambda function on schedule. Finally, I will verify that my Lambda function is being triggered using CloudWatch.


Learning Objectives

  • Create Lambda functions
  • Run Lambda functions on a schedule using EventBridge
  • Monitor Lambda functions using CloudWatch

Lab date

11-11-2021


Prerequisites

  • AWS account

Lab steps

  1. Start with creating a Lambda function. Choose Python as runtime. Use the following code, it’ll fetch provided website (‘URL’) and search for a word on it (‘TEXT’)
    from urllib.request import urlopen
    
    URL = 'https://cloudacademy.com'
    TEXT = 'AWS'
    
    def lambda_handler(event, context):
       print('Event: %s' % event)
       t = event.get('time') or "?"
       with urlopen(URL) as u:
           output = u.read().decode('utf-8')
           if output:
               if TEXT in output:
                   return "SUCCESS at %s" % t
    
       print('%s not found in %s at %s' % (TEXT, URL, t))
       return "FAILURE"
  2. Navigate to EventBridge and create a Rule. Define the scheduled pattern with a fixed rate of 1 minute. Use the default event bus. As a target choose the lambda from previous step.
  3. In order to verify the set-up go to CloudWatch Metrics and choose All metrics, then type name of your function in and choose Lambda by function name. Then display Invocations. Then switch to Graphed metrics and change period to 1 minute.
  4. Create a Dashboard in CloudWatch. Select Line type, configure it to show the lambda function and choose which metrics you want to show for example invocations and duration of execution

Lab files


Acknowledgements

Tags: