Skip to content

Commit 312251f

Browse files
committed
terraform code added
1 parent 0ddaea0 commit 312251f

File tree

14 files changed

+705
-2
lines changed

14 files changed

+705
-2
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ override.tf.json
1616
terraform.rc
1717
*.tfvars
1818
*.local.tfvars
19-
20-
/terraform
19+
spec.yaml
2120

2221
# env
2322
.envrc

terraform/dev/main.tf

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
provider "google" {
3+
project = "httparchive"
4+
region = "us-east1"
5+
request_timeout = "60m"
6+
}
7+
8+
terraform {
9+
backend "gcs" {
10+
bucket = var.project_bucket
11+
prefix = "dev"
12+
}
13+
}
14+
15+
module "backend-api" {
16+
source = "./../modules/api-gateway"
17+
environment = "dev"
18+
project = "httparchive"
19+
region = "us-east1"
20+
service_account_email = var.google_service_account_api_gateway
21+
}
22+
23+
module "cwvtech" {
24+
source = "./../modules/cloud-function"
25+
entry_point = "dispatcher"
26+
project = "httparchive"
27+
environment = "dev"
28+
source_directory = "../../functions/cwvtech"
29+
function_name = "cwvtech"
30+
service_account_email = var.google_service_account_cloud_functions
31+
service_account_api_gateway = var.google_service_account_api_gateway
32+
environment_variables = {
33+
"PROJECT" = "httparchive",
34+
"DATABASE" = var.project_database
35+
}
36+
}
37+
38+
module "lighthouse" {
39+
source = "./../modules/cloud-function"
40+
entry_point = "dispatcher"
41+
project = "httparchive"
42+
environment = "dev"
43+
source_directory = "../../functions/lighthouse"
44+
function_name = "lighthouse"
45+
service_account_email = var.google_service_account_cloud_functions
46+
service_account_api_gateway = var.google_service_account_api_gateway
47+
environment_variables = {
48+
"PROJECT" = "httparchive",
49+
"DATABASE" = var.project_database
50+
}
51+
}
52+
53+
module "adoption" {
54+
source = "./../modules/cloud-function"
55+
entry_point = "dispatcher"
56+
project = "httparchive"
57+
environment = "dev"
58+
source_directory = "../../functions/adoption"
59+
function_name = "adoption"
60+
service_account_email = var.google_service_account_cloud_functions
61+
service_account_api_gateway = var.google_service_account_api_gateway
62+
environment_variables = {
63+
"PROJECT" = "httparchive",
64+
"DATABASE" = var.project_database
65+
}
66+
}
67+
68+
module "page-weight" {
69+
source = "./../modules/cloud-function"
70+
entry_point = "dispatcher"
71+
project = "httparchive"
72+
environment = "dev"
73+
source_directory = "../../functions/page-weight"
74+
function_name = "page-weight"
75+
service_account_email = var.google_service_account_cloud_functions
76+
service_account_api_gateway = var.google_service_account_api_gateway
77+
environment_variables = {
78+
"PROJECT" = "httparchive",
79+
"DATABASE" = var.project_database
80+
}
81+
}
82+
83+
module "categories" {
84+
source = "./../modules/cloud-function"
85+
entry_point = "dispatcher"
86+
project = "httparchive"
87+
environment = "dev"
88+
source_directory = "../../functions/categories"
89+
function_name = "categories"
90+
service_account_email = var.google_service_account_cloud_functions
91+
service_account_api_gateway = var.google_service_account_api_gateway
92+
environment_variables = {
93+
"PROJECT" = "httparchive",
94+
"DATABASE" = var.project_database
95+
}
96+
}
97+
98+
module "technologies" {
99+
source = "./../modules/cloud-function"
100+
entry_point = "dispatcher"
101+
project = "httparchive"
102+
environment = "dev"
103+
source_directory = "../../functions/technologies"
104+
function_name = "technologies"
105+
service_account_email = var.google_service_account_cloud_functions
106+
service_account_api_gateway = var.google_service_account_api_gateway
107+
environment_variables = {
108+
"PROJECT" = "httparchive",
109+
"DATABASE" = var.project_database
110+
}
111+
}
112+
113+
module "ranks" {
114+
source = "./../modules/cloud-function"
115+
entry_point = "dispatcher"
116+
project = "httparchive"
117+
environment = "dev"
118+
source_directory = "../../functions/ranks"
119+
function_name = "ranks"
120+
service_account_email = var.google_service_account_cloud_functions
121+
service_account_api_gateway = var.google_service_account_api_gateway
122+
environment_variables = {
123+
"PROJECT" = "httparchive",
124+
"DATABASE" = var.project_database
125+
}
126+
}
127+
128+
module "geos" {
129+
source = "./../modules/cloud-function"
130+
entry_point = "dispatcher"
131+
project = "httparchive"
132+
environment = "dev"
133+
source_directory = "../../functions/geos"
134+
function_name = "geos"
135+
service_account_email = var.google_service_account_cloud_functions
136+
service_account_api_gateway = var.google_service_account_api_gateway
137+
environment_variables = {
138+
"PROJECT" = "httparchive",
139+
"DATABASE" = var.project_database
140+
}
141+
}

terraform/dev/variables.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "google_service_account_cloud_functions" {
2+
type = string
3+
description = "Service account for Cloud Functions"
4+
}
5+
6+
variable "google_service_account_api_gateway" {
7+
type = string
8+
description = "Service account for API Gateway"
9+
}
10+
11+
variable "project_database" {
12+
type = string
13+
description = "The database name"
14+
15+
}
16+
17+
variable "project_bucket" {
18+
type = string
19+
description = "The project name"
20+
}

