umma.dev

AWS: Automation and Integration

Getting Started with Amazon EventBridge Events

Create an AWS Lambda Function

  • Author from scratch
  • Name: <function-name>
  • Runtime: Python 3.12
  • Execution role: create a new one or choose an existing one
  • Click Create function
  • Replace the existing code:
import json

def lambda_handler(event, context):
    # Print the event as a single line JSON string
    print("Received event: " + json.dumps(event, separators=(',', ':')))
    return {
        'statusCode': 200,
        'body': json.dumps('Event processed.')
    }
  • Click Deploy

Create the Amazon EventBridge Rule

  • In EventBridge, click on Rules (on the left)
  • Click Create rule
  • Define rule details
    • Name: <event-name>
    • Description (optional)
  • Event bus: keep as default
  • Rule type
    • Select a rule with an event pattern
    • Click Next
  • Build the event pattern
    • Under event source choose AWS events or EventBridge partner events
    • Explore the sample event
    • Event source: AWS services
    • AWS service: EC2
    • Event type: select EC2 Instance State - change notification
    • Event type spec 1: choose any state to capture all state transitions
    • Event type spec 2: choose any instance to capture state changes for all EC2 instances
    • Click Next
  • Select targets
    • Under target 1, select AWS service
    • Select a target: Lambda function
    • Click Next
  • Review and create
    • Click Create rule

Testing the Setup

  • Go to EC2 and launch an instance
  • Name: <ec2-name>
  • Instance type: t2.micro
  • Key pair: proceed without a key pair
  • Leave the rest as default
  • Click Launch instance
  • Navigate to the Lambda function
  • Go to the Monitor tab and View CloudWatch logs
  • Stop the instance
  • Check the Lambda logs again
  • Start the instance up again
  • Verify Lambda logs
  • Terminate the instance
  • Final verification of logs

Automated File Processing with S3 Event Notifications and Lambda function

Creating the Lambda Function

  • Name: <func-name>
  • Runtime: Python 3.x
  • Execution role: choose an existing one or create a new one
  • Click Create function

Deploying the Lambda Function Code

  • Download zip file and upload it

Configuring S3 Event Notification

  • Create an S3 bucket in the N.Virginia region
  • Click on properties
  • Scroll down to Event notifications and click Create event notification
  • In General configuration
    • Event name: <event-name>
    • Prefix: input/
    • Suffix: .csv
  • Event types: put -Destination: Lambda function

Testing the Configuration

  • Create an input S3 bucket
  • Download the CSV file
  • Upload it into the input folder
  • Go back to Objects and find output folder, view the contents

Creating an Amazon EventBridge Scheduled Rule

  • *Amazon EventBridge is an event bus that allows you to create rules between AWS services
  • You can set up scheduled rules to trigger actions at regular intervals or specific dates and times

Create a Lambda Function

  • Author from scratch
  • Name: <func-name>
  • Runtime: Python 3.12
  • Execution role: choose an existing one or create a new one
  • Click Create function
  • Replace the code with:
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def lambda_handler(event, context):
    logger.info("Scheduled Event triggered")
    return {
        'statusCode': 200,
        'body': 'Success'
    }
  • Click Deploy

Create an EventBridge Scheduled Rule

  • Navigate to EventBridge and click on rules
  • Click Create rule
  • Name: <schedule-rule-name>
  • Description: <description>
  • Event source: schedule
    • Click Continue to create rule
  • Schedule pattern: a schedule that rules at regular rate, rate expression: 1 min
  • Click Next
  • Under targets choose AWS service and then select Lambda function
  • Click Review and create
  • Click Create rule

Monitor Lambda Function Logs

  • Wait for a few mins for the EventBridge rule to trigger the Lambda function
  • Go to Lambda and click on the Monitor tab and then click View CloudWatch logs
  • Look for the logs with the message Scheduled Event triggered

Creating an Amazon EventBridge RDS DB Instance Event Rule

Create an Amazon RDS Instance

  • Database creation method: standard create
  • Engine options
    • Type: MySQL
    • Version: leave as default
    • Templates: free tier
    • Settings
      • DB instance identifier: database-1
      • Master username: admin
      • Credentials management: self managed
      • Tick auto-generate password checkbox
    • Instance configuration
      • DB instance size: db.t3.micro
    • Storage
      • Storage type: General Purpose SSD (gp2)
      • Allocate storage: 20
    • Leave the rest as default and click Create Database

Create the Target Event Rule: AWS Lambda Function

  • Author from scratch
  • Name: <lambda-func-name>
  • Runtime: Python 3.12
  • Execution role: choose an existing one or create a new one
  • Click Create function
  • Replace existing code:
import json

def lambda_handler(event, context):
    print("RDS Event received:", json.dumps(event))
    return {
        'statusCode': 200,
        'body': 'Event processed successfully'
    }
  • Click Deploy

Create an Amazon EventBridge Rule for RDS Events

  • In Amazon EventBridge, click Rules (on the left)
  • Click on Create rule
  • Name: <event-rule-name>
  • Description: <description>
  • Rule type: rule with an event pattern
  • Event source: AWS services
  • Service name: RDS
  • Event type: RDS DB Instance Events
  • Click Next
  • Target for the rule
    • Type: AWS service
    • Target: Lambda function
  • Click Skip to review and create
  • Click Create rule

Test the Rule

  • Go to RDS and stop the instance temporarily
  • Go to the Lambda function, click on the Monitor tab and click on View CloudWatch logs
  • Check the RDS Event received log

Creating an Amazon EventBridge DynamoDB Event Rule

Create a DynamoDB Table

  • Table name: <table-name>
  • Primary key: ID (String)
  • Table settings: Select Default settings
  • Scroll down and click Create table
  • Ensure table status is Active

Create a target for the event rule: AWS Lambda Function

  • Author from scratch
  • Function name: <func-name>
  • Runtime: Python 3.12
  • Execution role: select an existing one or create one
  • Click Create function
  • Replace the code:
import json

def lambda_handler(event, context):
    print("DynamoDB Event received:", json.dumps(event))
    return {
        'statusCode': 200,
        'body': 'Event processed successfully'
    }

Create an Amazon EventBridge Rule for DynamoDB Events

  • In EventBridge, click rules (on the left)
  • Click Create rule
  • Name: <event-name>
  • Description: <description>
  • Rule type: rule with an event pattern
  • Event source: AWS services
  • Service Name: DynamoDB
  • Event type: all events
  • Click Next
  • Target type: AWS service
  • Under select target, choose Lambda function
  • Select Lambda function created earlier
  • Click Skip to Review and create
  • Click Create rule

Test the rule

  • Go to Amazon DynamoDB and turn on Deletion protection (in actions)
  • Now go to the Lambda function, go to the Monitor tab and click on View CloudWatch logs
  • Find the log with "deletionProtectionEnabled": true