Skip to content

Commit bc83f56

Browse files
authored
Merge pull request #5 from LaurentLesle/deployment-scripts-to-support-multiple-developers
Deployment scripts to support multiple developers
2 parents 40948e9 + 705b77d commit bc83f56

File tree

11 files changed

+127
-43
lines changed

11 files changed

+127
-43
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
**/.terraform
22
**/*.tfstate
33
.DS_Store
4+
**/terraform.tfstate.d
5+
**/terraform.tfstate.backup
6+
**/.terraform.tfstate.lock.info

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
Deployment steps
3+
4+
Initialise the Terraform remote state on Azure blob storage by running
5+
6+
./deploy.sh
7+
8+
When the state has been initialized, you can plan the blueprint
9+
10+
11+
./deploy.sh step1-aks plan
12+
13+
Apply changes with
14+
15+
./deploy.sh step1-aks apply
16+
17+
18+
To allow multiple deployments in the same subscription
19+
20+
One of the goal of this template is to support multiple developers working in parallel in the same azure subscription.
21+
22+
To achieve that the template is using a prefix to used to identify the resource groups:
23+
24+
zlra-TERRAFORM-STATE\
25+
zlra-AKS-CLUSTER1-NETWORKING
26+
27+
This is very convenient as multiple developers will have different prefixes and does not impact each others.
28+
29+
The other benefit is for bug fixing. Sometimes with Terraform fixing a bug breaks the current deployed infrastructure. As you are working towards a stable version of your blueprint you mays want to create a branch and work against a different tfstate file.
30+
31+
You can achieve that with the terraform workspaces who are isolating your different tfstates.
32+
33+
You can map the terraform workspace with the branch name \
34+
35+
terraform workspace list\
36+
default\
37+
"* master
38+

deploy.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/bin/bash
2+
3+
# To run the deployment:
4+
# Initialise the remote state first with ./deploy.sh
5+
# ./deploy.sh step1-aks [plan|apply|destroy]
6+
7+
# capture the current path
8+
current_path=$(pwd)
9+
path=$1
10+
tf_command=$2
11+
12+
13+
function initialize_state {
14+
cd tfstate
15+
terraform init
16+
terraform apply -auto-approve
17+
cd "${current_path}"
18+
}
19+
20+
function deploy_blueprint {
21+
cd tfstate
22+
storage_account_name=$(terraform output storage_account_name)
23+
echo ${storage_account_name}
24+
access_key=$(terraform output access_key)
25+
container=$(terraform output container)
26+
prefix=$(terraform output prefix)
27+
tf_name="${prefix}.tfstate"
28+
29+
cd "../${path}"
30+
pwd
31+
32+
terraform init \
33+
-reconfigure \
34+
-backend=true \
35+
-lock=false \
36+
-backend-config storage_account_name=${storage_account_name} \
37+
-backend-config container_name=${container} \
38+
-backend-config access_key=${access_key} \
39+
-backend-config key=${tf_name}
40+
41+
terraform ${tf_command} \
42+
-var prefix=${prefix}
43+
44+
cd "${current_path}"
45+
}
46+
47+
48+
# Initialise storage account to store remote terraform state
49+
if [[ -z "${path}" && -z "$2" ]]; then
50+
initialize_state
51+
fi
52+
53+
if [[ -n "${path}" && -n "${tf_command}" ]]; then
54+
echo ''
55+
echo "Deploying blueprint '${path}' with terraform command '${tf_command}'"
56+
echo ''
57+
deploy_blueprint
58+
else
59+
echo ''
60+
echo 'You have to run at least once ./deploy.sh with no parameters to setup the remote state.'
61+
echo 'To deploy a bluepring you have to specify the sub-folder name and the terraform command [plan|apply|destroy]'
62+
echo './deploy.sh step1-aks plan'
63+
echo ''
64+
echo 'Note: the script does the terraform init for you.'
65+
fi

step1-aks/deploy.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

step1-aks/gg

Whitespace-only changes.

step1-aks/init.tf

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11

22
provider "azurerm" {
3-
version = "~>1.25"
3+
version = "~>1.27.1"
44
}
55

66
terraform {
77
backend "azurerm" {}
88
}
99

10-
11-
# Used to make sure delete / re-create generate brand new names and reduce risk of being throttled during dev activities
12-
resource "random_string" "prefix" {
13-
length = 4
14-
special = false
15-
upper = false
16-
number = false
17-
}

step1-aks/main.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
module "resource_group" {
33
source = "modules/resource_group"
44

5-
prefix = "${random_string.prefix.result}"
5+
prefix = "${var.prefix}"
66
resource_groups = "${var.resource_groups}"
77
location = "${var.location_map["primary"]}"
88
}
@@ -11,7 +11,7 @@ module "resource_group" {
1111
module "monitoring_workspace" {
1212
source = "modules/log_analytics"
1313

14-
prefix = "${random_string.prefix.result}"
14+
prefix = "${var.prefix}"
1515
name = "${var.analytics_workspace_name}"
1616
resource_group_name = "${module.resource_group.names["aks"]}"
1717
}
@@ -27,7 +27,7 @@ module "azure_dns" {
2727
module "aks_primary" {
2828
source = "modules/blueprint_aks"
2929

30-
prefix = "${random_string.prefix.result}"
30+
prefix = "${var.prefix}"
3131
suffix = "sg"
3232
resource_group_names = "${module.resource_group.names}"
3333
log_analytics_workspace_id = "${module.monitoring_workspace.id}"
@@ -43,7 +43,7 @@ module "aks_primary" {
4343
module "aks_secondary" {
4444
source = "modules/blueprint_aks"
4545

46-
prefix = "${random_string.prefix.result}"
46+
prefix = "${var.prefix}"
4747
suffix = "hk"
4848
resource_group_names = "${module.resource_group.names}"
4949
log_analytics_workspace_id = "${module.monitoring_workspace.id}"

step1-aks/variables.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ variable "dns_zone" {
4040
variable "analytics_workspace_name" {
4141

4242
}
43+
44+
variable "prefix" {
45+
description = "Prefix generated by the remote state (./deploy.sh)"
46+
}

tfstate/main.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Used to make sure delete / re-create generate brand new names and reduce risk of being throttled during dev activities
2+
# used to enable multiple developers to work against the same subscription
3+
resource "random_string" "prefix" {
4+
length = 4
5+
special = false
6+
upper = false
7+
number = false
8+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ output "access_key" {
1111
value = "${azurerm_storage_account.stg.primary_access_key}"
1212
}
1313

14+
output "prefix" {
15+
value = "${random_string.prefix.result}"
16+
}

0 commit comments

Comments
 (0)