Skip to content

Commit 95f5980

Browse files
committed
working terraform scripts from local
1 parent adecece commit 95f5980

22 files changed

+976
-135
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ override.tf.json
3535
# Ignore CLI configuration files
3636
.terraformrc
3737
terraform.rc
38+
39+
# Ignore terraform local files
40+
backend.conf
41+
backend-*.conf

README.md

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,119 @@
1-
# gbfs-validator-java-infra
1+
# gbfs-validator-java-infra
2+
3+
4+
# Set up new GCP environment
5+
6+
`All roads lead to Rome!` This quote is a reminder that there are multiple ways to get to the same final state.
7+
Take the following steps as a guidance and adapt them to your own local and organizational requirements.
8+
For more information regarding Google Cloud Platform and terraform go to the [Official GCP Site](https://cloud.google.com/) and [Terraform Official Site](https://www.terraform.io/).
9+
10+
## Initial project and remote state set up
11+
12+
- Create GCP project
13+
14+
```shell
15+
gcloud projects create gbfs-validator-staging --name="GBFS Validator Staging"
16+
```
17+
18+
- Assign a billing account to the project
19+
- Create a Firebase project to host the UI
20+
- Create Oauth credentials and to be used as part of the terraform parameters
21+
- Create SSL certificates for the Load Balancer
22+
- Enable and configure Identity Platform
23+
- Login to gcloud cli using,
24+
25+
```shell
26+
gcloud auth application-default login
27+
```
28+
29+
- Point local project environment variable to the newly created project
30+
31+
```shell
32+
gcloud config set project gbfs-validator-staging
33+
```
34+
35+
- Create a cloud storage bucket to persist the terraform state
36+
```
37+
gcloud storage buckets create gs://mobilitydata-gbfs-validator-state-staging \
38+
--project=gbfs-validator-staging \
39+
--location=northamerica-northeast1 \
40+
--uniform-bucket-level-access
41+
```
42+
- Create a terraform backend file using the template `backend.conf.rename_me` with name backend-<environment>.conf and populate the file with valid values.
43+
44+
- Create deployer service account
45+
```
46+
gcloud iam service-accounts create gbfs-deployer-service-account \
47+
--display-name="GBFS Terraform Deployer"
48+
```
49+
- Execute,
50+
51+
```shell
52+
terraform init -backend-config=backend-<environment>.conf
53+
```
54+
55+
- Create a terraform variables file using the template `vars.tfvars.rename_me` with name vars-<env>.tfvars and populate the file with valid values.
56+
- Execute and review the terraform plan,
57+
58+
```shell
59+
terraform plan -var-file=vars-<environment>.tfvars
60+
```
61+
62+
- Once you had reviewed the plan, execute the terraform apply command to commit the changes to the GCP environment using,
63+
- To be able to execute the apply command on the terraform-init project you need Project IAM Admin role
64+
65+
```shell
66+
terraform apply -var-file=vars-<environment>.tfvars
67+
```
68+
69+
- Troubleshooting
70+
- Make sure you have the right permissions.
71+
- `There is a delay due to configuration propagation on newly GCP enabled services`. In this case wait for the change to be propagated and execute the terraform apply command again.
72+
- If you had a previous GCP environment set up in your local folders, remove `.terraform` folder and `terraform.state*` files locally before running `terraform init` command.
73+
74+
### Adding new GCP service to the stack
75+
76+
The initial project set up is required while setting up a GCP environment also when `a new GCP service` is added to the stack.
77+
When a new service is added to the stack the service account used to deploy the infrastructure needs to have the required permissions.
78+
In this case,
79+
80+
- Add/modify roles and policies as necessary to the deployer's servie account in the `infra/terraform-init/main.tf`
81+
- From `infra/terraform-init/` execute,
82+
83+
```shell
84+
terraform apply -var-file=vars-<environment>.tfvars
85+
```
86+
87+
- Now you are in position to execute the main terraform script from `infra` folder.
88+
89+
## Deploy Feeds API
90+
91+
- Open the terminal in the folder `<project_dir>/infra`
92+
- Create a terraform backend file using the template `backend.conf.rename_me` with name backend-<environment>.conf and populate the file with valid values.
93+
- Execute,
94+
95+
```shell
96+
terraform init -backend-config=backend-<environment>.conf
97+
```
98+
99+
- One-time artifact set up. Set up the GCP artifact registry before-hand to be able to publish docker images.
100+
101+
```shell
102+
terraform apply -var-file=vars-<environment>.tfvars -target=module.artifact-registry
103+
```
104+
105+
- Remember that: `There is a delay due to configuration propagation on newly GCP enabled services.`. You might get 403 responses while GCP is propagating the new configuration.
106+
- You need at least one docker image published to be able to deploy the cloud run service. Execute the following script,
107+
108+
```shell
109+
<project_dir>/scripts/docker-build-push.sh -project_id mobility-feeds-<environment> -service feed-api -repo_name feeds-<environment> -region northamerica-northeast1 -version <version_number>
110+
```
111+
112+
- Set the version number on the `infra/vars-<environment>.tfvars` file.
113+
- Execute apply from infra folder
114+
115+
```shell
116+
terraform apply -var-file=vars-<environment>.tfvars
117+
```
118+
119+
- Enjoy Coding!

gbfs-validator/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Use a lightweight JDK base image
2+
FROM eclipse-temurin:17-jdk-alpine
3+
4+
# Set working directory
5+
WORKDIR /app
6+
7+
# Copy the fat JAR
8+
COPY gbfs-validator-java-api.jar app.jar
9+
10+
# org.entur.gbfs.validator.api.handler.OpenApiGeneratorApplication
11+
# Run the app
12+
ENTRYPOINT ["java", "-jar", "app.jar"]

gbfs-validator/function_config.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name_suffix": "gbfs-validator-api",
3+
"description": "API containing the GBFS validator",
4+
"entry_point": "org.entur.gbfs.validator.api.handler.OpenApiGeneratorApplication",
5+
"timeout": 540,
6+
"available_memory": "1Gi",
7+
"trigger_http": true,
8+
"ingress_settings": "ALLOW_ALL",
9+
"max_instance_request_concurrency": 1,
10+
"max_instance_count": 5,
11+
"min_instance_count": 0,
12+
"available_cpu": 1
13+
}
34.8 MB
Binary file not shown.

