lab95_diagram

Purpose of this lab is to create an CloudWatch Event Rule that will trigger the Lambda function on a schedule. Lambdas code will query RDS MySQL database and will backup found entries into DynamoDB table.

Services Covered

  • RDS RDS MySQL database
  • Security Group for RDS
  • IAM IAM Role
  • CloudWatch CloudWatch Events
  • lambda Lambda

Lab description

Purpose of this lab is to create an CloudWatch Event Rule that will trigger the Lambda function on a schedule. Lambdas code will query RDS MySQL database and will backup found entries into DynamoDB table. MySQL Workbench will be used for connection and querying the RDS database.

  • Creating RDS MySQL database
  • Creating Security Group for RDS
  • Creating Role in IAM
  • CloudWatch Events
  • Creating and uploading Lambda code

Lab date

27-09-2021

Prerequisites

  • AWS account
  • MySQL Workbench for testing download

Lab source

Whizlabs.com

Lab steps

  1. In EC2 start with creating a Security Group for Amazon RDS database. Allow ingress traffic for type *MySQL/Aurora* from everywhere.
  2. Create a Role in IAM, choose *Lambda* as *trusted entity*. Attach following policies:
  3. Create a table in DynamoDB, give it a name, set a partition key.
  4. Create a database in RDS. Choose *Standard create*, *MySQL* as engine, Free tier as template, create user and password. As instance type choose db.t2.micro, grant it public access and assign the earlier created Security Group, rest leave as default. Under *Additional Configurations* give your database a name, and uncheck the *Enable automated backups*, leave rest as default.
  5. When the database becomes Available you can connect to the DB instance. I used MySQL Workbench. Get the endpoint and port form Connectivity & security fill in database details. In Workbench:
  6. When connected paste the following MySQL command that will create a StudentDB with three values with three fields:
CREATE DATABASE StudentDB;

Use StudentDB;

CREATE TABLE students (

studentId INT AUTO_INCREMENT,

studentName VARCHAR(50) NOT NULL,

Course VARCHAR(55),Semester VARCHAR(50) NOT NULL,PRIMARY KEY (studentId));

INSERT INTO students(studentName, Course, Semester) VALUES ('Paul', 'MBA', 'Second');

INSERT INTO students(studentName, Course, Semester) VALUES ('John', 'IT', 'Third');

INSERT INTO students(studentName, Course, Semester) VALUES ('Sebastian', 'Medicine', 'fifth');

SELECT * FROM students;
  1. Create Lambda function, in Python and attach earlier created Role. Upload the zip file and change values for your database, user and DynamoDB in the code.
  2. Create a CloudWatch Event and choose scheduled every minute and Lambda as a trigger. This will call Lambda function and create backup into DynamoDB table.
  3. After at least one minute you should see that those first three values should be populated in table by Lambda function. You can run additional MySQL queries to add more values and they will eventually get copied to the table. To see the items you need to choose the created table then Items summary -> View items -> Run.
  4. Delete all the created resources to avoid any extra costs.

Lab files

  • lambda_function.py – Lambda function triggered by CloudWatch Event, queries the RDS database and copies values into DynamoDB table

Acknowledgements

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *