Create a full stack webserver on 3 different cloud and launch wordpress on GCP and RDS service using AWS:

Create 3 different workspace and to check initial terraform workspaces use the below command:

terraform workspace list

#to create new ws, use : 'terraform workspace new [NAME]'
        

Step 1 :

Create awsprovider.tf file. (name could be anything)

provider "aws" {
  region = var.region
  shared_credentials_file = var.creds
  profile = "default"
}

resource "aws_instance" "linux-os" {

?ami??????= "ami-06dc09bb8854cbde3"
?instance_type = "t2.micro"
?key_name???= "TerraformKP"
?security_groups = ["sgtf for SSH"]
?tags = {
??Name = "HelloTerraform"
?}



}
        

Step 2 :

To make the EC2 instance a webserver, use script.sh

resource "null_resource"  "null1"  {
?connection {
? ? type? ? ?= "ssh" 
? ? user? ? ?= "ec2-user" 
? ? private_key = file("C:/Users/RASHNI/Downloads/TerraformKP.pem" )
? ? host? ? ?= aws_instance.linux-os.public_ip
? }

?provisioner "remote-exec"  {
? ? inline = [
? ? ? "sudo yum install http -y" ,
? ? ? "sudo yum install php -y" ,
? ? ? "sudo systemctl start httpd" ,
? ? ? "sudo systemctl start php" ,
      "cd /var/www/html"
  }
}        

Step 3 :

Create gcpprovider.tf file. (name could be anything)

provider "google" {

credentials = file("/Users/testuser/Desktop/gpsvc.json")

project = "googleproject"
 region  = "us-central1"
 zone    = "us-central1-c"
}


resource "google_compute_instance" "apache_GCP" {
    name = "apacheserver"
    machine_type = "f1-micro"
    tags = ["http-server"]
    boot_disk {
        initialize_params {
            image = "debian-cloud/debian-9"
        }
    }
    metadata_startup_script =  file("/Users/testuser/Desktop/apache2.sh")
scheduling {
        preemptible = true
        automatic_restart = false
    }

    network_interface {
        network ="default"
        access_config {

        }
    }

}        

Step 4 :

write a basic script as below :

!/bin/bash

sudo apt-get update && sudo apt -y install apache2

echo '<!doctype html><html><body><h1>Hello if you see this than you have apache running!</h1></body></html>' | sudo tee /var/www/html/index.html        

Step 5 :

Create azureprovider.tf file. (name could be anything)

provider "azurerm" {
	  version = "~> 1.4"
	  environment = "public"
}
resource "azurerm_resource_group" "network-rg" {
	  name     = "${lower(replace(var.app_name," ","-"))}-${var.environment}-rg"
	  location = var.location
	  tags = {
	    application = var.app_name
	    environment = var.environment
	  }
	}
	resource "azurerm_virtual_network" "network-vnet" {
	  name                = "${lower(replace(var.app_name," ","-"))}-${var.environment}-vnet"
	  address_space       = [var.network-vnet-cidr]
	  resource_group_name = azurerm_resource_group.network-rg.name
	  location            = azurerm_resource_group.network-rg.location
	  tags = {
	    application = var.app_name
	    environment = var.environment
	  }
	}
	resource "azurerm_subnet" "network-subnet" {
	  name                 = "${lower(replace(var.app_name," ","-"))}-${var.environment}-subnet"
	  address_prefix       = var.network-subnet-cidr
	  virtual_network_name = azurerm_virtual_network.network-vnet.name
	  resource_group_name  = azurerm_resource_group.network-rg.name
}        

$1) Now create a variable file(azurevariable.tf) :

variable "company" {
	  type        = string
	  description = "This variable defines thecompany name used to build resources"
	}
	variable "app_name" {
	  type        = string
	  description = "This variable defines the application name used to build resources"
	}
	variable "environment" {
	  type        = string
	  description = "This variable defines the environment to be built"
	}
	variable "location" {
	  type        = string
	  description = "Azure region where the resource group will be created"
	  default     = "north europe"
    }

variable "network-vnet-cidr" {
	  type        = string
	  description = "The CIDR of the network VNET"
	}
	variable "network-subnet-cidr" {
	  type        = string
	  description = "The CIDR for the network subnet"
	}        

$2) azureuserdata.tf

sudo apt-get update
	sudo apt-get install -y apache2
	sudo systemctl start apache2
	sudo systemctl enable apache2
	echo "<h1>Azure Linux VM with Web Server</h1>" | sudo tee /var/www/html/index.html        

$3) create an azurevm.tf

