This repository contains Terraform code to provision scalable Azure infrastructure for deploying NGINX applications with Docker support.
The infrastructure supports:
- Multiple environments (dev, staging, prod) using Terraform workspaces
- Azure deployment with VMs, Virtual Networks, and Application Gateway
- Modular architecture with reusable Terraform modules
- Load balancing with HTTPS using self-signed certificates
- Remote state management with locking in Azure Storage Account
- CI/CD integration with Jenkins
- compute: Creates Azure VMs with Docker and NGINX
- networking: Sets up Virtual Network, subnets, NAT Gateway
- loadbalancer: Deploys Azure Application Gateway with SSL
- nginx-app: Generates Docker setup scripts with OpenSSL certs
- dev: Single instance in nginx-dev-rg
- staging: Single instance in nginx-staging-rg
- prod: Multi-instance in nginx-prod-rg
- Terraform >= 1.5
- Azure CLI configured
- Docker
- Jenkins (for CI/CD)
- Azure Subscription with Contributor permissions
-
Log in to Azure CLI:
az login
-
Create a Resource Group for Terraform state:
az group create --name terraform-state-vs --location eastus
-
Create a Storage Account:
az storage account create --name terraformersprime --resource-group terraform-state-vs --location eastus --sku Standard_LRS
-
Create a Storage Container:
az storage container create --name tfstate --account-name terraformersprime
-
Create a Service Principal with Contributor role:
az ad sp create-for-rbac --name nginx-sp --role Contributor --scopes /subscriptions/YOUR_SUBSCRIPTION_ID
Note the
appId,password,tenant.
-
Run Jenkins in Docker:
docker run -d -p 8080:8080 -p 50000:50000 -v jenkins_home:/var/jenkins_home --name jenkins jenkins/jenkins:lts
-
Access Jenkins at
http://localhost:8080and complete setup. -
Install necessary plugins: Terraform, Azure Credentials.
-
Add Azure credentials in Jenkins:
- Go to Manage Jenkins > Manage Credentials > System > Global credentials
- Add credentials for:
azure-client-id: The appId from SPazure-client-secret: The password from SPazure-subscription-id: Your subscription IDazure-tenant-id: The tenant from SP
-
Create a new Pipeline job:
- Name: nginx-deployment
- Pipeline: Pipeline script from SCM
- SCM: Git
- Repository URL: https://github.com/VahantSharma/MultiCloudNginx.git
- Script Path: Jenkinsfile
Copy the example tfvars files and update with your values:
cp dev.tfvars.example dev.tfvars
cp staging.tfvars.example staging.tfvars
cp prod.tfvars.example prod.tfvarsEdit the .tfvars files (these are gitignored):
- Replace
REPLACE_WITH_YOUR_SUBSCRIPTION_IDwith your Azure Subscription ID - Replace
REPLACE_WITH_YOUR_TENANT_IDwith your Tenant ID - Replace
REPLACE_WITH_YOUR_SSH_PUBLIC_KEYwith your SSH public key
For prod, ensure instance_count = 2 for scalability.
For local testing, initialize with backend config:
terraform init -backend-config="resource_group_name=terraform-state-vs" -backend-config="storage_account_name=terraformersprime" -backend-config="container_name=tfstate" -backend-config="key=terraform-dev.tfstate"terraform workspace select dev # or 'terraform workspace new dev'
terraform plan -var-file=dev.tfvars
terraform apply -var-file=dev.tfvars- In Jenkins, select the pipeline job.
- Click "Build with Parameters".
- Choose ENVIRONMENT (dev, staging, prod) and ACTION (plan, apply, destroy).
- Click Build.
The pipeline will:
- Initialize Terraform
- Select/Create the workspace for the environment
- Plan or Apply the infrastructure
- For apply, provision VMs, network, load balancer with HTTPS
After deployment:
- Get the Application Gateway public IP from Terraform outputs or Azure portal.
- Visit
https://<app-gateway-ip>(accept self-signed cert warning). - The NGINX app should respond with "Hello from NGINX over HTTPS!" on HTTPS, and redirect HTTP to HTTPS.
To scale compute:
- Update
instance_countin the respective.tfvarsfile. - Run the Jenkins pipeline with apply for that environment.
The load balancer will automatically distribute traffic to all instances.
- Use Service Principal for authentication, store secrets securely in Jenkins.
- NSG allows SSH and HTTP/HTTPS; restrict
allowed_ipsin prod. - Self-signed certificates are used; replace with CA-signed for production.
- VMs have no public IPs; access via bastion or VPN if needed.
- If Terraform init fails, ensure Azure credentials are correct.
- If VM deployment fails, check SSH key and admin username.
- If HTTPS doesn't work, verify certificates and ports.
To add DNS failover:
- Create a Route 53 hosted zone or use Azure DNS.
- Point
nginx.example.comto the Application Gateway IP. - For multi-region, add another deployment and use latency-based routing.