chrisluiz16

Untitled

May 16th, 2023
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.53 KB | None | 0 0
  1. ########################_______ With variables ________#################
  2.  
  3.  
  4. [ec2-user@ip-172-31-41-59 zomato-mumbai]$ cat main.tf
  5. provider "aws" {
  6. region = "ap-south-1"
  7. }
  8.  
  9.  
  10. ### Variables ###
  11.  
  12. variable "project_name" {
  13. default = "Zomato"
  14. }
  15.  
  16. variable "project_env" {
  17. default = "Dev"
  18. }
  19.  
  20.  
  21. variable "ami_mumbai" {
  22. default = "ami-0b08bfc6ff7069aff"
  23. }
  24.  
  25. variable "ami_ohio" {
  26. default = "ami-08333bccc35d71140"
  27. }
  28.  
  29.  
  30. # Key pair generation
  31.  
  32. resource "aws_key_pair" "deployer" {
  33.   key_name   = "Zomato"
  34.   public_key = file("zomato.pub")
  35. }
  36.  
  37. # Security group generation
  38.  
  39. resource "aws_security_group" "traffic" {
  40.   name_prefix        = "${var.project_name} - ${var.project_env} - "
  41.   description = "Allow ssh and http"
  42.  
  43.   ingress {
  44.     description      = "HTTP"
  45.     from_port        = 80
  46.     to_port          = 80
  47.     protocol         = "tcp"
  48.     cidr_blocks      = ["0.0.0.0/0"]
  49.     ipv6_cidr_blocks = ["::/0"]
  50.   }
  51.  
  52.  
  53. ingress {
  54.     description      = "SSH"
  55.     from_port        = 22
  56.     to_port          = 22
  57.     protocol         = "tcp"
  58.     cidr_blocks      = ["0.0.0.0/0"]
  59.     ipv6_cidr_blocks = ["::/0"]
  60.   }
  61.  
  62.   egress {
  63.     from_port        = 0
  64.     to_port          = 0
  65.     protocol         = "-1"
  66.     cidr_blocks      = ["0.0.0.0/0"]
  67.     ipv6_cidr_blocks = ["::/0"]
  68.   }
  69.  
  70.   tags = {
  71.     Name = "${var.project_name} - ${var.project_env}"
  72.   }
  73. }
  74.  
  75.  
  76.  
  77. resource "aws_instance"  "frontend" {
  78.  
  79.    ami = var.ami_mumbai
  80.    instance_type = "t2.micro"
  81.    key_name =  aws_key_pair.deployer.id
  82.    vpc_security_group_ids = [ aws_security_group.traffic.id ]
  83.    tags = {
  84.        "Name" = "${var.project_name} - ${var.project_env}"
  85.    }
  86.  
  87. }
  88.  
  89.  
  90. Creayed the
  91.  
  92. ### Output ####
  93. Plan: 3 to add, 0 to change, 0 to destroy.
  94.  
  95.  
  96.  
  97.  
  98. ########################_______ With map using data.aws_reion ________#################
  99.  
  100. data "aws_region" "current" {}
  101.  
  102. provider "aws" {
  103. region = "us-east-2"
  104. }
  105.  
  106.  
  107. ### Variables ###
  108.  
  109. variable "project_name" {
  110. default = "Zomato"
  111. }
  112.  
  113. variable "project_env" {
  114. default = "Dev"
  115. }
  116.  
  117.  
  118. variable "ami_id" {
  119. type = map
  120.  
  121. default = {
  122.  
  123. "ap-south-1" = "ami-0b08bfc6ff7069aff",
  124. "us-east-2" = "ami-08333bccc35d71140"
  125. }
  126. }
  127.  
  128.  
  129. # Key pair generation
  130.  
  131. resource "aws_key_pair" "deployer" {
  132.   key_name   = "Zomato"
  133.   public_key = file("zomato.pub")
  134. }
  135.  
  136. # Security group generation
  137.  
  138. resource "aws_security_group" "traffic" {
  139.   name_prefix        = "${var.project_name}-${var.project_env} - "
  140.   description = "Allow ssh and http"
  141.  
  142.   ingress {
  143.     description      = "HTTP"
  144.     from_port        = 80
  145.     to_port          = 80
  146.     protocol         = "tcp"
  147.     cidr_blocks      = ["0.0.0.0/0"]
  148.     ipv6_cidr_blocks = ["::/0"]
  149.   }
  150.  
  151.  
  152. ingress {
  153.     description      = "SSH"
  154.     from_port        = 22
  155.     to_port          = 22
  156.     protocol         = "tcp"
  157.     cidr_blocks      = ["0.0.0.0/0"]
  158.     ipv6_cidr_blocks = ["::/0"]
  159.   }
  160.  
  161.   egress {
  162.     from_port        = 0
  163.     to_port          = 0
  164.     protocol         = "-1"
  165.     cidr_blocks      = ["0.0.0.0/0"]
  166.     ipv6_cidr_blocks = ["::/0"]
  167.   }
  168.  
  169.   tags = {
  170.     Name = "${var.project_name}-${var.project_env}"
  171.   }
  172. }
  173.  
  174.  
  175.  
  176. resource "aws_instance"  "frontend" {
  177.  
  178.    ami = var.ami_id["${data.aws_region.current.name}"]
  179.    instance_type = "t2.micro"
  180.    key_name =  aws_key_pair.deployer.id
  181.    vpc_security_group_ids = [ aws_security_group.traffic.id ]
  182.    tags = {
  183.        "Name" = "${var.project_name} - ${var.project_env}"
  184.    }
  185.  
  186. }
  187.  
  188. ###Output####
  189.  
  190. Plan: 3 to add, 0 to change, 0 to destroy.
  191.  
  192.  
  193. ########################_______ With map using variables ________#################
  194.  
  195. provider "aws" {
  196. region = "us-east-2"
  197. }
  198.  
  199.  
  200. ### Variables ###
  201.  
  202. variable "project_name" {
  203. default = "Zomato"
  204. }
  205.  
  206. variable "region" {
  207. default = "us-east-2"
  208.  
  209. }
  210.  
  211. variable "project_env" {
  212. default = "Dev"
  213. }
  214.  
  215.  
  216. variable "ami_id" {
  217. type = map
  218.  
  219. default = {
  220.  
  221. "ap-south-1" = "ami-0b08bfc6ff7069aff",
  222. "us-east-2" = "ami-08333bccc35d71140"
  223. }
  224. }
  225.  
  226.  
  227. # Key pair generation
  228.  
  229. resource "aws_key_pair" "deployer" {
  230.   key_name   = "Zomato"
  231.   public_key = file("zomato.pub")
  232. }
  233.  
  234. # Security group generation
  235.  
  236. resource "aws_security_group" "traffic" {
  237.   name_prefix        = "${var.project_name}-${var.project_env} - "
  238.   description = "Allow ssh and http"
  239.  
  240.   ingress {
  241.     description      = "HTTP"
  242.     from_port        = 80
  243.     to_port          = 80
  244.     protocol         = "tcp"
  245.     cidr_blocks      = ["0.0.0.0/0"]
  246.     ipv6_cidr_blocks = ["::/0"]
  247.   }
  248.  
  249.  
  250. ingress {
  251.     description      = "SSH"
  252.     from_port        = 22
  253.     to_port          = 22
  254.     protocol         = "tcp"
  255.     cidr_blocks      = ["0.0.0.0/0"]
  256.     ipv6_cidr_blocks = ["::/0"]
  257.   }
  258.  
  259.   egress {
  260.     from_port        = 0
  261.     to_port          = 0
  262.     protocol         = "-1"
  263.     cidr_blocks      = ["0.0.0.0/0"]
  264.     ipv6_cidr_blocks = ["::/0"]
  265.   }
  266.  
  267.   tags = {
  268.     Name = "${var.project_name}-${var.project_env}"
  269.   }
  270. }
  271.  
  272.  
  273.  
  274. resource "aws_instance"  "frontend" {
  275.  
  276.    ami = var.ami_id["${var.region}"]
  277.    instance_type = "t2.micro"
  278.    key_name =  aws_key_pair.deployer.id
  279.    vpc_security_group_ids = [ aws_security_group.traffic.id ]
  280.    tags = {
  281.        "Name" = "${var.project_name} - ${var.project_env}"
  282.    }
  283.  
  284. }
  285.  
  286.  
  287. ####### Lookup ##########
  288.  
  289.  
  290. provider "aws" {
  291.  region = var.region
  292.  
  293. }
  294.  
  295. variable "region" {
  296.  
  297. default = "ap-south-1"
  298. }
  299.  
  300.  
  301. variable "classmates_exp" {
  302. description = "My classmates total IT experience"
  303. type = map
  304. default = {
  305. "Chris" = "2",
  306. "ashna" = "4",
  307. "vysakh" = "9"
  308.  
  309. }
  310. }
  311.  
  312. output "chris_exp" {
  313. value = lookup(var.classmates_exp,"Chris",0)
  314. }
  315.  
  316. output "ashna_exp" {
  317. value = lookup(var.classmates_exp,"ashna",0)
  318. }
  319.  
  320. output "vysak_exp" {
  321. value = var.classmates_exp.vysakh
  322. }
  323.  
  324. output "prince_exp" {
  325. value = lookup(var.classmates_exp,"prince",0)
  326. }
  327.  
  328. #### Output ####
  329.  
  330. Changes to Outputs:
  331.   + ashna_exp  = "4"
  332.   + chris_exp  = "2"
  333.   + prince_exp = "0"
  334.   + vysak_exp  = "9"
  335.  
  336. You can apply this plan to save these new output values to the Terraform state, without changing any real
  337. infrastructure.
  338.  
  339.  
  340.  
  341.  
  342. ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
  343.  
  344. Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if
  345. you run "terraform apply" now.
  346. [ec2-user@ip-172-31-41-59 lookup]$
  347.  
  348.  
  349. #########________Copying files using lookup_________######
  350.  
  351. provider "aws" {
  352.   region = "ap-south-1"
  353. }
  354.  
  355. variable "mime_type" {
  356.   type = map(any)
  357.   default = {
  358.     "css"   = "text/css"
  359.     "html"  = "text/html"
  360.     "ico"   = "image/vnd.microsoft.icon"
  361.     "js"    = "application/javascript"
  362.     "json"  = "application/json"
  363.     "map"   = "application/json"
  364.     "png"   = "image/png"
  365.     "svg"   = "image/svg+xml"
  366.     "txt"   = "text/plain"
  367.     "jpg"   = "image/jpeg"
  368.     "woff"  = "application/font-woff"
  369.     "woff2" = "application/font-woff2"
  370.   }
  371. }
  372.  
  373.  
  374. resource "aws_s3_object" "object" {
  375.   bucket       = "terraform-s3bucket-zomato"
  376.   for_each     = fileset("/home/ec2-user/test", "**/*.*")
  377.   key          = each.key
  378.   source       = "/home/ec2-user/test/${each.key}"
  379.   content_type = lookup(var.mime_type, element(split(".", each.key), length(split(".", each.key)) - 1))
  380. }
  381.  
  382. ###Output ###
  383.  
  384. Apply complete! Resources: 32 added, 0 changed, 0 destroyed.
  385. [ec2-user@ip-172-31-41-59 lookup]$
  386.  
Advertisement
Add Comment
Please, Sign In to add comment