Skip to content

Instantly share code, notes, and snippets.

@jonathansd1
Created November 8, 2018 04:24
Show Gist options
  • Save jonathansd1/ab75ff3e90e365584685459c2aab0af4 to your computer and use it in GitHub Desktop.
Save jonathansd1/ab75ff3e90e365584685459c2aab0af4 to your computer and use it in GitHub Desktop.
Terraform templates which result in crash
provider "azurerm" {
environment = "public"
}
terraform {
backend "azurerm" {
storage_account_name = "tfstate"
resource_group_name = "tfstate-rg"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
resource "azurerm_resource_group" "hub-services" {
name = "hub-services-rg"
location = "eastus"
tags = {}
}
module "network" {
source = "./network/"
environment = "hub-services"
vnet_name = "hub-services-vnet"
resource_group_name = azurerm_resource_group.hub-services.name
region = "eastus"
address_space = list("10.0.0.0/22")
subnets = {
public = "10.0.0.0/24"
private = "10.0.1.0/24"
data = "10.0.2.0/24"
}
jumpbox_name = "hub-services-jumpbox"
vm_size = "Standard_DS1_V2"
vm_os_publisher = "OpenLogic"
vm_os_offer = "CentOS"
vm_os_sku = "7.5"
vm_os_version = "latest"
tags = {}
}
#Azure Generic vNet Module
resource "azurerm_virtual_network" "vnet" {
name = var.vnet_name
location = var.region
address_space = var.address_space
resource_group_name = var.resource_group_name
dns_servers = var.dns_servers
tags = var.tags
}
resource "azurerm_subnet" "subnet" {
count = length(var.subnets)
name = "${var.environment}-${keys(var.subnets)[count.index]}-subnet"
virtual_network_name = azurerm_virtual_network.vnet.name
resource_group_name = var.resource_group_name
address_prefix = var.subnets[keys(var.subnets)[count.index]]
}
data "azurerm_subnet" "jumpbox" {
depends_on = ["azurerm_subnet.subnet"]
name = "${var.environment}-${var.jumpbox_subnet_name}-subnet"
virtual_network_name = azurerm_virtual_network.vnet.name
resource_group_name = var.resource_group_name
}
module "jumpbox" {
depends_on = ["azurerm_subnet.subnet"]
source = "../standalone_vm"
environment = var.environment
resource_group_name = var.resource_group_name
region = var.region
subnet_id = data.azurerm_subnet.jumpbox.id
vm_name = var.jumpbox_name
vm_size = var.vm_size
vm_os_publisher = var.vm_os_publisher
vm_os_offer = var.vm_os_offer
vm_os_sku = var.vm_os_sku
vm_os_version = var.vm_os_version
tags = var.tags
}
variable "environment" {
description = "Name of target environment"
default = "my"
}
variable "vnet_name" {
description = "Name of the vnet to create"
}
variable "resource_group_name" {
description = "Default resource group name that the network will be created in."
}
variable "region" {
description = "The location/region where the core network will be created. The full list of Azure regions can be found at https://azure.microsoft.com/regions"
}
variable "address_space" {
description = "The address space that is used by the virtual network."
default = ["10.0.0.0/16"]
}
# If no values specified, this defaults to Azure DNS
variable "dns_servers" {
description = "The DNS servers to be used with vNet."
default = []
}
variable "subnets" {
type = "map"
description = "A key/value map of subnet names and corresponding address prefix"
default = {
public = "10.0.0.0/24"
}
}
variable "tags" {
description = "The tags to associate with your network and subnets."
type = "map"
default = {}
}
variable "jumpbox_name" {
description = "Name of the jumpbox to create"
default = "jumpbox"
}
variable "jumpbox_subnet_name" {
description = "Name of the target subnet for the jumpbox"
default = "public"
}
variable "vm_size" {
description = "Specifies the size of the virtual machine."
default = "Standard_DS1_V2"
}
variable "vm_os_publisher" {
description = "The name of the publisher of the image that you want to deploy. This is ignored when vm_os_id or vm_os_simple are provided."
default = "OpenLogic"
}
variable "vm_os_offer" {
description = "The name of the offer of the image that you want to deploy. This is ignored when vm_os_id or vm_os_simple are provided."
default = "CentOS"
}
variable "vm_os_sku" {
description = "The sku of the image that you want to deploy. This is ignored when vm_os_id or vm_os_simple are provided."
default = "7.5"
}
variable "vm_os_version" {
description = "The version of the image that you want to deploy. This is ignored when vm_os_id or vm_os_simple are provided."
default = "latest"
}
# Create a public IP for the VM.
resource "azurerm_public_ip" "vm" {
name = "${var.vm_name}-public-ip"
location = var.region
resource_group_name = var.resource_group_name
public_ip_address_allocation = "static"
tags = var.tags
}
# Create a security group for the VM.
resource "azurerm_network_security_group" "vm" {
name = "${var.vm_name}-sg"
location = var.region
resource_group_name = var.resource_group_name
security_rule {
name = "AllowSshInBound"
priority = 1000
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source_port_range = "*"
destination_port_range = "22"
source_address_prefix = "*"
destination_address_prefix = "*"
}
}
# Create a network interface for the VM.
resource "azurerm_network_interface" "vm" {
name = "${var.vm_name}-nic"
location = var.region
resource_group_name = var.resource_group_name
network_security_group_id = azurerm_network_security_group.vm.id
tags = var.tags
ip_configuration {
name = "${var.vm_name}-configuration"
subnet_id = var.subnet_id
private_ip_address_allocation = "dynamic"
public_ip_address_id = azurerm_public_ip.vm.id
}
}
# Provide a data source that references the public key to be used.
data "azurerm_key_vault_secret" "test" {
name = "vm-jumpbox-pub"
vault_uri = "https://dn-vm-keyvault.vault.azure.net/"
}
# Create Azure VM for VM.
resource "azurerm_virtual_machine" "vm" {
name = var.vm_name
location = var.region
resource_group_name = var.resource_group_name
network_interface_ids = list(azurerm_network_interface.vm.id)
vm_size = var.vm_size
tags = var.tags
storage_image_reference {
publisher = var.vm_os_publisher
offer = var.vm_os_offer
sku = var.vm_os_sku
version = var.vm_os_version
}
storage_os_disk {
name = "${var.vm_name}-osdisk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile_linux_config {
disable_password_authentication = true
ssh_keys {
path = "/home/centos/.ssh/authorized_keys"
key_data = data.azurerm_key_vault_secret.test.value
}
}
}
variable "environment" {
description = "Name of target environment"
default = "my"
}
variable "resource_group_name" {
description = "Default resource group name that the network will be created in."
}
variable "region" {
description = "The location/region where the core network will be created. The full list of Azure regions can be found at https://azure.microsoft.com/regions"
}
variable "subnet_id" {
description = "ID of the target subnet"
}
variable "tags" {
description = "The tags to associate with your network and subnets."
type = "map"
default = {}
}
variable "vm_name" {
description = "Name of the VM to create"
}
variable "vm_size" {
description = "Specifies the size of the virtual machine."
default = "Standard_DS1_V2"
}
variable "vm_os_publisher" {
description = "The name of the publisher of the image that you want to deploy. This is ignored when vm_os_id or vm_os_simple are provided."
default = "OpenLogic"
}
variable "vm_os_offer" {
description = "The name of the offer of the image that you want to deploy. This is ignored when vm_os_id or vm_os_simple are provided."
default = "CentOS"
}
variable "vm_os_sku" {
description = "The sku of the image that you want to deploy. This is ignored when vm_os_id or vm_os_simple are provided."
default = "7.5"
}
variable "vm_os_version" {
description = "The version of the image that you want to deploy. This is ignored when vm_os_id or vm_os_simple are provided."
default = "latest"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment