Compare commits
10 Commits
bdb8e87790
...
6c6eb8e161
| Author | SHA1 | Date | |
|---|---|---|---|
| 6c6eb8e161 | |||
| 5d09f36744 | |||
| af6e73101f | |||
| c12311ab9f | |||
| dfcbe54ebe | |||
| b2b2ad94b7 | |||
| 49c0913f2b | |||
| 0049839d98 | |||
| 59060033d3 | |||
| 8496c9de14 |
@@ -0,0 +1,31 @@
|
||||
# name: Terraform
|
||||
|
||||
# on:
|
||||
# pull_request:
|
||||
# branches: [ "main" ]
|
||||
# push:
|
||||
# branches: [ "main" ]
|
||||
|
||||
# jobs:
|
||||
# terraform:
|
||||
# runs-on: ubuntu-latest
|
||||
|
||||
# steps:
|
||||
# - name: Checkout code
|
||||
# uses: actions/checkout@v3
|
||||
|
||||
# - name: Setup Terraform
|
||||
# uses: hashicorp/setup-terraform@v2
|
||||
|
||||
# - name: Terraform Init
|
||||
# run: terraform -chdir=environments/dev init
|
||||
|
||||
# - name: Terraform Validate
|
||||
# run: terraform -chdir=environments/dev validate
|
||||
|
||||
# - name: Terraform Plan
|
||||
# run: terraform -chdir=environments/dev plan
|
||||
|
||||
# - name: Terraform Apply
|
||||
# if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
||||
# run: terraform -chdir=environments/dev apply -auto-approve
|
||||
@@ -1,6 +1,9 @@
|
||||
# Terraform
|
||||
.terraform/
|
||||
*.tfstate
|
||||
*.tfstate.backup
|
||||
.terraform.lock.hcl
|
||||
|
||||
# Local configs
|
||||
terraform.tfvars
|
||||
*.override.tf
|
||||
@@ -1 +1,21 @@
|
||||
# Terraform Azure Bootcamp
|
||||
|
||||
This repo contains hands-on Terraform projects on Azure, following a structured learning path:
|
||||
1. Foundations (VM, Virtual Network)
|
||||
2. Modules
|
||||
3. Remote State & Environments
|
||||
4. Scaling & Advanced Patterns
|
||||
5. Security & Testing
|
||||
6. Production-Ready 3-Tier Architecture
|
||||
|
||||
## 📂 Structure
|
||||
- `modules/` → reusable Terraform modules (network, compute, database, etc.)
|
||||
- `environments/` → environment-specific configs (`dev`, `prod`)
|
||||
- `.github/workflows/terraform.yml` → CI/CD with GitHub Actions
|
||||
|
||||
## 🚀 Usage
|
||||
```bash
|
||||
cd environments/dev
|
||||
terraform init
|
||||
terraform plan
|
||||
terraform apply
|
||||
@@ -0,0 +1,8 @@
|
||||
terraform {
|
||||
backend "azurerm" {
|
||||
resource_group_name = "terraform"
|
||||
storage_account_name = "tfstoracccom"
|
||||
container_name = "tfstate"
|
||||
key = "dev.terraform.tfstate"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
terraform {
|
||||
required_providers {
|
||||
azurerm = {
|
||||
source = "hashicorp/azurerm"
|
||||
version = "4.46.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
provider "azurerm" {
|
||||
features{}
|
||||
subscription_id = "4b2d6154-f4c5-4854-be82-12dfc82d4d23"
|
||||
}
|
||||
|
||||
module "network" {
|
||||
source = "../../modules/network"
|
||||
vnet_name = "dev-vnet"
|
||||
resource_group_name = "terraform"
|
||||
location = "UK South"
|
||||
address_space = ["10.0.0.0/16"]
|
||||
subnets = { public = "10.0.1.0/24", private = "10.0.2.0/24" }
|
||||
environment = "dev"
|
||||
project = "TerraformBootcamp"
|
||||
}
|
||||
|
||||
data "azurerm_ssh_public_key" "kp" {
|
||||
name = "terraform-kp"
|
||||
resource_group_name = "terraform"
|
||||
}
|
||||
|
||||
data "azurerm_ssh_public_key" "sd" {
|
||||
name = "sbcomsam-d-01"
|
||||
resource_group_name = "terraform"
|
||||
}
|
||||
|
||||
data "http" "my_ip" {
|
||||
url = "https://checkip.amazonaws.com/"
|
||||
}
|
||||
|
||||
locals {
|
||||
my_ip = chomp(data.http.my_ip.response_body)
|
||||
}
|
||||
|
||||
module "compute" {
|
||||
source = "../../modules/compute"
|
||||
resource_group_name = "terraform"
|
||||
location = "UK South"
|
||||
subnet_id = module.network.subnet_ids["public"]
|
||||
vm_size = "Standard_B1s"
|
||||
admin_username = "azureuser"
|
||||
ssh_public_key = data.azurerm_ssh_public_key.sd.public_key
|
||||
ex_ip = local.my_ip
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
output "my_ip" {
|
||||
value = local.my_ip
|
||||
}
|
||||
|
||||
output "vm_public_ip" {
|
||||
value = module.compute.vm_public_ip.ip_address
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
resource "azurerm_network_interface" "nic" {
|
||||
name = "vnic"
|
||||
location = var.location
|
||||
resource_group_name = var.resource_group_name
|
||||
|
||||
ip_configuration {
|
||||
name = "internal"
|
||||
subnet_id = var.subnet_id
|
||||
private_ip_address_allocation = "Dynamic"
|
||||
public_ip_address_id = azurerm_public_ip.vm_public.id
|
||||
}
|
||||
}
|
||||
|
||||
resource "azurerm_network_interface_security_group_association" "example" {
|
||||
network_interface_id = azurerm_network_interface.nic.id
|
||||
network_security_group_id = azurerm_network_security_group.vm_nsg.id
|
||||
|
||||
depends_on = [azurerm_network_security_group.vm_nsg]
|
||||
}
|
||||
|
||||
resource "azurerm_network_security_group" "vm_nsg" {
|
||||
name = "vm-nsg"
|
||||
resource_group_name = var.resource_group_name
|
||||
location = var.location
|
||||
|
||||
security_rule {
|
||||
name = "SSH"
|
||||
priority = 1001
|
||||
direction = "Inbound"
|
||||
access = "Allow"
|
||||
protocol = "Tcp"
|
||||
source_port_range = "*"
|
||||
destination_port_range = "22"
|
||||
source_address_prefix = var.ex_ip
|
||||
destination_address_prefix = "*"
|
||||
}
|
||||
}
|
||||
|
||||
resource "azurerm_public_ip" "vm_public" {
|
||||
name = "example-pip"
|
||||
location = var.location
|
||||
resource_group_name = var.resource_group_name
|
||||
allocation_method = "Static"
|
||||
}
|
||||
|
||||
resource "azurerm_linux_virtual_machine" "vm" {
|
||||
name = "example-vm"
|
||||
resource_group_name = var.resource_group_name
|
||||
location = var.location
|
||||
size = var.vm_size
|
||||
admin_username = var.admin_username
|
||||
network_interface_ids = [azurerm_network_interface.nic.id]
|
||||
admin_ssh_key {
|
||||
username = var.admin_username
|
||||
public_key = var.ssh_public_key
|
||||
}
|
||||
os_disk {
|
||||
caching = "ReadWrite"
|
||||
storage_account_type = "Standard_LRS"
|
||||
}
|
||||
source_image_reference {
|
||||
publisher = "Canonical"
|
||||
offer = "0001-com-ubuntu-server-jammy"
|
||||
sku = "22_04-lts-gen2"
|
||||
version = "latest"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
output "vm_public_ip" {
|
||||
value = azurerm_public_ip.vm_public
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
variable "resource_group_name" { type = string }
|
||||
variable "location" { type = string }
|
||||
variable "subnet_id" { type = string }
|
||||
variable "vm_size" { type = string }
|
||||
variable "admin_username" { type = string }
|
||||
variable "ssh_public_key" { type = string }
|
||||
variable "ex_ip" { type = string }
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
resource "azurerm_virtual_network" "main" {
|
||||
name = var.vnet_name
|
||||
resource_group_name = var.resource_group_name
|
||||
location = var.location
|
||||
address_space = var.address_space
|
||||
}
|
||||
|
||||
resource "azurerm_subnet" "subnets" {
|
||||
for_each = var.subnets
|
||||
name = each.key
|
||||
resource_group_name = var.resource_group_name
|
||||
virtual_network_name = azurerm_virtual_network.main.name
|
||||
address_prefixes = [each.value]
|
||||
}
|
||||
|
||||
@@ -1 +1,7 @@
|
||||
output "vnet_id" { value = "<placeholder>" }
|
||||
output "vnet_id" {
|
||||
value = azurerm_virtual_network.main.id
|
||||
}
|
||||
|
||||
output "subnet_ids" {
|
||||
value = { for s in azurerm_subnet.subnets : s.name => s.id }
|
||||
}
|
||||
@@ -1 +1,36 @@
|
||||
variable "resource_group_name" { type = string }
|
||||
variable "vnet_name" {
|
||||
description = "Name of the Virtual Network"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "resource_group_name" {
|
||||
description = "Resource Group to create resources in"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "location" {
|
||||
description = "Azure region"
|
||||
type = string
|
||||
}
|
||||
|
||||
variable "address_space" {
|
||||
description = "CIDR blocks for the VNet"
|
||||
type = list(string)
|
||||
}
|
||||
|
||||
variable "subnets" {
|
||||
description = "Map of subnet names to CIDR blocks"
|
||||
type = map(string)
|
||||
}
|
||||
|
||||
variable "environment" {
|
||||
description = "Environment tag (dev/prod)"
|
||||
type = string
|
||||
default = "dev"
|
||||
}
|
||||
|
||||
variable "project" {
|
||||
description = "Project tag"
|
||||
type = string
|
||||
default = "TerraformBootcamp"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user