terraform/modules/api-gateway/main.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
######################################
2+
# API Gateway
3+
######################################
4+
# Used to expose Internal resources to external sources, such as web applications
5+
# See https://cloud.google.com/api-gateway/docs for more information
6+
# The API used by the Gateway
7+
resource "google_api_gateway_api" "api" {
8+
provider = google-beta # API Gateway is still in beta
9+
api_id = "api-gw-${var.environment}"
10+
display_name = "The ${var.environment} API Gateway"
11+
project = var.project
12+
}
13+
# A Configuration, consisting of an OpenAPI specification
14+
resource "google_api_gateway_api_config" "api_config" {
15+
provider = google-beta # API Gateway is still in beta
16+
api = google_api_gateway_api.api.api_id
17+
api_config_id_prefix = "api"
18+
project = var.project
19+
display_name = "The ${var.environment} Config"
20+
openapi_documents {
21+
document {
22+
path = "spec.yaml" # File name is simply sugar to show on GCP
23+
contents = filebase64("spec.yaml") # This is based on *who* is call the module!
24+
}
25+
}
26+
gateway_config {
27+
backend_config {
28+
google_service_account = var.service_account_email
29+
}
30+
}
31+
}
32+
# The actual API Gateway
33+
resource "google_api_gateway_gateway" "gateway" {
34+
provider = google-beta
35+
project = var.project
36+
region = var.region
37+
api_config = google_api_gateway_api_config.api_config.id
38+
gateway_id = "${var.environment}-gw"
39+
display_name = "${var.environment} Api Gateway"
40+
labels = {
41+
owner = "tech_report_api"
42+
environment = var.environment
43+
}
44+
depends_on = [google_api_gateway_api_config.api_config]
45+
lifecycle {
46+
replace_triggered_by = [
47+
google_api_gateway_api_config.api_config
48+
]
49+
}
50+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
resource "google_compute_global_address" "default" {
2+
#count = var.environment == "prod" ? 1 : 0
3+
count = 0
4+
5+
project = var.project
6+
name = "httparchive-api-gateway-address"
7+
address_type = "EXTERNAL"
8+
ip_version = "IPV4"
9+
}
10+
11+
resource "google_compute_global_forwarding_rule" "https" {
12+
#count = var.environment == "prod" ? 1 : 0
13+
count = 0
14+
15+
provider = google-beta
16+
project = var.project
17+
name = "httparchive-api-gateway-https"
18+
target = google_compute_target_https_proxy.default[count.index].self_link
19+
ip_address = google_compute_global_address.default[count.index].id
20+
port_range = "443"
21+
load_balancing_scheme = "EXTERNAL_MANAGED"
22+
}
23+
24+
resource "google_compute_managed_ssl_certificate" "default" {
25+
#count = var.environment == "prod" ? 1 : 0
26+
count = 0
27+
28+
name = "httparchive-api-gateway-ssl"
29+
managed {
30+
domains = ["api.httparchive.org"]
31+
}
32+
33+
}
34+
35+
resource "google_compute_target_https_proxy" "default" {
36+
#count = var.environment == "prod" ? 1 : 0
37+
count = 0
38+
39+
provider = google-beta
40+
project = var.project
41+
name = "httparchive-api-gateway-https-proxy"
42+
url_map = google_compute_url_map.default[count.index].id
43+
ssl_certificates = [google_compute_managed_ssl_certificate.default[count.index].id]
44+
}
45+
46+
resource "google_compute_region_network_endpoint_group" "function_neg" {
47+
#count = var.environment == "prod" ? 1 : 0
48+
count = 0
49+
50+
provider = google-beta
51+
name = "httparchive-api-gateway-function-neg"
52+
network_endpoint_type = "SERVERLESS"
53+
project = var.project
54+
region = var.region
55+
56+
serverless_deployment {
57+
platform = "apigateway.googleapis.com"
58+
resource = google_api_gateway_gateway.gateway.gateway_id
59+
}
60+
61+
}
62+
63+
resource "google_compute_backend_service" "backend_neg" {
64+
#count = var.environment == "prod" ? 1 : 0
65+
count = 0
66+
67+
provider = google-beta
68+
name = "httparchive-api-gateway-backend-neg"
69+
project = var.project
70+
load_balancing_scheme = "EXTERNAL_MANAGED"
71+
protocol = "HTTP"
72+
backend {
73+
group = google_compute_region_network_endpoint_group.function_neg[count.index].self_link
74+
}
75+
76+
}
77+
78+
resource "google_compute_url_map" "default" {
79+
#count = var.environment == "prod" ? 1 : 0
80+
count = 0
81+
82+
provider = google-beta
83+
project = var.project
84+
name = "httparchive-api-gateway-url-map"
85+
default_service = google_compute_backend_service.backend_neg[count.index].self_link
86+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
variable "environment" {
2+
description = "The 'Environment' that is being created/deployed. Applied as a suffix to many resources."
3+
type = string
4+
}
5+
variable "project" {
6+
description = "The ID of the project in which the resource belongs. If it is not provided, the provider project is used."
7+
type = string
8+
}
9+
variable "region" {
10+
description = "The Region of this resource"
11+
type = string
12+
}
13+
variable "service_account_email" {
14+
description = "Email of the service account associated with and to run the API Gateway"
15+
type = string
16+
}
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+

0 commit comments

Comments
 (0)