Developing WordPress and MySQL with public and private subnet  through Terraform

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"
  }
}
}
No alt text provided for this image

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"
  }
}
}
No alt text provided for this image

4.Creating Internet gateway in our own VPC.

resource "aws_internet_gateway" "Rittikgateway" {
  vpc_id = aws_vpc.myvpc.id


  tags = {
    Name = "Rittik_gateway"
  }
}
}

No alt text provided for this image

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
}
No alt text provided for this image

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


No alt text provided for this image

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

}
No alt text provided for this image

8. Now our terraform code is complete.To initialize and launch the infrastructure we use the following commands.

terraform init

terraform validate

terraform apply
No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

9.Final Output:

We can connect to the WordPress instance by using the assigned Public IP.

No alt text provided for this image

10. In the end we use the following command to destroy the whole infrastructure.

terraform destroy
No alt text provided for this image

Thanks for the Reading.

要查看或添加评论,请登录

Rittik Gupta的更多文章

  • Neural Network and its use case

    Neural Network and its use case

    Neural Network: A neural network is a system designed to act like a human brain. It’s pretty simple but prevalent in…

  • Jenkins and its use case

    Jenkins and its use case

    Jenkins: Jenkins is a free and open source automation server. It helps automate the parts of software development…

  • AMAZON SQS USE CASE

    AMAZON SQS USE CASE

    AMAZON SQS: Amazon Simple Queue Service(SQS) is a managed message queue service offered by Amazon Web Services (AWS)…

  • Kubernetes and its use cases

    Kubernetes and its use cases

    Kubernetes: Kubernetes is an open source platform for deploying and managing containers. It provides a container…

  • How to configure webserver and python interpreter on the top of Docker

    How to configure webserver and python interpreter on the top of Docker

    Docker: Docker provides the ability to package and run an application in a loosely isolated environment called a…

  • AWS Command Line Interface

    AWS Command Line Interface

    The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and…

    2 条评论
  • How IBM uses Big Data

    How IBM uses Big Data

    Big Data: Big Data describes the large volume of data in a structured and unstructured manner. The data belongs to a…

  • How Disney+Hotstar uses AWS Cloud

    How Disney+Hotstar uses AWS Cloud

    Amazon Web Services: Amazon Web services is one the most popular cloud service provider in the world. Services of…

  • How You Tube uses Artificial Intelligence And Machine Learning

    How You Tube uses Artificial Intelligence And Machine Learning

    ARTIFICIAL INTELLIGENCE: Artificial intelligence (AI) refers to the simulation of human intelligence in machines that…

  • Deploy the WordPress Application on Kubernetes and RDS services of AWS using Terraform

    Deploy the WordPress Application on Kubernetes and RDS services of AWS using Terraform

    Amazon Relational Database Service (RDS): Amazon Relational Database Service (Amazon RDS) makes it easy to set up…

社区洞察

其他会员也浏览了