provider/azurerm: Add example of a VNET w/ Two Subnets (#14115)
* 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
This commit is contained in:
parent
03c7cfb799
commit
8e7f3cc09d
|
@ -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
|
||||
|
@ -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:
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
terraform.tfstate*
|
||||
provider.tf
|
||||
out.tfplan
|
|
@ -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.
|
|
@ -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;"
|
|
@ -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
|
|
@ -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"
|
||||
}
|
|
@ -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"
|
||||
}
|
Loading…
Reference in New Issue