Skip to content

Commit b86ef34

Browse files
authored
Merge pull request #1 from TerraHubCorp/google-first-demo
Google first demo
2 parents 856c186 + cef4d78 commit b86ef34

38 files changed

+555
-2
lines changed

.terrahub.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## project config
2+
project:
3+
name: demo-terraform-google
4+
code: f2754a99
5+
provider: google
6+
7+
## terraform config
8+
terraform:
9+
varFile:
10+
- default.tfvars
11+
var:
12+
google_project_id: project-123456789012
13+
google_region: us-central1
14+
google_org_id: 123456789012
15+
google_account_id: demo-team
16+
google_billing_account: 123456-ABCDEF-ZYXWVU
17+
google_location_id: us-central
18+
version: 0.11.7

README.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,97 @@
1-
# terraform-demo-google
2-
Terraform Demo using Google provider
1+
# Terraform Demo using Google provider
2+
3+
## Login to Google Cloud
4+
```shell
5+
gcloud auth login
6+
```
7+
8+
## Get Google Cloud values for ORG_ID and BILLING_ID
9+
```shell
10+
gcloud organizations list
11+
gcloud beta billing accounts list
12+
```
13+
14+
## Setup Google Cloud ENV Variables
15+
```shell
16+
export GOOGLE_CLOUD_PROJECT="" ## e.g. terrahub-123456
17+
export GOOGLE_APPLICATION_CREDENTIALS="" ## e.g. ${HOME}/.config/gcloud/terraform.json
18+
export ORG_ID="" ## e.g. 123456789012
19+
export BILLING_ID="" ## e.g. 123456-ABCDEF-ZYXWVU
20+
export PROJECT_NAME="" ## e.g. TerraHub
21+
export IAM_NAME="" ## e.g. terraform
22+
export IAM_DESC="" ## e.g. terraform service account
23+
```
24+
25+
## Create Google Cloud Project & Billing
26+
```shell
27+
gcloud projects create ${GOOGLE_CLOUD_PROJECT} \
28+
--name="${PROJECT_NAME}"
29+
--organization="${ORG_ID}" \
30+
--set-as-default
31+
32+
gcloud config set project ${GOOGLE_CLOUD_PROJECT}
33+
34+
gcloud services enable cloudresourcemanager.googleapis.com
35+
gcloud services enable cloudbilling.googleapis.com
36+
gcloud services enable iam.googleapis.com
37+
gcloud services enable compute.googleapis.com
38+
39+
gcloud beta billing projects link ${GOOGLE_CLOUD_PROJECT} \
40+
--billing-account="${BILLING_ID}"
41+
```
42+
43+
## Create Google Cloud IAM Service Account & Key
44+
```shell
45+
gcloud iam service-accounts create ${IAM_NAME} \
46+
--display-name="${IAM_DESC}"
47+
48+
gcloud iam service-accounts keys create ${GOOGLE_APPLICATION_CREDENTIALS} \
49+
--iam-account="${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com"
50+
```
51+
52+
## Add IAM Policy Binding to Google Cloud Project
53+
```shell
54+
gcloud projects add-iam-policy-binding ${GOOGLE_CLOUD_PROJECT} \
55+
--member="serviceAccount:${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
56+
--role="roles/editor"
57+
```
58+
59+
## Add IAM Policy Binding to Google Cloud Organization
60+
```shell
61+
gcloud organizations add-iam-policy-binding ${ORG_ID} \
62+
--member="serviceAccount:${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
63+
--role="roles/resourcemanager.projectCreator"
64+
65+
gcloud organizations add-iam-policy-binding ${ORG_ID} \
66+
--member="serviceAccount:${IAM_NAME}@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com" \
67+
--role="roles/billing.user"
68+
```
69+
70+
## Create TerraHub Project
71+
```shell
72+
mkdir demo-terraform-google
73+
cd demo-terraform-google
74+
terrahub project -n demo-terraform-google
75+
```
76+
77+
## Create TerraHub Component
78+
```shell
79+
terrahub component -t google_project -n project
80+
terrahub component -t google_service_account -n project_service_account -o ../project
81+
terrahub component -t google_service_account_key -n project_service_account_key -o ../project_service_account
82+
terrahub component -t google_project_iam_member -n project_members -o ../project
83+
terrahub component -t google_project_iam_binding -n project_iam_policy_binding_storage_admin -o ../project_members
84+
terrahub component -t google_project_iam_binding -n project_iam_policy_binding_compute_admin -o ../project_members
85+
```
86+
87+
## Update TerraHub Component Config
88+
```shell
89+
terrahub configure -c terraform.var.google_org_id="${ORG_ID}"
90+
terrahub configure -c terraform.var.google_billing_account="${BILLING_ID}"
91+
terrahub configure -c terraform.var.google_project_id="${GOOGLE_CLOUD_PROJECT}"
92+
```
93+
94+
## Execute TerraHub Component
95+
```shell
96+
terrahub run -a -y
97+
```

