Skip to content

Commit 64cab32

Browse files
committed
adding resource group creation option
1 parent f588bf7 commit 64cab32

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

main.tf

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,76 @@
22
# Local declarations
33
#---------------------------
44
locals {
5-
frontend_port_name = "appgw-${var.app_gateway_name}-${data.azurerm_resource_group.rg.location}-feport"
6-
frontend_ip_configuration_name = "appgw-${var.app_gateway_name}-${data.azurerm_resource_group.rg.location}-feip"
7-
gateway_ip_configuration_name = "appgw-${var.app_gateway_name}-${data.azurerm_resource_group.rg.location}-gwipc"
5+
frontend_port_name = "appgw-${var.app_gateway_name}-${local.location}-feport"
6+
frontend_ip_configuration_name = "appgw-${var.app_gateway_name}-${local.location}-feip"
7+
gateway_ip_configuration_name = "appgw-${var.app_gateway_name}-${local.location}-gwipc"
8+
9+
resource_group_name = element(coalescelist(data.azurerm_resource_group.rgrp.*.name, azurerm_resource_group.rg.*.name, [""]), 0)
10+
location = element(coalescelist(data.azurerm_resource_group.rgrp.*.location, azurerm_resource_group.rg.*.location, [""]), 0)
811
}
912

1013
#----------------------------------------------------------
1114
# Resource Group, VNet, Subnet selection & Random Resources
1215
#----------------------------------------------------------
13-
data "azurerm_resource_group" "rg" {
14-
name = var.resource_group_name
16+
data "azurerm_resource_group" "rgrp" {
17+
count = var.create_resource_group == false ? 1 : 0
18+
name = var.resource_group_name
19+
}
20+
21+
resource "azurerm_resource_group" "rg" {
22+
count = var.create_resource_group ? 1 : 0
23+
name = lower(var.resource_group_name)
24+
location = var.location
25+
tags = merge({ "ResourceName" = format("%s", var.resource_group_name) }, var.tags, )
1526
}
1627

1728
data "azurerm_virtual_network" "vnet" {
1829
name = var.virtual_network_name
19-
resource_group_name = data.azurerm_resource_group.rg.name
30+
resource_group_name = var.vnet_resource_group_name == null ? local.resource_group_name : var.vnet_resource_group_name
2031
}
2132

2233
data "azurerm_subnet" "snet" {
2334
name = var.subnet_name
2435
virtual_network_name = data.azurerm_virtual_network.vnet.name
25-
resource_group_name = data.azurerm_resource_group.rg.name
36+
resource_group_name = data.azurerm_virtual_network.vnet.resource_group_name
2637
}
2738

2839
data "azurerm_log_analytics_workspace" "logws" {
2940
count = var.log_analytics_workspace_name != null ? 1 : 0
3041
name = var.log_analytics_workspace_name
31-
resource_group_name = data.azurerm_resource_group.rg.name
42+
resource_group_name = local.resource_group_name
3243
}
3344

3445
data "azurerm_storage_account" "storeacc" {
3546
count = var.storage_account_name != null ? 1 : 0
3647
name = var.storage_account_name
37-
resource_group_name = data.azurerm_resource_group.rg.name
48+
resource_group_name = local.resource_group_name
3849
}
3950

4051
#-----------------------------------
4152
# Public IP for application gateway
4253
#-----------------------------------
4354
resource "azurerm_public_ip" "pip" {
44-
name = lower("${var.app_gateway_name}-${data.azurerm_resource_group.rg.location}-gw-pip")
45-
location = data.azurerm_resource_group.rg.location
46-
resource_group_name = data.azurerm_resource_group.rg.name
55+
name = lower("${var.app_gateway_name}-${local.location}-gw-pip")
56+
location = local.location
57+
resource_group_name = local.resource_group_name
4758
allocation_method = var.sku.tier == "Standard" ? "Dynamic" : "Static"
4859
sku = var.sku.tier == "Standard" ? "Basic" : "Standard"
4960
domain_name_label = var.domain_name_label
50-
tags = merge({ "ResourceName" = lower("${var.app_gateway_name}-${data.azurerm_resource_group.rg.location}-gw-pip") }, var.tags, )
61+
tags = merge({ "ResourceName" = lower("${var.app_gateway_name}-${local.location}-gw-pip") }, var.tags, )
5162
}
5263

5364
#----------------------------------------------
5465
# Application Gateway with all optional blocks
5566
#----------------------------------------------
5667
resource "azurerm_application_gateway" "main" {
57-
name = lower("appgw-${var.app_gateway_name}-${data.azurerm_resource_group.rg.location}")
58-
resource_group_name = data.azurerm_resource_group.rg.name
59-
location = data.azurerm_resource_group.rg.location
68+
name = lower("appgw-${var.app_gateway_name}-${local.location}")
69+
resource_group_name = local.resource_group_name
70+
location = local.location
6071
enable_http2 = var.enable_http2
6172
zones = var.zones
6273
firewall_policy_id = var.firewall_policy_id != null ? var.firewall_policy_id : null
63-
tags = merge({ "ResourceName" = lower("appgw-${var.app_gateway_name}-${data.azurerm_resource_group.rg.location}") }, var.tags, )
74+
tags = merge({ "ResourceName" = lower("appgw-${var.app_gateway_name}-${local.location}") }, var.tags, )
6475

6576
sku {
6677
name = var.sku.name

variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
variable "create_resource_group" {
2+
description = "Whether to create resource group and use it for all networking resources"
3+
default = false
4+
}
5+
16
variable "resource_group_name" {
27
description = "A container that holds related resources for an Azure solution"
38
default = ""
@@ -13,6 +18,11 @@ variable "virtual_network_name" {
1318
default = ""
1419
}
1520

21+
variable "vnet_resource_group_name" {
22+
description = "The resource group name where the virtual network is created"
23+
default = null
24+
}
25+
1626
variable "subnet_name" {
1727
description = "The name of the subnet to use in VM scale set"
1828
default = ""

0 commit comments

Comments
 (0)