Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Fetaching the details of the availablity zones
- data "aws_availability_zones" "az" {
- state = "available"
- }
- # creating VPC
- resource "aws_vpc" "main" {
- cidr_block = var.vpc_cidr
- instance_tenancy = "default"
- enable_dns_support = true
- enable_dns_hostnames = true
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- #Creating IGW
- resource "aws_internet_gateway" "igw" {
- vpc_id = aws_vpc.main.id
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- # Creating subnets
- resource "aws_subnet" "public" {
- count = 3
- vpc_id = aws_vpc.main.id
- cidr_block = cidrsubnet(var.vpc_cidr, 3, count.index)
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = true
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
- }
- }
- resource "aws_subnet" "private" {
- count = 3
- vpc_id = aws_vpc.main.id
- cidr_block = cidrsubnet(var.vpc_cidr, 3, count.index + 3)
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = false
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
- }
- }
- # creating elastic IP for NAT G/W
- resource "aws_eip" "ip" {
- vpc = true
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-"
- }
- }
- # creating NAT gateway
- resource "aws_nat_gateway" "nat" {
- allocation_id = aws_eip.ip.id
- subnet_id = aws_subnet.public[0].id
- tags = {
- "Name" = "${var.project_name}-${var.project_env}"
- }
- depends_on = [aws_internet_gateway.igw]
- }
- # Creating Route table
- resource "aws_route_table" "public" {
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- gateway_id = aws_internet_gateway.igw.id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-public"
- }
- }
- resource "aws_route_table" "private" {
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- nat_gateway_id = aws_nat_gateway.nat.id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private"
- }
- }
- #Association with the route table
- resource "aws_route_table_association" "public" {
- count = 3
- subnet_id = aws_subnet.public["${count.index}"].id
- route_table_id = aws_route_table.public.id
- }
- resource "aws_route_table_association" "private" {
- count = 3
- subnet_id = aws_subnet.private["${count.index}"].id
- route_table_id = aws_route_table.private.id
- }
- # Creating Security group
- resource "aws_security_group" "bastion" {
- name_prefix = "${var.project_name}-${var.project_env}-bastion-"
- description = "Allow ssh from my IP only"
- vpc_id = aws_vpc.main.id
- ingress {
- description = "22 from my IP"
- from_port = 22
- to_port = 22
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- Name = "${var.project_name}-${var.project_env}-bastion"
- }
- }
- resource "aws_security_group" "frontend" {
- name_prefix = "${var.project_name}-${var.project_env}-frontend-"
- description = "Allow http,https and ssh"
- vpc_id = aws_vpc.main.id
- ingress {
- description = "HTTP port"
- from_port = 80
- to_port = 80
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "HTTPS port"
- from_port = 443
- to_port = 443
- protocol = "tcp"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "22 from my IP"
- from_port = 22
- to_port = 22
- protocol = "tcp"
- security_groups = [aws_security_group.bastion.id]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- Name = "${var.project_name}-${var.project_env}-frontend"
- }
- }
- resource "aws_security_group" "backend" {
- name_prefix = "${var.project_name}-${var.project_env}-backend-"
- description = "Allow mysql and ssh port only"
- vpc_id = aws_vpc.main.id
- ingress {
- description = "22 from Bastion"
- from_port = 22
- to_port = 22
- protocol = "tcp"
- security_groups = [aws_security_group.bastion.id]
- ipv6_cidr_blocks = ["::/0"]
- }
- ingress {
- description = "MYSQL IP"
- from_port = 3306
- to_port = 3306
- protocol = "tcp"
- security_groups = [aws_security_group.frontend.id]
- ipv6_cidr_blocks = ["::/0"]
- }
- egress {
- from_port = 0
- to_port = 0
- protocol = "-1"
- cidr_blocks = ["0.0.0.0/0"]
- ipv6_cidr_blocks = ["::/0"]
- }
- tags = {
- Name = "${var.project_name}-${var.project_env}-backend"
- }
- }
- # Creating ec2-instance
- resource "aws_instance" "bastion" {
- ami = var.ami
- subnet_id = aws_subnet.public[0].id
- instance_type = var.instance_type
- key_name = "mumbai-new"
- vpc_security_group_ids = [aws_security_group.bastion.id]
- user_data = file("bastion.sh")
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-bastion"
- }
- }
- resource "aws_instance" "frontend" {
- ami = var.ami
- subnet_id = aws_subnet.public[0].id
- instance_type = var.instance_type
- key_name = "mumbai-new"
- vpc_security_group_ids = [aws_security_group.frontend.id]
- user_data = file("frontend.sh")
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-frontend"
- }
- }
- resource "aws_instance" "backend" {
- ami = var.ami
- subnet_id = aws_subnet.private[0].id
- instance_type = var.instance_type
- key_name = "mumbai-new"
- vpc_security_group_ids = [aws_security_group.backend.id]
- user_data = file("backend.sh")
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-backend"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment