fbpx

How to Build Application and MySQL RDS using Terraform

Here is a basic example of how you can use Terraform to create an EC2 instance with Apache and an RDS MySQL database. Keep in mind that this is just a starting point and you will likely need to customize it to fit your specific needs.

First, you will need to define the provider for AWS:

				
					provider "aws" {
  access_key = "ACCESS_KEY"
  secret_key = "SECRET_KEY"
  region     = "REGION"
}

				
			

Next, you can define the EC2 instance resource:

				
					resource "aws_instance" "example" {
  ami           = "AMI_ID"
  instance_type = "t2.micro"
  key_name      = "KEY_NAME"

  security_groups = [
    "SECURITY_GROUP_NAME"
  ]

  user_data = <<-EOF
              #!/bin/bash
              yum update -y
              yum install -y httpd
              service httpd start
              chkconfig httpd on
              echo "Hello, World!" > /var/www/html/index.html
              EOF
}

				
			

Next, you can define the EC2 instance resource:a

This will create an EC2 instance using the specified AMI, with Apache installed and running. The user_data script will run when the instance is launched, installing and starting Apache, and creating a simple “Hello, World!” index page.

To create an RDS MySQL database, you can use the following resource definition:

				
					resource "aws_db_instance" "example" {
  allocated_storage    = 10
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t2.micro"
  name                 = "DB_NAME"
  username             = "DB_USERNAME"
  password             = "DB_PASSWORD"
  parameter_group_name = "default.mysql5.7"
  skip_final_snapshot  = true
  vpc_security_group_ids = [
    "SECURITY_GROUP_ID"
  ]
}

				
			

This will create an RDS MySQL database with the specified parameters.

You will also need to create an output to display the public IP address of the EC2 instance:

				
					output "instance_ip" {
  value = "${aws_instance.example.public_ip}"
}

				
			

With these resources defined, you can use Terraform to create the EC2 instance and RDS MySQL database by running the following commands:

				
					terraform init
terraform apply

				
			

This will create the EC2 instance and RDS MySQL database, and display the public IP address of the EC2 instance. You can then visit this IP address in a web browser to see the “Hello, World!” page.

I hope this helps! Let me know if you have any questions or need further assistance.

See the Full Code:

				
					provider "aws" {
  access_key = "ACCESS_KEY"
  secret_key = "SECRET_KEY"
  region     = "REGION"
}

resource "aws_instance" "example" {
  ami           = "AMI_ID"
  instance_type = "t2.micro"
  key_name      = "KEY_NAME"

  security_groups = [
    "SECURITY_GROUP_NAME"
  ]

  user_data = <<-EOF
              #!/bin/bash
              yum update -y
              yum install -y httpd
              service httpd start
              chkconfig httpd on
              echo "Hello, World!" > /var/www/html/index.html
              EOF
}

resource "aws_db_instance" "example" {
  allocated_storage    = 10
  storage_type         = "gp2"
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t2.micro"
  name                 = "DB_NAME"
  username             = "DB_USERNAME"
  password             = "DB_PASSWORD"
  parameter_group_name = "default.mysql5.7"
  skip_final_snapshot  = true
  vpc_security_group_ids = [
    "SECURITY_GROUP_ID"
  ]
}

output "instance_ip" {
  value = "${aws_instance.example.public_ip}"
}

				
			

Create AWS EC2 From AWS CLI with Public IP

This article will explain creating an ec2 instance with the AWS CLI option from the command line interface. AWS CLI is an easy and fast option for creating single or multiple ec2 instances. Using AWS CLI, you can automate total AWS infrastructure build and also, and you can query the existing infrastructure.

Create EC2

				
					aws ec2 run-instances --image-id  ami-090fa75af13c156b4 --count 1 --instance-type t2.micro --key-name awscli --security-group-ids sg-8f6dc695 --subnet-id subnet-88e3ea86
				
			

Create EC2 with Public IP

				
					aws ec2 run-instances --image-id  ami-090fa75af13c156b4 --count 5 --instance-type t2.micro --key-name awscli --security-group-ids sg-8f6dc695 --subnet-id subnet-88e3ea86 --associate-public-ip-address
				
			

List Runing EC2 instances

				
					aws ec2 describe-instances --query "Reservations[*].Instances[*].{PublicDNS:PublicDnsName,PublicIP:PublicIpAddress,Name:Tags[?Key=='Name']|[0].Value,Status:State.Name}" --output table
				
			

Essential AWS Services for Database Administrators to Learn

Why AWS?

