Skip to content

Commit 40b4e2e

Browse files
rootroot
authored andcommitted
prepare new demo for google
1 parent 9247ee0 commit 40b4e2e

File tree

12 files changed

+262
-263
lines changed

12 files changed

+262
-263
lines changed

.terrahub.yml

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
## project config
21
project:
32
name: demo-terraform-automation-google
4-
code: f2754a99
5-
include: ['.']
6-
exclude: ['**/.terraform/*', '**/node_modules/*']
7-
8-
## template config
3+
code: 7b3c5d2c
4+
include:
5+
- .
6+
exclude:
7+
- '**/.terraform/**'
8+
- '**/node_modules/**'
9+
- '**/.git/**'
10+
terraform:
11+
varFile:
12+
- default.tfvars
913
template:
1014
provider:
1115
google: {}
1216
locals:
13-
google_project_id: project-123456789012
14-
google_region: us-central1
15-
google_org_id: 123456789012
16-
google_service_account_name: demo-team
17-
google_billing_account: 123456-ABCDEF-ZYXWVU
18-
google_location_id: us-central
19-
20-
## terraform config
21-
terraform:
22-
varFile: ['default.tfvars']
23-
version: 0.11.7
17+
google_project_id: terrahub-demo-f2754a99
18+
google_billing_account: 0194A4-8A0AAF-6F01FE

README.md