resource "random_password" "web-vm-password" {
	  length           = 16
	  min_upper        = 2
	  min_lower        = 2
	  min_special      = 2
	  number           = true
	  special          = true
	  override_special = "!@#$%&"
	}
	resource "random_string" "web-vm-name" {
	  length  = 8
	  upper   = false
	  number  = false
	  lower   = true
	  special = false
	}
	resource "azurerm_network_security_group" "web-vm-nsg" {
	  depends_on=[azurerm_resource_group.network-rg]
	  name                = "web-${lower(var.environment)}-${random_string.web-vm-name.result}-nsg"
	  location            = azurerm_resource_group.network-rg.location
	  resource_group_name = azurerm_resource_group.network-rg.name

	  security_rule {
	    name                       = "AllowWEB"
	    description                = "Allow web"
	    priority                   = 100
	    direction                  = "Inbound"
	    access                     = "Allow"
	    protocol                   = "Tcp"
	    source_port_range          = "*"
	    destination_port_range     = "80"
	    source_address_prefix      = "Internet"
	    destination_address_prefix = "*"
	  }
	

	  security_rule {
	    name                       = "AllowSSH"
	    description                = "Allow SSH"
	    priority                   = 150
	    direction                  = "Inbound"
	    access                     = "Allow"
	    protocol                   = "Tcp"
	    source_port_range          = "*"
	    destination_port_range     = "22"
	    source_address_prefix      = "Internet"
	    destination_address_prefix = "*"
	  }
	  tags = {
	    environment = var.environment
	  }
	}
	
	resource "azurerm_subnet_network_security_group_association" "web-vm-nsg-association" {
	  depends_on=[azurerm_resource_group.network-rg]
	

	  subnet_id                 = azurerm_subnet.network-subnet.id
	  network_security_group_id = azurerm_network_security_group.web-vm-nsg.id
	}
	
	resource "azurerm_public_ip" "web-vm-ip" {
	  depends_on=[azurerm_resource_group.network-rg]
	

	  name                = "web-${random_string.web-vm-name.result}-ip"
	  location            = azurerm_resource_group.network-rg.location
	  resource_group_name = azurerm_resource_group.network-rg.name
	  allocation_method   = "Static"
	  
	  tags = { 
	    environment = var.environment
	  }
	}

	resource "azurerm_network_interface" "web-private-nic" {
	  depends_on=[azurerm_resource_group.network-rg]
	  name                = "web-${random_string.web-vm-name.result}-nic"
	  location            = azurerm_resource_group.network-rg.location
	  resource_group_name = azurerm_resource_group.network-rg.name
	  
	  ip_configuration {
	    name                          = "internal"
	    subnet_id                     = azurerm_subnet.network-subnet.id
	    private_ip_address_allocation = "Dynamic"
	    public_ip_address_id          = azurerm_public_ip.web-vm-ip.id
	  }
	
	  tags = { 
	    environment = var.environment
	  }
	}

	resource "azurerm_virtual_machine" "web-vm" {
	  depends_on=[azurerm_network_interface.web-private-nic]
	  location              = azurerm_resource_group.network-rg.location
	  resource_group_name   = azurerm_resource_group.network-rg.name
	  name                  = "web-${random_string.web-vm-name.result}-vm"
	  network_interface_ids = [azurerm_network_interface.web-private-nic.id]
	  vm_size               = var.web_vm_size
	  license_type          = var.web_license_type
	  delete_os_disk_on_termination    = var.web_delete_os_disk_on_termination
	  delete_data_disks_on_termination = var.web_delete_data_disks_on_termination
	
	  storage_image_reference {
	    id        = lookup(var.web_vm_image, "id", null)
	    offer     = lookup(var.web_vm_image, "offer", null)
	    publisher = lookup(var.web_vm_image, "publisher", null)
	    sku       = lookup(var.web_vm_image, "sku", null)
	    version   = lookup(var.web_vm_image, "version", null)
	  }
	
	  storage_os_disk {
	    name              = "web-${random_string.web-vm-name.result}-disk"
	    caching           = "ReadWrite"
	    create_option     = "FromImage"
	    managed_disk_type = "Standard_LRS"
	  }
	
	  os_profile {
	    computer_name  = "web-${random_string.web-vm-name.result}-vm"
	    admin_username = var.web_admin_username
	    admin_password = random_password.web-vm-password.result
	    custom_data    = file("azure-user-data.sh")
	  }
	
	  os_profile_linux_config {
	    disable_password_authentication = false
	  }
	
	  tags = {
	    environment = var.environment
	  }
}

output "web_vm_name" {
	  description = "Virtual Machine name"
	  value       = azurerm_virtual_machine.web-vm.name
	}
	
	output "web_vm_ip_address" {
	  description = "Virtual Machine name IP Address"
	  value       = azurerm_public_ip.web-vm-ip.ip_address
	}
	
	output "web_vm_admin_username" {
	  description = "Username password for the Virtual Machine"
	  value       = azurerm_virtual_machine.web-vm.os_profile.*
	  #sensitive   = true
	}
	

	output "web_vm_admin_password" {
	  description = "Administrator password for the Virtual Machine"
	  value       = random_password.web-vm-password.result
	  
	}        

$4) Vmvar.tf

variable "web_vm_size" {
	  type        = string
	  description = "Size (SKU) of the virtual machine to create"
	}

	variable "web_license_type" {
	  type        = string
	  description = "Specifies the BYOL type for the virtual machine. Possible values are 'Windows_Client' and 'Windows_Server' if set"
	  default     = null
	}

	variable "web_delete_os_disk_on_termination" {
	  type        = string
	  description = "Should the OS Disk (either the Managed Disk / VHD Blob) be deleted when the Virtual Machine is destroyed?"
	  default     = "true"  # Update for your environment
	}
	

	variable "web_delete_data_disks_on_termination" {
	  description = "Should the Data Disks (either the Managed Disks / VHD Blobs) be deleted when the Virtual Machine is destroyed?"
	  type        = string
	  default     = "true"
	}
	

	variable "web_vm_image" {
	  type        = map(string)
	  description = "Virtual machine source image information"
	  default     = {
	    publisher = "Canonical"
	    offer     = "UbuntuServer"
	    sku       = "18.04-LTS" 
	    version   = "latest"
	  }
	}

	variable "web_admin_username" {
	  description = "Username for Virtual Machine administrator account"
	  type        = string
	  default     = ""
	}
	

	variable "web_admin_password" {
	  description = "Password for Virtual Machine administrator account"
	  type        = string
	  default     = ""
} 
Now do'terraform init'--> then go for 'terraform apply'



Follow the below link to launch wordpress on GCP and RDS service using AWS:

https://www.dhirubhai.net/pulse/launch-wordpress-gcp-rds-service-using-aws-rashni-ghosh/?published=t        

要查看或添加评论,请登录

Rashni Ghosh的更多文章

社区洞察

其他会员也浏览了