Skip to content

Commit 4a5a7bd

Browse files
DEVOPS-279 terraform module for azure vnet init commit (#9)
* DEVOPS-279 terraform module for azure vnet init commit * DEVOPS-279 fix typos * DEVOPS-279 fix rule isssue bya dding depends on * fix nsg rule issue clash DEVOPS-279
1 parent 601a4a3 commit 4a5a7bd

File tree

6 files changed

+221
-1
lines changed

6 files changed

+221
-1
lines changed

virtual-network/nsg.tf

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Inbound rules
2+
resource "azurerm_network_security_rule" "port80" {
3+
name = "AllowHttpOn80"
4+
description = "port 80 open for HTTP traffic"
5+
priority = 150
6+
direction = "Inbound"
7+
access = "Allow"
8+
protocol = "Tcp"
9+
source_port_range = "*"
10+
destination_port_range = "80"
11+
source_address_prefix = "Internet"
12+
destination_address_prefix = "VirtualNetwork"
13+
resource_group_name = azurerm_resource_group.rg.name
14+
network_security_group_name = azurerm_network_security_group.nsg.name
15+
}
16+
17+
resource "azurerm_network_security_rule" "port22" {
18+
name = "AllowSSHOn22"
19+
priority = 160
20+
description = "Ssh on port 22"
21+
direction = "Inbound"
22+
access = "Allow"
23+
protocol = "Tcp"
24+
source_port_range = "*"
25+
destination_port_range = "22"
26+
source_address_prefix = "*"
27+
destination_address_prefix = "VirtualNetwork"
28+
resource_group_name = azurerm_resource_group.rg.name
29+
network_security_group_name = azurerm_network_security_group.nsg.name
30+
depends_on = [azurerm_network_security_rule.port80]
31+
}
32+
33+
resource "azurerm_network_security_rule" "port8080" {
34+
name = "AllowAnyCustom8080Inbound"
35+
priority = 170
36+
description = "port 8080 opened for jenkins deployment as a docker container"
37+
direction = "Inbound"
38+
access = "Allow"
39+
protocol = "*"
40+
source_port_range = "*"
41+
destination_port_range = "8080"
42+
source_address_prefix = "*"
43+
destination_address_prefix = "VirtualNetwork"
44+
resource_group_name = azurerm_resource_group.rg.name
45+
network_security_group_name = azurerm_network_security_group.nsg.name
46+
depends_on = [azurerm_network_security_rule.port22,
47+
azurerm_network_security_rule.port80
48+
]
49+
}
50+
51+
resource "azurerm_network_security_rule" "port4243" {
52+
name = "AllowAnyCustom4243Inbound"
53+
priority = 180
54+
description = "TCP connection jenkins + docker set"
55+
direction = "Inbound"
56+
access = "Allow"
57+
protocol = "Tcp"
58+
source_port_range = "*"
59+
destination_port_range = "4243"
60+
source_address_prefix = "*"
61+
destination_address_prefix = "VirtualNetwork"
62+
resource_group_name = azurerm_resource_group.rg.name
63+
network_security_group_name = azurerm_network_security_group.nsg.name
64+
depends_on = [azurerm_network_security_rule.port22,
65+
azurerm_network_security_rule.port80, azurerm_network_security_rule.port8080
66+
]
67+
}

virtual-network/output.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
output "resource_group_name" {
2+
value = azurerm_resource_group.rg.name
3+
description = "Azure resource group name"
4+
}
5+
6+
output "location" {
7+
description = "Azure Location"
8+
value = azurerm_resource_group.rg.location
9+
}
10+
11+
output "vnet_name" {
12+
description = "Azure Virtual network name"
13+
value = azurerm_virtual_network.vnet.name
14+
}
15+
16+
output "vnet_address_range" {
17+
description = "Azure Vnet address range"
18+
value = azurerm_virtual_network.vnet.address_space
19+
}
20+
21+
output "subnet_ids" {
22+
description = "List of all subnet IDs"
23+
value = azurerm_subnet.subnet[*].id # Collect all subnet IDs
24+
}
25+
26+
output "network_security_group_id" {
27+
description = "Azure NSG ID"
28+
value = azurerm_network_security_group.nsg.id
29+
}
30+

virtual-network/providers.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
terraform {
2+
required_version = "~> 1.3"
3+
required_providers {
4+
azurerm = {
5+
source = "hashicorp/azurerm"
6+
version = "<= 4.0"
7+
}
8+
random = {
9+
source = "hashicorp/random"
10+
version = ">= 3.1"
11+
}
12+
}
13+
}
14+
provider "azurerm" {
15+
features {}
16+
}

virtual-network/variables.tf

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
variable "resource_group_name" {
2+
default = ""
3+
description = "Azure Vnet resource group name"
4+
type = string
5+
}
6+
7+
variable "vnet_name" {
8+
default = ""
9+
description = "Azure Vnet name"
10+
type = string
11+
}
12+
13+
variable "location" {
14+
default = ""
15+
description = "Azure location"
16+
type = string
17+
}
18+
19+
variable "application_name" {
20+
default = ""
21+
description = "Azure application name tag"
22+
type = string
23+
}
24+
25+
variable "environment" {
26+
default = ""
27+
description = "Environment tag value in Azure"
28+
type = string
29+
validation {
30+
condition = contains(["DEV", "QA", "UAT", "PROD"], var.environment)
31+
error_message = "Environment value should be one among DEV or QA or UAT or PROD."
32+
}
33+
}
34+
35+
variable "vnet_address_space" {
36+
description = "Azure VNET address space"
37+
type = list(string)
38+
default = []
39+
validation {
40+
condition = length(var.vnet_address_space) > 0
41+
error_message = "The address_space variable must contain at least one CIDR block."
42+
}
43+
}
44+
45+
variable "subnet_cidrs" {
46+
default = []
47+
type = list(string)
48+
description = "Azure Subnet Ip addresses"
49+
validation {
50+
condition = length(var.subnet_cidrs) > 0
51+
error_message = "Atleast a single subnet CIDR is required."
52+
}
53+
}
54+

virtual-network/vnet.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
resource "azurerm_resource_group" "rg" {
2+
name = upper(var.resource_group_name)
3+
location = var.location
4+
tags = {
5+
Environment = upper(var.environment)
6+
Orchestrator = "Terraform"
7+
DisplayName = upper(var.resource_group_name)
8+
ApplicationName = lower(var.application_name)
9+
}
10+
}
11+
12+
# Create NSG
13+
resource "azurerm_network_security_group" "nsg" {
14+
name = upper("${var.vnet_name}-nsg")
15+
location = var.location
16+
resource_group_name = upper(var.resource_group_name)
17+
tags = {
18+
Environment = upper(var.environment)
19+
Orchestrator = "Terraform"
20+
DisplayName = upper(var.resource_group_name)
21+
ApplicationName = lower(var.application_name)
22+
}
23+
}
24+
25+
# Create a Vnet
26+
resource "azurerm_virtual_network" "vnet" {
27+
name = upper(var.vnet_name)
28+
resource_group_name = upper(var.resource_group_name)
29+
location = var.location
30+
address_space = var.vnet_address_space
31+
tags = {
32+
Environment = upper(var.environment)
33+
Orchestrator = "Terraform"
34+
DisplayName = upper(var.resource_group_name)
35+
ApplicationName = lower(var.application_name)
36+
}
37+
}
38+
39+
# Create subnets dynamically based on the provided CIDR blocks
40+
resource "azurerm_subnet" "subnet" {
41+
count = length(var.subnet_cidrs) # Create as many subnets as in the CIDR list
42+
name = upper("${var.vnet_name}-subnet-${count.index}") # Dynamic subnet name: subnet-0, subnet-1, etc.
43+
resource_group_name = azurerm_resource_group.rg.name
44+
virtual_network_name = azurerm_virtual_network.vnet.name
45+
address_prefixes = [element(var.subnet_cidrs, count.index)] # Assign each CIDR from the list
46+
}
47+
48+
# Associate subnets to NSG
49+
resource "azurerm_subnet_network_security_group_association" "subnet2nsg" {
50+
count = length(var.subnet_cidrs)
51+
subnet_id = azurerm_subnet.subnet[count.index].id
52+
network_security_group_id = azurerm_network_security_group.nsg.id
53+
}

vmss-linux/providers.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ terraform {
33
required_providers {
44
azurerm = {
55
source = "hashicorp/azurerm"
6-
version = "~> 3.0"
6+
version = "<= 4.0"
77
}
88
random = {
99
source = "hashicorp/random"

0 commit comments

Comments
 (0)