-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild-folder-structure.sh
121 lines (107 loc) · 3.85 KB
/
build-folder-structure.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash set-ex
###########################################################################################
# Author: Lior Dux @zMynxx
# Description: This script generates a Terragrunt best practice folder structure with common files at it's level including inheritence.
# Usage: ./build-folder-structure.sh
# Example: ./build-folder-structure.sh
# Dependencies: None
# License: MIT
# Version: 1.0
###########################################################################################
###########
# Globals #
###########
# These globals are set using the get_input function
# accounts=('006262944085')
# regions=('il-central-1' 'us-east-2')
# environments=('prod' 'staging' 'dev')
# subjects=('storage' 'network' 'secrets' 'compute' 'databases')
#############
# Functions #
#############
# /**
# * @brief This function asks the user for input to set up the accounts, regions, environments, and subjects.
# * @param - None
# * @return - None
# */
get_input() {
echo "Enter the AWS account IDs separated by space: "
read -r -a accounts
export accounts
echo "Enter the AWS regions separated by space: "
read -r -a regions
export regions
echo "Enter the environments separated by space: "
read -r -a environments
export environments
echo "Enter the subjects separated by space: "
read -r -a subjects
export subjects
}
# /**
# * @brief This function generates a Terragrunt best practice folder structure with common files at it's level including inheritence.
# * @param - None
# * @return - None
# */
create_structure() {
mkdir -p modules
infrastructure_commons_filename="infrastructure.hcl"
account_commons_filename="account.hcl"
environment_commons_filename="env.hcl"
region_commons_filename="region.hcl"
# Create the infrastructure_commons.hcl file - this file will be used to store the common variables for all the accounts, regions, and environments
mkdir -p infrastructure-live
cat <<-HCL >infrastructure-live/"${infrastructure_commons_filename}"
locals {}
HCL
for account in "${accounts[@]}"; do
# Create the account_commons.hcl file - this file will be used to store the common variables for all the regions and environments
mkdir -p infrastructure-live/"${account}"
cat <<HCL >infrastructure-live/"${account}"/"${account_commons_filename}"
locals {
account_id = "${account}"
account_name = "my-account-name" # Set this to your account name
kms_key_id = "arn:aws:kms:us-east-1:${account}:key/12345678-1234-1234-1234-123456789012" # Set this to your KMS key ID
profile = "my-profile" # Set this to your AWS profile
}
HCL
# cat ./scripts/provider.hcl > infrastructure-live/"${account}"/provider.hcl
ls -la ./scripts/provider.hcl
cp ./scripts/provider.hcl infrastructure-live/"${account}"/provider.hcl
for environment in "${environments[@]}"; do
# Create the environment_commons.hcl file - this file will be used to store the common variables for all the regions
mkdir -p infrastructure-live/"${account}"/"${environment}"/"${environment}"-1
cat <<HCL >infrastructure-live/"${account}"/"${environment}"/"${environment_commons_filename}"
locals {
environment = "${environment}"
env = "${environment}"
}
HCL
for region in "${regions[@]}"; do
mkdir -p infrastructure-live/"${account}"/"${environment}"/"${environment}"-1/"${region}"
cat <<HCL >infrastructure-live/"${account}"/"${environment}"/"${environment}"-1/"${region}"/"${region_commons_filename}"
locals {
region = "${region}"
}
HCL
# Create directories & commons for each of them using a loop
for dir in "${subjects[@]}"; do
mkdir -p infrastructure-live/"${account}"/"${environment}"/"${environment}"-1/"${region}"/"${dir}"
cat <<HCL >infrastructure-live/"${account}"/"${environment}"/"${environment}"-1/"${region}"/"${dir}"/"${dir}.hcl"
locals {
field = "${dir}"
}
HCL
done
done
done
done
}
########
# Main #
########
main() {
get_input
create_structure
}
main