DEV Community

Cover image for ๐ŸŒ Seamless Web App Integration with RDS MySQL & Automated Deployment ๐Ÿค–
Praful Patel
Praful Patel

Posted on

๐ŸŒ Seamless Web App Integration with RDS MySQL & Automated Deployment ๐Ÿค–

๐Ÿš€ Introduction

In the ever-evolving landscape of cloud computing, efficiency and automation are the keys to success. Imagine a seamless integration between Amazon Elastic Compute Cloud (EC2) instances and Amazon Relational Database Service (RDS) using the powerful tool, Terraform. This journey embarks on an exciting exploration of how to effortlessly bring these two fundamental AWS services together, streamlining your infrastructure deployment process while minimizing manual configuration steps. Let's delve into this automation adventure step by step, unlocking the full potential of your AWS resources! ๐ŸŒ๐Ÿ› ๏ธ๐Ÿ’ผ

๐ŸŽฏ Objective

The objective of this post is to bring and spread a knowledge about #aws cloud services, how to ? and where to consume the #aws services to solve the real world business challenges

๐Ÿš€ Use Case:

Imagine seamlessly integrating your web application with an RDS MySQL database while automating the entire deployment process. In a real-time use case scenario, this means that when your web application needs to access or store data, it can do so effortlessly with the RDS MySQL database. This integration ensures your application can quickly and efficiently manage user data, transactions, or any other data-related functions without manual intervention.

Moreover, by automating the deployment process, you eliminate the need for manual configuration and setup. When your web application needs to scale, recover from failures, or adapt to changes, it can do so automatically. This ensures that your application remains responsive, available, and resilient, providing a seamless experience for both users and administrators.

This real-time use case exemplifies how combining RDS MySQL with automated deployment can streamline your web application's data management, making it more robust and adaptable to the demands of a dynamic online environment.

Challenge: Automating RDS Endpoint and MySQL Connection String

Objective: Can you automate the generation of an RDS endpoint and MySQL connection string using Terraform?

Scenario: You're managing a dynamic cloud environment, and part of your task involves setting up an RDS database. To ensure the applications running on your EC2 instances can seamlessly connect to the database, you need an automated way to retrieve the RDS endpoint and construct the MySQL connection string.

Challenge: Create a Terraform solution that automatically fetches the RDS endpoint and uses it to construct the MySQL connection string in the user_data section of an EC2 instance.

Solution: In your Terraform configuration, you can use the templatefile function to automatically generate the MySQL connection string with the RDS endpoint and other database credentials. Here's an example of how this can be achieved in

Terraform:

Image description

Image description

user_data = templatefile("user_data.tfpl", { rds_endpoint = "${aws_db_instance.rds.endpoint}", user = var.database_user, password = var.database_password, dbname = var.database_name })

This code automatically fetches the RDS endpoint (aws_db_instance.rds.endpoint) and uses it to construct the MySQL connection string in the user_data section of your EC2 instance.

With this solution, you've automated the process of setting up the MySQL connection, ensuring that your EC2 instances can seamlessly connect to the RDS database without manual intervention. ๐Ÿš€๐Ÿ”—๐Ÿค–

Tools & Technologies Covered in Our AWS Cloud Infrastructure Journey ๐Ÿ› ๏ธ๐ŸŒ

AWS Cloud โ˜๏ธ: Our foundation for this journey, the AWS Cloud, provides limitless possibilities for building and deploying applications.

VPC ๐Ÿž๏ธ: The Virtual Private Cloud gives us control over our network environment.

Subnets ๐ŸŒ: These segments of the VPC help us organize and secure resources effectively.

Internet Gateway ๐Ÿšช: The gateway to the internet, enabling external access and communication.

Route Tables ๐Ÿšฆ: These are like roadmaps for network traffic within the VPC, ensuring data flows where it should.

Security Groups ๐Ÿ”’: Acting as virtual bouncers, security groups manage inbound and outbound traffic to keep our resources safe.

EC2 Machine ๐Ÿ’ป: Elastic Compute Cloud instances are like virtual Swiss Army knives, ready to handle various computing tasks.

RDS Database - MySQL ๐Ÿ—„๏ธ: Amazon RDS provides us with a managed MySQL database to store and manage our data securely.

Mobaxterm SSH Client ๐Ÿš€: Mobaxterm gives us the keys to securely access and manage our EC2 instances using SSH.

Terraform โ›๏ธ: This Infrastructure as Code tool automates the provisioning of cloud resources with elegance.

Shell Script ๐Ÿš: Good ol' shell scripts, our trusty sidekicks for automating tasks and configurations.

๐Ÿ› ๏ธ Solution Diagram:

Streamlining Web App Integration with RDS MySQL and Terraform Automation