Lines changed: 110 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,11 @@ Manual Setup (set values in double quotes and run the following command in termi
1616
```shell
1717
export GOOGLE_CLOUD_PROJECT="" ## e.g. terrahub-123456
1818
export GOOGLE_APPLICATION_CREDENTIALS="" ## e.g. ${HOME}/.config/gcloud/terraform.json
19-
export ORG_ID="" ## e.g. 123456789012
2019
export BILLING_ID="" ## e.g. 123456-ABCDEF-ZYXWVU
2120
export PROJECT_NAME="" ## e.g. TerraHub
2221
export IAM_NAME="" ## e.g. terraform
2322
export IAM_DESC="" ## e.g. terraform service account
2423
```
25-
26-
### Setup ORG_ID Programmatically
27-
28-
Automated Setup (run the following command in terminal):
29-
```shell
30-
export ORG_ID="$(gcloud organizations list --format=json | jq '.[0].name[14:]')"
31-
```
32-
33-
> NOTE: If you don't have JQ CLI, check out this
34-
[installation guide](https://stedolan.github.io/jq/download/)
35-
3624
### Setup BILLING_ID Programmatically
3725

3826
Automated Setup (run the following command in terminal):
@@ -49,18 +37,15 @@ Run the following command in terminal:
4937
```shell
5038
gcloud projects create ${GOOGLE_CLOUD_PROJECT} \
5139
--name="${PROJECT_NAME}" \
52-
--organization="${ORG_ID}" \
5340
--set-as-default
5441

55-
gcloud config set project ${GOOGLE_CLOUD_PROJECT}
42+
gcloud beta billing projects link ${GOOGLE_CLOUD_PROJECT} \
43+
--billing-account="${BILLING_ID}"
5644

5745
gcloud services enable cloudresourcemanager.googleapis.com
5846
gcloud services enable cloudbilling.googleapis.com
5947
gcloud services enable iam.googleapis.com
60-
gcloud services enable compute.googleapis.com
61-
62-
gcloud beta billing projects link ${GOOGLE_CLOUD_PROJECT} \
63-
--billing-account="${BILLING_ID}"
48+
gcloud services enable cloudfunctions.googleapis.com
6449
```
6550

6651
Your output should be similar to the one below:
@@ -95,108 +80,174 @@ Your output should be similar to the one below:
9580
```
9681
```
9782

98-
## Add IAM Policy Binding to Google Cloud Organization
83+
## Create Terraform Configurations Using TerraHub
9984

100-
Run the following command in terminal:
85+
Run the following commands in terminal:
10186
```shell
102-
gcloud organizations add-iam-policy-binding ${ORG_ID} \
103-
--member="serviceAccount:${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
104-
--role="roles/resourcemanager.projectCreator"
105-
106-
gcloud organizations add-iam-policy-binding ${ORG_ID} \
107-
--member="serviceAccount:${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
108-
--role="roles/billing.user"
87+
terrahub --help | head -3
10988
```
11089

11190
Your output should be similar to the one below:
11291
```
92+
Usage: terrahub [command] [options]
93+
94+
terrahub@0.2.2 (built: 2019-02-28T13:32:40.386Z)
11395
```
11496

115-
## Create Terraform Configurations Using TerraHub
97+
> NOTE: If you don't have TerraHub CLI, check out this
98+
[installation guide](https://www.npmjs.com/package/terrahub)
11699

117-
Run the following commands in terminal:
100+
Run the following command in terminal:
118101
```shell
119-
terrahub --help | head -3
102+
mkdir demo-terraform-automation-google
103+
cd demo-terraform-automation-google
104+
terrahub project -n demo-terraform-automation-google
120105
```
121106

122107
Your output should be similar to the one below:
123108
```
124-
Usage: terrahub [command] [options]
109+
✅ Project successfully initialized
110+
```
111+
112+
## Create TerraHub Components from Templates
125113

126-
terrahub@0.1.28 (built: 2019-02-08T17:17:41.912Z)
114+
Run the following command in terminal:
115+
```shell
116+
terrahub component -t google_storage_bucket -n demo_storage_bucket && \
117+
terrahub component -t google_storage_bucket_object -n demo_object -o ../demo_storage_bucket && \
118+
terrahub component -t google_cloudfunctions_function -n demo_function -o ../demo_object
127119
```
128120

129-
> NOTE: If you don't have TerraHub CLI, check out this
130-
[installation guide](https://www.npmjs.com/package/terrahub)
121+
Your output should be similar to the one below:
122+
```
123+
✅ Done
124+
```
125+
126+
## Update Project Config
131127

132128
Run the following command in terminal:
133129
```shell
134-
mkdir demo-terraform-google
135-
cd demo-terraform-google
136-
terrahub project -n demo-terraform-google
130+
terrahub configure -c template.provider.google={}
131+
terrahub configure -c template.locals.google_project_id="${GOOGLE_CLOUD_PROJECT}"
132+
terrahub configure -c template.locals.google_billing_account="${BILLING_ID}"
137133
```
138134

139135
Your output should be similar to the one below:
140136
```
141-
Project successfully initialized
137+
Done
142138
```
143139

144-
## Create TerraHub Components
140+
## Customize TerraHub Component for Storage Bucket
145141

146142
Run the following command in terminal:
147143
```shell
148-
terrahub component -t google_project -n project
149-
terrahub component -t google_service_account -n service_account -o ../project
150-
terrahub component -t google_service_account_key -n service_account_key -o ../service_account
151-
terrahub component -t google_project_iam_member -n project_iam_member -o ../project
152-
terrahub component -t google_project_iam_binding -n project_iam_policy_binding_storage_admin -o ../project_iam_member
153-
terrahub component -t google_project_iam_binding -n project_iam_policy_binding_compute_admin -o ../project_iam_member
144+
terrahub configure -i demo_storage_bucket -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/demo_storage_bucket/terraform.tfstate'
145+
terrahub configure -i demo_storage_bucket -c component.template.resource.google_storage_bucket.demo_storage_bucket.name='demo_storage_bucket_${local.project["code"]}'
146+
terrahub configure -i demo_storage_bucket -c component.template.resource.google_storage_bucket.demo_storage_bucket.location='US'
147+
terrahub configure -i demo_storage_bucket -c component.template.resource.google_storage_bucket.demo_storage_bucket.project="${GOOGLE_CLOUD_PROJECT}"
148+
terrahub configure -i demo_storage_bucket -c component.template.variable -D -y
154149
```
155150

156151
Your output should be similar to the one below:
157152
```
158153
✅ Done
159154
```
160155

161-
## Visualize TerraHub Components
156+
## Customize TerraHub Component for Storage Bucket Object
162157

163158
Run the following command in terminal:
164159
```shell
165-
terrahub graph
160+
terrahub configure -i demo_object -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/demo_object/terraform.tfstate'
161+
terrahub configure -i demo_object -c component.template.data.terraform_remote_state.storage.backend='local'
162+
terrahub configure -i demo_object -c component.template.data.terraform_remote_state.storage.config.path='/tmp/.terrahub/local_backend/demo_storage_bucket/terraform.tfstate'
163+
terrahub configure -i demo_object -c component.template.resource.google_storage_bucket_object.demo_object.name='demo.zip'
164+
terrahub configure -i demo_object -c component.template.resource.google_storage_bucket_object.demo_object.bucket='${data.terraform_remote_state.storage.thub_id}'
165+
terrahub configure -i demo_object -c component.template.resource.google_storage_bucket_object.demo_object.source='./demo.zip'
166+
terrahub configure -i demo_object -c component.template.variable -D -y
167+
terrahub configure -i demo_object -c build.env.variables.THUB_LAMBDA_ZIP='demo.zip'
168+
terrahub configure -i demo_object -c build.env.variables.THUB_BUILD_PATH='..'
169+
terrahub configure -i demo_object -c build.env.variables.THUB_BUILD_OK='true'
170+
terrahub configure -i demo_object -c build.phases.build.commands[0]='echo "BUILD: Running build step"'
171+
terrahub configure -i demo_object -c build.phases.build.commands[1]='zip -j ${THUB_LAMBDA_ZIP} ${THUB_BUILD_PATH}/index.js'
172+
terrahub configure -i demo_object -c build.phases.build.finally[0]='echo "BUILD: build step successful"'
173+
terrahub configure -i demo_object -c build.phases.post_build.commands[0]='echo "BUILD: Running post_build step"'
174+
terrahub configure -i demo_object -c build.phases.post_build.commands[1]='rm -rf ./nodejs-docs-samples'
175+
terrahub configure -i demo_object -c build.phases.post_build.finally[0]='echo "BUILD: post_build step successful"'
166176
```
167177

168178
Your output should be similar to the one below:
169179
```
170-
Project: demo-terraform-google
171-
└─ project [path: ./project]
172-
├─ project_iam_member [path: ./project_iam_member]
173-
│ ├─ project_iam_binding_compute_admin [path: ./project_iam_binding_compute_admin]
174-
│ └─ project_iam_binding_storage_admin [path: ./project_iam_binding_storage_admin]
175-
└─ service_account [path: ./service_account]
176-
└─ service_account_key [path: ./service_account_key]
180+
✅ Done
177181
```
178182

179-
## Update Project Config
183+
## Customize TerraHub Component for Google Cloud Function
180184

181185
Run the following command in terminal:
182186
```shell
183-
terrahub configure -c template.locals.google_org_id="${ORG_ID}"
184-
terrahub configure -c template.locals.google_billing_account="${BILLING_ID}"
185-
terrahub configure -c template.locals.google_project_id="${GOOGLE_CLOUD_PROJECT}"
187+
terrahub configure -i demo_function -c component.template.terraform.backend.local.path='/tmp/.terrahub/local_backend/demo_function/terraform.tfstate'
188+
terrahub configure -i demo_function -c component.template.data.terraform_remote_state.storage.backend='local'
189+
terrahub configure -i demo_function -c component.template.data.terraform_remote_state.storage.config.path='/tmp/.terrahub/local_backend/demo_storage_bucket/terraform.tfstate'
190+
terrahub configure -i demo_function -c component.template.data.terraform_remote_state.object.backend='local'
191+
terrahub configure -i demo_function -c component.template.data.terraform_remote_state.object.config.path='/tmp/.terrahub/local_backend/demo_object/terraform.tfstate'
192+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.name='demofunction${local.project["code"]}'
193+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.region='us-central1'
194+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.runtime='nodejs8'
195+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.description='My demo function ${data.terraform_remote_state.object.md5hash}'
196+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.available_memory_mb=128
197+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.source_archive_bucket='${data.terraform_remote_state.storage.thub_id}'
198+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.source_archive_object='${data.terraform_remote_state.object.output_name}'
199+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.trigger_http=true
200+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.timeout=60
201+
terrahub configure -i demo_function -c component.template.resource.google_cloudfunctions_function.demo_function.entry_point='helloGET'
202+
terrahub configure -i demo_function -c component.template.variable -D -y
203+
terrahub configure -i demo_function -c component.template.output -D -y
204+
terrahub configure -i demo_function -c component.template.output.id.value='${google_cloudfunctions_function.demo_function.id}'
205+
terrahub configure -i demo_function -c component.template.output.trigger_url.value='${google_cloudfunctions_function.demo_function.https_trigger_url}'
206+
terrahub configure -i demo_function -c build.env.variables.THUB_LAMBDA_ZIP='demo.zip'
207+
terrahub configure -i demo_function -c build.env.variables.THUB_BUILD_PATH='../demo_object'
208+
terrahub configure -i demo_function -c build.env.variables.THUB_BUILD_OK='true'
209+
terrahub configure -i demo_function -c build.phases.post_build.commands[0]='echo "BUILD: Running post_build step"'
210+
terrahub configure -i demo_function -c build.phases.post_build.commands[1]='rm ${THUB_BUILD_PATH}/${THUB_LAMBDA_ZIP}'
211+
terrahub configure -i demo_function -c build.phases.post_build.finally[0]='echo "BUILD: post_build step successful"'
186212
```
187213

188214
Your output should be similar to the one below:
189215
```
190216
✅ Done
191217
```
192218

219+
## Visualize TerraHub Components
220+
221+
Run the following command in terminal:
222+
```shell
223+
terrahub graph
224+
```
225+
226+
Your output should be similar to the one below:
227+
```
228+
Project: demo-terraform-automation-google
229+
└─ demo_storage_bucket_7b3c5d2c [path: ./demo_storage_bucket_7b3c5d2c]
230+
└─ demo_object_7b3c5d2c [path: ./demo_object_7b3c5d2c]
231+
└─ demo_function_7b3c5d2c [path: ./demo_function_7b3c5d2c]
232+
```
233+
234+
193235
## Run TerraHub Automation
194236

195237
Run the following command in terminal:
196238
```shell
197-
terrahub run -a -y
239+
terrahub build -i=demo_object && \
240+
terrahub run -a -y && \
241+
terrahub build -i=demo_function
198242
```
199243

200244
Your output should be similar to the one below:
201245
```
202246
```
247+
248+
## Run Test Command
249+
250+
Run the following command in terminal:
251+
```
252+
curl https://us-central1-terrahub-demo-xxxxxxxx.cloudfunctions.net/demofunctionxxxxxxxx
253+
```

demo_function/.terrahub.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
component:
2+
name: demo_function
3+
mapping:
4+
- .
5+
dependsOn:
6+
- ../demo_object
7+
template:
8+
resource:
9+
google_cloudfunctions_function:
10+
demo_function:
11+
name: 'demofunction${local.project["code"]}'
12+
region: us-central1
13+
runtime: nodejs8
14+
description: 'My demo function ${data.terraform_remote_state.object.md5hash}'
15+
available_memory_mb: 128
16+
source_archive_bucket: '${data.terraform_remote_state.storage.thub_id}'
17+
source_archive_object: '${data.terraform_remote_state.object.output_name}'
18+
trigger_http: true
19+
timeout: 60
20+
entry_point: helloGET
21+
terraform:
22+
backend:
23+
local:
24+
path: /tmp/.terrahub/local_backend/demo_function/terraform.tfstate
25+
data:
26+
terraform_remote_state:
27+
storage:
28+
backend: local
29+
config:
30+
path: /tmp/.terrahub/local_backend/demo_storage_bucket/terraform.tfstate
31+
object:
32+
backend: local
33+
config:
34+
path: /tmp/.terrahub/local_backend/demo_object/terraform.tfstate
35+
output:
36+
id:
37+
value: '${google_cloudfunctions_function.demo_function.id}'
38+
trigger_url:
39+
value: '${google_cloudfunctions_function.demo_function.https_trigger_url}'
40+
build:
41+
env:
42+
variables:
43+
THUB_LAMBDA_ZIP: demo.zip
44+
THUB_BUILD_PATH: ../demo_object
45+
THUB_BUILD_OK: true
46+
phases:
47+
post_build:
48+
commands:
49+
- 'echo "BUILD: Running post_build step"'
50+
- 'rm ${THUB_BUILD_PATH}/${THUB_LAMBDA_ZIP}'
51+
finally:
52+
- 'echo "BUILD: post_build step successful"'

0 commit comments

Comments
 (0)