Skip to content

Commit 2d5fba4

Browse files
authored
Merge pull request #17 from artefactory/dev
0.0.1
2 parents dc5a6d6 + 741b875 commit 2d5fba4

29 files changed

+762
-2
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#terraform
2+
.terraform
3+
*.tfstate
4+
*.tfstate.backup
5+
*.tfvars

IaC/main.tf

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
terraform {
2+
backend "gcs" {
3+
}
4+
required_version = "=0.12.29"
5+
required_providers {
6+
google = "~> 3.13"
7+
}
8+
}
9+
10+
provider "google" {
11+
project = var.project_id
12+
}
13+
14+
provider "google-beta" {
15+
project = var.project_id
16+
}
17+
18+
19+
module "network" {
20+
source = "./modules/network"
21+
vpc_name = var.network_name
22+
}
23+
24+
module "mlflow" {
25+
source = "./modules/mlflow"
26+
artifacts_bucket_name = var.artifacts_bucket
27+
db_password_value = var.db_password_value
28+
private_vpc_connection = module.network.private_vpc_connection
29+
network_link = module.network.network_link
30+
server_docker_image = var.mlflow_docker_image
31+
project_id = var.project_id
32+
vpc_connector = module.network.vpc_connector
33+
}

IaC/modules/mlflow/artifacts/main.tf

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
resource "google_storage_bucket" "this" {
2+
name = var.bucket_name
3+
location = var.bucket_location
4+
storage_class = var.storage_class
5+
versioning {
6+
enabled = var.versioning_enabled
7+
}
8+
lifecycle_rule {
9+
condition {
10+
num_newer_versions = var.number_of_version
11+
}
12+
action {
13+
type = "Delete"
14+
}
15+
}
16+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "url" {
2+
description = "gcs uri"
3+
value = google_storage_bucket.this.url
4+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "bucket_name" {
2+
description = "Name of the bucket."
3+
type = string
4+
}
5+
variable "bucket_location" {
6+
description = "Location of the bucket."
7+
type = string
8+
default = "EUROPE-WEST1"
9+
}
10+
variable "versioning_enabled" {
11+
description = "True if you want to version your bucket."
12+
type = bool
13+
default = true
14+
}
15+
variable "number_of_version" {
16+
description = "Number of version you want to keep with the versionning."
17+
type = number
18+
default = 1
19+
}
20+
variable "storage_class" {
21+
description = "Storage class of your bucket"
22+
type = string
23+
default ="STANDARD"
24+
}
25+
variable "module_depends_on" {
26+
type = any
27+
default = null
28+
}

IaC/modules/mlflow/database/main.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
resource "random_id" "db_name_suffix" {
2+
byte_length = 5
3+
}
4+
5+
resource "google_sql_database_instance" "this_instance" {
6+
name = "${var.instance_prefix}-${random_id.db_name_suffix.hex}"
7+
database_version = var.database_version
8+
region = var.region
9+
10+
depends_on = [var.private_vpc_connection]
11+
12+
settings {
13+
tier = var.size
14+
ip_configuration {
15+
ipv4_enabled = false
16+
private_network = var.network_link
17+
}
18+
backup_configuration {
19+
enabled = true
20+
}
21+
availability_type = var.availability_type
22+
23+
}
24+
}
25+
26+
resource "google_sql_database" "this_database" {
27+
name = var.database_name
28+
instance = google_sql_database_instance.this_instance.name
29+
depends_on = [google_sql_database_instance.this_instance]
30+
}
31+
32+
resource "google_sql_user" "this_user" {
33+
name = var.username
34+
instance = google_sql_database_instance.this_instance.name
35+
password = var.password
36+
depends_on = [google_sql_database_instance.this_instance]
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
output "instance_connection_name" {
2+
description = "Connection string used to connect to the instance"
3+
value = google_sql_database_instance.this_instance.connection_name
4+
}
5+
output "private_ip" {
6+
description = "Private ip connect to the instance"
7+
value = google_sql_database_instance.this_instance.private_ip_address
8+
}
9+
output "database_name" {
10+
description = "The name of the database"
11+
value = google_sql_database.this_database.name
12+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
variable "instance_prefix" {
2+
type = string
3+
description = "Name of the database instance you want to deploy"
4+
default = "mlflow"
5+
}
6+
variable "database_version" {
7+
type = string
8+
description = "Version of the database instance you use"
9+
default = "MYSQL_5_7"
10+
}
11+
variable "region" {
12+
type = string
13+
description = "Region of the database instance"
14+
default = "europe-west1"
15+
}
16+
variable "private_vpc_connection" {
17+
type = any
18+
description = "Private connection used to connect your instance with"
19+
}
20+
variable "size" {
21+
type = string
22+
description = "Size of the database instance"
23+
default = "db-f1-micro"
24+
}
25+
variable "network_link" {
26+
type = string
27+
description = "Network link you want to connect your database with"
28+
}
29+
variable "availability_type" {
30+
type = string
31+
description = "Availability of your instance"
32+
default = "ZONAL"
33+
}
34+
variable "database_name" {
35+
type = string
36+
description = "Name of the database created"
37+
default = "mlflow"
38+
}
39+
variable "username" {
40+
type = string
41+
description = "Username to connect to database instance"
42+
}
43+
variable "password" {
44+
type = string
45+
description = "Password to connect to database instance"
46+
}
47+
variable "module_depends_on" {
48+
type = any
49+
default = null
50+
}

IaC/modules/mlflow/main.tf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module "artifacts" {
2+
source = "./artifacts"
3+
bucket_name = var.artifacts_bucket_name
4+
bucket_location = var.artifacts_bucket_location
5+
number_of_version = var.artifacts_number_of_version
6+
storage_class = var.artifacts_storage_class
7+
}
8+
9+
module "db_secret" {
10+
source = "./secret_manager"
11+
secret_id = var.db_password_name
12+
secret_value = var.db_password_value
13+
}
14+
15+
module "database" {
16+
source = "./database"
17+
instance_prefix = var.db_instance_prefix
18+
database_version = var.db_version
19+
region = var.db_region
20+
private_vpc_connection = var.private_vpc_connection
21+
size = var.db_size
22+
network_link = var.network_link
23+
availability_type = var.db_availability_type
24+
database_name = var.db_name
25+
username = var.db_username
26+
password = module.db_secret.secret_value
27+
}
28+
29+
module "server" {
30+
source = "./server"
31+
server_name = var.mlflow_server
32+
location = var.server_location
33+
docker_image_name = var.server_docker_image
34+
env_variables = var.server_env_variables
35+
sql_instance_name = module.database.instance_connection_name
36+
db_private_ip = module.database.private_ip
37+
project_id = var.project_id
38+
db_password_name = var.db_password_name
39+
db_username = var.db_username
40+
db_name = var.db_name
41+
gcs_backend = module.artifacts.url
42+
vpc_connector = var.vpc_connector
43+
module_depends_on = var.module_depends_on
44+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "google_secret_manager_secret" "secret" {
2+
provider = google-beta
3+
4+
secret_id = var.secret_id
5+
6+
replication {
7+
automatic = true
8+
}
9+
}
10+
11+
12+
resource "google_secret_manager_secret_version" "secret-version" {
13+
provider = google-beta
14+
15+
secret = google_secret_manager_secret.secret.id
16+
17+
secret_data = var.secret_value
18+
depends_on = [google_secret_manager_secret.secret]
19+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "secret_value" {
2+
description = "Value of the created secret"
3+
value = google_secret_manager_secret_version.secret-version.secret_data
4+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
variable "secret_id" {
2+
type = string
3+
description = "Name of the secret you want to create"
4+
}
5+
variable "secret_value" {
6+
type = string
7+
description = "value of the secret you want to create"
8+
}
9+
variable "module_depends_on" {
10+
type = any
11+
default = null
12+
}

IaC/modules/mlflow/server/main.tf

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
locals {
2+
env_variables = merge(
3+
{
4+
"GCP_PROJECT"=var.project_id,
5+
"DB_PASSWORD_NAME"=var.db_password_name,
6+
"DB_USERNAME"=var.db_username,
7+
"DB_NAME"=var.db_name,
8+
"DB_PRIVATE_IP"=var.db_private_ip,
9+
"GCS_BACKEND"=var.gcs_backend
10+
}, var.env_variables)
11+
}
12+
13+
14+
resource "google_service_account" "service_account_cloud_run" {
15+
account_id = format("cloud-run-%s", var.server_name)
16+
display_name = "Cloud run service account used"
17+
}
18+
19+
resource "google_project_iam_member" "cloudsql" {
20+
project = google_service_account.service_account_cloud_run.project
21+
role = "roles/cloudsql.client"
22+
member = format("serviceAccount:%s", google_service_account.service_account_cloud_run.email)
23+
}
24+
25+
resource "google_project_iam_member" "secret" {
26+
project = google_service_account.service_account_cloud_run.project
27+
role = "roles/secretmanager.secretAccessor"
28+
member = format("serviceAccount:%s", google_service_account.service_account_cloud_run.email)
29+
}
30+
31+
resource "google_project_iam_member" "gcs" {
32+
project = google_service_account.service_account_cloud_run.project
33+
role = "roles/storage.objectAdmin"
34+
member = format("serviceAccount:%s", google_service_account.service_account_cloud_run.email)
35+
}
36+
37+
38+
resource "google_cloud_run_service" "default" {
39+
name = var.server_name
40+
location = var.location
41+
42+
template {
43+
spec {
44+
service_account_name = google_service_account.service_account_cloud_run.email
45+
containers {
46+
image = var.docker_image_name
47+
dynamic "env" {
48+
for_each = local.env_variables
49+
content {
50+
name = env.key
51+
value = env.value
52+
}
53+
}
54+
resources {
55+
limits = {
56+
cpu = var.cpu_limit
57+
memory = var.memory_limit
58+
}
59+
}
60+
}
61+
}
62+
metadata {
63+
annotations = {
64+
"run.googleapis.com/cloudsql-instances" = var.sql_instance_name
65+
"run.googleapis.com/vpc-access-connector" = var.vpc_connector
66+
}
67+
}
68+
}
69+
70+
traffic {
71+
percent = 100
72+
latest_revision = true
73+
}
74+
autogenerate_revision_name = true
75+
depends_on = [google_project_iam_member.cloudsql, google_project_iam_member.secret, google_project_iam_member.gcs, var.module_depends_on]
76+
}
77+
78+
79+
data "google_iam_policy" "noauth" {
80+
binding {
81+
role = "roles/run.invoker"
82+
members = [
83+
"allUsers",
84+
]
85+
}
86+
}
87+
88+
resource "google_cloud_run_service_iam_policy" "noauth" {
89+
location = google_cloud_run_service.default.location
90+
project = google_cloud_run_service.default.project
91+
service = google_cloud_run_service.default.name
92+
93+
policy_data = data.google_iam_policy.noauth.policy_data
94+
}

0 commit comments

Comments
 (0)