Skip to content

Commit

Permalink
Add example and README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
aashari committed Oct 15, 2021
1 parent a9bf504 commit a49a4e5
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 2 deletions.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Terraform GCP Cloud Run

This is a Terraform module to provision GCP Cloud Run to serve your container application or service

## Resources

This Terraform module will create below resources:

* Google Container Image
* Google Service Account for Cloud Run
* Google Cloud Run service

## Dependencies

* Terraform: ~> 1.0.0
* Terraform provider: hashicorp/archive v2.2.0
* Terraform provider: hashicorp/google v3.88.0
* Terraform provider: kreuzwerker/docker v2.15.0
* Terraform provider: hashicorp/null v3.1.0
* Terraform provider: hashicorp/random v3.1.0

## Input Variables
| Variable Name | Data Type | Is Required | Default Value | Description |
|-----------------------|--------------|-----------------------------------|---------------|----------------------------------------------------------------------------------------------------------|
| service_name | string | yes | | The name of your service, e.g. `my-web`, `ashari`, `ashweb`, or `andi-web` |
| gcp_project_name | string | yes | | The name of your GCP project |
| gcp_region | string | yes | | The name of GCP region |
| remove_image_when_destroy | bool | no | true | When you destroy the stacks, do you want to remove all of the images as well? |
| docker_source_path | string | yes | | The path of the service/application source code |
| docker_region | string | no | | The default region name, available value `asia`, `us`, `eu` |
| container_concurrency | string | no | 30 | The maximum concurrent per container |
| container_port | string | no | 3000 | The port to access the container |
| container_cpu | string | no | 2000m | The amount of CPU allocated for the container |
| container_memory | string | no | 4096Mi | The amount of memory allocated for the container |

## Output Variables

* **docker-image-url**: A generated docker image URL
* **docker-image-tag**: A generated docker image tag
* **service-endpoint**: A generated service URL

## Sample Usage

### Simple Implementation

```
module "gcp-cloud-run" {
source = "git@github.com:aashari/terraform-gcp-cloud-run.git?ref=v1.0.0"
service_name = "ashxyz"
gcp_project_name = "ashari-tech-main"
gcp_region = "asia-southeast2"
docker_source_path = "src"
docker_region = "asia"
}
```

## Contribute

Feel free to contribute, don't forget to raise an issue first then create a PR with referenced to that issue, thanks!
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testing
9 changes: 9 additions & 0 deletions examples/simple/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module "gcp-cloud-run" {
source = "../../"

service_name = "ashxyz"
gcp_project_name = "ashari-tech-main"
gcp_region = "asia-southeast2"
docker_source_path = "src"
docker_region = "asia"
}
19 changes: 19 additions & 0 deletions examples/simple/src/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Stage-1:
## Build image ~300MB
FROM node:16 as build

WORKDIR /app
COPY ./src /app/

RUN npm install

## Stage-2:
## Deployment image ~70MB
FROM node:16-slim

WORKDIR /app
COPY --from=build /app .

EXPOSE 3000

CMD ["node", "index.js"]
11 changes: 11 additions & 0 deletions examples/simple/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
res.send('Hello World!')
})

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
27 changes: 25 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ resource "google_service_account" "run" {
}

resource "google_project_iam_member" "run-iam" {
role = "roles/editor"
member = "serviceAccount:${google_service_account.run.email}"
role = "roles/editor"
member = "serviceAccount:${google_service_account.run.email}"
}

resource "google_cloud_run_service" "run" {
Expand Down Expand Up @@ -87,3 +87,26 @@ resource "google_cloud_run_service_iam_member" "member" {
role = "roles/run.invoker"
member = "allUsers"
}

resource "null_resource" "image-removal" {

count = var.remove_image_when_destroy == true ? 1 : 0

triggers = {
docker_image_url = local.docker_image_url
remove_image_when_destroy = var.remove_image_when_destroy
}

provisioner "local-exec" {
when = destroy
command = <<EOT
gcloud container images list-tags ${self.triggers.docker_image_url} --format=json \
| grep "tags" -A1 \
| tail -n 1 \
| sed -e 's/^[ \t]*//' \
| cut -d '"' -f 2 \
| xargs -L1 -I'{}' gcloud container images delete ${self.triggers.docker_image_url}:{} --force-delete-tags
EOT
}

}
14 changes: 14 additions & 0 deletions output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "docker-image-url" {
value = local.docker_image_url
description = "A generated docker image URL"
}

output "docker-image-tag" {
value = data.archive_file.init.output_sha
description = "A generated docker image tag"
}

output "service-endpoint" {
value = google_cloud_run_service.run.status[0].url
description = "A generated service URL"
}
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ variable "gcp_region" {
type = string
}

variable "remove_image_when_destroy" {
description = "When you destroy the stacks, do you want to remove all of the images as well?"
type = bool
default = true
}

variable "docker_source_path" {
description = "The path of the service/application source code"
type = string
Expand Down

0 comments on commit a49a4e5

Please sign in to comment.