Last active
December 3, 2019 23:22
-
-
Save dascenciohz/bf5abda064d6ac601093633176b027ac to your computer and use it in GitHub Desktop.
Terraform Google compute instance with docker and docker-compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GOOGLE CLOUD PROVIDER | |
provider "google" { | |
credentials = "${file("account.json")}" | |
project = "${var.project_name}" | |
region = "${var.project_region}" | |
zone = "${var.project_zone}" | |
} | |
# FIREWALL WEB SERVER PORTS | |
resource "google_compute_firewall" "web-server" { | |
name = "web-server" | |
network = "default" | |
direction = "INGRESS" | |
allow { | |
protocol = "tcp" | |
ports = ["80", "443"] | |
} | |
source_ranges = ["${var.allow_all_ip}"] | |
target_tags = ["web-server"] | |
} | |
# INSTANCE COMPUTE ENGINE | |
resource "google_compute_instance" "instance" { | |
name = "${var.instance_name}" | |
machine_type = "${var.instance_type}" | |
zone = "${var.instance_zone}" | |
allow_stopping_for_update = "true" | |
tags = ["web-server"] | |
boot_disk { | |
initialize_params { | |
image = "${var.instance_image}" | |
} | |
} | |
network_interface { | |
network = "${var.instance_network}" | |
access_config { } | |
} | |
metadata = { | |
ssh-keys = "${var.instance_username}:${file("~/.ssh/id_rsa.pub")}" | |
} | |
metadata_startup_script = "sudo apt-get update; sudo apt-get -y install wget; sudo wget ${var.instance_script_url}; sudo chmod +x docker-install-ubuntu.sh; sudo ./docker-install-ubuntu.sh ${var.docker_compose_version} ${var.instance_username}" | |
} | |
# VARIABLES | |
variable "project_name" { | |
default = "project-name" | |
} | |
variable "project_region" { | |
default = "us-central1" | |
} | |
variable "project_zone" { | |
default = "us-central1-b" | |
} | |
variable "instance_name" { | |
default = "web-app" | |
} | |
variable "instance_type" { | |
default = "n1-standard-1" | |
} | |
variable "instance_zone" { | |
default = "us-central1-b" | |
} | |
variable "instance_image" { | |
default = "ubuntu-os-cloud/ubuntu-minimal-1804-lts" | |
} | |
variable "instance_network" { | |
default = "default" | |
} | |
variable "allow_all_ip" { | |
default = "0.0.0.0/0" | |
} | |
variable "instance_username" { | |
default = "username" | |
} | |
variable "instance_script_url" { | |
default = "https://gist.githubusercontent.com/dascenciohz/b399436262e633a13a0344f2ad6b4359/raw/10910937fc8d11e1d8e20127d35d9ce15767eb5a/docker-install-ubuntu.sh" | |
} | |
variable "docker_compose_version" { | |
default = "1.24.1" | |
} | |
# OUTPUTS | |
output "instance_name" { | |
value = "${google_compute_instance.instance.name}" | |
} | |
output "ip" { | |
value = "${google_compute_instance.instance.network_interface.0.access_config.0.nat_ip}" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🎉