Today's Topic is on AWS RDS, DynamoDB & AWS Lambda
Tasks
Read about AWS RDS, DynamoDB and AWS lambda and write a post on LinkedIn with an example in your own words.
Amazon Relational Database Service:
It provides affordable relational databases in the cloud, that are easy to use.
Is a collection of managed services that makes it simple to set up, operate, and scale databases in the cloud.
Amazon Relational Database Service (RDS) is a managed SQL database service provided by Amazon Web Services (AWS). Amazon RDS supports an array of database engines to store and organize data. It also helps in relational database management tasks like data migration, backup, recovery and patching.
Amazon RDS facilitates the deployment and maintenance of relational databases in the cloud. Cloud administrators use Amazon RDS to set up, operate, manage, and scale relational instances of cloud databases. Amazon RDS itself is not a database; It is a service used to manage relational databases.
Amazon RDS currently supports the following engines:
MySQL (preferred)
PostgreSQL
Oracle
MariaDB
AuroraDB
Amazon DynamoDB:
It is a fully managed, serverless, key-value NoSQL database designed to run high-performance applications at any scale. DynamoDB offers built-in security, continuous backups, automated multi-region replication, in-memory caching, and data import and export tools.
Amazon DynamoDB is a fast and flexible NoSQL database service for all applications that require consistent single-digit millisecond latency at any scale.
It is a fully managed database that supports both document and key-value data models.
Its flexible data model and performance make it a great fit for mobile, web, gaming, ad tech, IOT, and many other applications.
It is stored in SSD storage.
It is spread across three geographical data centers.
AWS Lambda:
Serverless computing helps abstract the infrastructure in the cloud.
As a result, it reduces costs and can help to increase innovation.
For Example:
You can look at serverless as a car.
You start the car to travel to your destination.
Once you are at the destination, you stop it.
You only used fuel when driving.
Lambda works in the same way. The use is on-demand when you run the code.
You are part of a team responsible for migrating the database of an existing e-commerce platform to Amazon RDS. The goal is to improve scalability, performance, and manageability. The current setup uses a self-managed MySQL database on an on-premises server. π
What needs to be done:
Set up and configure a MySQL database on AWS RDS, ensuring optimal performance.
Establish a connection between the RDS instance and your EC2 environment
Configuring Amazon RDS:
Database Instance Creation: Navigate to the AWS Management Console, select Amazon RDS, and create a new database instance. Choose MySQL as the engine.
Instance Settings: Define your instance details, including DB credentials, storage, and network configurations.
Connecting EC2 to RDS:
Go to your RDS database. Scroll to the "Connected compute resources" section --> click on 'Actions' -->' Set up EC2connection' and add your ec2 instance( ex: webserver1-ec2).
Then go to EC2 instance and install
# Install mysql client
sudo apt update
sudo apt install mysql-client
# Connecting your ec2 instance to the rds database.
mysql -u admin -h <endpoint> -P 3306 -p
Deploy a scalable web application. The application consists of a MySQL database managed by Amazon RDS and a flask-based web application that automatically scales based on demand using an Auto Scaling group and an Elastic Load Balancer.
Step 1: Creation of a webserver
1)Created launch template.
2)Created Auto scaling group with desired capacity -1, min capacity-1, max.capacity-2.
3)created an Application load balancer with a target group(webserver-target)
Now, an instance for our flask app is created with high availabilty and scalability.
Step 2:Dockerization of Web Application
Cloning the Repository: Clone this repository which contains the Dockerfile.
Image Creation: Execute
docker build
to create an image from your Dockerfile. This encapsulates your app and its dependencies.
Step 3:
- In your instance terminal, run the following commands. Navigate to the RDS homepage, select the database you have already created (named "database-1"), copy the endpoint, and paste it here.
# Install mysql client
sudo apt update
sudo apt install mysql-client
# Connecting your ec2 instance to the rds database.
mysql -u admin -h <endpoint> -P 3306 -p
Then, Enter the password. Now we can see the MySQL monitor and run the following commands for creating a new database.
create database database; #select db1 use database; #create the table CREATE TABLE messages ( id INT AUTO_INCREMENT PRIMARY KEY, message TEXT );
#First create database
Create Database db;
Use db;
#Create Table
Create Table message(
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT
);
Step4: Deployment
Running the docker container: Run the docker container using the following command.
sudo docker run -d -p 5000:5000 -e MYSQL_HOST=<endpoint> -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin123 -e MYSQL_DB=db two-tier-flask-app
Step 5: Check the result
Check the URL on your browser
http://<ec2-public-ip>:5000
.Now, you can see that our two-tier-flask app is running successfully.
4) Scenario:
You're an AWS expert managing a budget-friendly project with EC2 instances. To save money, you're using AWS Lambda to automatically start and stop instances when they're not needed during non-business hours. π
What needs to be done:
- Create an AWS Lambda function that will start/stop instances based on their instance tag.
Note
: Learn about Python boto3 to solve this task.
What needs to be done:
Create an AWS Lambda function that will start/stop instances based on their instance tag.
Solution:
For this task, I am demonstrating how to stop an instance using AWS Lambda. For reference, please watch this link. LAMBDA
Step 1: Create an ec2 instance.
Step2: Create IAM policy
Use the JSON policy editor to create an IAM policy. Paste the following JSON policy document into the policy editor. (refer to the above link).
Step3: Create IAM role for Lambda
Important: When you attach a permissions policy to Lambda, make sure that you choose the IAM policy.
Step 4: Create a Lambda function
Open the Lambda console, and then choose the Create function.
On the Code tab, under Code source, paste the following code for stopping instances (refer to LAMBDA).
Replace us-west-1 with the AWS Region that your instances are in. Replace InstanceIds with the IDs of the instances that you want to stop and start.
Now, go back to your instances page and check the status of the mentioned instance; yes, it's stopped. You can also start/stop instances automatically by using a Lambda function with EventBridge rules.
DAY 4 Completed, Challenge taken from GitHub Repo.
Take the same, and you will enjoy this journey π
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Happy Learning!
~kritika :)