Skip to content

Commit 6f15f08

Browse files
committed
Welcome to Stack Simplify
1 parent fd3e6e3 commit 6f15f08

File tree

8 files changed

+216
-125
lines changed

8 files changed

+216
-125
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Provision Azure AKS Cluster using Terraform and Azure DevOps
2+
3+
## For Step by Step Instructions
4+
- [Step by Step Instructions](https://github.com/stacksimplify/azure-aks-kubernetes-masterclass/tree/master/25-Azure-DevOps-Terraform-Azure-AKS)
Lines changed: 114 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
2-
31
trigger:
42
- master
53

64
pool:
75
vmImage: 'ubuntu-latest'
86

9-
10-
# Define Variables
7+
# Define Variables for Environments
118
variables:
129
- name: DEV_ENVIRONMENT
13-
value: dev
10+
value: dev
1411
- name: QA_ENVIRONMENT
15-
value: qa
16-
12+
value: qa
1713

18-
# Stage-1: Validate Stage
19-
## Step-1: Install Latest Terraform (Ideally not needed if we use default Agents)
20-
## Step-2: Validate Terraform Manifests
14+
# Stage-1: Terraform Validate Stage
15+
## Step-1: Publish Artifacts to Pipeline (Pipeline artifacts provide a way to share files between stages in a pipeline or between different pipelines. )
16+
## Step-2: Install Latest Terraform (0.13.5) (Ideally not needed if we use default Ubuntu Agents)
17+
## Step-3: Validate Terraform Manifests (terraform init, terraform validate)
2118

2219
stages:
23-
- stage: Validate
20+
- stage: TerraformValidate
2421
jobs:
25-
- job: ValidateJob
26-
continueOnError: false
27-
steps:
28-
- publish: terraform-manifests
29-
artifact: terraform-manifests-out
22+
- job: TerraformValidateJob
23+
continueOnError: false
24+
steps:
25+
- task: PublishPipelineArtifact@1
26+
displayName: Publish Artifacts
27+
inputs:
28+
targetPath: '$(System.DefaultWorkingDirectory)/terraform-manifests'
29+
artifact: 'terraform-manifests-out'
30+
publishLocation: 'pipeline'
3031
- task: TerraformInstaller@0
31-
displayName: Terraform Installer
32+
displayName: Terraform Install
3233
inputs:
3334
terraformVersion: 'latest'
3435
- task: TerraformCLI@0
@@ -37,9 +38,9 @@ stages:
3738
command: 'init'
3839
workingDirectory: '$(System.DefaultWorkingDirectory)/terraform-manifests'
3940
backendType: 'azurerm'
40-
backendServiceArm: 'terraform-aks-azurerm-for-pipe3'
41-
backendAzureRmResourceGroupName: 'terraform-state-storage-rg2'
42-
backendAzureRmStorageAccountName: 'tfstatekalyan123'
41+
backendServiceArm: 'terraform-aks-azurerm-svc-con'
42+
backendAzureRmResourceGroupName: 'terraform-storage-rg'
43+
backendAzureRmStorageAccountName: 'terraformstatexlrwdrzs'
4344
backendAzureRmContainerName: 'tfstatefiles'
4445
backendAzureRmKey: 'aks-base.tfstate'
4546
allowTelemetryCollection: false
@@ -49,112 +50,107 @@ stages:
4950
command: 'validate'
5051
workingDirectory: '$(System.DefaultWorkingDirectory)/terraform-manifests'
5152
allowTelemetryCollection: false
52-
53-
54-
5553

5654
# Stage-2: Deploy Stages for Dev & QA
5755
# Deployment-1: Deploy Dev AKS Cluster
58-
## Step-1: Download Secure File
59-
## Step-2: Terraform Initialize (State Storage to store in Azure Storage Account)
60-
## Step-3: Terraform Plan
61-
## Step-4: Terraform Apply
62-
- stage: DeployAKS
63-
jobs:
64-
- deployment: DeployDev
65-
pool:
66-
vmImage: 'ubuntu-latest'
67-
environment: $(DEV_ENVIRONMENT)
68-
strategy:
69-
# default deployment strategy
70-
runOnce:
71-
deploy:
72-
steps:
73-
- task: DownloadSecureFile@1
74-
displayName: Download SSH Key for Linux VMs
75-
name: sshkey
76-
inputs:
77-
secureFile: 'aks-terraform-devops-ssh-key-ububtu.pub'
78-
- task: TerraformCLI@0
79-
displayName: Terraform Init
80-
inputs:
81-
command: 'init'
82-
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
83-
backendType: 'azurerm'
84-
backendServiceArm: 'terraform-aks-azurerm-for-pipe3'
85-
backendAzureRmResourceGroupName: 'terraform-state-storage-rg2'
86-
backendAzureRmStorageAccountName: 'tfstatekalyan123'
87-
backendAzureRmContainerName: 'tfstatefiles'
88-
backendAzureRmKey: 'aks-$(DEV_ENVIRONMENT).tfstate'
89-
allowTelemetryCollection: false
56+
## Step-1: Define Variables for environments
57+
## Step-2: Download SSH Secure File
58+
## Step-3: Terraform Initialize (State Storage to store in Azure Storage Account for Dev AKS Cluster)
59+
## Step-4: Terraform Plan (Create Plan)
60+
## Step-5: Terraform Apply (Use the plan created in previous step)
9061

91-
- task: TerraformCLI@0
92-
displayName: Terraform Plan
93-
inputs:
94-
command: 'plan'
95-
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
96-
environmentServiceName: 'terraform-aks-azurerm-for-pipe3'
97-
commandOptions: '-var ssh_public_key=$(sshkey.secureFilePath) -var environment=$(DEV_ENVIRONMENT) -out $(Pipeline.Workspace)/terraform-manifests-out/$(DEV_ENVIRONMENT)-$(Build.BuildId).out'
98-
allowTelemetryCollection: false
99-
100-
- task: TerraformCLI@0
101-
displayName: Terraform Apply
102-
inputs:
103-
command: 'apply'
104-
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
105-
environmentServiceName: 'terraform-aks-azurerm-for-pipe3'
106-
commandOptions: '$(Pipeline.Workspace)/terraform-manifests-out/$(DEV_ENVIRONMENT)-$(Build.BuildId).out'
107-
allowTelemetryCollection: false
62+
- stage: DeployAKSClusters
63+
jobs:
64+
- deployment: DeployDevAKSCluster
65+
displayName: DeployDevAKSCluster
66+
pool:
67+
vmImage: 'ubuntu-latest'
68+
environment: $(DEV_ENVIRONMENT)
69+
strategy:
70+
runOnce:
71+
deploy:
72+
steps:
73+
- task: DownloadSecureFile@1
74+
displayName: Download SSH Key
75+
name: sshkey
76+
inputs:
77+
secureFile: 'aks-terraform-devops-ssh-key-ububtu.pub'
78+
- task: TerraformCLI@0
79+
displayName: Terraform Init
80+
inputs:
81+
command: 'init'
82+
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
83+
backendType: 'azurerm'
84+
backendServiceArm: 'terraform-aks-azurerm-svc-con'
85+
backendAzureRmResourceGroupName: 'terraform-storage-rg'
86+
backendAzureRmStorageAccountName: 'terraformstatexlrwdrzs'
87+
backendAzureRmContainerName: 'tfstatefiles'
88+
backendAzureRmKey: 'aks-$(DEV_ENVIRONMENT).tfstate'
89+
allowTelemetryCollection: false
90+
- task: TerraformCLI@0
91+
displayName: Terraform Plan
92+
inputs:
93+
command: 'plan'
94+
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
95+
environmentServiceName: 'terraform-aks-azurerm-svc-con'
96+
commandOptions: '-var ssh_public_key=$(sshkey.secureFilePath) -var environment=$(DEV_ENVIRONMENT) -out $(Pipeline.Workspace)/terraform-manifests-out/$(DEV_ENVIRONMENT)-$(Build.BuildId).out'
97+
allowTelemetryCollection: false
98+
- task: TerraformCLI@0
99+
displayName: Terraform Apply
100+
inputs:
101+
command: 'apply'
102+
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
103+
environmentServiceName: 'terraform-aks-azurerm-svc-con'
104+
commandOptions: '$(Pipeline.Workspace)/terraform-manifests-out/$(DEV_ENVIRONMENT)-$(Build.BuildId).out'
105+
allowTelemetryCollection: false
108106

109107
# Stage-2: Deploy Stages for Dev & QA
110108
# Deployment-2: Deploy QA AKS Cluster
111109
## Step-1: Download Secure File
112110
## Step-2: Terraform Initialize (State Storage to store in Azure Storage Account)
113111
## Step-3: Terraform Plan
114-
## Step-4: Terraform Apply
115-
116-
- deployment: DeployQA
117-
dependsOn: DeployDev
118-
pool:
119-
vmImage: 'ubuntu-latest'
120-
environment: $(QA_ENVIRONMENT)
121-
strategy:
122-
# default deployment strategy
123-
runOnce:
124-
deploy:
125-
steps:
126-
- task: DownloadSecureFile@1
127-
displayName: Download SSH Key for Linux VMs
128-
name: sshkey
129-
inputs:
130-
secureFile: 'aks-terraform-devops-ssh-key-ububtu.pub'
131-
- task: TerraformCLI@0
132-
displayName: Terraform Init
133-
inputs:
134-
command: 'init'
135-
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
136-
backendType: 'azurerm'
137-
backendServiceArm: 'terraform-aks-azurerm-for-pipe3'
138-
backendAzureRmResourceGroupName: 'terraform-state-storage-rg2'
139-
backendAzureRmStorageAccountName: 'tfstatekalyan123'
140-
backendAzureRmContainerName: 'tfstatefiles'
141-
backendAzureRmKey: 'aks-$(QA_ENVIRONMENT).tfstate'
142-
allowTelemetryCollection: false
112+
## Step-4: Terraform Apply
143113

144-
- task: TerraformCLI@0
145-
displayName: Terraform Plan
146-
inputs:
147-
command: 'plan'
148-
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
149-
environmentServiceName: 'terraform-aks-azurerm-for-pipe3'
150-
commandOptions: '-var ssh_public_key=$(sshkey.secureFilePath) -var environment=$(QA_ENVIRONMENT) -out $(Pipeline.Workspace)/terraform-manifests-out/$(QA_ENVIRONMENT)-$(Build.BuildId).out'
151-
allowTelemetryCollection: false
152-
153-
- task: TerraformCLI@0
154-
displayName: Terraform Apply
155-
inputs:
156-
command: 'apply'
157-
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
158-
environmentServiceName: 'terraform-aks-azurerm-for-pipe3'
159-
commandOptions: '$(Pipeline.Workspace)/terraform-manifests-out/$(QA_ENVIRONMENT)-$(Build.BuildId).out'
160-
allowTelemetryCollection: false
114+
- deployment: DeployQAAKSCluster
115+
dependsOn: DeployDevAKSCluster
116+
displayName: DeployQAAKSCluster
117+
pool:
118+
vmImage: 'ubuntu-latest'
119+
environment: $(QA_ENVIRONMENT)
120+
strategy:
121+
runOnce:
122+
deploy:
123+
steps:
124+
- task: DownloadSecureFile@1
125+
displayName: Download SSH Key
126+
name: sshkey
127+
inputs:
128+
secureFile: 'aks-terraform-devops-ssh-key-ububtu.pub'
129+
- task: TerraformCLI@0
130+
displayName: Terraform Init
131+
inputs:
132+
command: 'init'
133+
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
134+
backendType: 'azurerm'
135+
backendServiceArm: 'terraform-aks-azurerm-svc-con'
136+
backendAzureRmResourceGroupName: 'terraform-storage-rg'
137+
backendAzureRmStorageAccountName: 'terraformstatexlrwdrzs'
138+
backendAzureRmContainerName: 'tfstatefiles'
139+
backendAzureRmKey: 'aks-$(QA_ENVIRONMENT).tfstate'
140+
allowTelemetryCollection: false
141+
- task: TerraformCLI@0
142+
displayName: Terraform Plan
143+
inputs:
144+
command: 'plan'
145+
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
146+
environmentServiceName: 'terraform-aks-azurerm-svc-con'
147+
commandOptions: '-var ssh_public_key=$(sshkey.secureFilePath) -var environment=$(QA_ENVIRONMENT) -out $(Pipeline.Workspace)/terraform-manifests-out/$(QA_ENVIRONMENT)-$(Build.BuildId).out'
148+
allowTelemetryCollection: false
149+
- task: TerraformCLI@0
150+
displayName: Terraform Apply
151+
inputs:
152+
command: 'apply'
153+
workingDirectory: '$(Pipeline.Workspace)/terraform-manifests-out'
154+
environmentServiceName: 'terraform-aks-azurerm-svc-con'
155+
commandOptions: '$(Pipeline.Workspace)/terraform-manifests-out/$(QA_ENVIRONMENT)-$(Build.BuildId).out'
156+
allowTelemetryCollection: false
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
--------------------------------------------
2+
# Terraform State Storage Azure RM
3+
Resource Group: terraform-storage-rg
4+
Storage Account: terraformstatexlrwdrzs
5+
Container: tfstatefiles
6+
7+
# Manifests Folder
8+
terraform-manifests
9+
10+
# System Default Working Directory
11+
$(System.DefaultWorkingDirectory)/terraform-manifests
12+
$(Pipeline.Workspace)/terraform-manifests-out
13+
# List Files
14+
- bash: echo Contents in Pipeline Workspace Artifact Directory; ls -Ra $(System.DefaultWorkingDirectory)/terraform-manifests
15+
--------------------------------------------
16+
# Environment Name
17+
$(DEV_ENVIRONMENT)
18+
19+
# Dev State File Name
20+
aks-$(DEV_ENVIRONMENT).tfstate
21+
22+
# Pipeline Working Directory
23+
$(Pipeline.Workspace)/terraform-manifests-out
24+
25+
# Dev Command Arguments for Terraform Plan
26+
-var ssh_public_key=$(sshkey.secureFilePath)
27+
-var environment=$(DEV_ENVIRONMENT)
28+
-out $(Pipeline.Workspace)/terraform-manifests-out/$(DEV_ENVIRONMENT)-$(Build.BuildId).out
29+
30+
# 1- way
31+
terraform plan
32+
terraform apply
33+
34+
# 2-way
35+
terraform plan -out v1plan.out
36+
terraform apply v1plan.out
37+
38+
39+
# Dev Command Arguments for Terraform Apply
40+
$(Pipeline.Workspace)/terraform-manifests-out/$(DEV_ENVIRONMENT)-$(Build.BuildId).out
41+
42+
# List Files
43+
- bash: echo Contents in Pipeline Workspace Artifact Directory; ls -Ra $(Pipeline.Workspace)/terraform-manifests-out
44+
--------------------------------------------
45+
# Environment Name
46+
$(QA_ENVIRONMENT)
47+
48+
# QA State File
49+
aks-$(QA_ENVIRONMENT).tfstate
50+
51+
# Pipeline Working Directory
52+
$(Pipeline.Workspace)/terraform-manifests-out
53+
54+
# QA Command Arguments for Terraform Plan
55+
-var ssh_public_key=$(sshkey.secureFilePath)
56+
-var environment=$(QA_ENVIRONMENT)
57+
-out $(Pipeline.Workspace)/terraform-manifests-out/$(QA_ENVIRONMENT)-$(Build.BuildId).out
58+
59+
# QA Command Arguments for Terraform Apply
60+
$(Pipeline.Workspace)/terraform-manifests-out/$(QA_ENVIRONMENT)-$(Build.BuildId).out
61+
--------------------------------------------
62+
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Create Azure AD Group in Active Directory for AKS Admins
22
resource "azuread_group" "aks_administrators" {
3-
name = "${azurerm_resource_group.aks_rg.name}-${var.environment}-administrators"
4-
description = "Azure AKS Kubernetes administrators for the ${azurerm_resource_group.aks_rg.name}-${var.environment} cluster."
3+
name = "${azurerm_resource_group.aks_rg.name}-administrators"
4+
description = "Azure AKS Kubernetes administrators for the ${azurerm_resource_group.aks_rg.name}-administrators cluster."
55
}

25-Azure-DevOps-Terraform-Azure-AKS/Git-Repo-Files/terraform-manifests/07-aks-cluster.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "azurerm_kubernetes_cluster" "aks_cluster" {
2-
dns_prefix = "${azurerm_resource_group.aks_rg.name}-${var.environment}"
2+
dns_prefix = "${azurerm_resource_group.aks_rg.name}"
33
location = azurerm_resource_group.aks_rg.location
44
name = "${azurerm_resource_group.aks_rg.name}-cluster"
55
resource_group_name = azurerm_resource_group.aks_rg.name

25-Azure-DevOps-Terraform-Azure-AKS/Git-Repo-Files/terraform-manifests/09-aks-cluster-linux-user-nodepools.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Create Linux Azure AKS Node Pool
2-
2+
/*
33
resource "azurerm_kubernetes_cluster_node_pool" "linux101" {
44
availability_zones = [1, 2, 3]
55
enable_auto_scaling = true
@@ -26,4 +26,4 @@ resource "azurerm_kubernetes_cluster_node_pool" "linux101" {
2626
"app" = "java-apps"
2727
}
2828
}
29-
29+
*/

25-Azure-DevOps-Terraform-Azure-AKS/Git-Repo-Files/terraform-manifests/10-aks-cluster-windows-user-nodepools.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Create Windows Azure AKS Node Pool
2-
2+
/*
33
resource "azurerm_kubernetes_cluster_node_pool" "win101" {
44
availability_zones = [1, 2, 3]
55
enable_auto_scaling = true
@@ -26,4 +26,4 @@ resource "azurerm_kubernetes_cluster_node_pool" "win101" {
2626
"app" = "dotnet-apps"
2727
}
2828
}
29-
29+
*/

0 commit comments

Comments
 (0)