-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #631 from rguthriemsft/pull-request-627
feat: adding Azure Network module
- Loading branch information
Showing
18 changed files
with
1,170 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Terraform Azure Network Example | ||
|
||
This folder contains a simple Terraform module that deploys resources in [Azure](https://azure.microsoft.com/) to demonstrate | ||
how you can use Terratest to write automated tests for your Azure Terraform code. This module deploys to a Virtual Network two Network Interface Cards, one with an internal only IP and another with an internal and external Public IP. | ||
|
||
- A [Virtual Network](https://azure.microsoft.com/en-us/services/virtual-network/) module that includes the following resources: | ||
- [Virtual Network](https://docs.microsoft.com/en-us/azure/virtual-network/) with the name specified in the `virtual_network_name` variable. | ||
- [Subnet](https://docs.microsoft.com/en-us/rest/api/virtualnetwork/subnets) with the name specified in the `subnet_name` variable. | ||
- [Public Address](https://docs.microsoft.com/en-us/azure/virtual-network/public-ip-addresses) with the name specified in the `public_ip_name` variable. | ||
- [Internal Network Interface](https://docs.microsoft.com/en-us/azure/virtual-network/virtual-network-network-interface) with the name specified in the `network_interface_internal` variable. | ||
- [ExternalNetwork Interface](https://docs.microsoft.com/en-us/azure/virtual-network/virtual-network-network-interface) with the name specified in the `network_interface_external` variable. | ||
|
||
Check out [test/azure/terraform_azure_network_test.go](/test/azure/terraform_azure_network_example_test.go) to see how you can write | ||
automated tests for this module. | ||
|
||
Note that the Azure Virtual Network, Subnet, Network Interface and Public IP resources in this module don't actually do anything; it just runs the resources for | ||
demonstration purposes. | ||
|
||
**WARNING**: This module and the automated tests for it deploy real resources into your Azure account which can cost you | ||
money. The resources are all part of the [Azure Free Account](https://azure.microsoft.com/en-us/free/), so if you haven't used that up, | ||
it should be free, but you are completely responsible for all Azure charges. | ||
|
||
## Running this module manually | ||
|
||
1. Sign up for [Azure](https://azure.microsoft.com/) | ||
1. Configure your Azure credentials using one of the [supported methods for Azure CLI | ||
tools](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest) | ||
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH` | ||
1. Ensure [environment variables](../README.md#review-environment-variables) are available | ||
1. Run `terraform init` | ||
1. Run `terraform apply` | ||
1. When you're done, run `terraform destroy` | ||
|
||
## Running automated tests against this module | ||
|
||
1. Sign up for [Azure](https://azure.microsoft.com/) | ||
1. Configure your Azure credentials using one of the [supported methods for Azure CLI | ||
tools](https://docs.microsoft.com/en-us/cli/azure/azure-cli-configuration?view=azure-cli-latest) | ||
1. Install [Terraform](https://www.terraform.io/) and make sure it's on your `PATH` | ||
1. Configure your Terratest [Go test environment](../README.md) | ||
1. `cd test/azure` | ||
1. `go build terraform_azure_network_example_test.go` | ||
1. `go test -v -run TestTerraformAzureNetworkExample` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
# DEPLOY AN AZURE NETWORK | ||
# This is an example of how to deploy frequent Azure Networking Resources. Note this network doesn't actually do | ||
# anything and is only created for the example to test their commonly needed and integrated properties. | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
# See test/azure/terraform_azure_network_example_test.go for how to write automated tests for this code. | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
provider "azurerm" { | ||
version = "~>2.20" | ||
features {} | ||
} | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# PIN TERRAFORM VERSION TO >= 0.12 | ||
# The examples have been upgraded to 0.12 syntax | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
terraform { | ||
# This module is now only being tested with Terraform 0.13.x. However, to make upgrading easier, we are setting | ||
# 0.12.26 as the minimum version, as that version added support for required_providers with source URLs, making it | ||
# forwards compatible with 0.13.x code. | ||
required_version = ">= 0.12.26" | ||
} | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# DEPLOY A RESOURCE GROUP | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
resource "azurerm_resource_group" "net" { | ||
name = "terratest-network-rg-${var.postfix}" | ||
location = var.location | ||
} | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# DEPLOY VIRTUAL NETWORK | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
resource "azurerm_virtual_network" "net" { | ||
name = "vnet-${var.postfix}" | ||
location = azurerm_resource_group.net.location | ||
resource_group_name = azurerm_resource_group.net.name | ||
address_space = ["10.0.0.0/16"] | ||
dns_servers = [var.dns_ip_01, var.dns_ip_02] | ||
} | ||
|
||
resource "azurerm_subnet" "net" { | ||
name = "subnet-${var.postfix}" | ||
resource_group_name = azurerm_resource_group.net.name | ||
virtual_network_name = azurerm_virtual_network.net.name | ||
address_prefixes = [var.subnet_prefix] | ||
} | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# DEPLOY PRIVATE NETWORK INTERFACE | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
resource "azurerm_network_interface" "net01" { | ||
name = "nic-private-${var.postfix}" | ||
location = azurerm_resource_group.net.location | ||
resource_group_name = azurerm_resource_group.net.name | ||
|
||
ip_configuration { | ||
name = "terratestconfiguration1" | ||
subnet_id = azurerm_subnet.net.id | ||
private_ip_address_allocation = "Static" | ||
private_ip_address = var.private_ip | ||
} | ||
} | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# DEPLOY PUBLIC ADDRESS AND NETWORK INTERFACE | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
resource "azurerm_public_ip" "net" { | ||
name = "pip-${var.postfix}" | ||
resource_group_name = azurerm_resource_group.net.name | ||
location = azurerm_resource_group.net.location | ||
allocation_method = "Static" | ||
ip_version = "IPv4" | ||
sku = "Basic" | ||
idle_timeout_in_minutes = "4" | ||
domain_name_label = var.domain_name_label | ||
} | ||
|
||
resource "azurerm_network_interface" "net02" { | ||
name = "nic-public-${var.postfix}" | ||
location = azurerm_resource_group.net.location | ||
resource_group_name = azurerm_resource_group.net.name | ||
|
||
ip_configuration { | ||
name = "terratestconfiguration1" | ||
subnet_id = azurerm_subnet.net.id | ||
private_ip_address_allocation = "Dynamic" | ||
public_ip_address_id = azurerm_public_ip.net.id | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
output "resource_group_name" { | ||
value = azurerm_resource_group.net.name | ||
} | ||
|
||
output "virtual_network_name" { | ||
value = azurerm_virtual_network.net.name | ||
} | ||
|
||
output "subnet_name" { | ||
value = azurerm_subnet.net.name | ||
} | ||
|
||
output "public_address_name" { | ||
value = azurerm_public_ip.net.name | ||
} | ||
|
||
output "network_interface_internal" { | ||
value = azurerm_network_interface.net01.name | ||
} | ||
|
||
output "network_interface_external" { | ||
value = azurerm_network_interface.net02.name | ||
} | ||
|
||
|
60 changes: 60 additions & 0 deletions
60
examples/azure/terraform-azure-network-example/variables.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
# ENVIRONMENT VARIABLES | ||
# Define these secrets as environment variables | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
# ARM_CLIENT_ID | ||
# ARM_CLIENT_SECRET | ||
# ARM_SUBSCRIPTION_ID | ||
# ARM_TENANT_ID | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# REQUIRED PARAMETERS | ||
# You must provide a value for each of these parameters. | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
variable "domain_name_label" { | ||
description = "The Domain Name Label for the Public IP Address" | ||
type = string | ||
} | ||
|
||
# --------------------------------------------------------------------------------------------------------------------- | ||
# OPTIONAL PARAMETERS | ||
# These parameters have reasonable defaults. | ||
# --------------------------------------------------------------------------------------------------------------------- | ||
|
||
variable "dns_ip_01" { | ||
description = "The first DNS Server IP for the Virtual Network" | ||
type = string | ||
default = "10.0.0.5" | ||
} | ||
|
||
variable "dns_ip_02" { | ||
description = "The second DNS Server IP for the Virtual Network" | ||
type = string | ||
default = "10.0.0.6" | ||
} | ||
|
||
variable "location" { | ||
description = "The Azure Region to deploy resources too" | ||
type = string | ||
default = "East US" | ||
} | ||
|
||
variable "postfix" { | ||
description = "The postfix that will be attached to all resources deployed" | ||
type = string | ||
default = "resource" | ||
} | ||
|
||
variable "private_ip" { | ||
description = "The Static Private IP for the Internal NIC" | ||
type = string | ||
default = "10.0.20.5" | ||
} | ||
|
||
variable "subnet_prefix" { | ||
description = "The subnet range of IPs for the Virtual Network" | ||
type = string | ||
default = "10.0.20.0/24" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.