-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
156 lines (133 loc) · 4.01 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
provider "google" {
project = var.project_id
region = var.region
}
provider "google-beta" {
project = var.project_id
region = var.region
}
terraform {
required_providers {
time = {
source = "hashicorp/time"
}
}
required_version = ">= 1.8.0"
}
locals {
plan_time = provider::time::rfc3339_parse(plantimestamp())
}
data "external" "get_artifact_registry_repo" {
program = ["bash", "gcp.sh"]
query = {
project_id = var.project_id
region = var.region
repository_id = var.repository_id
}
}
output "artifact_registry_repo" {
value = data.external.get_artifact_registry_repo.result.name
}
resource "google_artifact_registry_repository" "my_repo" {
provider = google-beta
project = var.project_id
location = var.region
repository_id = var.repository_id
format = "DOCKER"
labels = {
env = "main"
}
count = data.external.get_artifact_registry_repo.result.name == "" ? 1 : 0
}
resource "null_resource" "build_and_push_image" {
provisioner "local-exec" {
command = <<EOT
gcloud auth configure-docker ${var.region}-docker.pkg.dev
docker build -t ${var.region}-docker.pkg.dev/${var.project_id}/${var.repository_id}/circle-app:${local.plan_time.unix} ./
docker push ${var.region}-docker.pkg.dev/${var.project_id}/${var.repository_id}/circle-app:${local.plan_time.unix}
EOT
}
}
resource "google_compute_instance" "circle_instance" {
name = "circle-instance"
machine_type = "e2-custom-8-16384"
zone = var.zone
boot_disk {
auto_delete = true
device_name = "circle-instance"
initialize_params {
image = "projects/cos-cloud/global/images/cos-109-17800-218-69"
size = 50
type = "pd-balanced"
}
mode = "READ_WRITE"
}
can_ip_forward = false
deletion_protection = false
enable_display = false
labels = {
container-vm = "cos-109-17800-218-69"
goog-ec-src = "vm_add-tf"
}
metadata = {
gce-container-declaration = <<-EOF
spec:
containers:
- name: circle-instance
image: ${var.region}-docker.pkg.dev/${var.project_id}/${var.repository_id}/circle-app:${local.plan_time.unix}
env:
- name: MONGO_URI
value: ${var.MONGO_URI}
- name: GOOGLE_TYPE
value: ${var.GOOGLE_TYPE}
- name: GOOGLE_PROJECT_ID
value: ${var.GOOGLE_PROJECT_ID}
- name: GOOGLE_PRIVATE_KEY_ID
value: ${var.GOOGLE_PRIVATE_KEY_ID}
- name: GOOGLE_PRIVATE_KEY
value: ${var.GOOGLE_PRIVATE_KEY}
- name: GOOGLE_CLIENT_EMAIL
value: ${var.GOOGLE_CLIENT_EMAIL}
- name: GOOGLE_CLIENT_ID
value: ${var.GOOGLE_CLIENT_ID}
- name: GOOGLE_AUTH_URI
value: ${var.GOOGLE_AUTH_URI}
- name: GOOGLE_TOKEN_URI
value: ${var.GOOGLE_TOKEN_URI}
- name: GOOGLE_AUTH_PROVIDER_X509_CERT_URL
value: ${var.GOOGLE_AUTH_PROVIDER_X509_CERT_URL}
- name: GOOGLE_CLIENT_X509_CERT_URL
value: ${var.GOOGLE_CLIENT_X509_CERT_URL}
- name: GOOGLE_UNIVERSE_DOMAIN
value: ${var.GOOGLE_UNIVERSE_DOMAIN}
stdin: false
tty: false
restartPolicy: OnFailure
EOF
google-logging-enabled = "true"
}
network_interface {
network = "default"
subnetwork = "projects/${var.project_id}/regions/${var.region}/subnetworks/default"
access_config {
network_tier = "PREMIUM"
}
}
scheduling {
automatic_restart = true
on_host_maintenance = "MIGRATE"
preemptible = false
provisioning_model = "STANDARD"
}
service_account {
email = var.service_account_email
scopes = ["https://www.googleapis.com/auth/cloud-platform"]
}
shielded_instance_config {
enable_integrity_monitoring = true
enable_secure_boot = false
enable_vtpm = true
}
tags = ["http-server", "https-server"]
depends_on = [null_resource.build_and_push_image]
}