Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #######--------File, remote-exec,local-exec provisioner Task 1 -------#######
- provider "aws" {
- region = var.region
- access_key = var.access_key
- secret_key = var.secret_key
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- key_name = "mumbai-new"
- vpc_security_group_ids = ["sg-09a5a43f8400c81c4"]
- tags = {
- "Name" = "Webserver"
- "Env" = "dev"
- }
- provisioner "file" {
- source = "userdata.sh"
- destination = "/tmp/userdata.sh"
- connection {
- type = "ssh"
- user = "ec2-user"
- private_key = file("./aws.pem")
- host = self.public_ip
- }
- }
- provisioner "remote-exec" {
- inline = [
- "sudo chmod +x /tmp/userdata.sh",
- "sudo /tmp/userdata.sh"
- ]
- connection {
- type = "ssh"
- user = "ec2-user"
- private_key = file("./aws.pem")
- host = self.public_ip
- }
- }
- provisioner "local-exec" {
- command = "echo public-ip ${self.public_ip} >> ip.txt"
- }
- }
- ### Output ###
- [ec2-user@ip-172-31-32-32 new]$ cat ip.txt
- public-ip 13.127.153.252
- ### version2 upgrade ####
- [ec2-user@ip-172-31-32-32 new]$ cat userdata.sh
- #!/bin/bash
- echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
- echo "LANG=en_US.utf-8" >> /etc/environment
- echo "LC_ALL=en_US.utf-8" >> /etc/environment
- systemctl restart sshd.service
- yum install httpd php -y
- cat <<EOF > /var/www/html/index.php
- <?php
- \$output = shell_exec('echo $HOSTNAME');
- echo "<h1><center><pre>\$output</pre></center></h1>";
- echo "<h1><center>Terraform-version2</center></h1>"
- ?>
- EOF
- systemctl restart httpd.service php-fpm.service
- systemctl enable httpd.service php-fpm.service
- #### NO Change ####
- [ec2-user@ip-172-31-32-32 new]$ terraform apply -auto-approve
- aws_instance.frontend: Refreshing state... [id=i-0f1db8ca24107a41d]
- No changes. Your infrastructure matches the configuration.
- Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
- Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
- #### replace ####
- [ec2-user@ip-172-31-32-32 new]$ terraform apply -replace=aws_instance.frontend -auto-approve
- aws_instance.frontend: Refreshing state... [id=i-0f1db8ca24107a41d]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- -/+ destroy and then create replacement
- Terraform will perform the following actions:
- # aws_instance.frontend will be replaced, as requested
- aws_instance.frontend (remote-exec): Complete!
- aws_instance.frontend (remote-exec): Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
- aws_instance.frontend (remote-exec): Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
- aws_instance.frontend: Provisioning with 'local-exec'...
- aws_instance.frontend (local-exec): Executing: ["/bin/sh" "-c" "echo public-ip 35.154.37.71 >> ip.txt"]
- aws_instance.frontend: Creation complete after 56s [id=i-0edc80e5f48e6720e]
- Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
- #######-------- Terraform destroy Task 2 -------#######
- Plan: 0 to add, 0 to change, 1 to destroy.
- aws_instance.frontend: Destroying... [id=i-0edc80e5f48e6720e]
- aws_instance.frontend: Still destroying... [id=i-0edc80e5f48e6720e, 10s elapsed]
- aws_instance.frontend: Still destroying... [id=i-0edc80e5f48e6720e, 20s elapsed]
- aws_instance.frontend: Destruction complete after 29s
- Destroy complete! Resources: 1 destroyed.
- [ec2-user@ip-172-31-32-32 new]$
- #######-------- Terraform null_resource Task 3 -------#######
- ### Modifying userdata to version1 ####
- [ec2-user@ip-172-31-32-32 new]$ cat userdata.sh
- #!/bin/bash
- echo "ClientAliveInterval 60" >> /etc/ssh/sshd_config
- echo "LANG=en_US.utf-8" >> /etc/environment
- echo "LC_ALL=en_US.utf-8" >> /etc/environment
- systemctl restart sshd.service
- yum install httpd php -y
- cat <<EOF > /var/www/html/index.php
- <?php
- \$output = shell_exec('echo $HOSTNAME');
- echo "<h1><center><pre>\$output</pre></center></h1>";
- echo "<h1><center>Terraform-version1</center></h1>"
- ?>
- EOF
- systemctl restart httpd.service php-fpm.service
- systemctl enable httpd.service php-fpm.service
- ### Main.tf code ####
- [ec2-user@ip-172-31-32-32 new]$ cat main.tf
- provider "aws" {
- region = var.region
- access_key = var.access_key
- secret_key = var.secret_key
- }
- resource "aws_instance" "frontend" {
- ami = "ami-0b08bfc6ff7069aff"
- instance_type = "t2.micro"
- key_name = "mumbai-new"
- vpc_security_group_ids = ["sg-09a5a43f8400c81c4"]
- tags = {
- "Name" = "Webserver"
- "Env" = "dev"
- }
- }
- resource "null_resource" "cluster" {
- triggers = {
- file_changes = md5(file("userdata.sh"))
- }
- provisioner "file" {
- source = "userdata.sh"
- destination = "/tmp/userdata.sh"
- connection {
- type = "ssh"
- user = "ec2-user"
- private_key = file("./aws.pem")
- host = aws_instance.frontend.public_ip
- }
- }
- provisioner "remote-exec" {
- inline = [
- "sudo chmod +x /tmp/userdata.sh",
- "sudo /tmp/userdata.sh"
- ]
- connection {
- type = "ssh"
- user = "ec2-user"
- private_key = file("./aws.pem")
- host = aws_instance.frontend.public_ip
- }
- }
- provisioner "local-exec" {
- command = "echo public-ip ${aws_instance.frontend.public_ip} >> version2_ip.txt"
- }
- depends_on = [aws_instance.frontend]
- }
- ### Output ###
- version1
- null_resource.cluster (remote-exec): Complete!
- null_resource.cluster (remote-exec): Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
- null_resource.cluster (remote-exec): Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
- null_resource.cluster: Provisioning with 'local-exec'...
- null_resource.cluster (local-exec): Executing: ["/bin/sh" "-c" "echo public-ip 3.110.62.92 >> version2_ip.txt"]
- null_resource.cluster: Creation complete after 28s [id=5919292716054902593]
- Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
- ##version2
- Plan: 1 to add, 0 to change, 1 to destroy.
- null_resource.cluster: Destroying... [id=5919292716054902593]
- null_resource.cluster: Destruction complete after 0s
- null_resource.cluster: Creating...
- null_resource.cluster: Provisioning with 'file'...
- null_resource.cluster: Provisioning with 'remote-exec'...
- null_resource.cluster (remote-exec): Connecting to remote host via SSH...
- null_resource.cluster (remote-exec): Host: 3.110.62.92
- null_resource.cluster (remote-exec): User: ec2-user
- null_resource.cluster (remote-exec): Password: false
- null_resource.cluster (remote-exec): Private key: true
- null_resource.cluster (remote-exec): Certificate: false
- null_resource.cluster (remote-exec): SSH Agent: true
- null_resource.cluster (remote-exec): Checking Host Key: false
- null_resource.cluster (remote-exec): Target Platform: unix
- null_resource.cluster (remote-exec): Connected!
- null_resource.cluster (remote-exec): Last metadata expiration check: 0:12:59 ago on Mon 15 May 2023 11:44:07 AM UTC.
- null_resource.cluster (remote-exec): Package httpd-2.4.56-1.amzn2023.x86_64 is already installed.
- null_resource.cluster (remote-exec): Package php8.1-8.1.16-1.amzn2023.0.1.x86_64 is already installed.
- null_resource.cluster (remote-exec): Dependencies resolved.
- null_resource.cluster (remote-exec): Nothing to do.
- null_resource.cluster (remote-exec): Complete!
- null_resource.cluster: Provisioning with 'local-exec'...
- null_resource.cluster (local-exec): Executing: ["/bin/sh" "-c" "echo public-ip 3.110.62.92 >> version2_ip.txt"]
- null_resource.cluster: Creation complete after 3s [id=7056250333644496576]
- Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
- [ec2-user@ip-172-31-32-32 new]$ curl http://3.110.62.92 && echo
- <h1><center><pre>ip-172-31-44-195.ap-south-1.compute.internal
- </pre></center></h1><h1><center>Terraform-version2</center></h1>
- #######-------- Terraform destroy Task 4 -------#######
- [ec2-user@ip-172-31-32-32 new]$ terraform destroy -auto-approve
- aws_instance.frontend: Refreshing state... [id=i-08c7881cc574a03ec]
- null_resource.cluster: Refreshing state... [id=7056250333644496576]
- Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
- - destroy
- Terraform will perform the following actions:
- Plan: 0 to add, 0 to change, 2 to destroy.
- null_resource.cluster: Destroying... [id=7056250333644496576]
- null_resource.cluster: Destruction complete after 0s
- aws_instance.frontend: Destroying... [id=i-08c7881cc574a03ec]
- aws_instance.frontend: Still destroying... [id=i-08c7881cc574a03ec, 10s elapsed]
- aws_instance.frontend: Still destroying... [id=i-08c7881cc574a03ec, 20s elapsed]
- aws_instance.frontend: Destruction complete after 30s
- Destroy complete! Resources: 2 destroyed.
- #######-------- Terraform destroy Task 5 -------#######
- provider "aws" {
- region = "us-east-1"
- access_key = "AKIA5YKGNPEXMXI3AEUM"
- secret_key = "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"
- default_tags {
- tags = {
- "Env" = var.project_env,
- "Project" = var.project_name
- }
- }
- }
- # Should you choose to have a nat gateway to allow internet connectivity on private subnet
- variable "nat" {
- type = bool
- default = false
- }
- # Fetaching the details of the availablity zones
- data "aws_availability_zones" "az" {
- state = "available"
- }
- locals {
- az = length(data.aws_availability_zones.az.names)
- }
- # creating VPC
- resource "aws_vpc" "main" {
- cidr_block = var.vpc_cidr
- instance_tenancy = "default"
- enable_dns_support = true
- enable_dns_hostnames = true
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- #Creating IGW
- resource "aws_internet_gateway" "igw" {
- vpc_id = aws_vpc.main.id
- tags = {
- Name = "${var.project_name}-${var.project_env}"
- }
- }
- # Creating subnets
- resource "aws_subnet" "public" {
- count = local.az
- vpc_id = aws_vpc.main.id
- cidr_block = cidrsubnet(var.vpc_cidr, 4, count.index)
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = true
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-public-${count.index + 1}"
- }
- }
- resource "aws_subnet" "private" {
- count = local.az
- vpc_id = aws_vpc.main.id
- cidr_block = cidrsubnet(var.vpc_cidr, 4, count.index + 6)
- availability_zone = data.aws_availability_zones.az.names["${count.index}"]
- map_public_ip_on_launch = false
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private-${count.index + 1}"
- }
- }
- # creating elastic IP for NAT G/W if var =true
- resource "aws_eip" "ip_true" {
- count = 1
- vpc = true
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-Nat"
- }
- }
- resource "aws_nat_gateway" "nat_true" {
- count = 1
- allocation_id = aws_eip.ip_true[0].id
- subnet_id = aws_subnet.public[0].id
- tags = {
- "Name" = "${var.project_name}-${var.project_env}"
- }
- depends_on = [aws_internet_gateway.igw]
- }
- # Creating Route table
- resource "aws_route_table" "public" {
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- gateway_id = aws_internet_gateway.igw.id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-public"
- }
- }
- # Creating route table if var = true
- resource "aws_route_table" "private_true" {
- count = 1
- vpc_id = aws_vpc.main.id
- route {
- cidr_block = "0.0.0.0/0"
- nat_gateway_id = aws_nat_gateway.nat_true[0].id
- }
- tags = {
- "Name" = "${var.project_name}-${var.project_env}-private"
- }
- }
- #Association with the route table
- resource "aws_route_table_association" "public" {
- count = local.az
- subnet_id = aws_subnet.public["${count.index}"].id
- route_table_id = aws_route_table.public.id
- }
- resource "aws_route_table_association" "nat_enabled" {
- count = local.az
- subnet_id = aws_subnet.private["${count.index}"].id
- route_table_id = aws_route_table.private_true[0].id
- }
- ### Output ####
- Plan: 18 to add, 0 to change, 0 to destroy - mumbai region
- Plan: 30 to add, 0 to change, 0 to destroy - us-east-region
- #######-------- EXTRA TASK -------#######
- [ec2-user@ip-172-31-32-32 new]$ echo $TF_LOG
- TRACE
- [ec2-user@ip-172-31-32-32 new]$ echo $TF_LOG_PATH
- /home/ec2-user/new/terraform.log
- [ec2-user@ip-172-31-32-32 new]$ tail terraform.log
- var.project_env - *terraform.NodeRootVariable
- var.project_name - *terraform.NodeRootVariable
- var.region - *terraform.NodeRootVariable
- var.secret_key - *terraform.NodeRootVariable
- var.vpc_cidr - *terraform.NodeRootVariable
- ------
- 2023-05-15T13:00:13.071Z [INFO] backend/local: plan operation completed
- 2023-05-15T13:00:13.071Z [TRACE] LoadSchemas: retrieving schema for provider type "registry.terraform.io/hashicorp/aws"
- 2023-05-15T13:00:13.132Z [TRACE] statemgr.Filesystem: removing lock metadata file .terraform.tfstate.lock.info
- 2023-05-15T13:00:13.132Z [TRACE] statemgr.Filesystem: unlocking terraform.tfstate using fcntl flock
- [ec2-user@ip-172-31-32-32 new]$ date
- Mon May 15 01:08:20 PM UTC 2023
Advertisement
Add Comment
Please, Sign In to add comment