Developing WordPress and MySQL with public and private subnet through Terraform
Amazon Virtual Private Cloud:
Amazon Virtual Private Cloud (Amazon VPC) lets you provision a logically isolated section of the AWS Cloud where you can launch AWS resources in a virtual network that you define. You have complete control over your virtual networking environment, including selection of your own IP address range, creation of subnets, and configuration of route tables and network gateways. You can use both IPv4 and IPv6 in your VPC for secure and easy access to resources and applications.
You can easily customize the network configuration of your Amazon VPC.
Terraform:
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.
Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.
The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.
Description of the task:
1 Write a Infrastructure as code using terraform, which automatically create a VPC.
2 In that VPC we have to create 2 subnets:
a) public subnet [ Accessible for Public World! ]
b) private subnet [ Restricted for Public World! ]
3 Create a public facing internet gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC.
4 Create a routing table for Internet gateway so that instance can connect to outside world, update and associate it with public subnet.
5 Launch an ec2 instance which has Wordpress setup already having the security group allowing port 80 so that our client can connect to our wordpress site.
Also attach the key to instance for further login into it.
6 Launch an EC2 instance which has MySQL setup already with security group allowing port 3306 in private Subnet so that our Wordpress can connect with the same.Also attach the key with the same.
Solution:
1.Creating the provider.
provider "aws" { version = "~> 2.69" region = "ap-south-1" }
2. Creating VPC.
resource "aws_vpc" "myvpc" { cidr_block = "192.168.0.0/16" instance_tenancy = "default" tags = { Name = "my_vpc" } } }
3. Creating two subnets . one will be public while the other once will be private.
resource "aws_subnet" "public" { vpc_id = aws_vpc.myvpc.id cidr_block = "192.168.0.0/24" availability_zone = "ap-south-1a" tags = { Name = "Rittik_subnet1" } } resource "aws_subnet" "private" { vpc_id = aws_vpc.myvpc.id cidr_block = "192.168.1.0/24" availability_zone = "ap-south-1b" tags = { Name = "Rittik_subnet2" } } }
4.Creating Internet gateway in our own VPC.
resource "aws_internet_gateway" "Rittikgateway" { vpc_id = aws_vpc.myvpc.id tags = { Name = "Rittik_gateway" } } }
5.Creating Routing Table,updating and associate it with public subnet.
resource "aws_route_table" "My_table" { vpc_id = aws_vpc.myvpc.id route { cidr_block = "0.0.0.0/0" gateway_id = aws_internet_gateway.Rittikgateway.id } tags = { Name = "Rittik_routetable" } } resource "aws_route_table_association" "rta_subnet_public" { subnet_id = aws_subnet.public.id route_table_id = aws_route_table.My_table.id }
6. Creating security group for WordPress instance and MySQL instance.
resource "aws_security_group" "MyWebSecurity" { name = "My_Web_Security" description = "Allow http,ssh,icmp" vpc_id = aws_vpc.myvpc.id ingress { description = "HTTP" from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { description = "SSH" from_port = 22 to_port = 22 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } ingress { description = "MYSQL" from_port = 3306 to_port = 3306 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress { from_port = 0 to_port = 0 protocol = "-1" cidr_blocks = ["0.0.0.0/0"] } tags = { Name = "MyWebServer_sg" } } }
7. Creating WordPress and MySQL instances.
For WordPress instance.
resource "aws_instance" "wordpress" { ami = "ami-000cbce3e1b899ebd" instance_type = "t2.micro" associate_public_ip_address = true subnet_id = aws_subnet.public.id vpc_security_group_ids = [aws_security_group.MyWebSecurity.id] key_name = "cloudclasskey" availability_zone = "ap-south-1a" tags = { Name = "wordpress" } }
For MySQL instance.
resource "aws_instance" "mysql" { ami = "ami-0019ac6129392a0f2" instance_type = "t2.micro" subnet_id = aws_subnet.private.id vpc_security_group_ids = [aws_security_group.MyWebSecurity.id] key_name = "cloudclasskey" availability_zone = "ap-south-1b" tags = { Name = "mysql" } }
8. Now our terraform code is complete.To initialize and launch the infrastructure we use the following commands.
terraform init terraform validate terraform apply
9.Final Output:
We can connect to the WordPress instance by using the assigned Public IP.
10. In the end we use the following command to destroy the whole infrastructure.
terraform destroy
Thanks for the Reading.