1
0
Fork 0
mirror of https://github.com/imjasonh/kontain.me synced 2026-07-08 09:04:54 +00:00
kontain.me/kontainme.tf
2021-07-25 14:26:31 -04:00

214 lines
4.1 KiB
HCL

////// Providers
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.54.0"
}
}
}
provider "google" {
project = var.project
}
////// Variables
variable "project" {
type = string
default = "kontaindotme"
}
variable "bucket" {
type = string
default = "kontaindotme"
}
variable "region" {
type = string
default = "us-central1"
}
variable "images" {
type = map(string)
description = "images to deploy"
}
variable "services" {
type = map(object({
memory = string
cpu = string
concurrency = string
timeout = string
}))
description = "services to deploy"
default = {
"buildpack" = {
memory = "4Gi"
cpu = "2"
concurrency = "1"
timeout = "900" # 15m
}
"estargz" = {
memory = "4Gi"
cpu = "1"
concurrency = "80"
timeout = "300" # 5m
}
"flatten" = {
memory = "1Gi"
cpu = "1"
concurrency = "80"
timeout = "120" # 2m
}
"kaniko" = {
memory = "4Gi"
cpu = "2"
concurrency = "1"
timeout = "900" # 15m
}
"ko" = {
memory = "4Gi"
cpu = "2"
concurrency = "1"
timeout = "900" # 15m
}
"mirror" = {
memory = "1Gi"
cpu = "1"
concurrency = "80"
timeout = "60" # 1m
}
"mirror" = {
memory = "1Gi"
cpu = "1"
concurrency = "80"
timeout = "60" # 1m
}
"random" = {
memory = "1Gi"
cpu = "1"
concurrency = "80"
timeout = "60" # 1m
}
"viz" = {
memory = "1Gi"
cpu = "1"
concurrency = "80"
timeout = "60" # 1m
}
"wait" = {
memory = "1Gi"
cpu = "1"
concurrency = "80"
timeout = "60" # 1m
}
}
}
////// Cloud Storage
resource "google_storage_bucket" "bucket" {
name = var.bucket
location = "US"
uniform_bucket_level_access = false
// Delete objects older than 1 day.
lifecycle_rule {
condition {
age = 1
}
action {
type = "Delete"
}
}
}
// TODO: Configure public access + compute SA writer
////// App Engine + Cloud Tasks
// Enable Cloud Tasks API.
resource "google_project_service" "cloudtasks" {
service = "cloudtasks.googleapis.com"
}
resource "google_app_engine_application" "app" {
project = var.project
location_id = "us-central" // TODO: gross, GAE locations != GCP regions :'(
}
resource "google_cloud_tasks_queue" "wait-queue" {
name = "wait-queue"
location = var.region
depends_on = [google_project_service.cloudtasks, google_app_engine_application.app]
}
////// Cloud Run
// Enable Cloud Run API.
resource "google_project_service" "run" {
service = "run.googleapis.com"
}
// TODO: don't need this; can't delete it?
resource "google_project_service" "datastore" {
service = "datastore.googleapis.com"
}
// Deploy each service with its image.
resource "google_cloud_run_service" "services" {
for_each = var.services
name = each.key
location = var.region
template {
spec {
timeout_seconds = each.value.timeout
containers {
image = var.images[each.key]
env {
name = "BUCKET"
value = var.bucket
}
resources {
limits = {
memory = each.value.memory
cpu = each.value.cpu
}
}
}
}
}
traffic {
percent = 100
latest_revision = true
}
depends_on = [google_project_service.run]
}
// Make each service invokable by all users.
resource "google_cloud_run_service_iam_member" "allUsers" {
for_each = var.services
service = google_cloud_run_service.services[each.key].name
location = var.region
role = "roles/run.invoker"
member = "allUsers"
depends_on = [google_cloud_run_service.services]
}
// TODO: Ensure domain mappings.
output "services" {
value = {
for svc in google_cloud_run_service.services :
svc.name => svc.status[0].url
}
}