Image description

๐Ÿš€ GitHub Repository: terraform-aws

02-aws-rds-integration-tf

๐Ÿ“‚Description:

Amazon Elastic Compute Cloud (Amazon EC2) โš™๏ธ provides on-demand, scalable computing capacity in the Amazon Web Services (AWS) Cloud โ˜๏ธ. Using Amazon EC2 reduces hardware costs ๐Ÿ’ฐ, allowing you to develop and deploy applications faster โฉ. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security ๐Ÿ” and networking ๐ŸŒ, and manage storage ๐Ÿ“‚. You can add capacity (scale up) to handle compute-heavy tasks, such as monthly or yearly processes ๐Ÿ“…, or spikes in website traffic ๐Ÿš€. When usage decreases, you can reduce capacity (scale down) again โฌ‡๏ธ.

Features of EC2:

Instances

Amazon Machine Images (AMIs)

Instance types

Key pairs

Instance store volumes

Amazon EBS volumes

Regions, Availability Zones, Local Zones, AWS Outposts, and Wavelength Zones

Security groups

Elastic IP addresses

Tags

Virtual private clouds (VPCs)

๐Ÿ“š AWS EC2 Documentation ๐Ÿฆ

What is Amazon RDS?

Amazon Relational Database Service (Amazon RDS) is a web service that makes it easier to set up, operate, and scale a relational database in the AWS Cloud. It provides cost-efficient, resizable capacity for an industry-standard relational database and manages common database administration tasks.

DB instances

A DB instance is an isolated database environment in the AWS Cloud. The basic building block of Amazon RDS is the DB instance.

DB engines

A DB engine is the specific relational database software that runs on your DB instance. Amazon RDS currently supports the following engines:

MariaDB

Microsoft SQL Server

MySQL

Oracle

PostgreSQL

๐Ÿ“š AWS RDS Documentation ๐Ÿ˜

๐Ÿ’ก Solution:

The challenge of seamlessly integrating a web application with an RDS MySQL database and automating the deployment process demands an effective solution. This is where Terraform automation comes into play, providing a powerful toolset to address these challenges.

Using Terraform, we can define and provision the necessary infrastructure, including the RDS MySQL instance, security groups, and other components. This infrastructure is described in code, making it easy to manage, version, and replicate. By automating this process, we ensure that our RDS MySQL database and the associated resources are consistently deployed according to our specifications.

Terraform's automation capabilities further extend to the web application's deployment. We can define the deployment process as code, specifying how the application should be packaged, configured, and launched. This eliminates the need for manual intervention, reducing the risk of human errors and ensuring a more predictable and reliable deployment process.

By integrating our web application with the RDS MySQL database and automating the deployment with Terraform, we create a robust and efficient solution. This approach enables the web application to seamlessly connect with the database, allowing for data storage and retrieval. Moreover, the automation ensures that the entire process is repeatable and can be scaled as needed, making it a cost-effective and time-saving solution for real-time use cases.

In summary, Terraform automation solves the challenges of integrating a web application with an RDS MySQL database and streamlining the deployment process. It empowers us to efficiently manage our infrastructure and application deployments, ultimately resulting in a more agile and responsive system.

Implementation Steps ๐Ÿ› ๏ธ

Planning your Terraform files and structure is a crucial step in effectively managing your infrastructure as code. Here's a description of the key considerations and steps to help you plan your Terraform files and structure.

Terraform Planning:

Project Scope and Goals: Begin by clearly defining the scope and goals of your Terraform project. What infrastructure components are you managing, and what do you aim to achieve with Terraform? Having a well-defined project scope will guide your decisions throughout the planning process.

File Organization: Think about how you want to organize your Terraform files. A common approach is to create separate directories for different components or environments (e.g., "network," "app," "prod," "dev"). This makes it easier to manage and maintain your code as your project grows.

Provider Configuration: Identify the cloud providers (e.g., AWS, Azure, Google Cloud) and configure the required provider blocks in your Terraform files. Ensure you have the necessary credentials and access rights to interact with these providers.

Variables and Input Data: Define the variables that your Terraform modules will use. These can include region-specific settings, instance types, security groups, and more. You can organize variables in separate files or use variable.tf files within each module.

Outputs: Define outputs in your modules to extract information you need after provisioning infrastructure. Outputs can include IP addresses, URLs, or any other attributes that are required for application configuration.

Phase 1: Setting the Stage ๐Ÿ—๏ธ

To get started, we'll establish the groundwork for this automation project. We'll create the necessary Terraform files that define the infrastructure components, including main.tf, variables.tf, and outputs.tf. These files serve as the blueprints for our EC2 and RDS instances.

Phase 2: Preparing EC2 for Integration ๐Ÿ› ๏ธ

The first step towards complete automation is configuring the EC2 instance. We'll set up the server, ensuring it's ready to host Praful's Portfolio web application. This includes installing any required software and dependencies to support the dynamic web page.

Phase 3: RDS Database Creation ๐Ÿ—„๏ธ

The heart of this project is the AWS RDS MySQL database. We'll automate the process of creating the database, specifying the necessary parameters such as the database engine, version, and security settings. Terraform's power will shine as it orchestrates the creation of a robust and secure database environment.

Phase 4: Database-EC2 Integration ๐Ÿ“Š

This is where the magic happens. We'll automate the integration of the EC2 instance and the RDS MySQL database, ensuring they communicate seamlessly. Terraform will handle the network configurations, security groups, and database access permissions. This phase ensures that the web application can efficiently interact with the database.

Phase 5: Dynamic Web Page and Data Flow ๐Ÿ’ผ

Praful's Portfolio web application features a dynamic employee (Emp) page. With this automation, we'll allow users to input employee details, such as name and location, directly on the web page. The data entered by users will be automatically saved into the RDS MySQL database, creating a smooth data flow from the web application to the database. This phase showcases the full potential of automation in keeping data up-to-date and synchronized.

Pre-requisite:

AWS Free Tier

Web Application source code

Webserver installation script file

SSH Client

Terraform file structure:

Image description

user_data.tfpl

Image description

Write all aws services terraform configurations:

Configure VPC

Configure Subnets

Configure internet gateway

Configure Security Group

Configure Route Tables for webserver and RDS Db server

Configure EC2 Machine with userdata script

Configure RDS DB MYSQL Server

Configure AWS Provider

Image description

main.tf

Image description

variables.tf

Image description

outputs.tf

Image description

Let's do automate the infrastructure:

terraform init

Image description

terraform fmt

terraform validate

Image description

terraform plan

Image description

terraform apply -auto-approve

Image description

terraform apply complete

Image description

Let's validate in the AWS console that the services are created.

VPC created

Image description

EC2 web server is running

Image description

RDS MySQL instances created

Image description

Let's copy the IP address in the browser to access web application

Web application accessed successfully

Image description

Access emp.php page

Image description

Insert some test data into web app and validate that it is inserted successfully

Image description

Let's login to RDS MySQL database and validate from backend that data is present in database

Login to mysql through web server as jump host

Copy the RDS endpoint

Image description

RDS Endpoint:

terraform-20231025010716744900000001.c5xf4htadaog.us-east-1.rds.amazonaws.com

Connect to EC2 server

Image description

Image description

Provide MySQL connection string to login

mysql -h -u -p

Image description

Login successfully to MySQL

Image description

Verify that "empdb" database is created and exists in database

show databases;

Image description

use empdb;

Image description

Verify that "EMPLOYEE" table exists in db

Image description

Query table "Employees" to validate that data entered from web app is EXISTS in database

select * from EMPLOYEES;

Image description

Image description

Image description

Congratulations: we have done it.

Now, let's destroy all the resources from the AWS to avoid unnecessary billing.

terraform destroy -auto-approve

Image description

Image description

Conclusion: ๐ŸŒŸ

This automation project demonstrates the power of Terraform in streamlining complex infrastructure setups. By integrating Praful's Portfolio web application with an AWS RDS MySQL database, we've created a dynamic and efficient data management system. This blog post serves as your guide through this exciting journey, providing insights and expertise to help you master automation in the AWS cloud.

Get ready to explore the beauty of complete automation as we dive into the integration of EC2 and AWS RDS! ๐Ÿ”—โœจ

Happy automating! ๐Ÿš€

Let's Stay Connected:

๐ŸŒ Website: Visit my website for the latest updates and articles.

๐Ÿ’ผ LinkedIn: Connect with me on LinkedIn for professional networking and insights.

๐Ÿ“Ž GitHub: Check out my projects and repositories on GitHub.

๐ŸŽฅ YouTube: Subscribe to my YouTube channel for tech tutorials and more.

๐Ÿ“ Medium: Find my tech articles on Medium.

๐Ÿ“ฐ Dev.to: Explore my developer-focused content on Dev.to.

Let's connect and stay updated with the latest in technology and development! ๐Ÿš€๐Ÿ”—

AWS #CloudEngineering #CloudComputing #AmazonWebServices #AWSArchitecture #DevOps #CloudSolutions #CloudSecurity #InfrastructureAsCode #AWSCertification #Serverless #AWSCommunity #TechBlogs #CloudExperts #CloudMigration #CloudOps #AWSJobs #TechIndustry #CareerInTech #InnovationInCloud #devops #cloudengineerjobs #devopsjobs #azure #gcp #oci #cloudjobs

Top comments (0)