project/.terrahub.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## local config
2+
component:
3+
name: 'project'
4+
5+
ci:
6+
mapping:
7+
- '.' ## current folder relative to where .terrahub.yml is defined

project/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# google_project
2+
3+
Allows creation and management of a Google Cloud Platform project.
4+
5+
Projects created with this resource must be associated with an Organization. See the Organization documentation for more details.
6+
7+
The service account used to run Terraform when creating a google_project resource must have roles/resourcemanager.projectCreator. See the Access Control for Organizations Using IAM doc for more information.
8+
9+
Note that prior to 0.8.5, google_project functioned like a data source, meaning any project referenced by it had to be created and managed outside Terraform. As of 0.8.5, google_project functions like any other Terraform resource, with Terraform creating and managing the project. To replicate the old behavior, either:
10+
11+
- Use the project ID directly in whatever is referencing the project, using the google_project_iam_policy to replace the old policy_data property.
12+
- Use the import functionality to import your pre-existing project into Terraform, where it can be referenced and used just like always, keeping in mind that Terraform will attempt to undo any changes made outside Terraform.
13+
14+
It's important to note that any project resources that were added to your Terraform config prior to 0.8.5 will continue to function as they always have, and will not be managed by Terraform. Only newly added projects are affected.
15+
16+
## input variables
17+
18+
| Name | Description | Type | Default | Required |
19+
|------|-------------|:----:|:-----:|:-----:|
20+
|google_project_name|The display name of the project.|string|project|No|
21+
|google_project_id|The project ID. Changing this forces a new project to be created.|string|project-f2754a99|No|
22+
|google_org_id|The numeric ID of the organization this project belongs to. Changing this forces a new project to be created. Only one of org_id or folder_id may be specified. If the org_id is specified then the project is created at the top level. Changing this forces the project to be migrated to the newly specified organization.|string||No|
23+
|google_project_folder_id|The numeric ID of the folder this project should be created under. Only one of org_id or folder_id may be specified. If the folder_id is specified, then the project is created under the specified folder. Changing this forces the project to be migrated to the newly specified folder.|string||No|
24+
|google_billing_account|The alphanumeric ID of the billing account this project belongs to. The user or service account performing this operation with Terraform must have Billing Account Administrator privileges (roles/billing.admin) in the organization. See Google Cloud Billing API Access Control for more details.|string||No|
25+
|google_project_skip_delete|If true, the Terraform resource can be deleted without deleting the Project via the Google API.|boolean|false|No|
26+
|google_project_auto_create_network|If true, the Terraform resource can be deleted without deleting the Project via the Google API.|boolean|false|No|
27+
|google_location_id|The location to serve the app from.|string|us-central|No|
28+
|custom_labels|Custom labels. A set of key/value label pairs to assign to the project.|map||No|
29+
|default_labels|Default labels. A set of key/value label pairs to assign to the project.|map|{"ThubName"= "project","ThubCode"= "f2754a99","ThubEnv"= "default","Description" = "Managed by TerraHub"}|No|
30+
31+
## output parameters
32+
33+
| Name | Description | Type |
34+
|------|-------------|:----:|
35+
|number|The numeric identifier of the project.|string|

