chrisluiz16

Untitled

May 17th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.67 KB | None | 0 0
  1. ######## Packer in one region creation #########
  2.  
  3.  
  4. [ec2-user@ip-172-31-41-59 zomato-test-mumbai]$ cat main.pkr.hcl
  5. variable "project_name" {
  6.   default = "Zomato"
  7. }
  8.  
  9. variable "project_env" {
  10.   default = "Dev"
  11. }
  12.  
  13. variable "region" {
  14.   default = "ap-south-1"
  15. }
  16.  
  17. variable "ami_id" {
  18.   default = "ami-0b08bfc6ff7069aff"
  19. }
  20.  
  21. variable "instance_type" {
  22.   default = "t2.micro"
  23. }
  24.  
  25. locals {
  26.   image-timestamp = "${formatdate("DD-MMM-YYYY-hh-mm", timestamp())}"
  27. }
  28.  
  29. locals {
  30.   ami_name = "${var.project_name}-${var.project_env}-${local.image-timestamp}"
  31. }
  32.  
  33. source "amazon-ebs" "webserver" {
  34.   ami_name      = local.ami_name
  35.   instance_type = var.instance_type
  36.   region        = var.region
  37.   source_ami     = var.ami_id
  38.   ssh_username  = "ec2-user"
  39.   tags = {
  40.     "Name"    = "${local.ami_name}",
  41.     "Project" = "${var.project_name}",
  42.     "Env"     = "${var.project_env}"
  43.   }
  44. }
  45.  
  46. build {
  47.   sources = ["sourve.amazon-ebs.webserver"]
  48.   provisioner "shell" {
  49.     script          = "userdata.sh"
  50.     execute_command = "sudo {{.Path}}"
  51.   }
  52. }
  53.  
  54.  
  55. ####Output#####
  56.  
  57. ==> Wait completed after 3 minutes 19 seconds
  58.  
  59. ==> Builds finished. The artifacts of successful builds are:
  60. --> amazon-ebs.webserver: AMIs were created:
  61. ap-south-1: ami-08d7a1a325fe6bdf1
  62.  
  63.  
  64.  
  65. ################ Testing Random ID ############
  66.  
  67. locals {
  68.   ami_name = "${var.project_name}-${var.project_env}-${uuidv4()}"
  69. }
  70.  
  71.  
  72. [ec2-user@ip-172-31-41-59 zomato-test-mumbai]$ packer console .
  73. > local.ami_name
  74. > Zomato-Dev-2701a25a-a605-41eb-b96a-2de8396de7ea
  75. > exit
  76. [ec2-user@ip-172-31-41-59 zomato-test-mumbai]$ packer console .
  77. > local.ami_name
  78. > Zomato-Dev-eb3b0051-392b-412a-a053-91be8938faf5
  79. > exit
  80.  
  81.  
  82.  
  83. #######################_________ Instance spin up in one region _________________##################
  84.  
  85. provider "aws" {
  86.   region = "ap-south-1"
  87.   default_tags {
  88.     tags = {
  89.       "Project" = var.project_name,
  90.       "Env"     = var.project_env
  91.     }
  92.   }
  93. }
  94.  
  95. variable "project_name" {
  96.   default = "Zomato"
  97. }
  98.  
  99. variable "project_env" {
  100.   default = "Dev"
  101. }
  102.  
  103.  
  104. variable "domain" {
  105.   type    = string
  106.   default = "webserver-mumbai"
  107. }
  108.  
  109.  
  110.  
  111. variable "instance_type" {
  112.   default = "t2.micro"
  113. }
  114.  
  115.  
  116. # Fetching the details from AWS
  117.  
  118. data "aws_ami" "example" {
  119.   owners      = ["self"]
  120.   most_recent = true
  121.  
  122.   filter {
  123.     name   = "name"
  124.     values = ["${var.project_name}-${var.project_env}-*"]
  125.   }
  126.   filter {
  127.     name   = "tag:Env"
  128.     values = ["${var.project_env}"]
  129.   }
  130.  
  131.   filter {
  132.     name   = "tag:Project"
  133.     values = ["${var.project_name}"]
  134.   }
  135. }
  136.  
  137. data "aws_route53_zone" "selected" {
  138.   name = "chrisich.fun"
  139. }
  140.  
  141.  
  142. ### Elastic IP allocation #####
  143.  
  144. resource "aws_eip" "web" {
  145.   instance = aws_instance.frontend.id
  146.   vpc      = true
  147. }
  148.  
  149. #### Record Creation #####
  150.  
  151. resource "aws_route53_record" "domain" {
  152.   zone_id = data.aws_route53_zone.selected.id
  153.   name    = var.domain
  154.   type    = "A"
  155.   ttl     = "5"
  156.   records = [aws_eip.web.public_ip]
  157. }
  158.  
  159.  
  160. #### SG Creattion #####
  161.  
  162. resource "aws_security_group" "frontend" {
  163.  
  164.   name_prefix = "${var.project_name}-${var.project_env}-"
  165.   description = "allows http & ssh traffic"
  166.  
  167.   ingress {
  168.  
  169.     from_port        = 80
  170.     to_port          = 80
  171.     protocol         = "tcp"
  172.     cidr_blocks      = ["0.0.0.0/0"]
  173.     ipv6_cidr_blocks = ["::/0"]
  174.   }
  175.   ingress {
  176.  
  177.     from_port        = 22
  178.     to_port          = 22
  179.     protocol         = "tcp"
  180.     cidr_blocks      = ["0.0.0.0/0"]
  181.     ipv6_cidr_blocks = ["::/0"]
  182.   }
  183.  
  184.   egress {
  185.     from_port        = 0
  186.     to_port          = 0
  187.     protocol         = "-1"
  188.     cidr_blocks      = ["0.0.0.0/0"]
  189.     ipv6_cidr_blocks = ["::/0"]
  190.   }
  191.  
  192.   tags = {
  193.  
  194.     "Name" = "${var.project_name}-${var.project_env}-fronetnd"
  195.   }
  196.   lifecycle {
  197.     create_before_destroy = true
  198.   }
  199.  
  200. }
  201.  
  202. #### Key pair creation #####
  203.  
  204. resource "aws_key_pair" "deployer" {
  205.   key_name   = "Oasis"
  206.   public_key = file("oasis.pub")
  207. }
  208.  
  209. resource "aws_instance" "frontend" {
  210.  
  211.   ami                    = data.aws_ami.example.id
  212.   instance_type          = var.instance_type
  213.   key_name               = aws_key_pair.deployer.key_name
  214.   vpc_security_group_ids = [aws_security_group.frontend.id]
  215.   tags = {
  216.  
  217.     "Name" = "${var.project_name}-${var.project_env}-fronetnd"
  218.   }
  219. }
  220.  
  221. output "URL" {
  222.   value = "http://${var.domain}.${data.aws_route53_zone.selected.name}"
  223. }
  224.  
  225.  
  226.  
  227.  
  228. ###############_______________ Multi region packer _____________#######################
  229.  
  230. variable "project_name" {
  231.   default = "Zomato"
  232. }
  233.  
  234. variable "project_env" {
  235.   default = "Dev"
  236. }
  237.  
  238. variable "region" {
  239.   type = map(string)
  240.   default = {
  241.     "region1" = "ap-south-1",
  242.     "region2" = "us-east-2"
  243.   }
  244. }
  245.  
  246. variable "ami_id" {
  247.   type = map(string)
  248.   default = {
  249.     "region1" = "ami-0b08bfc6ff7069aff"
  250.     "region2" = "ami-08333bccc35d71140"
  251.   }
  252. }
  253.  
  254. variable "instance_type" {
  255.   default = "t2.micro"
  256. }
  257.  
  258. locals {
  259.   image-timestamp = "${formatdate("DD-MMM-YYYY-hh-mm", timestamp())}"
  260. }
  261.  
  262. locals {
  263.   ami_name = "${var.project_name}-${var.project_env}-${uuidv4()}"
  264. }
  265.  
  266. source "amazon-ebs" "webserver-mumbai" {
  267.   ami_name      = local.ami_name
  268.   instance_type = var.instance_type
  269.   region        = var.region.region1
  270.   source_ami    = var.ami_id.region1
  271.   ssh_username  = "ec2-user"
  272.   tags = {
  273.     "Name"    = "${local.ami_name}",
  274.     "Project" = "${var.project_name}",
  275.     "Env"     = "${var.project_env}"
  276.   }
  277. }
  278.  
  279. source "amazon-ebs" "webserver-ohio" {
  280.   ami_name      = local.ami_name
  281.   instance_type = var.instance_type
  282.   region        = var.region.region2
  283.   source_ami    = var.ami_id.region2
  284.   ssh_username  = "ec2-user"
  285.   tags = {
  286.     "Name"    = "${local.ami_name}",
  287.     "Project" = "${var.project_name}",
  288.     "Env"     = "${var.project_env}"
  289.   }
  290. }
  291.  
  292. build {
  293.   sources = ["source.amazon-ebs.webserver-mumbai", "source.amazon-ebs.webserver-ohio"]
  294.   provisioner "shell" {
  295.     script          = "userdata.sh"
  296.     execute_command = "sudo {{.Path}}"
  297.   }
  298. }
  299.  
  300.  
  301.  
  302. ##############______ Ohio Provision __________________#########
  303.  
  304.  
  305. ec2-user@ip-172-31-41-59 zomato-test-mumbai]$ cat ../prod-zomato/main.tf
  306. provider "aws" {
  307.   region = "us-east-2"
  308.   default_tags {
  309.     tags = {
  310.       "Project" = var.project_name,
  311.       "Env"     = var.project_env
  312.     }
  313.   }
  314.  
  315. }
  316.  
  317. variable "project_name" {
  318.   default = "Zomato"
  319. }
  320.  
  321. variable "project_env" {
  322.   default = "Dev"
  323. }
  324.  
  325.  
  326. variable "domain" {
  327.   type    = string
  328.   default = "webserver-ohio"
  329. }
  330.  
  331.  
  332.  
  333. variable "instance_type" {
  334.   default = "t2.micro"
  335. }
  336.  
  337.  
  338. # Fetching the details from AWS
  339.  
  340. data "aws_ami" "example" {
  341.   owners      = ["self"]
  342.   most_recent = true
  343.  
  344.   filter {
  345.     name   = "name"
  346.     values = ["${var.project_name}-${var.project_env}-*"]
  347.   }
  348.   filter {
  349.     name   = "tag:Env"
  350.     values = ["${var.project_env}"]
  351.   }
  352.  
  353.   filter {
  354.     name   = "tag:Project"
  355.     values = ["${var.project_name}"]
  356.   }
  357. }
  358.  
  359. data "aws_route53_zone" "selected" {
  360.   name = "chrisich.fun"
  361. }
  362.  
  363.  
  364. ### Elastic IP allocation #####
  365.  
  366. resource "aws_eip" "web" {
  367.   instance = aws_instance.frontend.id
  368.   vpc      = true
  369. }
  370.  
  371. resource "aws_route53_record" "domain" {
  372.   zone_id = data.aws_route53_zone.selected.id
  373.   name    = var.domain
  374.   type    = "A"
  375.   ttl     = "5"
  376.   records = [aws_eip.web.public_ip]
  377. }
  378.  
  379.  
  380. resource "aws_security_group" "frontend" {
  381.  
  382.   name_prefix = "${var.project_name}-${var.project_env}-"
  383.   description = "allows http & ssh traffic"
  384.  
  385.   ingress {
  386.  
  387.     from_port        = 80
  388.     to_port          = 80
  389.     protocol         = "tcp"
  390.     cidr_blocks      = ["0.0.0.0/0"]
  391.     ipv6_cidr_blocks = ["::/0"]
  392.   }
  393.   ingress {
  394.  
  395.     from_port        = 22
  396.     to_port          = 22
  397.     protocol         = "tcp"
  398.     cidr_blocks      = ["0.0.0.0/0"]
  399.     ipv6_cidr_blocks = ["::/0"]
  400.   }
  401.  
  402.   egress {
  403.     from_port        = 0
  404.     to_port          = 0
  405.     protocol         = "-1"
  406.     cidr_blocks      = ["0.0.0.0/0"]
  407.     ipv6_cidr_blocks = ["::/0"]
  408.   }
  409.  
  410.   tags = {
  411.  
  412.     "Name" = "${var.project_name}-${var.project_env}-fronetnd"
  413.   }
  414.   lifecycle {
  415.     create_before_destroy = true
  416.   }
  417.  
  418. }
  419.  
  420.  
  421. resource "aws_key_pair" "deployer" {
  422.   key_name   = "Oasis"
  423.   public_key = file("oasis.pub")
  424. }
  425.  
  426. resource "aws_instance" "frontend" {
  427.  
  428.   ami                    = data.aws_ami.example.id
  429.   instance_type          = var.instance_type
  430.   key_name               = aws_key_pair.deployer.key_name
  431.   vpc_security_group_ids = [aws_security_group.frontend.id]
  432.   tags = {
  433.  
  434.     "Name" = "${var.project_name}-${var.project_env}-fronetnd"
  435.   }
  436. }
  437.  
  438. output "URL" {
  439.   value = "http://${var.domain}.${data.aws_route53_zone.selected.name}"
  440. }
  441.  
  442.  
  443. ####Output#####
  444. [ec2-user@ip-172-31-41-59 prod-zomato]$ terraform output
  445. URL = "http://webserver-ohio.chrisich.fun"
  446.  
  447.  
  448.  
Advertisement
Add Comment
Please, Sign In to add comment