<aside> 🔗 All scripts can be accessed from this Github repository.

</aside>

Creating AWS EC2 instances and RDB (MySQL & PostgreSQL) can be a complex and time-consuming task, especially when done manually. However, with the help of Terraform, an open-source infrastructure as code software tool, this process can be automated and streamlined. In this blog post, we will guide you through the process of creating AWS EC2 instances with Ubuntu operating system and RDB (MySQL & PostgreSQL) using Terraform. We will also cover best practices and tips to help you get the most out of your infrastructure. Whether you are new to Terraform or an experienced user, this post will provide you with valuable insights and practical examples to help you create and manage your AWS infrastructure with ease. So, let's get started!

Directory Structure

Create new directory using mkdir command. This directory contains following files.

├── main.tf
├── mysql_db.tf
├── postgres_db.tf
├── provider.tf
├── security_group.tf
├── ubuntu-key.pem
└── userdata
    └── install_node_docker.sh

Configuring the AWS Provider in Terraform

The provider "aws" block in the Terraform script is used to configure the AWS provider. The provider is responsible for creating and managing AWS resources.

In this block, the region attribute specifies the AWS region where the resources will be created. In this case, the region is set to us-east-1.

provider "aws" {
  region = "us-east-1" # the specified region
#  access_key = "ACCESS_KEY"  replace with your AWS access key
#  secret_key = "SECRET_KEY" replace with your AWS secret key
}

Creating an AWS EC2 Instance with User Data and Security Group using Terraform

Set subnet_id value according to your AWS account. Each AWS account has different subnet values. it is also recommended to find the corresponding AMI ID for the region you are using. This code also contains one more important property key_name which contains name of key pair to use for the instance.

resource "aws_instance" "ubuntu_instance" {
  ami           = "ami-04b70fa74e45c3917" # the provided AMI ID (you should find the corresponding AMI ID for us-east-1)
  instance_type = "t2.micro" # replace with your desired instance type
  key_name      = "ubuntu-key" # the specified key name
  subnet_id     = "subnet-0e8d558d206adbb3c" # the specified subnet ID

  user_data = file("userdata/install_node_docker.sh") # reference the shell script file

  vpc_security_group_ids = [aws_security_group.ubuntu_sg.id] # reference the security group

  tags = {
    Name = "ubuntu_instance" # the specified name tag
  }
}

output "public_ip" {
  value = aws_instance.ubuntu_instance.public_ip
}

This line of code will allow users to install packages to the EC2 VM at the time of instance initialization. install_node_docker.sh file is stored in userdata directrory.

user_data = file("userdata/install_node_docker.sh") # reference the shell script file
#!/bin/bash -i

# This script updates system packages, installs Docker, Nginx, Node.js, and starts their services.

# Update system packages
sudo apt update -y

# Install Docker and Nginx
sudo apt install -y docker.io nginx

# Start and enable Docker and Nginx services
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl start nginx
sudo systemctl enable nginx

# Install Node.js using NodeSource repository
curl -fsSL <https://deb.nodesource.com/setup_20.x> | sudo bash -
sudo apt-get install -y nodejs

Subnet ID values from AWS Account.

  1. Login to AWS IAS account and search for VPC in search bar and click Subnet

Untitled