diff --git a/examples/azure-traffic-manager-lb-scale-set/Readme.md b/examples/azure-traffic-manager-lb-scale-set/Readme.md new file mode 100644 index 000000000..49679d33c --- /dev/null +++ b/examples/azure-traffic-manager-lb-scale-set/Readme.md @@ -0,0 +1,27 @@ +# Azure traffic manager with load balanced scale sets + +This example shows how to create a load balanced scale set in multiple locations and then geographically load balance these using traffic manager. This example the scale set uses a market place Ubuntu image, this could be customised using an extension or a generalized image created using packer. + +This script demonstrates how variable can be passed in and out of reusable modules. You will need to run `terraform get` for terrafrom to get so that modules are pre-processed. + +## Keys and variables + +To use this you will need to populate the `terraform.tfvars.example` file with your Azure credentials and key. Rename this to `terraform.tfvars` and copy this somewhere private. If you need to generate credentials follow the instructions on the Azure provider documented [here](https://www.terraform.io/docs/providers/azurerm) + +You may also want to modify some of the settings in `variables.tf`, DNS names must be unique within an Azure location and globally for traffic management + +## To start the script + +### Planning + +`terraform get` + +`terraform plan -var-file="C:\Users\eltimmo\.terraform\keys.tfvars"` + +### Apply phase + +`terraform apply -var-file="C:\Users\eltimmo\.terraform\keys.tfvars"` + +### Destroy + +`terraform destroy -var-file="C:\Users\eltimmo\.terraform\keys.tfvars"` diff --git a/examples/azure-traffic-manager-lb-scale-set/main.tf b/examples/azure-traffic-manager-lb-scale-set/main.tf new file mode 100644 index 000000000..34b65f578 --- /dev/null +++ b/examples/azure-traffic-manager-lb-scale-set/main.tf @@ -0,0 +1,102 @@ +# Provider accounts must be passed + +variable "subscription_id" {} +variable "client_id" {} +variable "client_secret" {} +variable "tenant_id" {} + +provider "azurerm" { + subscription_id = "${var.subscription_id}" + client_id = "${var.client_id}" + client_secret = "${var.client_secret}" + tenant_id = "${var.tenant_id}" + } + +# Create the resource group and assets for first location +module "location01" { + source = "./tf_modules" + + location = "${var.location01_location}" + resource_prefix = "${var.location01_resource_prefix}" + webserver_prefix = "${var.location01_webserver_prefix}" + lb_dns_label = "${var.location01_lb_dns_label}" + + instance_count = "${var.instance_count}" + instance_vmprofile = "${var.instance_vmprofile}" + + image_admin_username = "${var.image_admin_username}" + image_admin_password = "${var.image_admin_password}" + + image_publisher = "${var.image_publisher}" + image_offer = "${var.image_offer}" + image_sku = "${var.image_sku}" + image_version = "${var.image_version}" + +} + +# Create the resource group and assets for second location +module "location02" { + source = "./tf_modules" + + location = "${var.location02_location}" + resource_prefix = "${var.location02_resource_prefix}" + webserver_prefix = "${var.location02_webserver_prefix}" + lb_dns_label = "${var.location02_lb_dns_label}" + + instance_count = "${var.instance_count}" + instance_vmprofile = "${var.instance_vmprofile}" + + image_admin_username = "${var.image_admin_username}" + image_admin_password = "${var.image_admin_password}" + + image_publisher = "${var.image_publisher}" + image_offer = "${var.image_offer}" + image_sku = "${var.image_sku}" + image_version = "${var.image_version}" + +} + +# Create global resource group +resource "azurerm_resource_group" "global_rg" { + name = "global_rg" + location = "${var.global_location}" +} + +# Create the traffic manager +resource "azurerm_traffic_manager_profile" "trafficmanagerhttp" { + name = "trafficmanagerhttp" + resource_group_name = "${azurerm_resource_group.global_rg.name}" + + traffic_routing_method = "Weighted" + + dns_config { + relative_name = "${var.dns_relative_name}" + ttl = 100 + } + + monitor_config { + protocol = "http" + port = 80 + path = "/" + } +} + +# Add endpoint mappings to traffic manager, location01 +resource "azurerm_traffic_manager_endpoint" "trafficmanagerhttp_01" { + name = "trafficmanagerhttp_ukw" + resource_group_name = "${azurerm_resource_group.global_rg.name}" + profile_name = "${azurerm_traffic_manager_profile.trafficmanagerhttp.name}" + target_resource_id = "${module.location01.webserverpublic_ip_id}" + type = "azureEndpoints" + weight = 100 +} + +# Add endpoint mappings to traffic manager, location02 +resource "azurerm_traffic_manager_endpoint" "trafficmanagerhttp_02" { + name = "trafficmanagerhttp_wus" + resource_group_name = "${azurerm_resource_group.global_rg.name}" + profile_name = "${azurerm_traffic_manager_profile.trafficmanagerhttp.name}" + target_resource_id = "${module.location02.webserverpublic_ip_id}" + type = "azureEndpoints" + weight = 100 +} \ No newline at end of file diff --git a/examples/azure-traffic-manager-lb-scale-set/terraform.tfvars.example b/examples/azure-traffic-manager-lb-scale-set/terraform.tfvars.example new file mode 100644 index 000000000..fdb09a2b0 --- /dev/null +++ b/examples/azure-traffic-manager-lb-scale-set/terraform.tfvars.example @@ -0,0 +1,7 @@ +# Azure provide keys example. This should not be included in your repository for security reasons +# Use terrafrom -var-file="FULLPATH" + +subscription_id = "" +client_id = "" +client_secret = "" +tenant_id = "" \ No newline at end of file diff --git a/examples/azure-traffic-manager-lb-scale-set/tf_modules/location.tf b/examples/azure-traffic-manager-lb-scale-set/tf_modules/location.tf new file mode 100644 index 000000000..dbd3992b3 --- /dev/null +++ b/examples/azure-traffic-manager-lb-scale-set/tf_modules/location.tf @@ -0,0 +1,165 @@ +variable "location" {} +variable "resource_prefix" {} +variable "webserver_prefix" {} +variable "lb_dns_label" {} + +variable "instance_count" {} +variable "instance_vmprofile" {} + +variable "image_admin_username" {} +variable "image_admin_password" {} + +variable "image_publisher" {} +variable "image_offer" {} +variable "image_sku" {} +variable "image_version" {} + +# Create webserver resource group +resource "azurerm_resource_group" "webservers_rg" { + name = "${var.resource_prefix}_rg" + location = "${var.location}" +} + +# Create virtual network +resource "azurerm_virtual_network" "webservers_vnet" { + name = "webservers_vnet" + address_space = ["10.1.0.0/24"] + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" +} + +# Create subnet +resource "azurerm_subnet" "webservers_subnet" { + name = "webservers_subnet" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + virtual_network_name = "${azurerm_virtual_network.webservers_vnet.name}" + address_prefix = "10.1.0.0/24" +} + +# Create a public ip for the location LB +resource "azurerm_public_ip" "webserverpublic_ip" { + name = "${var.resource_prefix}_publicip" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + public_ip_address_allocation = "static" + domain_name_label = "${var.lb_dns_label}" +} + +# Create webservers LB +resource "azurerm_lb" "webservers_lb" { + name = "webservers_lb" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + + frontend_ip_configuration { + name = "webserverpublic_ip" + public_ip_address_id = "${azurerm_public_ip.webserverpublic_ip.id}" + } +} + +# Add the backend for webserver LB +resource "azurerm_lb_backend_address_pool" "webservers_lb_backend" { + name = "webservers_lb_backend" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + loadbalancer_id = "${azurerm_lb.webservers_lb.id}" +} + +# Create HTTP probe on port 80 +resource "azurerm_lb_probe" "httpprobe" { + name = "httpprobe" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + loadbalancer_id = "${azurerm_lb.webservers_lb.id}" + protocol = "tcp" + port = 80 +} + +# Create LB rule for HTTP and add to webserver LB +resource "azurerm_lb_rule" "webservers_lb_http" { + name = "webservers_lb_http" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + loadbalancer_id = "${azurerm_lb.webservers_lb.id}" + protocol = "Tcp" + frontend_port = "80" + backend_port = "80" + frontend_ip_configuration_name = "webserverpublic_ip" + probe_id = "${azurerm_lb_probe.httpprobe.id}" + backend_address_pool_id = "${azurerm_lb_backend_address_pool.webservers_lb_backend.id}" +} + +# Create storage account +resource "azurerm_storage_account" "webservers_sa" { + name = "${var.resource_prefix}storage" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + location = "${var.location}" + account_type = "Standard_LRS" +} + +# Create container +resource "azurerm_storage_container" "webservers_ct" { + name = "vhds" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + storage_account_name = "${azurerm_storage_account.webservers_sa.name}" + container_access_type = "private" +} + +# Configure the scale set using library image +resource "azurerm_virtual_machine_scale_set" "webserver_ss" { + name = "webserver_ss" + location = "${var.location}" + resource_group_name = "${azurerm_resource_group.webservers_rg.name}" + upgrade_policy_mode = "Manual" + + sku { + name = "${var.instance_vmprofile}" + tier = "Standard" + capacity = "${var.instance_count}" + } + + os_profile { + computer_name_prefix = "${var.webserver_prefix}" + admin_username = "${var.image_admin_username}" + admin_password = "${var.image_admin_password}" + } + + os_profile_linux_config { + disable_password_authentication = false + } + + network_profile { + name = "web_ss_net_profile" + primary = true + + ip_configuration { + name = "web_ss_ip_profile" + subnet_id = "${azurerm_subnet.webservers_subnet.id}" + load_balancer_backend_address_pool_ids = ["${azurerm_lb_backend_address_pool.webservers_lb_backend.id}"] + } + } + + storage_profile_os_disk { + name = "osDiskProfile" + caching = "ReadWrite" + create_option = "FromImage" + vhd_containers = ["${azurerm_storage_account.webservers_sa.primary_blob_endpoint}${azurerm_storage_container.webservers_ct.name}"] + } + + storage_profile_image_reference { + publisher = "${var.image_publisher}" + offer = "${var.image_offer}" + sku = "${var.image_sku}" + version = "${var.image_version}" + } + + extension { + name = "CustomScriptForLinux" + publisher = "Microsoft.OSTCExtensions" + type = "CustomScriptForLinux" + type_handler_version = "1.4" + settings = <