This Terraform module deploys a Virtual Network in Azure with a subnet or a set of subnets passed in as input parameters.
Basically this module is a modern version of terraform-azurerm-vnet(Github repo).
The terraform-azurerm-vnet
module used count
because it was the only option, nowadays we encourage using for_each
instead, but there is no way for us to refactor existing resources from count
to for_each
without breaking users' infrastructure.
For the new infrastructure, you should use this module instead of terraform-azurerm-vnet
. For existing infrastructure, we'll maintain terraform-azurerm-vnet
module, fix bugs and amend new features.
The module does not create nor expose a security group. This would need to be defined separately as additional security rules on subnets in the deployed network.
Now Terraform core's version is v1.x and terraform-provider-azurerm's version is v3.x.
Please refer to the sub folders under examples
folder. You can execute terraform apply
command in examples
's sub folder to try the module. These examples are tested against every PR with the E2E Test.
We assumed that you have setup service principal's credentials in your environment variables like below:
export ARM_SUBSCRIPTION_ID="<azure_subscription_id>"
export ARM_TENANT_ID="<azure_subscription_tenant_id>"
export ARM_CLIENT_ID="<service_principal_appid>"
export ARM_CLIENT_SECRET="<service_principal_password>"
On Windows Powershell:
$env:ARM_SUBSCRIPTION_ID="<azure_subscription_id>"
$env:ARM_TENANT_ID="<azure_subscription_tenant_id>"
$env:ARM_CLIENT_ID="<service_principal_appid>"
$env:ARM_CLIENT_SECRET="<service_principal_password>"
We provide a docker image to run the pre-commit checks and tests for you: mcr.microsoft.com/azterraform:latest
To run the pre-commit task, we can run the following command:
$ docker run --rm -v $(pwd):/src -w /src mcr.microsoft.com/azterraform:latest make pre-commit
On Windows Powershell:
$ docker run --rm -v ${pwd}:/src -w /src mcr.microsoft.com/azterraform:latest make pre-commit
In pre-commit task, we will:
- Run
terraform fmt -recursive
command for your Terraform code. - Run
terrafmt fmt -f
command for markdown files and go code files to ensure that the Terraform code embedded in these files are well formatted. - Run
go mod tidy
andgo mod vendor
for test folder to ensure that all the dependencies have been synced. - Run
gofmt
for all go code files. - Run
gofumpt
for all go code files. - Run
terraform-docs
onREADME.md
file, then runmarkdown-table-formatter
to format markdown tables inREADME.md
.
Then we can run the pr-check task to check whether our code meets our pipeline's requirement(We strongly recommend you run the following command before you commit):
$ docker run --rm -v $(pwd):/src -w /src mcr.microsoft.com/azterraform:latest make pr-check
On Windows Powershell:
$ docker run --rm -v ${pwd}:/src -w /src mcr.microsoft.com/azterraform:latest make pr-check
To run the e2e-test, we can run the following command:
docker run --rm -v $(pwd):/src -w /src -e ARM_SUBSCRIPTION_ID -e ARM_TENANT_ID -e ARM_CLIENT_ID -e ARM_CLIENT_SECRET mcr.microsoft.com/azterraform:latest make e2e-test
On Windows Powershell:
docker run --rm -v ${pwd}:/src -w /src -e ARM_SUBSCRIPTION_ID -e ARM_TENANT_ID -e ARM_CLIENT_ID -e ARM_CLIENT_SECRET mcr.microsoft.com/azterraform:latest make e2e-test
Name | Version |
---|---|
terraform | >= 1.2 |
azurerm | >= 3.11, < 4.0 |
Name | Version |
---|---|
azurerm | >= 3.11, < 4.0 |
No modules.
Name | Type |
---|---|
azurerm_subnet.subnet | resource |
azurerm_subnet_nat_gateway_association.nat_gw | resource |
azurerm_subnet_network_security_group_association.vnet | resource |
azurerm_subnet_route_table_association.vnet | resource |
azurerm_virtual_network.vnet | resource |
azurerm_virtual_network_dns_servers.vnet_dns | resource |
Name | Description | Type | Default | Required |
---|---|---|---|---|
resource_group_name | (Required) The name of the resource group in which to create the subnets. Changing this forces new resources to be created. | string |
n/a | yes |
subnets | Subnets to create | map(object( |
n/a | yes |
virtual_network_address_space | (Required) The address space that is used the virtual network. You can supply more than one address space. | list(string) |
n/a | yes |
virtual_network_bgp_community | (Optional) The BGP community attribute in format <as-number>:<community-value> . |
string |
null |
no |
virtual_network_ddos_protection_plan | AzureNetwork DDoS Protection Plan. | object({ |
null |
no |
virtual_network_dns_servers | (Optional) List of IP addresses of DNS servers | object({ |
null |
no |
virtual_network_edge_zone | (Optional) Specifies the Edge Zone within the Azure Region where this Virtual Network should exist. Changing this forces a new Virtual Network to be created. | string |
null |
no |
virtual_network_flow_timeout_in_minutes | (Optional) The flow timeout in minutes for the Virtual Network, which is used to enable connection tracking for intra-VM flows. Possible values are between 4 and 30 minutes. |
number |
null |
no |
virtual_network_location | (Required) The location/region where the virtual network is created. Changing this forces new resources to be created. | string |
n/a | yes |
virtual_network_name | (Required) The name of the virtual network to which to attach the subnet. Changing this forces a new resource to be created. | string |
n/a | yes |
virtual_network_tags | (Optional) A mapping of tags to assign to the virtual network. | map(string) |
null |
no |
Name | Description |
---|---|
vnet_address_space | The address space of the newly created vNet |
vnet_id | The id of the newly created vNet |
vnet_location | The location of the newly created vNet |
vnet_name | The Name of the newly created vNet |
vnet_subnets_name_id | Can be queried subnet-id by subnet name by using lookup(module.vnet.vnet_subnets_name_id, subnet1) |