Last active
August 22, 2019 13:05
-
-
Save mansurali901/348cbb118eea0a9faece456a5c157b20 to your computer and use it in GitHub Desktop.
This script consist with State Full Web Server with Nginx using terraform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################################ | |
# Author Mansur Ul Hasan | |
# EMail : [email protected] | |
# YouTube : https://www.youtube.com/user/mansur7820/about?view_as=subscriber | |
# This script consist with State Full Web Server with Nginx using terraform | |
# This script will setup | |
# - VPC | |
# - Subnets | |
# - Route Tables | |
# - Internet Gateway | |
# - Keypairs | |
# - Setup Nginx | |
# - Download Web code & install it | |
# Clone the repo and install everything with single command | |
# terraform plan | |
# terraform apply | |
############################################################################ | |
provider "aws" { | |
region = "us-east-2" | |
} | |
variable "cidr_vpc" { | |
description = "CIDR block for the VPC" | |
default = "192.168.0.0/16" | |
} | |
variable "cidr_subnet" { | |
description = "CIDR block for the subnet" | |
default = "192.168.1.0/24" | |
} | |
variable "availability_zone" { | |
description = "availability zone to create subnet" | |
default = "us-east-2a" | |
} | |
variable "instance_type" { | |
description = "type for aws EC2 instance" | |
default = "t2.micro" | |
} | |
variable "environment_tag" { | |
description = "Environment tag" | |
default = "Production" | |
} | |
variable "public_key_path" { | |
description = "Public key path" | |
default = "/Users/krypton/.ssh/id_rsa.pub" | |
} | |
variable "private_key_path" { | |
description = "Public key path" | |
default = "/Users/krypton/.ssh/id_rsa" | |
} | |
variable "server_port" { | |
description = "The port the server will use for HTTP requests" | |
default = 8080 | |
} | |
### ++++++++++++++++++ | |
resource "aws_key_pair" "ec2key" { | |
key_name = "publicKey" | |
public_key = "${file(var.public_key_path)}" | |
} | |
#### AWS VPC Creation | |
resource "aws_vpc" "vpc" { | |
cidr_block = "${var.cidr_vpc}" | |
enable_dns_support = true | |
enable_dns_hostnames = true | |
tags = { | |
Environment = "${var.environment_tag}" | |
} | |
} | |
resource "aws_internet_gateway" "igw" { | |
vpc_id = "${aws_vpc.vpc.id}" | |
tags = { | |
Environment = "${var.environment_tag}" | |
} | |
} | |
### ++++++++++++++++++ | |
#### Subnet Creation | |
resource "aws_subnet" "subnet_public" { | |
vpc_id = "${aws_vpc.vpc.id}" | |
cidr_block = "${var.cidr_subnet}" | |
map_public_ip_on_launch = "true" | |
availability_zone = "${var.availability_zone}" | |
tags = { | |
Environment = "${var.environment_tag}" | |
} | |
} | |
### ++++++++++++++++++ | |
#### Route Table Creation | |
resource "aws_route_table" "rtb_public" { | |
vpc_id = "${aws_vpc.vpc.id}" | |
route { | |
cidr_block = "0.0.0.0/0" | |
gateway_id = "${aws_internet_gateway.igw.id}" | |
} | |
tags = { | |
Environment = "${var.environment_tag}" | |
} | |
} | |
### ++++++++++++++++++ | |
#### Route Table Association | |
resource "aws_route_table_association" "rta_subnet_public" { | |
subnet_id = "${aws_subnet.subnet_public.id}" | |
route_table_id = "${aws_route_table.rtb_public.id}" | |
} | |
### ++++++++++++++++++ | |
#### Security Group | |
resource "aws_security_group" "sg_22" { | |
name = "sg_22" | |
vpc_id = "${aws_vpc.vpc.id}" | |
ingress { | |
from_port = 22 | |
to_port = 22 | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
ingress { | |
from_port = 80 | |
to_port = 80 | |
protocol = "tcp" | |
cidr_blocks = ["0.0.0.0/0"] | |
} | |
ingress { | |
from_port = ${var.server_port} | |
to_port = ${var.server_port} | |
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 = { | |
Environment = "${var.environment_tag}" | |
} | |
} | |
### ++++++++++++++++ | |
#### AMI naming setup | |
data "aws_ami" "ubuntu" { | |
most_recent = true | |
filter { | |
name = "name" | |
values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"] | |
} | |
filter { | |
name = "virtualization-type" | |
values = ["hvm"] | |
} | |
owners = ["099720109477"] | |
} | |
## ++++++++++++++ | |
#### EC2 Launch Defination | |
resource "aws_instance" "web-1" { | |
ami = "${data.aws_ami.ubuntu.id}" | |
instance_type = "t2.micro" | |
key_name = "${aws_key_pair.ec2key.key_name}" | |
subnet_id = "${aws_subnet.subnet_public.id}" | |
vpc_security_group_ids = ["${aws_security_group.sg_22.id}"] | |
connection { | |
host = self.public_ip | |
user = "ubuntu" | |
private_key = "${file(var.private_key_path)}" | |
} | |
provisioner "remote-exec" { | |
inline = [ | |
"sudo mkdir -p /etc/nginx/sites-enabled/" | |
] | |
} | |
provisioner "file" { | |
source = "main-site.conf" | |
destination = "/tmp/main-site.conf" | |
} | |
provisioner "remote-exec" { | |
inline = [ | |
"sudo apt-get update -y", | |
"sudo apt-get install wget git curl zip unzip nginx -y", | |
"sudo wget https://colorlibvault-divilabltd.netdna-ssl.com/personal.zip", | |
"sudo unzip personal.zip", | |
"sudo mkdir -p /var/www/html", | |
"sudo mv -v personal/* /var/www/html/", | |
"sudo cp -rv /tmp/main-site.conf /etc/nginx/sites-enabled/", | |
"sudo /etc/init.d/nginx restart" | |
] | |
} | |
} | |
## ++++++++++++++ | |
output "public_ip" { | |
value = "http://${aws_instance.web-1.public_ip}:${var.server_port}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment