chrisluiz16

Untitled

May 15th, 2023
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 13.70 KB | None | 0 0
  1. #######--------File, remote-exec,local-exec provisioner  Task 1 -------#######
  2.  
  3.  
  4.  
  5. provider "aws" {
  6.   region = var.region
  7.   access_key = var.access_key
  8.   secret_key = var.secret_key
  9. }
  10.  
  11. resource "aws_instance" "frontend" {
  12.   ami                    = "ami-0b08bfc6ff7069aff"
  13.   instance_type          = "t2.micro"
  14.   key_name               = "mumbai-new"
  15.   vpc_security_group_ids = ["sg-09a5a43f8400c81c4"]
  16.   tags = {
  17.     "Name" = "Webserver"
  18.     "Env"  = "dev"
  19.   }
  20.  
  21.   provisioner "file" {
  22.     source      = "userdata.sh"
  23.     destination = "/tmp/userdata.sh"
  24.     connection {
  25.       type        = "ssh"
  26.       user        = "ec2-user"
  27.       private_key = file("./aws.pem")
  28.       host        = self.public_ip
  29.     }
  30.   }
  31.  
  32.  
  33.   provisioner "remote-exec" {
  34.     inline = [
  35.       "sudo chmod +x /tmp/userdata.sh",
  36.       "sudo /tmp/userdata.sh"
  37.     ]
  38.     connection {
  39.       type        = "ssh"
  40.       user        = "ec2-user"
  41.       private_key = file("./aws.pem")
  42.       host        = self.public_ip
  43.     }
  44.   }
  45.  
  46.   provisioner "local-exec" {
  47.     command = "echo public-ip ${self.public_ip} >> ip.txt"
  48.   }
  49.  
  50. }
  51.  
  52. ### Output ###
  53.  
  54. [ec2-user@ip-172-31-32-32 new]$ cat ip.txt
  55. public-ip 13.127.153.252
  56.  
  57.  
  58. ### version2 upgrade ####
  59. [ec2-user@ip-172-31-32-32 new]$ cat userdata.sh
  60. #!/bin/bash
  61.  
  62. echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
  63. echo "LANG=en_US.utf-8" >> /etc/environment
  64. echo "LC_ALL=en_US.utf-8" >> /etc/environment
  65. systemctl restart sshd.service
  66.  
  67. yum install httpd php -y
  68.  
  69. cat <<EOF > /var/www/html/index.php
  70. <?php
  71. \$output = shell_exec('echo $HOSTNAME');
  72. echo "<h1><center><pre>\$output</pre></center></h1>";
  73. echo "<h1><center>Terraform-version2</center></h1>"
  74. ?>
  75.  
  76.  
  77. EOF
  78.  
  79. systemctl restart httpd.service php-fpm.service
  80. systemctl enable httpd.service php-fpm.service
  81.  
  82.  
  83. #### NO Change ####
  84.  
  85. [ec2-user@ip-172-31-32-32 new]$ terraform apply -auto-approve
  86. aws_instance.frontend: Refreshing state... [id=i-0f1db8ca24107a41d]
  87.  
  88. No changes. Your infrastructure matches the configuration.
  89.  
  90. Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
  91.  
  92. Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
  93.  
  94.  
  95. #### replace ####
  96.  
  97. [ec2-user@ip-172-31-32-32 new]$ terraform apply -replace=aws_instance.frontend -auto-approve
  98. aws_instance.frontend: Refreshing state... [id=i-0f1db8ca24107a41d]
  99.  
  100. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  101. -/+ destroy and then create replacement
  102.  
  103. Terraform will perform the following actions:
  104.  
  105.   # aws_instance.frontend will be replaced, as requested
  106.  
  107. aws_instance.frontend (remote-exec): Complete!
  108. aws_instance.frontend (remote-exec): Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
  109. aws_instance.frontend (remote-exec): Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
  110. aws_instance.frontend: Provisioning with 'local-exec'...
  111. aws_instance.frontend (local-exec): Executing: ["/bin/sh" "-c" "echo public-ip 35.154.37.71 >> ip.txt"]
  112. aws_instance.frontend: Creation complete after 56s [id=i-0edc80e5f48e6720e]
  113.  
  114. Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
  115.  
  116.  
  117.  
  118. #######-------- Terraform destroy  Task 2 -------#######
  119.  
  120. Plan: 0 to add, 0 to change, 1 to destroy.
  121. aws_instance.frontend: Destroying... [id=i-0edc80e5f48e6720e]
  122. aws_instance.frontend: Still destroying... [id=i-0edc80e5f48e6720e, 10s elapsed]
  123. aws_instance.frontend: Still destroying... [id=i-0edc80e5f48e6720e, 20s elapsed]
  124. aws_instance.frontend: Destruction complete after 29s
  125. Destroy complete! Resources: 1 destroyed.
  126. [ec2-user@ip-172-31-32-32 new]$
  127.  
  128.  
  129.  
  130. #######-------- Terraform null_resource  Task 3 -------#######
  131.  
  132.  
  133. ### Modifying userdata to version1 ####
  134.  
  135. [ec2-user@ip-172-31-32-32 new]$ cat userdata.sh
  136. #!/bin/bash
  137. echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
  138. echo "LANG=en_US.utf-8" >> /etc/environment
  139. echo "LC_ALL=en_US.utf-8" >> /etc/environment
  140. systemctl restart sshd.service
  141. yum install httpd php -y
  142. cat <<EOF > /var/www/html/index.php
  143. <?php
  144. \$output = shell_exec('echo $HOSTNAME');
  145. echo "<h1><center><pre>\$output</pre></center></h1>";
  146. echo "<h1><center>Terraform-version1</center></h1>"
  147. ?>
  148. EOF
  149. systemctl restart httpd.service php-fpm.service
  150. systemctl enable httpd.service php-fpm.service
  151.  
  152.  
  153.  
  154. ### Main.tf code ####
  155.  
  156.  
  157. [ec2-user@ip-172-31-32-32 new]$ cat main.tf
  158. provider "aws" {
  159.   region = var.region
  160.   access_key = var.access_key
  161.   secret_key = var.secret_key
  162. }
  163.  
  164. resource "aws_instance" "frontend" {
  165.   ami                    = "ami-0b08bfc6ff7069aff"
  166.   instance_type          = "t2.micro"
  167.   key_name               = "mumbai-new"
  168.   vpc_security_group_ids = ["sg-09a5a43f8400c81c4"]
  169.   tags = {
  170.     "Name" = "Webserver"
  171.     "Env"  = "dev"
  172.   }
  173.  
  174. }
  175.  
  176.  
  177. resource "null_resource" "cluster" {
  178.   triggers = {
  179.     file_changes = md5(file("userdata.sh"))
  180.   }
  181.  
  182.   provisioner "file" {
  183.     source      = "userdata.sh"
  184.     destination = "/tmp/userdata.sh"
  185.     connection {
  186.       type        = "ssh"
  187.       user        = "ec2-user"
  188.       private_key = file("./aws.pem")
  189.       host        = aws_instance.frontend.public_ip
  190.     }
  191.   }
  192.  
  193.  
  194.   provisioner "remote-exec" {
  195.     inline = [
  196.       "sudo chmod +x /tmp/userdata.sh",
  197.       "sudo /tmp/userdata.sh"
  198.     ]
  199.     connection {
  200.       type        = "ssh"
  201.       user        = "ec2-user"
  202.       private_key = file("./aws.pem")
  203.       host        = aws_instance.frontend.public_ip
  204.     }
  205.   }
  206.  
  207.   provisioner "local-exec" {
  208.     command = "echo public-ip ${aws_instance.frontend.public_ip} >> version2_ip.txt"
  209.   }
  210.  
  211. depends_on = [aws_instance.frontend]
  212.  
  213. }
  214.  
  215.  
  216. ### Output ###
  217.  
  218. version1
  219. null_resource.cluster (remote-exec): Complete!
  220. null_resource.cluster (remote-exec): Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
  221. null_resource.cluster (remote-exec): Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
  222. null_resource.cluster: Provisioning with 'local-exec'...
  223. null_resource.cluster (local-exec): Executing: ["/bin/sh" "-c" "echo public-ip 3.110.62.92 >> version2_ip.txt"]
  224. null_resource.cluster: Creation complete after 28s [id=5919292716054902593]
  225. Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
  226.  
  227.  
  228. ##version2
  229.  
  230. Plan: 1 to add, 0 to change, 1 to destroy.
  231. null_resource.cluster: Destroying... [id=5919292716054902593]
  232. null_resource.cluster: Destruction complete after 0s
  233. null_resource.cluster: Creating...
  234. null_resource.cluster: Provisioning with 'file'...
  235. null_resource.cluster: Provisioning with 'remote-exec'...
  236. null_resource.cluster (remote-exec): Connecting to remote host via SSH...
  237. null_resource.cluster (remote-exec):   Host: 3.110.62.92
  238. null_resource.cluster (remote-exec):   User: ec2-user
  239. null_resource.cluster (remote-exec):   Password: false
  240. null_resource.cluster (remote-exec):   Private key: true
  241. null_resource.cluster (remote-exec):   Certificate: false
  242. null_resource.cluster (remote-exec):   SSH Agent: true
  243. null_resource.cluster (remote-exec):   Checking Host Key: false
  244. null_resource.cluster (remote-exec):   Target Platform: unix
  245. null_resource.cluster (remote-exec): Connected!
  246. null_resource.cluster (remote-exec): Last metadata expiration check: 0:12:59 ago on Mon 15 May 2023 11:44:07 AM UTC.
  247. null_resource.cluster (remote-exec): Package httpd-2.4.56-1.amzn2023.x86_64 is already installed.
  248. null_resource.cluster (remote-exec): Package php8.1-8.1.16-1.amzn2023.0.1.x86_64 is already installed.
  249. null_resource.cluster (remote-exec): Dependencies resolved.
  250. null_resource.cluster (remote-exec): Nothing to do.
  251. null_resource.cluster (remote-exec): Complete!
  252. null_resource.cluster: Provisioning with 'local-exec'...
  253. null_resource.cluster (local-exec): Executing: ["/bin/sh" "-c" "echo public-ip 3.110.62.92 >> version2_ip.txt"]
  254. null_resource.cluster: Creation complete after 3s [id=7056250333644496576]
  255.  
  256. Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
  257.  
  258.  
  259. [ec2-user@ip-172-31-32-32 new]$ curl http://3.110.62.92 && echo
  260. <h1><center><pre>ip-172-31-44-195.ap-south-1.compute.internal
  261. </pre></center></h1><h1><center>Terraform-version2</center></h1>
  262.  
  263.  
  264. #######-------- Terraform destroy  Task 4 -------#######
  265.  
  266. [ec2-user@ip-172-31-32-32 new]$ terraform destroy -auto-approve
  267. aws_instance.frontend: Refreshing state... [id=i-08c7881cc574a03ec]
  268. null_resource.cluster: Refreshing state... [id=7056250333644496576]
  269.  
  270. Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  271.   - destroy
  272.  
  273. Terraform will perform the following actions:
  274. Plan: 0 to add, 0 to change, 2 to destroy.
  275. null_resource.cluster: Destroying... [id=7056250333644496576]
  276. null_resource.cluster: Destruction complete after 0s
  277. aws_instance.frontend: Destroying... [id=i-08c7881cc574a03ec]
  278. aws_instance.frontend: Still destroying... [id=i-08c7881cc574a03ec, 10s elapsed]
  279. aws_instance.frontend: Still destroying... [id=i-08c7881cc574a03ec, 20s elapsed]
  280. aws_instance.frontend: Destruction complete after 30s
  281.  
  282. Destroy complete! Resources: 2 destroyed.
  283.  
  284.  
  285. #######-------- Terraform destroy  Task 5 -------#######
  286.  
  287. provider "aws" {
  288.   region     = "us-east-1"
  289.   access_key = "AKIA5YKGNPEXMXI3AEUM"
  290.   secret_key = "kMNKrzW97p9gbqU180ruCM0PykHAmzjtXLo7wkro"
  291.   default_tags {
  292.     tags = {
  293.  
  294.       "Env"     = var.project_env,
  295.       "Project" = var.project_name
  296.     }
  297.   }
  298. }
  299.  
  300.  
  301. # Should you choose to have a nat gateway to allow internet connectivity on private subnet
  302.  
  303. variable "nat" {
  304.   type    = bool
  305.   default = false
  306.  
  307. }
  308.  
  309.  
  310. # Fetaching the details of the availablity zones
  311.  
  312. data "aws_availability_zones" "az" {
  313.   state = "available"
  314. }
  315.  
  316. locals {
  317.   az = length(data.aws_availability_zones.az.names)
  318.  
  319. }
  320.  
  321. # creating VPC
  322.  
  323. resource "aws_vpc" "main" {
  324.   cidr_block           = var.vpc_cidr
  325.   instance_tenancy     = "default"
  326.   enable_dns_support   = true
  327.   enable_dns_hostnames = true
  328.   tags = {
  329.     Name = "${var.project_name}-${var.project_env}"
  330.   }
  331. }
  332.  
  333. #Creating IGW
  334.  
  335. resource "aws_internet_gateway" "igw" {
  336.   vpc_id = aws_vpc.main.id
  337.   tags = {
  338.     Name = "${var.project_name}-${var.project_env}"
  339.   }
  340. }
  341. # Creating subnets
  342.  
  343. resource "aws_subnet" "public" {
  344.   count                   = local.az
  345.   vpc_id                  = aws_vpc.main.id
  346.   cidr_block              = cidrsubnet(var.vpc_cidr, 4, count.index)
  347.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  348.   map_public_ip_on_launch = true
  349.   tags = {
  350.     "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
  351.  
  352.   }
  353. }
  354.  
  355. resource "aws_subnet" "private" {
  356.   count                   = local.az
  357.   vpc_id                  = aws_vpc.main.id
  358.   cidr_block              = cidrsubnet(var.vpc_cidr, 4, count.index + 6)
  359.   availability_zone       = data.aws_availability_zones.az.names["${count.index}"]
  360.   map_public_ip_on_launch = false
  361.   tags = {
  362.     "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
  363.  
  364.   }
  365. }
  366.  
  367. # creating elastic IP for NAT G/W if var =true
  368.  
  369. resource "aws_eip" "ip_true" {
  370.  
  371.   count = 1
  372.   vpc   = true
  373.   tags = {
  374.     "Name" = "${var.project_name}-${var.project_env}-Nat"
  375.   }
  376. }
  377.  
  378.  
  379.  
  380. resource "aws_nat_gateway" "nat_true" {
  381.   count         = 1
  382.   allocation_id = aws_eip.ip_true[0].id
  383.   subnet_id     = aws_subnet.public[0].id
  384.   tags = {
  385.     "Name" = "${var.project_name}-${var.project_env}"
  386.   }
  387.  
  388.   depends_on = [aws_internet_gateway.igw]
  389.  
  390. }
  391.  
  392.  
  393. # Creating Route table
  394.  
  395. resource "aws_route_table" "public" {
  396.   vpc_id = aws_vpc.main.id
  397.  
  398.   route {
  399.     cidr_block = "0.0.0.0/0"
  400.     gateway_id = aws_internet_gateway.igw.id
  401.   }
  402.   tags = {
  403.     "Name" = "${var.project_name}-${var.project_env}-public"
  404.   }
  405. }
  406.  
  407. # Creating route table if var = true
  408.  
  409. resource "aws_route_table" "private_true" {
  410.   count  = 1
  411.   vpc_id = aws_vpc.main.id
  412.  
  413.   route {
  414.     cidr_block     = "0.0.0.0/0"
  415.     nat_gateway_id = aws_nat_gateway.nat_true[0].id
  416.   }
  417.   tags = {
  418.     "Name" = "${var.project_name}-${var.project_env}-private"
  419.   }
  420. }
  421.  
  422.  
  423.  
  424.  
  425.  
  426. #Association with the route table
  427.  
  428. resource "aws_route_table_association" "public" {
  429.   count          = local.az
  430.   subnet_id      = aws_subnet.public["${count.index}"].id
  431.   route_table_id = aws_route_table.public.id
  432. }
  433.  
  434. resource "aws_route_table_association" "nat_enabled" {
  435.   count          = local.az
  436.   subnet_id      = aws_subnet.private["${count.index}"].id
  437.   route_table_id = aws_route_table.private_true[0].id
  438. }
  439.  
  440.  
  441. ### Output ####
  442.  
  443. Plan: 18 to add, 0 to change, 0 to destroy - mumbai region
  444. Plan: 30 to add, 0 to change, 0 to destroy - us-east-region
  445.  
  446.  
  447. #######-------- EXTRA TASK -------#######
  448.  
  449. [ec2-user@ip-172-31-32-32 new]$ echo $TF_LOG
  450. TRACE
  451. [ec2-user@ip-172-31-32-32 new]$ echo $TF_LOG_PATH
  452. /home/ec2-user/new/terraform.log
  453. [ec2-user@ip-172-31-32-32 new]$ tail terraform.log
  454.   var.project_env - *terraform.NodeRootVariable
  455.   var.project_name - *terraform.NodeRootVariable
  456.   var.region - *terraform.NodeRootVariable
  457.   var.secret_key - *terraform.NodeRootVariable
  458.   var.vpc_cidr - *terraform.NodeRootVariable
  459.   ------
  460. 2023-05-15T13:00:13.071Z [INFO]  backend/local: plan operation completed
  461. 2023-05-15T13:00:13.071Z [TRACE] LoadSchemas: retrieving schema for provider type "registry.terraform.io/hashicorp/aws"
  462. 2023-05-15T13:00:13.132Z [TRACE] statemgr.Filesystem: removing lock metadata file .terraform.tfstate.lock.info
  463. 2023-05-15T13:00:13.132Z [TRACE] statemgr.Filesystem: unlocking terraform.tfstate using fcntl flock
  464. [ec2-user@ip-172-31-32-32 new]$ date
  465. Mon May 15 01:08:20 PM UTC 2023
  466.  
  467.  
  468.  
  469.  
  470.  
Advertisement
Add Comment
Please, Sign In to add comment