Welcome to the Azure Infrastructure Setup repository! This repository contains Terraform configurations to create and manage an Azure environment with a resource group, virtual network, subnets, public IP, NAT gateway, and necessary associations.
Before you begin, ensure you have the following installed:
Step 1: Create terraform.tfvars file Create a file named terraform.tfvars and add the following content:
name = "resource-group-name"
Step 2: Apply the Terraform Configuration Run the following command to apply your Terraform configuration using the variables defined in terraform.tfvars:
terraform apply -var-file="terraform.tfvars"
Creates an Azure resource group.
resource "azurerm_resource_group" "this" {
location = var.location
name = "${var.name}-Group"
}
provider "azurerm" {
features {}
}
Creates a virtual network in Azure.
resource "azurerm_virtual_network" "VN" {
name = var.name
address_space = [var.address_space]
location = azurerm_resource_group.this.location
resource_group_name = azurerm_resource_group.this.name
}
Creates public and private subnets.
resource "azurerm_subnet" "public-subnet" {
name = "${var.name}-public-subnet"
resource_group_name = azurerm_resource_group.this.name
virtual_network_name = azurerm_virtual_network.VN.name
address_prefixes = [cidrsubnet(var.address_space, 8, 1)]
}
resource "azurerm_subnet" "private-subnet" {
name = "${var.name}-private-subnet"
resource_group_name = azurerm_resource_group.this.name
virtual_network_name = azurerm_virtual_network.VN.name
address_prefixes = [cidrsubnet(var.address_space, 8, 10)]
}
Creates a static public IP address.
resource "azurerm_public_ip" "public-IP" {
name = "${var.name}-Public-IP"
location = azurerm_resource_group.this.location
resource_group_name = azurerm_resource_group.this.name
allocation_method = "Static"
sku = "Standard"
zones = [1]
}
Creates a NAT gateway.
resource "azurerm_nat_gateway" "natgateway" {
name = "${var.name}-NAT"
location = azurerm_resource_group.this.location
resource_group_name = azurerm_resource_group.this.name
sku_name = "Standard"
idle_timeout_in_minutes = 10
zones = [1]
}
Associates the NAT gateway with the public IP and private subnet.
resource "azurerm_nat_gateway_public_ip_association" "this" {
nat_gateway_id = azurerm_nat_gateway.natgateway.id
public_ip_address_id = azurerm_public_ip.public-IP.id
}
resource "azurerm_subnet_nat_gateway_association" "this" {
subnet_id = azurerm_subnet.private-subnet.id
nat_gateway_id = azurerm_nat_gateway.natgateway.id
}
-
Clone the repository:
git clone https://github.com/yourusername/azure-infrastructure-setup.git cd azure-infrastructure-setup
-
Initialize Terraform:
terraform init
-
Apply the configuration:
terraform apply
For more details, check the Terraform documentation.
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.