chrisluiz16

Untitled

May 12th, 2023
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.73 KB | None | 0 0
  1. # backend Configuration
  2.  
  3. provider "aws" {
  4.   region = var.region
  5.   default_tags {
  6.     tags = {
  7.       "Project" = var.project_name,
  8.       "Env"     = var.project_env
  9.  
  10.     }
  11.  
  12.   }
  13.  
  14. }
  15.  
  16. terraform {
  17.   backend "s3" {
  18.     bucket = "terraform-s3bucket-zomato"
  19.     key    = "dev/terraform.tfstate"
  20.     region = "ap-south-1"
  21.   }
  22. }
  23.  
  24.  
  25.  
  26. # Secutity group creation
  27.  
  28. resource "aws_security_group" "frontend" {
  29.   name_prefix = "${var.project_name}-${var.project_env}-"
  30.   description = "Allow ssh,http,https"
  31.  
  32.   ingress {
  33.     description      = "HTTPS"
  34.     from_port        = 443
  35.     to_port          = 443
  36.     protocol         = "tcp"
  37.     cidr_blocks      = ["0.0.0.0/0"]
  38.     ipv6_cidr_blocks = ["::/0"]
  39.   }
  40.   ingress {
  41.     description      = "HTTP"
  42.     from_port        = 80
  43.     to_port          = 80
  44.     protocol         = "tcp"
  45.     cidr_blocks      = ["0.0.0.0/0"]
  46.     ipv6_cidr_blocks = ["::/0"]
  47.   }
  48.   ingress {
  49.     description      = "SSH"
  50.     from_port        = 22
  51.     to_port          = 22
  52.     protocol         = "tcp"
  53.     cidr_blocks      = ["0.0.0.0/0"]
  54.     ipv6_cidr_blocks = ["::/0"]
  55.   }
  56.  
  57.   egress {
  58.     from_port        = 0
  59.     to_port          = 0
  60.     protocol         = "-1"
  61.     cidr_blocks      = ["0.0.0.0/0"]
  62.     ipv6_cidr_blocks = ["::/0"]
  63.   }
  64.  
  65.   tags = {
  66.     Name = "${var.project_name}-${var.project_env}"
  67.  
  68.   }
  69. }
  70.  
  71.  
  72. # Ec2-instance creation
  73.  
  74. resource "aws_instance" "shopping" {
  75.   ami                    = var.ami
  76.   instance_type          = var.instance_type
  77.   availability_zone      = var.az
  78.   key_name               = var.key
  79.   user_data              = file("userdata.sh")
  80.   vpc_security_group_ids = [aws_security_group.frontend.id]
  81.   tags = {
  82.     "Name" = "Shopping-app-template",
  83.   }
  84. }
  85.  
  86.  
  87. # Fetching the details of the latest ami
  88.  
  89.  
  90. data "aws_ami" "shopping" {
  91.   owners      = ["self"]
  92.   most_recent = true
  93.  
  94.   filter {
  95.     name   = "name"
  96.     values = ["shopping-app-version-*"]
  97.   }
  98.  
  99.   filter {
  100.     name   = "tag:Env"
  101.     values = ["Dev"]
  102.   }
  103.  
  104.   filter {
  105.     name   = "tag:Project"
  106.     values = ["Zomato"]
  107.   }
  108. }
  109.  
  110.  
  111. # Creating the Dev Instance from the latest AMI
  112.  
  113. resource "aws_instance" "dev" {
  114.   ami                    = data.aws_ami.shopping.id
  115.   instance_type          = var.instance_type
  116.   availability_zone      = var.az
  117.   key_name               = var.key
  118.   vpc_security_group_ids = [aws_security_group.frontend.id]
  119.   tags = {
  120.     "Name" = "${var.project_name}-${var.project_env}-"
  121.   }
  122.  
  123.   lifecycle {
  124.     create_before_destroy = true
  125.   }
  126. }
  127.  
  128.  
  129. # Fetching the details of the public hosted zone
  130.  
  131. data "aws_route53_zone" "domain" {
  132.   name         = var.domain_name
  133. }
  134.  
  135.  
  136. # EIP allocation
  137.  
  138. resource "aws_eip" "ip" {
  139.   instance = aws_instance.dev.id
  140.   vpc      = true
  141. }
  142.  
  143.  
  144. # Record add
  145.  
  146. resource "aws_route53_record" "www" {
  147.   zone_id = data.aws_route53_zone.domain.zone_id
  148.   name    = var.domain_prefix
  149.   type    = "A"
  150.   ttl     = "5"
  151.   records = [aws_eip.ip.public_ip]
  152. }
  153.  
  154.  
  155. ##### Variables.tf code #####
  156.  
  157. [ec2-user@ip-172-31-35-236 s3-versioning]$ cat variables.tf
  158. variable "project_name" {
  159.   type    = string
  160.   default = "Zomato"
  161. }
  162.  
  163. variable "project_env" {
  164.   type    = string
  165.   default = "Dev"
  166. }
  167.  
  168. variable "region" {
  169.   description = "Region of the aws console where we execute the code"
  170.   type        = string
  171.   default     = "ap-south-1"
  172. }
  173.  
  174. variable "domain_prefix" {
  175. type = string
  176. default = "blog"
  177. }
  178.  
  179. variable "domain_name" {
  180.   description = "Name of the domain"
  181.   type        = string
  182.   default     = "chrisich.fun"
  183. }
  184.  
  185. variable "instance_type" {
  186.   description = "instance type to launch"
  187.   type        = string
  188.   default     = "t2.micro"
  189. }
  190.  
  191. variable "ami" {
  192.   default = "ami-0b08bfc6ff7069aff"
  193. }
  194. variable "az" {
  195.   description = "which AZ"
  196.   type        = string
  197.   default     = "ap-south-1a"
  198. }
  199.  
  200. variable "key" {
  201.   type    = string
  202.   default = "mumbai-new"
  203. }
  204.  
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
  211. ###### Output.tf code ##########
  212.  
  213. [ec2-user@ip-172-31-35-236 s3-versioning]$ cat output.tf
  214. output "public_ip_zomato" {
  215.   value = aws_eip.ip.public_ip
  216. }
  217.  
  218. output "http_url_zomato" {
  219.   value = "http://${aws_instance.dev.public_dns}"
  220. }
  221.  
  222. output "AMI_ID_latest" {
  223.   value = data.aws_ami.shopping.id
  224. }
  225.  
  226. output "frontend_URL" {
  227. value = "http://${var.domain_prefix}.${var.domain_name}"
  228. }
  229.  
  230. output "public_ip_template" {
  231.   value = aws_instance.shopping.public_ip
  232. }
  233.  
  234.  
  235.  
  236.  
  237. ##### Terraform Output ########
  238.  
  239. [ec2-user@ip-172-31-35-236 s3-versioning]$ terraform output
  240. AMI_ID_latest = "ami-03a12834ecdf42438"
  241. frontend_URL = "http://blog.chrisich.fun"
  242. http_url_zomato = "http://ec2-13-200-54-5.ap-south-1.compute.amazonaws.com"
  243. public_ip_template = "43.205.233.33"
  244. public_ip_zomato = "13.200.54.5"
  245. [ec2-user@ip-172-31-35-236 s3-versioning]$
  246.  
  247.  
  248.  
  249.  
Advertisement
Add Comment
Please, Sign In to add comment