Skip to content

Commit ae8a8f4

Browse files
authored
Merge pull request #16 from TerraHubCorp/dev
Improve readme
2 parents a7c7fc6 + f4a01f3 commit ae8a8f4

File tree

2 files changed

+120
-21
lines changed

2 files changed

+120
-21
lines changed

README.md

Lines changed: 119 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
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.
215

316
## Login to Google Cloud
417

@@ -12,7 +25,7 @@ gcloud auth login
1225

1326
## Setup Environment Variables (Will Be Used Later)
1427

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):
1629
```shell
1730
export GOOGLE_CLOUD_PROJECT="" ## e.g. terrahub-123456
1831
export GOOGLE_APPLICATION_CREDENTIALS="" ## e.g. ${HOME}/.config/gcloud/terraform.json
@@ -39,10 +52,30 @@ Run the following command in terminal:
3952
gcloud projects create ${GOOGLE_CLOUD_PROJECT} \
4053
--name="${PROJECT_NAME}" \
4154
--set-as-default
55+
```
4256

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
4365
gcloud beta billing projects link ${GOOGLE_CLOUD_PROJECT} \
4466
--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+
```
4576

77+
Finally, run the following commands in terminal:
78+
```shell
4679
gcloud services enable cloudresourcemanager.googleapis.com
4780
gcloud services enable cloudbilling.googleapis.com
4881
gcloud services enable iam.googleapis.com
@@ -52,6 +85,7 @@ gcloud services enable storage-component.googleapis.com
5285

5386
Your output should be similar to the one below:
5487
```
88+
Operation "operations/***" finished successfully.
5589
```
5690

5791
## Create Google Cloud IAM Service Account & Key
@@ -60,13 +94,22 @@ Run the following command in terminal:
6094
```shell
6195
gcloud iam service-accounts create ${IAM_NAME} \
6296
--display-name="${IAM_DESC}"
97+
```
98+
99+
Your output should be similar to the one below:
100+
```
101+
Created service account [***].
102+
```
63103

104+
After that, run the following command in terminal:
105+
```shell
64106
gcloud iam service-accounts keys create ${GOOGLE_APPLICATION_CREDENTIALS} \
65107
--iam-account="${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com"
66108
```
67109

68110
Your output should be similar to the one below:
69111
```
112+
Created key [***] of type [json] as [***] for [***@***.iam.gserviceaccount.com]
70113
```
71114

72115
## Add IAM Policy Binding to Google Cloud Project
@@ -80,9 +123,18 @@ gcloud projects add-iam-policy-binding ${GOOGLE_CLOUD_PROJECT} \
80123

81124
Your output should be similar to the one below:
82125
```
126+
Updated IAM policy for project [***].
127+
bindings:
128+
...
129+
version: 1
83130
```
84131

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).
86138

87139
Run the following commands in terminal:
88140
```shell
@@ -99,23 +151,44 @@ terrahub@0.0.1 (built: 2018-04-07T19:15:39.787Z)
99151
> NOTE: If you don't have TerraHub CLI, check out this
100152
[installation guide](https://www.npmjs.com/package/terrahub)
101153

102-
Run the following command in terminal:
154+
## Build Terraform Configurations
155+
156+
Run the following commands in terminal:
103157
```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
107161
```
108162

109163
Your output should be similar to the one below:
110164
```
111165
✅ Project successfully initialized
112166
```
113167

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
115176

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
117186
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
119192

120193
## Create TerraHub Components from Templates
121194

@@ -134,7 +207,7 @@ Your output should be similar to the one below:
134207

135208
## Update Project Config
136209

137-
Run the following command in terminal:
210+
Run the following commands in terminal:
138211
```shell
139212
terrahub configure -c terraform.version=0.11.11
140213
terrahub configure -c template.provider.google={}
@@ -149,7 +222,7 @@ Your output should be similar to the one below:
149222

150223
## Customize TerraHub Component for Storage Bucket
151224

152-
Run the following command in terminal:
225+
Run the following commands in terminal:
153226
```shell
154227
terrahub configure -i google_storage -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/google_storage/terraform.tfstate'
155228
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:
166239

167240
## Customize TerraHub Component for Google Cloud Function
168241

169-
Run the following command in terminal:
242+
Run the following commands in terminal:
170243
```shell
171244
terrahub configure -i google_function -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/google_function/terraform.tfstate'
172245
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:
219292

220293
## Customize TerraHub Component for Google Cloud Static WebSite
221294

222-
Run the following command in terminal:
295+
Run the following commands in terminal:
223296
```shell
224297
terrahub configure -i google_static_website -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/google_static_website/terraform.tfstate'
225298
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:
258331

259332
## Customize TerraHub Component for IAM Member Object Viewer
260333

261-
Run the following command in terminal:
334+
Run the following commands in terminal:
262335
```shell
263336
terrahub configure -i iam_member_object_viewer -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/iam_member_object_viewer/terraform.tfstate'
264337
terrahub configure -i iam_member_object_viewer -c component.template.data.terraform_remote_state.storage.backend='local'
@@ -283,31 +356,57 @@ terrahub graph
283356

284357
Your output should be similar to the one below:
285358
```
286-
Project: demo-terraform-automation-google
359+
Project: demo-terraform-automation-gcp
287360
├─ google_storage [path: ./google_storage]
288361
│ └─ google_function [path: ./google_function]
289362
└─ google_static_website [path: ./www/.terrahub/google_static_website]
290363
└─ iam_member_object_viewer [path: ./www/.terrahub/iam_member_object_viewer]
291364
```
292365

293-
294366
## Run TerraHub Automation
295367

368+
### Prepare Storage Resources
369+
296370
Run the following command in terminal:
297371
```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
299384
terrahub build -i google_function,google_static_website
300-
terrahub run -a -y
301385
```
302386

303387
Your output should be similar to the one below:
304388
```
305389
```
306390

307-
## Run Test Command
391+
### Run TerraHub Automation
308392

309393
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:
310405
```
311406
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+
```
312411
curl https://${STORAGE_BUCKET}_website.storage.googleapis.com/index.html
313412
```

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
exports.helloGET = (req, res) => {
2-
res.send("Hello World!\n");
2+
res.send("Hello World!\n");
33
};

0 commit comments

Comments
 (0)