project/main.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
resource "google_project" "project" {
2+
name = "${var.google_project_name}"
3+
project_id = "${var.google_project_id}"
4+
org_id = "${var.google_org_id}"
5+
folder_id = "${var.google_project_folder_id}"
6+
billing_account = "${var.google_billing_account}"
7+
#skip_delete = "${var.google_project_skip_delete}"
8+
auto_create_network = "${var.google_project_auto_create_network}"
9+
10+
#app_engine = {
11+
# location_id = "${var.google_location_id}"
12+
#}
13+
14+
labels = "${merge(var.default_labels, var.custom_labels)}"
15+
}

project/output.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Define list of variables to be output
2+
3+
output "number" {
4+
value = "${google_project.project.number}"
5+
}

project/provider.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
provider "google" {
2+
}

project/variables.tf

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Define list of variables to be used in main.tf
2+
3+
#############
4+
# top level #
5+
#############
6+
variable "google_project_name" {
7+
description = "The display name of the project."
8+
}
9+
10+
variable "google_project_id" {
11+
description = "The project ID. Changing this forces a new project to be created."
12+
}
13+
14+
variable "google_org_id" {
15+
description = "The numeric ID of the organization this project belongs to. Changing this forces a new project to be created. Only one of org_id or folder_id may be specified. If the org_id is specified then the project is created at the top level. Changing this forces the project to be migrated to the newly specified organization."
16+
}
17+
18+
variable "google_project_folder_id" {
19+
description = "The numeric ID of the folder this project should be created under. Only one of org_id or folder_id may be specified. If the folder_id is specified, then the project is created under the specified folder. Changing this forces the project to be migrated to the newly specified folder."
20+
}
21+
22+
variable "google_billing_account" {
23+
description = "The alphanumeric ID of the billing account this project belongs to. The user or service account performing this operation with Terraform must have Billing Account Administrator privileges (roles/billing.admin) in the organization. See Google Cloud Billing API Access Control for more details."
24+
}
25+
26+
variable "google_project_skip_delete" {
27+
description = "If true, the Terraform resource can be deleted without deleting the Project via the Google API."
28+
}
29+
30+
variable "google_project_auto_create_network" {
31+
description = "If true, the Terraform resource can be deleted without deleting the Project via the Google API."
32+
}
33+
34+
variable "google_location_id" {
35+
description = "The location to serve the app from."
36+
}
37+
38+
##########
39+
# labels #
40+
##########
41+
variable "custom_labels" {
42+
type = "map"
43+
description = "Custom labels. A set of key/value label pairs to assign to the project."
44+
default = {}
45+
}
46+
47+
variable "default_labels" {
48+
type = "map"
49+
description = "Default labels. A set of key/value label pairs to assign to the project."
50+
default = {}
51+
}
52+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## local config
2+
component:
3+
name: 'project_iam_policy_binding_compute_admin'
4+
dependsOn:
5+
- '../project_members'
6+
7+
ci:
8+
mapping:
9+
- '.' ## current folder relative to where .terrahub.yml is defined
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# google_project_iam_binding
2+
3+
Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the project are preserved.
4+
5+
Note: google_project_iam_binding resources can be used in conjunction with google_project_iam_member resources only if they do not grant privilege to the same role.
6+
7+
## input variables
8+
9+
| Name | Description | Type | Default | Required |
10+
|------|-------------|:----:|:-----:|:-----:|
11+
|google_project_id|The project ID. If not specified, uses the ID of the project configured with the provider.|string||Yes|
12+
|google_project_role|The project ID. If not specified, uses the ID of the project configured with the provider.|string|roles/viewer|No|
13+
|google_project_members|Identities that will be granted the privilege in role.|list|[]|No|
14+
15+
## output parameters
16+
17+
| Name | Description | Type |
18+
|------|-------------|:----:|
19+
|etag|The etag of the project's IAM policy.|string|

0 commit comments

Comments
 (0)