iam.tf

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

infra/.terraform.lock.hcl

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

infra/backend.conf.rename_me

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#
2+
# MobilityData 2025
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
# This file represents a template for setting up a remote terraform state.
18+
# If you choose to use a remote state:
19+
# - Create a GCP storage bucket with the target name.
20+
# - Make sure the `deployer` account has the right permissions on the newly created bucket.
21+
# - Rename this file to backend.conf
22+
# - Replace variable values.
23+
# - Execute: terraform init -backend-conf=backend.conf
24+
# - Enjoy coding!
25+
# More info: https://developer.hashicorp.com/terraform/language/state/remote
26+
27+
bucket = mobilitydata-gbfs-validator-state-{{BUCKET_NAME}}
28+
prefix = {{OBJECT_PREFIX}}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#
2+
# MobilityData 2025
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
18+
# This make the google project information accessible only keeping the project_id as a parameter in the previous provider resource
19+
data "google_project" "project" {
20+
}
21+
22+
terraform {
23+
required_providers {
24+
google = {
25+
source = "hashicorp/google"
26+
version = "~> 4.0"
27+
}
28+
}
29+
}
30+
31+
locals {
32+
gbfs_validator_config = jsondecode(file("${path.module}/../../gbfs-validator/function_config.json"))
33+
artifact_registry_repo = "gbfs-validator-${var.environment}"
34+
}
35+
36+
provider "google" {
37+
project = var.project_id
38+
region = var.gcp_region
39+
}
40+
41+
# This is a temporary patch until the publising of the Java jar is defined
42+
data "archive_file" "source_zip" {
43+
type = "zip"
44+
source_file = "${path.module}/../../gbfs-validator/${var.jar_file_name}"
45+
output_path = "/tmp/function-source.zip" # Temporary path for the zipped JAR
46+
}
47+
48+
resource "google_cloud_run_v2_service" "gbfs_validator_api" {
49+
name = "${var.environment}-${local.gbfs_validator_config.name_suffix}"
50+
location = var.gcp_region
51+
# ingress = "INGRESS_TRAFFIC_INTERNAL_LOAD_BALANCER"
52+
53+
template {
54+
service_account = var.gbfs_validator_service_account_email
55+
# vpc_access {
56+
# connector = data.google_vpc_access_connector.vpc_connector.id
57+
# egress = "ALL_TRAFFIC"
58+
# }
59+
containers {
60+
image = "${var.gcp_region}-docker.pkg.dev/${var.project_id}/${local.artifact_registry_repo}/${var.gbfs_api_service}:${var.feed_api_image_version}"
61+
resources {
62+
limits = {
63+
cpu = local.gbfs_validator_config.available_cpu
64+
memory = local.gbfs_validator_config.available_memory
65+
}
66+
}
67+
}
68+
}
69+
70+
labels = {
71+
environment = var.environment
72+
app = var.gbfs_validator_app_name
73+
}
74+
}

infra/cloud-run-service/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "function_url" {
2+
description = "The HTTPS trigger URL for the Cloud Function."
3+
value = google_cloud_run_v2_service.gbfs_validator_api.uri
4+
}
5+
6+
output "cloud_run_service_name" {
7+
description = "Name of the cloud run resource"
8+
value = google_cloud_run_v2_service.gbfs_validator_api.name
9+
}

0 commit comments

Comments
 (0)