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.
- RDS MySQL database
- Security Group for RDS
- IAM Role
- CloudWatch Events
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
- AWS account
- MySQL Workbench for testing download
- In EC2 start with creating a Security Group for Amazon RDS database. Allow ingress traffic for type *MySQL/Aurora* from everywhere.
- Create a Role in IAM, choose *Lambda* as *trusted entity*. Attach following policies:
- Create a table in DynamoDB, give it a name, set a partition key.
- 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.
- 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:
- 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;
- 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.
- 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.
- 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.
- Delete all the created resources to avoid any extra costs.
- lambda_function.py – Lambda function triggered by CloudWatch Event, queries the RDS database and copies values into DynamoDB table