Skip to content

Commit 2b3681e

Browse files
committed
add .env with terraform
1 parent 8a389e5 commit 2b3681e

File tree

6 files changed

+44
-6
lines changed

6 files changed

+44
-6
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,7 @@ terraform.rc
4040
credentials.json
4141

4242
# ssh
43-
.ssh/
43+
.ssh/
44+
45+
# env
46+
.env

Makefile

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ GREEN = $(shell tput -Txterm setab 2 && tput -Txterm setaf 0)
33
PURPLE = $(shell tput -Txterm setab 5 && tput -Txterm setaf 7)
44
RESET = $(shell tput -Txterm sgr0)
55

6+
VAR_FILE = terraform.prod.tfvars
7+
68
all: create
79

810
create: create-compute-engine
@@ -19,7 +21,7 @@ create-compute-engine:
1921
@echo ""
2022
@echo "$(GREEN) Terraform compute engine init $(RESET)"
2123
@cd src && terraform init
22-
@cd src && terraform apply -auto-approve
24+
@cd src && terraform apply -auto-approve -var-file=$(VAR_FILE)
2325

2426
destroy-s3:
2527
@echo ""

src/main.tf

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ module "training_worker" {
5656
gpu_type = var.gpu_type
5757
zone = var.zone
5858
username = var.username
59+
env_file = var.env_file
60+
dockerhub_id = var.dockerhub_id
61+
dockerhub_pwd = var.dockerhub_pwd
5962

6063
depends_on = [ module.vpc_network ]
6164
}

src/modules/worker/main.tf

+16-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ resource "google_compute_instance" "gpu_instance" {
5151
service_account {
5252
scopes = [ "https://www.googleapis.com/auth/devstorage.read_write" ]
5353
}
54-
5554
provisioner "file" {
5655
// We use a provisioner to copy our local ssh key inside the worker. This will be used to authenticate with GitHub.
5756
source = var.ssh_file_private
@@ -66,6 +65,20 @@ resource "google_compute_instance" "gpu_instance" {
6665
}
6766
}
6867

68+
provisioner "file" {
69+
// We use a provisioner to copy our local ssh key inside the worker. This will be used to authenticate with GitHub.
70+
source = var.env_file
71+
destination = "/home/${var.username}/.env"
72+
73+
connection {
74+
type = "ssh"
75+
user = var.username
76+
port = 22
77+
private_key = "${file(var.ssh_file_private)}"
78+
host = google_compute_instance.gpu_instance.network_interface[0].access_config[0].nat_ip
79+
}
80+
}
81+
6982
provisioner "remote-exec" {
7083
// Here we define some init steps we want to run when our instance is created
7184
inline = [
@@ -88,7 +101,8 @@ resource "google_compute_instance" "gpu_instance" {
88101
"sudo apt update",
89102
"sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin",
90103
"sudo systemctl start docker",
91-
"sudo systemctl enable docker"
104+
"sudo systemctl enable docker",
105+
"echo ${var.dockerhub_pwd} | docker login -u ${var.dockerhub_pwd} --password-stdin" # dockerhub login
92106
]
93107
connection {
94108
type = "ssh"

src/modules/worker/variables.tf

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ variable "gpu_type" {}
1212

1313
variable "zone" {}
1414

15-
variable "username" {}
15+
variable "username" {}
16+
17+
variable "env_file" {}
18+
19+
variable "dockerhub_id" {}
20+
21+
variable "dockerhub_pwd" {}

src/variables.tf

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ variable "ssh_file_private" {
1010
default = "../.ssh/id_ed25519"
1111
}
1212

13+
variable "env_file" {
14+
description = "env file path"
15+
type = string
16+
default = "../.env"
17+
}
18+
1319
variable "git_ssh_url" {
1420
description = "git clone url"
1521
type = string
@@ -68,4 +74,8 @@ variable "username" {
6874
description = "my google email id"
6975
type = string
7076
default = "sangyleegcp1"
71-
}
77+
}
78+
79+
variable "dockerhub_id" {}
80+
81+
variable "dockerhub_pwd" {}

0 commit comments

Comments
 (0)