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}"
}