chrisluiz16

Untitled

May 12th, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.33 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. ######################   Output.tf code  ###################
  208.  
  209. [ec2-user@ip-172-31-35-236 s3-versioning]$ cat output.tf
  210. output "public_ip_zomato" {
  211.   value = aws_eip.ip.public_ip
  212. }
  213.  
  214. output "http_url_zomato" {
  215.   value = "http://${aws_instance.dev.public_dns}"
  216. }
  217.  
  218. output "AMI_ID_latest" {
  219.   value = data.aws_ami.shopping.id
  220. }
  221.  
  222. output "frontend_URL" {
  223. value = "http://${var.domain_prefix}.${var.domain_name}"
  224. }
  225.  
  226. output "public_ip_template" {
  227.   value = aws_instance.shopping.public_ip
  228. }
  229.  
  230.  
  231. ############################# Terraform Output ############################
  232.  
  233. [ec2-user@ip-172-31-35-236 s3-versioning]$ terraform output
  234. AMI_ID_latest = "ami-03a12834ecdf42438"
  235. frontend_URL = "http://blog.chrisich.fun"
  236. http_url_zomato = "http://ec2-13-200-54-5.ap-south-1.compute.amazonaws.com"
  237. public_ip_template = "43.205.233.33"
  238. public_ip_zomato = "13.200.54.5"
  239. [ec2-user@ip-172-31-35-236 s3-versioning]$
  240.  
  241.  
  242.  
  243. ############################## Terraform STATE show ############################
  244.  
  245. [ec2-user@ip-172-31-35-236 s3-versioning]$ terraform state show data.aws_ami.shopping
  246. # data.aws_ami.shopping:
  247. data "aws_ami" "shopping" {
  248.     architecture          = "x86_64"
  249.     arn                   = "arn:aws:ec2:ap-south-1::image/ami-03a12834ecdf42438"
  250.     block_device_mappings = [
  251.         {
  252.             device_name  = "/dev/xvda"
  253.             ebs          = {
  254.                 "delete_on_termination" = "true"
  255.                 "encrypted"             = "false"
  256.                 "iops"                  = "3000"
  257.                 "snapshot_id"           = "snap-09d60775e858703f1"
  258.                 "throughput"            = "125"
  259.                 "volume_size"           = "8"
  260.                 "volume_type"           = "gp3"
  261.             }
  262.             no_device    = ""
  263.             virtual_name = ""
  264.         },
  265.     ]
  266.     boot_mode             = "uefi-preferred"
  267.     creation_date         = "2023-05-12T11:06:09.000Z"
  268.     description           = "shopping-app-version-3"
  269.     ena_support           = true
  270.     hypervisor            = "xen"
  271.     id                    = "ami-03a12834ecdf42438"
  272.     image_id              = "ami-03a12834ecdf42438"
  273.     image_location        = "945577359662/shopping-app-version-3"
  274.     image_type            = "machine"
  275.     imds_support          = "v2.0"
  276.     include_deprecated    = false
  277.     most_recent           = true
  278.     name                  = "shopping-app-version-3"
  279.     owner_id              = "945577359662"
  280.     owners                = [
  281.         "self",
  282.     ]
  283.     platform_details      = "Linux/UNIX"
  284.     product_codes         = []
  285.     public                = false
  286.     root_device_name      = "/dev/xvda"
  287.     root_device_type      = "ebs"
  288.     root_snapshot_id      = "snap-09d60775e858703f1"
  289.     sriov_net_support     = "simple"
  290.     state                 = "available"
  291.     state_reason          = {
  292.         "code"    = "UNSET"
  293.         "message" = "UNSET"
  294.     }
  295.     tags                  = {
  296.         "Env"     = "Dev"
  297.         "Name"    = "shopping-app-version-3"
  298.         "Project" = "Zomato"
  299.     }
  300.     usage_operation       = "RunInstances"
  301.     virtualization_type   = "hvm"
  302.  
  303.     filter {
  304.         name   = "name"
  305.         values = [
  306.             "shopping-app-version-*",
  307.         ]
  308.     }
  309.     filter {
  310.         name   = "tag:Env"
  311.         values = [
  312.             "Dev",
  313.         ]
  314.     }
  315.     filter {
  316.         name   = "tag:Project"
  317.         values = [
  318.             "Zomato",
  319.         ]
  320.     }
  321. }
  322.  
  323.  
Advertisement
Add Comment
Please, Sign In to add comment