|
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! |
0 commit comments