Skip to content

Commit

Permalink
provider/azurerm: Add example of a VNET w/ Two Subnets (hashicorp#14115)
Browse files Browse the repository at this point in the history
* merge master

* added new constructs/naming for deploy scripts, etc.

* suppress az login output

* removed .tfvars and provider.tf; updated prev merge

* reverted .travis.yml back to Hashicorp's

* Reverting back to the Hashicorp travis file
  • Loading branch information
anniehedgpeth authored and tombuildsstuff committed May 4, 2017
1 parent 03c7cfb commit 8e7f3cc
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ dist: trusty
sudo: false
language: go
go:
- 1.8.x
- 1.8

# add TF_CONSUL_TEST=1 to run consul tests
# they were causing timouts in travis
Expand All @@ -25,7 +25,7 @@ install:
- bash scripts/gogetcookie.sh
- go get github.com/kardianos/govendor
script:
- make vendor-status test vet
- make vet vendor-status test
- GOOS=windows go build
branches:
only:
Expand Down
2 changes: 1 addition & 1 deletion examples/azure-vm-simple-linux-managed-disk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Azure requires that an application is added to Azure Active Directory to generat
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.

## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.
2 changes: 1 addition & 1 deletion examples/azure-vm-simple-linux-managed-disk/deploy.ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ docker run --rm -it \
--workdir=/data \
--entrypoint "/bin/sh" \
hashicorp/terraform:light \
-c "/bin/terraform destroy -force -var dns_name=$KEY -var hostname=$KEY -var resource_group=$KEY -var admin_password=$PASSWORD;"
-c "/bin/terraform destroy -force -var dns_name=$KEY -var hostname=$KEY -var resource_group=$KEY -var admin_password=$PASSWORD;"
2 changes: 1 addition & 1 deletion examples/azure-vm-simple-linux-managed-disk/deploy.mac.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ if docker -v; then

else
echo "Docker is used to run terraform commands, please install before run: https://docs.docker.com/docker-for-mac/install/"
fi
fi
2 changes: 1 addition & 1 deletion examples/azure-vm-simple-linux-managed-disk/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ output "vm_fqdn" {

output "ssh_command" {
value = "ssh ${var.admin_username}@${azurerm_public_ip.pip.fqdn}"
}
}
2 changes: 1 addition & 1 deletion examples/azure-vm-simple-linux-managed-disk/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ variable "admin_username" {

variable "admin_password" {
description = "administrator password (recommended to disable password auth)"
}
}
3 changes: 3 additions & 0 deletions examples/azure-vnet-two-subnets/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform.tfstate*
provider.tf
out.tfplan
18 changes: 18 additions & 0 deletions examples/azure-vnet-two-subnets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Virtual Network with Two Subnets

This template allows you to create a Virtual Network with two subnets.

## 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.

## variables.tf
The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template.
41 changes: 41 additions & 0 deletions examples/azure-vnet-two-subnets/deploy.ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash

set -o errexit -o nounset

# generate a unique string for CI deployment
# KEY=$(cat /dev/urandom | tr -cd 'a-z' | head -c 12)
# PASSWORD=$KEY$(cat /dev/urandom | tr -cd 'A-Z' | head -c 2)$(cat /dev/urandom | tr -cd '0-9' | head -c 2)

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 resource_group=$KEY; \
/bin/terraform apply out.tfplan; \
/bin/terraform show;"

# check that resources exist 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 network vnet subnet show -n subnet1 -g $KEY --vnet-name '$KEY'vnet; \
az network vnet subnet show -n subnet2 -g $KEY --vnet-name '$KEY'vnet;"

# 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 resource_group=$KEY;"
15 changes: 15 additions & 0 deletions examples/azure-vnet-two-subnets/deploy.mac.sh
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions examples/azure-vnet-two-subnets/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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_virtual_network" "vnet" {
name = "${var.resource_group}vnet"
location = "${var.location}"
address_space = ["10.0.0.0/16"]
resource_group_name = "${azurerm_resource_group.rg.name}"
}

resource "azurerm_subnet" "subnet1" {
name = "subnet1"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.rg.name}"
address_prefix = "10.0.0.0/24"
}

resource "azurerm_subnet" "subnet2" {
name = "subnet2"
virtual_network_name = "${azurerm_virtual_network.vnet.name}"
resource_group_name = "${azurerm_resource_group.rg.name}"
address_prefix = "10.0.1.0/24"
}
8 changes: 8 additions & 0 deletions examples/azure-vnet-two-subnets/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
variable "resource_group" {
description = "The name of the resource group in which to create the virtual network."
}

variable "location" {
description = "The location/region where the virtual network is created. Changing this forces a new resource to be created."
default = "southcentralus"
}

0 comments on commit 8e7f3cc

Please sign in to comment.