Skip to content

Commit e3683a2

Browse files
committed
cleanup
1 parent ad93b49 commit e3683a2

File tree

6 files changed

+35
-63
lines changed

6 files changed

+35
-63
lines changed

terraform/dev/main.tf

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ provider "docker" {
3636
}
3737

3838
module "gateway" {
39-
source = "./../modules/api-gateway"
39+
source = "../modules/api-gateway"
4040
project = var.project
4141
environment = var.environment
4242
region = var.region
@@ -130,12 +130,11 @@ EOF
130130
}
131131

132132
module "endpoints" {
133-
source = "./../modules/run-service"
134-
entry_point = "app"
133+
source = "../modules/run-service"
135134
project = var.project
136135
environment = var.environment
137136
source_directory = "../../src"
138-
function_name = "tech-report-api"
137+
service_name = "tech-report-api"
139138
region = var.region
140139
service_account_email = var.google_service_account_cloud_functions
141140
service_account_api_gateway = var.google_service_account_api_gateway

terraform/modules/api-gateway/variables.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ variable "environment" {
66
variable "project" {
77
description = "The ID of the project in which the resource belongs. If it is not provided, the provider project is used."
88
type = string
9+
default = "httparchive"
910
}
1011
variable "region" {
1112
description = "The Region of this resource"
1213
type = string
14+
default = "us-central1"
1315
}
1416
variable "service_account_email" {
1517
description = "Email of the service account associated with and to run the API Gateway"

terraform/modules/run-service/docker.tf

Lines changed: 0 additions & 20 deletions
This file was deleted.

terraform/modules/run-service/main.tf

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,33 @@ terraform {
77
}
88
}
99

10+
# Calculate hash of source files to determine if rebuild is needed
11+
locals {
12+
source_files = fileset(path.root, "${var.source_directory}/*")
13+
source_hash = substr(sha1(join("", [for f in local.source_files : filesha1(f)])), 0, 8)
14+
}
15+
16+
# Build Docker image
17+
resource "docker_image" "function_image" {
18+
name = "${var.region}-docker.pkg.dev/${var.project}/tech-report-api/${var.service_name}:${local.source_hash}"
19+
20+
build {
21+
context = var.source_directory
22+
dockerfile = "Dockerfile"
23+
platform = "linux/amd64"
24+
}
25+
}
26+
27+
resource "docker_registry_image" "registry_image" {
28+
name = docker_image.function_image.name
29+
}
30+
1031
resource "google_cloud_run_v2_service" "service" {
11-
name = "${var.function_name}-${var.environment}"
32+
name = "${var.service_name}-${var.environment}"
1233
location = var.region
1334

1435
deletion_protection = false
15-
ingress = var.ingress_settings
36+
ingress = "INGRESS_TRAFFIC_INTERNAL_LOAD_BALANCER"
1637

1738
template {
1839
service_account = var.service_account_email
@@ -32,16 +53,11 @@ resource "google_cloud_run_v2_service" "service" {
3253
value = env.value
3354
}
3455
}
35-
3656
}
37-
38-
3957
timeout = var.timeout
4058
max_instance_request_concurrency = var.max_instance_request_concurrency
41-
4259
}
4360
scaling {
44-
4561
min_instance_count = var.min_instances
4662
}
4763
traffic {

terraform/modules/run-service/variables.tf

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@ variable "region" {
55
variable "environment" {
66
description = "The 'Environment' that is being created/deployed. Applied as a suffix to many resources."
77
type = string
8+
default = "dev"
89
}
910
variable "source_directory" {
1011
description = "The folder of the package containing function that will be executed when the Google Cloud Function is triggered!"
1112
type = string
1213
}
13-
variable "function_name" {
14+
variable "service_name" {
1415
description = "Optional: Can be used to create more than function from the same package"
1516
type = string
1617
}
17-
variable "entry_point" {
18-
description = "The entry point; This is either what is registered with 'http' or exported from the code as a handler!"
19-
type = string
20-
}
2118
variable "available_memory_gb" {
2219
default = "2Gi"
2320
type = string
@@ -28,19 +25,10 @@ variable "available_cpu" {
2825
type = string
2926
description = "The amount of CPU for the Cloud Function"
3027
}
31-
variable "ingress_settings" {
32-
type = string
33-
default = "INGRESS_TRAFFIC_INTERNAL_LOAD_BALANCER"
34-
description = "String value that controls what traffic can reach the function. Allowed values are ALLOW_ALL, ALLOW_INTERNAL_AND_GCLB and ALLOW_INTERNAL_ONLY. Check ingress documentation to see the impact of each settings value. Changes to this field will recreate the cloud function."
35-
}
36-
variable "vpc_connector_egress_settings" {
37-
type = string
38-
default = null
39-
description = "The egress settings for the connector, controlling what traffic is diverted through it. Allowed values are ALL_TRAFFIC and PRIVATE_RANGES_ONLY. Defaults to PRIVATE_RANGES_ONLY. If unset, this field preserves the previously set value."
40-
}
4128
variable "project" {
4229
description = "The ID of the project in which the resource belongs. If it is not provided, the provider project is used."
4330
type = string
31+
default = "httparchive"
4432
}
4533
variable "timeout" {
4634
default = "60s"
@@ -55,11 +43,7 @@ variable "service_account_api_gateway" {
5543
type = string
5644
description = "API Gateway service account who can invoke this function. This is required!"
5745
}
58-
variable "max_instances" {
59-
default = 10
60-
type = number
61-
description = "(Optional) The limit on the maximum number of function instances that may coexist at a given time."
62-
}
46+
6347
variable "min_instances" {
6448
description = "(Optional) The limit on the minimum number of function instances that may coexist at a given time."
6549
type = number

terraform/prod/main.tf

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ provider "google-beta" {
2222
region = var.region
2323
}
2424

25-
2625
# Get current Google Cloud access token
2726
data "google_client_config" "default" {}
2827

@@ -35,13 +34,9 @@ provider "docker" {
3534
}
3635
}
3736

38-
39-
4037
module "gateway" {
41-
source = "./../modules/api-gateway"
42-
project = var.project
38+
source = "../modules/api-gateway"
4339
environment = var.environment
44-
region = var.region
4540
service_account_email = var.google_service_account_api_gateway
4641
spec_yaml = <<EOF
4742
swagger: "2.0"
@@ -125,13 +120,10 @@ EOF
125120
}
126121

127122
module "endpoints" {
128-
source = "./../modules/run-service"
129-
entry_point = "app"
130-
project = var.project
123+
source = "../modules/run-service"
131124
environment = var.environment
132125
source_directory = "../../src"
133-
function_name = "tech-report-api"
134-
region = var.region
126+
service_name = "tech-report-api"
135127
service_account_email = var.google_service_account_cloud_functions
136128
service_account_api_gateway = var.google_service_account_api_gateway
137129
min_instances = var.min_instances
@@ -141,7 +133,6 @@ module "endpoints" {
141133
}
142134
}
143135

144-
145136
moved {
146137
from = google_api_gateway_api.api
147138
to = module.gateway.google_api_gateway_api.api

0 commit comments

Comments
 (0)