diff --git a/examples/azure-cdn-with-storage-account/deploy.ci.sh b/examples/azure-cdn-with-storage-account/deploy.ci.sh index 8f1ff7c9f..406ac1d9e 100755 --- a/examples/azure-cdn-with-storage-account/deploy.ci.sh +++ b/examples/azure-cdn-with-storage-account/deploy.ci.sh @@ -26,4 +26,4 @@ docker run --rm -it \ --workdir=/data \ --entrypoint "/bin/sh" \ hashicorp/terraform:light \ - -c "/bin/terraform destroy -force -var resource_group=$KEY;" + -c "/bin/terraform destroy -force -var resource_group=$KEY;" \ No newline at end of file diff --git a/examples/azure-traffic-manager-vm/README.md b/examples/azure-traffic-manager-vm/README.md new file mode 100644 index 000000000..5fcc99aae --- /dev/null +++ b/examples/azure-traffic-manager-vm/README.md @@ -0,0 +1,29 @@ +# Azure Traffic Manager with virtual machines + +This Terraform template was based on [this](https://github.com/Azure/azure-quickstart-templates/tree/master/201-traffic-manager-vm) Azure Quickstart Template. Changes to the ARM template that may have occurred since the creation of this example may not be reflected here. + +This template shows how to create an Azure Traffic Manager profile to load-balance across a couple of Azure virtual machines. Each endpoint has an equal weight but different weights can be specified to distribute load non-uniformly. + +See also: + +- Traffic Manager routing methods for details of the different routing methods available. +- Create or update a Traffic Manager profile for details of the JSON elements relating to a Traffic Manager profile. + +## main.tf +The `main.tf` file contains the actual resources that will be deployed. It also contains the Azure Resource Group definition and any defined variables. + +## outputs.tf +This data is outputted when `terraform apply` is called, and can be queried using the `terraform output` command. + +## provider.tf +Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file. + +## terraform.tfvars +If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it. + +If you are committing this template to source control, please insure that you add this file to your `.gitignore` file. + +## variables.tf +The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template. + +![`terraform graph`](/examples/azure-traffic-manager-vm/graph.png) diff --git a/examples/azure-traffic-manager-vm/deploy.ci.sh b/examples/azure-traffic-manager-vm/deploy.ci.sh new file mode 100755 index 000000000..c9f291444 --- /dev/null +++ b/examples/azure-traffic-manager-vm/deploy.ci.sh @@ -0,0 +1,36 @@ +#!/bin/bash + +set -o errexit -o nounset + +docker run --rm -it \ + -e ARM_CLIENT_ID \ + -e ARM_CLIENT_SECRET \ + -e ARM_SUBSCRIPTION_ID \ + -e ARM_TENANT_ID \ + -v $(pwd):/data \ + --workdir=/data \ + --entrypoint "/bin/sh" \ + hashicorp/terraform:light \ + -c "/bin/terraform get; \ + /bin/terraform validate; \ + /bin/terraform plan -out=out.tfplan -var dns_name=$KEY -var resource_group=$KEY -var admin_password=$PASSWORD; \ + /bin/terraform apply out.tfplan; \ + /bin/terraform show;" + +# cleanup deployed azure resources via azure-cli +docker run --rm -it \ + azuresdk/azure-cli-python \ + sh -c "az login --service-principal -u $ARM_CLIENT_ID -p $ARM_CLIENT_SECRET --tenant $ARM_TENANT_ID > /dev/null; \ + az vm show -g $KEY -n rgvm" + +# cleanup deployed azure resources via terraform +docker run --rm -it \ + -e ARM_CLIENT_ID \ + -e ARM_CLIENT_SECRET \ + -e ARM_SUBSCRIPTION_ID \ + -e ARM_TENANT_ID \ + -v $(pwd):/data \ + --workdir=/data \ + --entrypoint "/bin/sh" \ + hashicorp/terraform:light \ + -c "/bin/terraform destroy -force -var dns_name=$KEY -var resource_group=$KEY -var admin_password=$PASSWORD;" \ No newline at end of file diff --git a/examples/azure-traffic-manager-vm/deploy.mac.sh b/examples/azure-traffic-manager-vm/deploy.mac.sh new file mode 100755 index 000000000..dfc34c2be --- /dev/null +++ b/examples/azure-traffic-manager-vm/deploy.mac.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -o errexit -o nounset + +if docker -v; then + + # generate a unique string for CI deployment + export KEY=$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-z' | head -c 12) + export PASSWORD=$KEY$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'A-Z' | head -c 2)$(cat /dev/urandom | env LC_CTYPE=C tr -cd '0-9' | head -c 2) + + /bin/sh ./deploy.ci.sh + +else + echo "Docker is used to run terraform commands, please install before run: https://docs.docker.com/docker-for-mac/install/" +fi \ No newline at end of file diff --git a/examples/azure-traffic-manager-vm/graph.png b/examples/azure-traffic-manager-vm/graph.png new file mode 100644 index 000000000..fddb46a66 Binary files /dev/null and b/examples/azure-traffic-manager-vm/graph.png differ diff --git a/examples/azure-traffic-manager-vm/main.tf b/examples/azure-traffic-manager-vm/main.tf new file mode 100644 index 000000000..ef34a8ad5 --- /dev/null +++ b/examples/azure-traffic-manager-vm/main.tf @@ -0,0 +1,125 @@ +# provider "azurerm" { +# subscription_id = "REPLACE-WITH-YOUR-SUBSCRIPTION-ID" +# client_id = "REPLACE-WITH-YOUR-CLIENT-ID" +# client_secret = "REPLACE-WITH-YOUR-CLIENT-SECRET" +# tenant_id = "REPLACE-WITH-YOUR-TENANT-ID" +# } + +resource "azurerm_resource_group" "rg" { + name = "${var.resource_group}" + location = "${var.location}" +} + +resource "azurerm_public_ip" "pip" { + name = "ip${count.index}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.rg.name}" + public_ip_address_allocation = "dynamic" + domain_name_label = "${var.dns_name}${count.index}" + count = "${var.num_vms}" +} + +resource "azurerm_virtual_network" "vnet" { + name = "${var.vnet}" + location = "${var.location}" + address_space = ["${var.address_space}"] + resource_group_name = "${azurerm_resource_group.rg.name}" +} + +resource "azurerm_subnet" "subnet" { + name = "${var.subnet_name}" + virtual_network_name = "${azurerm_virtual_network.vnet.name}" + resource_group_name = "${azurerm_resource_group.rg.name}" + address_prefix = "${var.subnet_prefix}" +} + +resource "azurerm_network_interface" "nic" { + name = "nic${count.index}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.rg.name}" + count = "${var.num_vms}" + + ip_configuration { + name = "ipconfig${count.index}" + subnet_id = "${azurerm_subnet.subnet.id}" + private_ip_address_allocation = "Dynamic" + public_ip_address_id = "${element(azurerm_public_ip.pip.*.id, count.index)}" + } +} + +resource "azurerm_virtual_machine" "vm" { + name = "vm${count.index}" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.rg.name}" + vm_size = "${var.vm_size}" + count = "${var.num_vms}" + network_interface_ids = ["${element(azurerm_network_interface.nic.*.id, count.index)}"] + + storage_image_reference { + publisher = "${var.image_publisher}" + offer = "${var.image_offer}" + sku = "${var.image_sku}" + version = "${var.image_version}" + } + + storage_os_disk { + name = "osdisk${count.index}" + create_option = "FromImage" + } + + os_profile { + computer_name = "vm${count.index}" + admin_username = "${var.admin_username}" + admin_password = "${var.admin_password}" + } + + os_profile_linux_config { + disable_password_authentication = false + } +} + +resource "azurerm_virtual_machine_extension" "ext" { + depends_on = ["azurerm_virtual_machine.vm"] + name = "CustomScript" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.rg.name}" + virtual_machine_name = "vm${count.index}" + publisher = "Microsoft.Azure.Extensions" + type = "CustomScript" + type_handler_version = "2.0" + count = "${var.num_vms}" + auto_upgrade_minor_version = true + + settings = <