1
- # Terraform Automation using Google Provider
1
+ # Terraform Automation Demo using Google Cloud Provider
2
2
3
3
## Login to Google Cloud
4
4
@@ -12,7 +12,7 @@ gcloud auth login
12
12
13
13
## Setup Environment Variables (Will Be Used Later)
14
14
15
- Manual Setup (set values in double quotes and run the following command in terminal):
15
+ Manual Setup (set values in double quotes and run the following commands in terminal):
16
16
``` shell
17
17
export GOOGLE_CLOUD_PROJECT=" " # # e.g. terrahub-123456
18
18
export GOOGLE_APPLICATION_CREDENTIALS=" " # # e.g. ${HOME}/.config/gcloud/terraform.json
@@ -39,10 +39,30 @@ Run the following command in terminal:
39
39
gcloud projects create ${GOOGLE_CLOUD_PROJECT} \
40
40
--name=" ${PROJECT_NAME} " \
41
41
--set-as-default
42
+ ```
43
+
44
+ Your output should be similar to the one below:
45
+ ```
46
+ Create in progress for [https://cloudresourcemanager.googleapis.com/v1/projects/***].
47
+ Waiting for [operations/***] to finish ... done.
48
+ ```
42
49
50
+ Next, run the following command in terminal:
51
+ ``` shell
43
52
gcloud beta billing projects link ${GOOGLE_CLOUD_PROJECT} \
44
53
--billing-account=" ${BILLING_ID} "
54
+ ```
55
+
56
+ Your output should be similar to the one below:
57
+ ```
58
+ billingAccountName: billingAccounts/***
59
+ billingEnabled: true
60
+ name: projects/***
61
+ projectId: ***
62
+ ```
45
63
64
+ Finally, run the following command in terminal:
65
+ ``` shell
46
66
gcloud services enable cloudresourcemanager.googleapis.com
47
67
gcloud services enable cloudbilling.googleapis.com
48
68
gcloud services enable iam.googleapis.com
@@ -52,6 +72,7 @@ gcloud services enable storage-component.googleapis.com
52
72
53
73
Your output should be similar to the one below:
54
74
```
75
+ Operation "operations/***" finished successfully.
55
76
```
56
77
57
78
## Create Google Cloud IAM Service Account & Key
@@ -60,13 +81,22 @@ Run the following command in terminal:
60
81
``` shell
61
82
gcloud iam service-accounts create ${IAM_NAME} \
62
83
--display-name=" ${IAM_DESC} "
84
+ ```
85
+
86
+ Your output should be similar to the one below:
87
+ ```
88
+ Created service account [***].
89
+ ```
63
90
91
+ After that, run the following command in terminal:
92
+ ``` shell
64
93
gcloud iam service-accounts keys create ${GOOGLE_APPLICATION_CREDENTIALS} \
65
94
--iam-account=" ${IAM_NAME} @${GOOGLE_CLOUD_PROJECT} .iam.gserviceaccount.com"
66
95
```
67
96
68
97
Your output should be similar to the one below:
69
98
```
99
+ Created key [***] of type [json] as [***] for [***@***.iam.gserviceaccount.com]
70
100
```
71
101
72
102
## Add IAM Policy Binding to Google Cloud Project
@@ -80,6 +110,10 @@ gcloud projects add-iam-policy-binding ${GOOGLE_CLOUD_PROJECT} \
80
110
81
111
Your output should be similar to the one below:
82
112
```
113
+ Updated IAM policy for project [***].
114
+ bindings:
115
+ ...
116
+ version: 1
83
117
```
84
118
85
119
## Create Terraform Configurations Using TerraHub
@@ -99,23 +133,35 @@ terrahub@0.0.1 (built: 2018-04-07T19:15:39.787Z)
99
133
> NOTE: If you don't have TerraHub CLI, check out this
100
134
[ installation guide] ( https://www.npmjs.com/package/terrahub )
101
135
102
- Run the following command in terminal:
136
+ Run the following commands in terminal:
103
137
``` shell
104
- mkdir demo-terraform-automation-google
105
- cd demo-terraform-automation-google
106
- terrahub project -n demo-terraform-automation-google
138
+ mkdir demo-terraform-automation-gcp
139
+ cd demo-terraform-automation-gcp
140
+ terrahub project -n demo-terraform-automation-gcp
107
141
```
108
142
109
143
Your output should be similar to the one below:
110
144
```
111
145
✅ Project successfully initialized
112
146
```
113
147
114
- > NOTE: For this demo we have added "index.js" file which you have to copy in "demo-terraform-automation-google" dir
148
+ ## Copy Source Code for Google Function and Static Website
149
+
150
+ The source code of Google Function is stored in ` index.js ` :
151
+
152
+ ``` shell
153
+ cat ' exports.helloGET = (req, res) => { res.send("Hello World!\n"); };' > index.js
154
+ ```
155
+
156
+ The source code of Static Website is cloned from another public repository:
115
157
116
- > NOTE: For this demo we have run the following command in terminal
158
+ ``` shell
117
159
git clone https://github.com/TerraHubCorp/www.git \
118
- && rm -rf ./www/.terrahub*
160
+ && rm -rf ./www/.terrahub*
161
+ ```
162
+
163
+ > NOTE: We are removing ` .terrahub ` folder (as shown above) because
164
+ already included terraform configuration is not relevant for this demo
119
165
120
166
## Create TerraHub Components from Templates
121
167
@@ -134,7 +180,7 @@ Your output should be similar to the one below:
134
180
135
181
## Update Project Config
136
182
137
- Run the following command in terminal:
183
+ Run the following commands in terminal:
138
184
``` shell
139
185
terrahub configure -c terraform.version=0.11.11
140
186
terrahub configure -c template.provider.google={}
@@ -149,7 +195,7 @@ Your output should be similar to the one below:
149
195
150
196
## Customize TerraHub Component for Storage Bucket
151
197
152
- Run the following command in terminal:
198
+ Run the following commands in terminal:
153
199
``` shell
154
200
terrahub configure -i google_storage -c component.template.terraform.backend.local.path=' /tmp/.terrahub/local_backend/google_storage/terraform.tfstate'
155
201
terrahub configure -i google_storage -c component.template.resource.google_storage_bucket.google_storage.name=" ${STORAGE_BUCKET} "
@@ -166,7 +212,7 @@ Your output should be similar to the one below:
166
212
167
213
## Customize TerraHub Component for Google Cloud Function
168
214
169
- Run the following command in terminal:
215
+ Run the following commands in terminal:
170
216
``` shell
171
217
terrahub configure -i google_function -c component.template.terraform.backend.local.path=' /tmp/.terrahub/local_backend/google_function/terraform.tfstate'
172
218
terrahub configure -i google_function -c component.template.data.terraform_remote_state.storage.backend=' local'
@@ -219,7 +265,7 @@ Your output should be similar to the one below:
219
265
220
266
## Customize TerraHub Component for Google Cloud Static WebSite
221
267
222
- Run the following command in terminal:
268
+ Run the following commands in terminal:
223
269
``` shell
224
270
terrahub configure -i google_static_website -c component.template.terraform.backend.local.path=' /tmp/.terrahub/local_backend/google_static_website/terraform.tfstate'
225
271
terrahub configure -i google_static_website -c component.template.resource.google_storage_bucket.google_static_website.name=" ${STORAGE_BUCKET} _website"
@@ -258,7 +304,7 @@ Your output should be similar to the one below:
258
304
259
305
## Customize TerraHub Component for IAM Member Object Viewer
260
306
261
- Run the following command in terminal:
307
+ Run the following commands in terminal:
262
308
``` shell
263
309
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
310
terrahub configure -i iam_member_object_viewer -c component.template.data.terraform_remote_state.storage.backend=' local'
@@ -283,31 +329,57 @@ terrahub graph
283
329
284
330
Your output should be similar to the one below:
285
331
```
286
- Project: demo-terraform-automation-google
332
+ Project: demo-terraform-automation-gcp
287
333
├─ google_storage [path: ./google_storage]
288
334
│ └─ google_function [path: ./google_function]
289
335
└─ google_static_website [path: ./www/.terrahub/google_static_website]
290
336
└─ iam_member_object_viewer [path: ./www/.terrahub/iam_member_object_viewer]
291
337
```
292
338
293
-
294
339
## Run TerraHub Automation
295
340
341
+ ### Prepare Storage Resources
342
+
296
343
Run the following command in terminal:
297
344
``` shell
298
345
terrahub run -a -y -i google_storage,google_static_website
346
+ ```
347
+
348
+ Your output should be similar to the one below:
349
+ ```
350
+ ```
351
+
352
+ ### Prepare Source Code for Deployment
353
+
354
+ Run the following command in terminal:
355
+
356
+ ``` shell
299
357
terrahub build -i google_function,google_static_website
300
- terrahub run -a -y
301
358
```
302
359
303
360
Your output should be similar to the one below:
304
361
```
305
362
```
306
363
307
- ## Run Test Command
364
+ ### Run TerraHub Automation
308
365
309
366
Run the following command in terminal:
367
+ ``` shell
368
+ terrahub run -a -y
369
+ ```
370
+
371
+ Your output should be similar to the one below:
372
+ ```
373
+ ```
374
+
375
+ ## Testing Deployed Cloud Resources
376
+
377
+ Check if backend was deployed successfully. Run the following command in terminal:
310
378
```
311
379
curl https://us-central1-terrahub-123456.cloudfunctions.net/demofunctionxxxxxxxx
380
+ ```
381
+
382
+ Check if frontend was deployed successfully. Run the following command in terminal:
383
+ ```
312
384
curl https://${STORAGE_BUCKET}_website.storage.googleapis.com/index.html
313
385
```
0 commit comments