This project provisions a complete Azure environment using Terraform to automate the deployment of a Linux Virtual Machine (VM) with networking, security, and Docker installation. The configuration is designed for reproducibility and compliance with organizational policies, such as requiring a Network Security Group (NSG) on the NIC.
- Terraform: Infrastructure as Code (IaC) tool for provisioning and managing Azure resources.
- Azure Portal: Occasionally used for manual resource creation and verification.
- Azure CLI: Used for authentication (
az login
) and resource inspection. - VS Code: Editor for writing and managing Terraform and shell/cloud-init scripts.
- SSH: For connecting to the provisioned VM.
- Creates an Azure Resource Group (
mtc-resources
) in theEast US
region. - Creates a Virtual Network (
mtc-network
) and a Subnet (mtc-subnet
). - Creates a Network Security Group (
mtc-sg
) with an inbound rule (mtc-dev-rule
) allowing all traffic (for demo/dev purposes). - Associates the NSG with the Subnet to comply with Azure policy.
- Creates a Public IP Address (
mtc-ip
) with Basic SKU and Dynamic allocation. - Creates a Network Interface (
mtc-nic
) attached to the subnet and public IP, with the required NSG association. - Creates a Linux Virtual Machine (
mtc-vm
) using an Ubuntu 20.04 LTS image, with SSH key authentication. - Bootstraps the VM with Docker using a custom data script (
customdata.tpl
). - Configures local SSH using a template (
mac-ssh-script.tpl
) for easy access. - Outputs the VM's public IP address for quick connection.
main.tf
: Main Terraform configuration for all Azure resources.variables.tf
: Defines input variables (e.g.,host_os
).customdata.tpl
: Bash script for installing Docker on the VM at boot.mac-ssh-script.tpl
: Template for updating your local SSH config.terraform.tfstate
: Terraform state file (do not edit manually).
-
Terraform Initialization and Authentication
- Ran
terraform init
to initialize the project. - Logged in to Azure using
az login
and set the correct subscription.
- Ran
-
Resource Creation
- Defined all resources in
main.tf
. - Used
terraform apply
to provision resources. - When Azure policy required an NSG on the NIC at creation, either:
- Upgraded the AzureRM provider to v3.x+ to use
network_security_group_id
(if possible), or - Created the NIC in the Azure Portal and imported it with
terraform import
.
- Upgraded the AzureRM provider to v3.x+ to use
- Defined all resources in
-
Manual Steps (if needed)
- Created and configured the NIC in the Azure Portal when policy blocked Terraform-only creation.
- Associated the public IP and NSG with the NIC via the portal UI.
- Imported manually created resources into Terraform state.
-
VM Provisioning
- Used a custom data script (
customdata.tpl
) to install Docker automatically on the VM. - Verified Docker installation by SSH-ing into the VM.
- Used a custom data script (
-
SSH Configuration
- Used a template (
mac-ssh-script.tpl
) to update the local SSH config for easy access to the VM.
- Used a template (
-
Output
- Used a Terraform output to display the VM name and public IP after deployment.
- Azure Policy Enforcement: Some policies require NSG attachment at NIC creation, which may require provider upgrades or manual steps.
- Provider Version Compatibility: Features like
network_security_group_id
on NICs require AzureRM provider v3.x+. - Resource Importing: Manual Azure Portal resources can be imported into Terraform state for continued management.
- Dynamic Public IPs: Azure only allocates a dynamic public IP after the VM is running and the NIC is attached.
- Custom Data Scripts: Ensure correct syntax and permissions for successful VM bootstrapping.
- Clone the repository and navigate to the project directory.
- Run
terraform init
to initialize. - Run
az login
to authenticate with Azure. - Run
terraform apply
to provision resources. - Use the outputted public IP to SSH into your VM.
To destroy all resources:
terraform destroy
- Terraform Azure Provider Documentation
- Azure Portal
- Azure CLI Documentation
- Cloud-init Documentation
- Learn Terraform with Azure by Building a Dev Environment