https://github.com/574n13y/gcp-cloudrun
Deploying to Google Cloud Run with Terraform
https://github.com/574n13y/gcp-cloudrun
cloudrun gcp gitlab gitlab-ci terraform
Last synced: 2 months ago
JSON representation
Deploying to Google Cloud Run with Terraform
- Host: GitHub
- URL: https://github.com/574n13y/gcp-cloudrun
- Owner: 574n13y
- License: mit
- Created: 2024-01-27T09:59:01.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-01-27T10:57:24.000Z (over 1 year ago)
- Last Synced: 2025-02-01T20:30:31.972Z (4 months ago)
- Topics: cloudrun, gcp, gitlab, gitlab-ci, terraform
- Language: HCL
- Homepage: https://574n13y.github.io/GCP-CloudRun/
- Size: 40 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# GCP-CloudRun
Deploying to Google Cloud Run with Terraform# gcp-microservices-iac
## Getting started
To make it easy for you to get started with GitLab, here's a list of recommended next steps.
Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
## Add your files
- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:```
cd existing_repo
git remote add origin https://gitlab.com/574n13y/gcp-microservices-iac.git
git branch -M main
git push -uf origin main
```## Integrate with your tools
- [ ] [Set up project integrations](https://gitlab.com/574n13y/gcp-microservices-iac/-/settings/integrations)
## Collaborate with your team
- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)## Test and Deploy
Use the built-in continuous integration in GitLab.
- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)***
## Project status
### Prerequisites
To follow this tutorial you will need:
- Terraform CLI. I recommend using the latest version, currently v0.14. Instructions to download and install Terraform can be found here.
- Google Cloud SDK. The most recent version should also work well for this tutorial. Installation instructions here.
- A Google Cloud account. If you don’t have one, create it here.### Initial setup
- Start by authenticating the SDK to Google Cloud:
- Create a new project where your Cloud Run service will be deployed. Replace PROJECT_ID and PROJECT_NAME with the desired values:
- Creating your first service
```
terraform {
required_version = ">= 0.14"required_providers {
# Cloud Run support was added on 3.3.0
google = ">= 3.3"
}
}provider "google" {
# Replace `PROJECT_ID` with your project
project = "vivesh-405513"
}resource "google_project_service" "run_api" {
service = "run.googleapis.com"disable_on_destroy = true
}resource "google_cloud_run_service" "run_service" {
name = "app"
location = "us-central1"template {
spec {
containers {
image = "gcr.io/google-samples/hello-app:1.0"
}
}
}traffic {
percent = 100
latest_revision = true
}# Waits for the Cloud Run API to be enabled
depends_on = [google_project_service.run_api]
}resource "google_cloud_run_service_iam_member" "run_all_users" {
service = google_cloud_run_service.run_service.name
location = google_cloud_run_service.run_service.location
role = "roles/run.invoker"
member = "allUsers"
}resource "google_storage_bucket" "auto-expire" {
name = "stanley_bucket_iac"
location = "US"
force_destroy = truepublic_access_prevention = "enforced"
}output "service_url" {
value = google_cloud_run_service.run_service.status[0].url
}
```
- Let’s stop for a while and check what the code above is doing:
```
name: the name of your service. It will be displayed in the public URL.
location: the region where your service will run. See all the options here.
image: The Docker image that will be used to create the container. Cloud Run has direct support for images from the Container Registry and Artifact Registry.
traffic: controls the traffic for this revision. The percent property indicates how much traffic will be redirected to this revision. latest_revision specifies that this traffic configuration needs to be used for the latest revision.
depends_on: waits for a resource to be ready, in this case, the Cloud Run API.
```
- Invoking the service --> By default, Cloud Run services are private and secured by IAM. To access them, you would need valid credentials with at least the Cloud Run Invoker permission set.
- Deploying the infrastructure
`` terraform init ``
`` terraform plan ``
`` terrafrom apply ``


- Updating the service ``image = "gcr.io/google-samples/hello-app:2.0" ``
- Run terraform apply to deploy the changes:


- Cleaning up
- To delete all resources created with Terraform, run the following command and confirm the prompt:


- This will disable the Cloud Run API, delete the Cloud Run service and its permissions.
- The project was created using the gcloud CLI tool, so you will need to delete it manually. For that, you can run:
## Gitlab Validate


