1
- # Terraform Automation using Google Provider
1
+ # Terraform Automation Demo using Google Cloud Provider
2
+
3
+ The purpose of this repository is to show case terraform automation for Google
4
+ Cloud. This demo will provision the following cloud resources associated to
5
+ corresponding terraform configurations:
6
+
7
+ | GCP Resource | Terraform Resource | Link to TerraHub Config |
8
+ | -----------------------| --------------------| -------------------------|
9
+ | Cloud Function | google_cloudfunctions_function | https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_function/.terrahub.yml#L24 |
10
+ | Cloud Storage | google_storage_bucket | https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_storage/.terrahub.yml#L7 |
11
+ | Static Website | google_storage_bucket_object | https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_function/.terrahub.yml#L19 |
12
+ | IAM Policy | iam_member_object_viewer | https://github.com/TerraHubCorp/demo-terraform-automation-gcp/blob/master/google_storage/.terrahub.yml#L7 |
13
+
14
+ Follow below instructions to try this out in your own Google Cloud account.
2
15
3
16
## Login to Google Cloud
4
17
@@ -12,7 +25,7 @@ gcloud auth login
12
25
13
26
## Setup Environment Variables (Will Be Used Later)
14
27
15
- Manual Setup (set values in double quotes and run the following command in terminal):
28
+ Manual Setup (set values in double quotes and run the following commands in terminal):
16
29
``` shell
17
30
export GOOGLE_CLOUD_PROJECT=" " # # e.g. terrahub-123456
18
31
export GOOGLE_APPLICATION_CREDENTIALS=" " # # e.g. ${HOME}/.config/gcloud/terraform.json
@@ -39,10 +52,30 @@ Run the following command in terminal:
39
52
gcloud projects create ${GOOGLE_CLOUD_PROJECT} \
40
53
--name=" ${PROJECT_NAME} " \
41
54
--set-as-default
55
+ ```
42
56
57
+ Your output should be similar to the one below:
58
+ ```
59
+ Create in progress for [https://cloudresourcemanager.googleapis.com/v1/projects/***].
60
+ Waiting for [operations/***] to finish ... done.
61
+ ```
62
+
63
+ Next, run the following command in terminal:
64
+ ``` shell
43
65
gcloud beta billing projects link ${GOOGLE_CLOUD_PROJECT} \
44
66
--billing-account=" ${BILLING_ID} "
67
+ ```
68
+
69
+ Your output should be similar to the one below:
70
+ ```
71
+ billingAccountName: billingAccounts/***
72
+ billingEnabled: true
73
+ name: projects/***
74
+ projectId: ***
75
+ ```
45
76
77
+ Finally, run the following commands in terminal:
78
+ ``` shell
46
79
gcloud services enable cloudresourcemanager.googleapis.com
47
80
gcloud services enable cloudbilling.googleapis.com
48
81
gcloud services enable iam.googleapis.com
@@ -52,6 +85,7 @@ gcloud services enable storage-component.googleapis.com
52
85
53
86
Your output should be similar to the one below:
54
87
```
88
+ Operation "operations/***" finished successfully.
55
89
```
56
90
57
91
## Create Google Cloud IAM Service Account & Key
@@ -60,13 +94,22 @@ Run the following command in terminal:
60
94
``` shell
61
95
gcloud iam service-accounts create ${IAM_NAME} \
62
96
--display-name=" ${IAM_DESC} "
97
+ ```
98
+
99
+ Your output should be similar to the one below:
100
+ ```
101
+ Created service account [***].
102
+ ```
63
103
104
+ After that, run the following command in terminal:
105
+ ``` shell
64
106
gcloud iam service-accounts keys create ${GOOGLE_APPLICATION_CREDENTIALS} \
65
107
--iam-account=" ${IAM_NAME} @${GOOGLE_CLOUD_PROJECT} .iam.gserviceaccount.com"
66
108
```
67
109
68
110
Your output should be similar to the one below:
69
111
```
112
+ Created key [***] of type [json] as [***] for [***@***.iam.gserviceaccount.com]
70
113
```
71
114
72
115
## Add IAM Policy Binding to Google Cloud Project
@@ -80,9 +123,18 @@ gcloud projects add-iam-policy-binding ${GOOGLE_CLOUD_PROJECT} \
80
123
81
124
Your output should be similar to the one below:
82
125
```
126
+ Updated IAM policy for project [***].
127
+ bindings:
128
+ ...
129
+ version: 1
83
130
```
84
131
85
- ## Create Terraform Configurations Using TerraHub
132
+ ## Terraform Automation and Orchestration Tool
133
+
134
+ The next couple of paragraphs are show casing the process of creating terraform
135
+ configurations using [ TerraHub CLI] ( https://github.com/TerraHubCorp/terrahub ) .
136
+ We have opted to use YML format instead of HCL because it's easier and faster
137
+ to customize and automate terraform runs (see ` terrahub component ` command).
86
138
87
139
Run the following commands in terminal:
88
140
``` shell
@@ -99,23 +151,44 @@ terrahub@0.0.1 (built: 2018-04-07T19:15:39.787Z)
99
151
> NOTE: If you don't have TerraHub CLI, check out this
100
152
[ installation guide] ( https://www.npmjs.com/package/terrahub )
101
153
102
- Run the following command in terminal:
154
+ ## Build Terraform Configurations
155
+
156
+ Run the following commands in terminal:
103
157
``` shell
104
- mkdir demo-terraform-automation-google
105
- cd demo-terraform-automation-google
106
- terrahub project -n demo-terraform-automation-google
158
+ mkdir demo-terraform-automation-gcp
159
+ cd demo-terraform-automation-gcp
160
+ terrahub project -n demo-terraform-automation-gcp
107
161
```
108
162
109
163
Your output should be similar to the one below:
110
164
```
111
165
✅ Project successfully initialized
112
166
```
113
167
114
- > NOTE: For this demo we have added "index.js" file which you have to copy in "demo-terraform-automation-google" dir
168
+ > NOTE: If you want to jump directly to terraform automation part of the demo,
169
+ instead of creating ` demo-terraform-automation-gcp ` from scratch, clone current
170
+ repository, follow the instructions for `Copy Source Code for Google Function
171
+ and Static Website` , after jump to ` Update Project Config` and skip down to
172
+ ` Visualize TerraHub Components ` . This way you will fast forward through terrahub
173
+ components creation and customization, and switch directly to the automation part.
174
+
175
+ ## Copy Source Code for Google Function and Static Website
115
176
116
- > NOTE: For this demo we have run the following command in terminal
177
+ The source code of Google Function is stored in ` index.js ` :
178
+
179
+ ``` shell
180
+ cat ' exports.helloGET = (req, res) => { res.send("Hello World!\n"); };' > index.js
181
+ ```
182
+
183
+ The source code of Static Website is cloned from another public repository:
184
+
185
+ ``` shell
117
186
git clone https://github.com/TerraHubCorp/www.git \
118
- && rm -rf ./www/.terrahub*
187
+ && rm -rf ./www/.terrahub*
188
+ ```
189
+
190
+ > NOTE: We are removing ` .terrahub ` folder (as shown above) because
191
+ already included terraform configuration is not relevant for this demo
119
192
120
193
## Create TerraHub Components from Templates
121
194
@@ -134,7 +207,7 @@ Your output should be similar to the one below:
134
207
135
208
## Update Project Config
136
209
137
- Run the following command in terminal:
210
+ Run the following commands in terminal:
138
211
``` shell
139
212
terrahub configure -c terraform.version=0.11.11
140
213
terrahub configure -c template.provider.google={}
@@ -149,7 +222,7 @@ Your output should be similar to the one below:
149
222
150
223
## Customize TerraHub Component for Storage Bucket
151
224
152
- Run the following command in terminal:
225
+ Run the following commands in terminal:
153
226
``` shell
154
227
terrahub configure -i google_storage -c component.template.terraform.backend.local.path=' /tmp/.terrahub/local_backend/google_storage/terraform.tfstate'
155
228
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.name=" ${STORAGE_BUCKET} "
@@ -166,7 +239,7 @@ Your output should be similar to the one below:
166
239
167
240
## Customize TerraHub Component for Google Cloud Function
168
241
169
- Run the following command in terminal:
242
+ Run the following commands in terminal:
170
243
``` shell
171
244
terrahub configure -i google_function -c component.template.terraform.backend.local.path=' /tmp/.terrahub/local_backend/google_function/terraform.tfstate'
172
245
terrahub configure -i google_function -c component.template.data.terraform_remote_state.storage.backend=' local'
@@ -219,7 +292,7 @@ Your output should be similar to the one below:
219
292
220
293
## Customize TerraHub Component for Google Cloud Static WebSite
221
294
222
- Run the following command in terminal:
295
+ Run the following commands in terminal:
223
296
``` shell
224
297
terrahub configure -i google_static_website -c component.template.terraform.backend.local.path=' /tmp/.terrahub/local_backend/google_static_website/terraform.tfstate'
225
298
terrahub configure -i google_static_website -c component.template.resource.google_storage_bucket.google_static_website.name=" ${STORAGE_BUCKET} _website"
@@ -258,7 +331,7 @@ Your output should be similar to the one below:
258
331
259
332
## Customize TerraHub Component for IAM Member Object Viewer
260
333
261
- Run the following command in terminal:
334
+ Run the following commands in terminal:
262
335
``` shell
263
336
terrahub configure -i iam_member_object_viewer -c component.template.terraform.backend.local.path=' /tmp/.terrahub/local_backend/iam_member_object_viewer/terraform.tfstate'
264
337
terrahub configure -i iam_member_object_viewer -c component.template.data.terraform_remote_state.storage.backend=' local'
@@ -283,31 +356,57 @@ terrahub graph
283
356
284
357
Your output should be similar to the one below:
285
358
```
286
- Project: demo-terraform-automation-google
359
+ Project: demo-terraform-automation-gcp
287
360
├─ google_storage [path: ./google_storage]
288
361
│ └─ google_function [path: ./google_function]
289
362
└─ google_static_website [path: ./www/.terrahub/google_static_website]
290
363
└─ iam_member_object_viewer [path: ./www/.terrahub/iam_member_object_viewer]
291
364
```
292
365
293
-
294
366
## Run TerraHub Automation
295
367
368
+ ### Prepare Storage Resources
369
+
296
370
Run the following command in terminal:
297
371
``` shell
298
- terrahub run -a -y -i google_storage,google_static_website
372
+ terrahub run -y -a -i google_storage,google_static_website
373
+ ```
374
+
375
+ Your output should be similar to the one below:
376
+ ```
377
+ ```
378
+
379
+ ### Prepare Source Code for Deployment
380
+
381
+ Run the following command in terminal:
382
+
383
+ ``` shell
299
384
terrahub build -i google_function,google_static_website
300
- terrahub run -a -y
301
385
```
302
386
303
387
Your output should be similar to the one below:
304
388
```
305
389
```
306
390
307
- ## Run Test Command
391
+ ### Run TerraHub Automation
308
392
309
393
Run the following command in terminal:
394
+ ``` shell
395
+ terrahub run -y -a
396
+ ```
397
+
398
+ Your output should be similar to the one below:
399
+ ```
400
+ ```
401
+
402
+ ## Testing Deployed Cloud Resources
403
+
404
+ Check if backend was deployed successfully. Run the following command in terminal:
310
405
```
311
406
curl https://us-central1-terrahub-123456.cloudfunctions.net/demofunctionxxxxxxxx
407
+ ```
408
+
409
+ Check if frontend was deployed successfully. Run the following command in terminal:
410
+ ```
312
411
curl https://${STORAGE_BUCKET}_website.storage.googleapis.com/index.html
313
412
```
0 commit comments