Terraform Azure RM Compute Module
Перейти к файлу
Yuping Wei 995219821c
upgrade to Terraform v0.12 (#122)
* upgrade to v0.12

* add comment for test

* add comment for test

* remove useless test code

* change bool type

* remove useless import

* add os back
2020-01-10 11:15:24 +08:00
.github Configure files for terraformbot (#82) 2018-11-04 13:24:50 -08:00
os bumb os version (#94) 2019-04-03 16:11:33 -07:00
test upgrade to Terraform v0.12 (#122) 2020-01-10 11:15:24 +08:00
.gitignore Add Terratest Example for Compute Module (#66) 2018-07-28 15:19:51 +08:00
.travis.yml upgrade to Terraform v0.12 (#122) 2020-01-10 11:15:24 +08:00
Dockerfile upgrade to Terraform v0.12 (#122) 2020-01-10 11:15:24 +08:00
Gemfile upgrade to Terraform v0.12 (#122) 2020-01-10 11:15:24 +08:00
Gopkg.lock Add Terratest Example for Compute Module (#66) 2018-07-28 15:19:51 +08:00
Gopkg.toml Add Terratest Example for Compute Module (#66) 2018-07-28 15:19:51 +08:00
LICENSE Initial Release (#1) 2017-09-01 21:31:56 -07:00
README.md updated var and readme for windows vm example (#96) 2019-04-02 17:46:46 -07:00
Rakefile Add Terratest Example for Compute Module (#66) 2018-07-28 15:19:51 +08:00
bug_report.md Zunlihu issue template (#75) 2018-09-06 21:19:39 -07:00
main.tf upgrade to Terraform v0.12 (#122) 2020-01-10 11:15:24 +08:00
outputs.tf upgrade to Terraform v0.12 (#122) 2020-01-10 11:15:24 +08:00
variables.tf upgrade to Terraform v0.12 (#122) 2020-01-10 11:15:24 +08:00

README.md

terraform-azurerm-compute

Build Status

Deploys 1+ Virtual Machines to your provided VNet

This Terraform module deploys Virtual Machines in Azure with the following characteristics:

  • Ability to specify a simple string to get the latest marketplace image using var.vm_os_simple
  • All VMs use managed disks
  • Network Security Group (NSG) created with a single remote access rule which opens var.remote_port port or auto calculated port number if using var.vm_os_simple to all nics
  • VM nics attached to a single virtual network subnet of your choice (new or existing) via var.vnet_subnet_id.
  • Control the number of Public IP addresses assigned to VMs via var.nb_public_ip. Create and attach one Public IP per VM up to the number of VMs or create NO public IPs via setting var.nb_public_ip to 0.

Note: Terraform module registry is incorrect in the number of required parameters since it only deems required based on variables with non-existent values. The actual minimum required variables depends on the configuration and is specified below in the usage.

Simple Usage

This contains the bare minimum options to be configured for the VM to be provisioned. The entire code block provisions a Windows and a Linux VM, but feel free to delete one or the other and corresponding outputs. The outputs are also not necessary to provision, but included to make it convenient to know the address to connect to the VMs after provisioning completes.

Provisions an Ubuntu Server 16.04-LTS VM and a Windows 2016 Datacenter Server VM using vm_os_simple to a new VNet and opens up ports 22 for SSH and 3389 for RDP access via the attached public IP to each VM. All resources are provisioned into the default resource group called terraform-compute. The Ubuntu Server will use the ssh key found in the default location ~/.ssh/id_rsa.pub.

  module "linuxservers" {
    source              = "Azure/compute/azurerm"
    location            = "West US 2"
    vm_os_simple        = "UbuntuServer"
    public_ip_dns       = ["linsimplevmips"] // change to a unique name per datacenter region
    vnet_subnet_id      = "${module.network.vnet_subnets[0]}"
  }

  module "windowsservers" {
    source              = "Azure/compute/azurerm"
    location            = "West US 2"
    vm_hostname         = "mywinvm" // line can be removed if only one VM module per resource group
    admin_password      = "ComplxP@ssw0rd!"
    vm_os_simple        = "WindowsServer"
    is_windows_image    = "true"
    public_ip_dns       = ["winsimplevmips"] // change to a unique name per datacenter region
    vnet_subnet_id      = "${module.network.vnet_subnets[0]}"
  }

  module "network" {
    source              = "Azure/network/azurerm"
    version             = "~> 1.1.1"
    location            = "West US 2"
    allow_rdp_traffic   = "true"
    allow_ssh_traffic   = "true"
    resource_group_name = "terraform-compute"
  }

  output "linux_vm_public_name"{
    value = "${module.linuxservers.public_ip_dns_name}"
  }

  output "windows_vm_public_name"{
    value = "${module.windowsservers.public_ip_dns_name}"
  }

Advanced Usage

The following example illustrates some of the configuration options available to deploy a virtual machine. Feel free to remove the Linux or Windows modules and corresponding outputs.

More specifically this provisions:

1 - New vnet for all vms

2 - Ubuntu 14.04 Server VMs using vm_os_publisher, vm_os_offer and vm_os_sku which is configured with:

  • No public IP assigned, so access can only happen through another machine on the vnet.
  • Opens up port 22 for SSH access with the default ~/.ssh/id_rsa.pub key
  • Boot diagnostics is enabled.
  • Additional tags are added to the resource group.
  • OS disk is deleted upon deletion of the VM
  • Add one 64GB premium managed data disk

2 - Windows Server 2012 R2 VMs using vm_os_publisher, vm_os_offer and vm_os_sku which is configured with:

  • Two Public IP addresses (one for each VM)
  • Opens up port 3389 for RDP access using the password as shown
  module "linuxservers" {
    source                        = "Azure/compute/azurerm"
    resource_group_name           = "terraform-advancedvms"
    location                      = "westus2"
    vm_hostname                   = "mylinuxvm"
    nb_public_ip                  = "0"
    remote_port                   = "22"
    nb_instances                  = "2"
    vm_os_publisher               = "Canonical"
    vm_os_offer                   = "UbuntuServer"
    vm_os_sku                     = "14.04.2-LTS"
    vnet_subnet_id                = "${module.network.vnet_subnets[0]}"
    boot_diagnostics              = "true"
    delete_os_disk_on_termination = "true"
    data_disk                     = "true"
    data_disk_size_gb             = "64"
    data_sa_type                  = "Premium_LRS"

    tags = {
      environment = "dev"
      costcenter  = "it"
    }

    enable_accelerated_networking = "true"
  }

  module "windowsservers" {
    source                        = "Azure/compute/azurerm"
    resource_group_name           = "terraform-advancedvms"
    location                      = "westus2"
    vm_hostname                   = "mywinvm"
    admin_password                = "ComplxP@ssw0rd!"
    public_ip_dns                 = ["winterravmip", "winterravmip1"]
    nb_public_ip                  = "2"
    remote_port                   = "3389"
    nb_instances                  = "2"
    vm_os_publisher               = "MicrosoftWindowsServer"
    vm_os_offer                   = "WindowsServer"
    vm_os_sku                     = "2012-R2-Datacenter"
    vm_size                       = "Standard_DS2_V2"
    vnet_subnet_id                = "${module.network.vnet_subnets[0]}"
    enable_accelerated_networking = "true"
  }

  module "network" {
    source              = "Azure/network/azurerm"
    version             = "~> 1.1.1"
    location            = "westus2"
    allow_rdp_traffic   = "true"
    allow_ssh_traffic   = "true"
    resource_group_name = "terraform-advancedvms"
  }

  output "linux_vm_private_ips" {
    value = "${module.linuxservers.network_interface_private_ip}"
  }

  output "windows_vm_public_name"{
    value = "${module.windowsservers.public_ip_dns_name}"
  }

  output "windows_vm_public_ip" {
    value = "${module.windowsservers.public_ip_address}"
  }

  output "windows_vm_private_ips" {
    value = "${module.windowsservers.network_interface_private_ip}"
  }

Test

Configurations

We provide 2 ways to build, run, and test the module on a local development machine. Native (Mac/Linux) or Docker.

Native (Mac/Linux)

Prerequisites

Quick Run

We provide simple script to quickly set up module development environment:

$ curl -sSL https://raw.githubusercontent.com/Azure/terramodtest/master/tool/env_setup.sh | sudo bash

Then simply run it in local shell:

$ cd $GOPATH/src/{directory_name}/
$ bundle install
$ rake build
$ rake e2e

Docker

We provide a Dockerfile to build a new image based FROM the microsoft/terraform-test Docker hub image which adds additional tools / packages specific for this module (see Custom Image section). Alternatively use only the microsoft/terraform-test Docker hub image by using these instructions.

Prerequisites

Custom Image

This builds the custom image:

$ docker build --build-arg BUILD_ARM_SUBSCRIPTION_ID=$ARM_SUBSCRIPTION_ID --build-arg BUILD_ARM_CLIENT_ID=$ARM_CLIENT_ID --build-arg BUILD_ARM_CLIENT_SECRET=$ARM_CLIENT_SECRET --build-arg BUILD_ARM_TENANT_ID=$ARM_TENANT_ID -t azure-compute .

This runs the build and unit tests:

$ docker run --rm azure-compute /bin/bash -c "bundle install && rake build"

This runs the end to end tests:

$ docker run --rm azure-compute /bin/bash -c "bundle install && rake e2e"

This runs the full tests:

$ docker run --rm azure-compute /bin/bash -c "bundle install && rake full"

Authors

Originally created by David Tesar

License

MIT