Cloud is becoming a vital part of Database Administration because it provides various database services & Infrastructure to run Database Ecosystems instantly. AWS (Amazon Web Services) is one of the pioneers in the Cloud according to the Gartner magic quadrant. Knowing more cloud infrastructure technologies is going to give more mileage to your Administrator career. In this article, you will find some of the AWS services which Database Administrators should know as they are basic to run Database opration.

Essential AWS Services List For Database Administrator (DBA)

Network
VPCs
Subnets
Elastic IPs
Internet Gateways
Network ACLs
Route Tables
Security Groups
Private Subnets
Public Subnets
AWS Direct Connect

Virtual Machine 
EC2
AWS Work Space

Storage
EBS
EFS
S3

Database as Services (RDS)
MySQL / MariaDB
PostgreSQL
Oracle
Micrsoft SQL Server
AWS Aurora PostgreSQL/MySQL

Database Managed Services
AWS Dynamo DB
AWS Elasticsearch 
Amazon DocumentDB

Messaging & Event Base Processing 
Apache Kafka (Amazon MSK)

 

Warehousing/ OLAP /Analytics Stagging DB
AWS Redshift

 

Monitoring 
Cloud watch
Amazon Grafana
Amazon Prometheus

 

Email Service
Amazon Simple Notification Service

Security 
IAM
Secrets Manager

Database Task Automation
AWS Batch
AWS Lambda
Cloud Formation

Command-line interface (CLI) to Manage AWS Services
AWSCLI

Migration 
Database Migration Service

Budget 
AWS Cost Explorer
AWS Budgets

Some other Services & Combination worth of Exploring

Bastion Host For DBA
MongoDB running on EC2
ELK (Elastic Search , LogStach, Kibana) running on EC2
Tunnels for Non stranded ports for Database Connections for more security
pg_pool or pg_Bouncer for PostgreSQL Databases

Stay Tuned For Latest Database, Cloud & Technology Trends

Read More >>

How to create AWS RDS PostgreSQL Database

How to create AWS rds PostgreSQL Database

 

In this article, we are going to talk about detailed steps to create an AWS Postgres database instance on AWS cloud. Amazon RDS has two options for PostgreSQL. The first one is the normal AWS Postgres RDS database and Another one is aurora PostgreSQL. AWS Postgres gives you a lot of performance gain and HA options comparing with the normal data center (DC) database. You can get AWS Postgres with few steps. I have mentioned all possible details in the post about AWS PostgreSQL. If you need any AWS PostgreSQL pricing details you can open the AWS calculator portal and can get details about estimated the cost.

In case if you have any question let me know my comments. Let’s start creating an AWS PostgreSQL RDS database instance.

 

 

1# Login to AWS console and Search for ‘RDS‘.

 

 

#2. Click on create on Database

 

 

#3. Select PostgreSQL .

 

 

 

#4.  Select the database template. In my case, I have selected ‘Free tier’ you can select ‘Production’ or ‘Dev/Test‘ according to your requirement. Production and Dev/test will not be free from the day one it will be chargeable.

 

 

#5. Give some meaningful database name and Master database username with password.

 

 

#6. I have selected the free database classes which come with the free tier.

 

 

#7. Based on your database storage needs to select the ‘Allocated space’.

 

 

#8. If your database is business-critical you can choose a Multi-Availability Zone for database high availability.

 

 

#9. Chose the default VPC. If you are configuring it for Production or development chose the appropriate VPC using the dropdown button.

 

 

[su_box title=”IMP Note for Additional connectivity configuration” box_color=”#fe2227″ title_color=”#101112″]In case if you like to access the database from your laptop/Desktop. Select the option ‘Publicly Accessible‘ to yes.[/su_box]

 

 

 

#10. Chose the option according to your application and the AWS ecosystem setup.

 

 

#11.  To enable  Cloudwatch logs or backup you can configure ‘Additional configuration‘.

 

 

#12. Click on ‘Create Database

 

 

#13.  It will take a few minutes to create database.

 

 

#14.  You can view the database credentials after clicking on ‘View credentials details‘ .

 

 

 

#15. You will see this message once your database is ready for action.

 

 

Please comment if you have any doubt. Like our Facebook page

 

Read more

AWS MySQL RDS Database Creation using AWS CLI
How to Create MySQL Database with AWS RDS
How to connect to AWS MySQL / MariaDB RDS or EC2 database from MySQL WorkBench

How to Become Oracle apps DBA?
What does DBA mean?
150 Oracle DBA Interview Questions
Top 5 Future Technologies for Database Administrators (DBA) to learn in 2020
Top 5 Software Technology Trends in 2020 List