terraform-google-compute-engine-instance
Create virtual machines on Google Cloud. With DNS A record for easy access. Published in the Terraform registry.
Usage example:
Create VMs with public IP addresses and DNS A record alias. Machine type can be changed without destroying the boot disk.
- Create a new directory for this terraform configuration
- Create a main.tf, for example:
# Configure the Google Cloud provider
provider "google" {
credentials = "${file("king-of-my-google-cloud-castle.json")}"
project = "smashing-dash-1992"
}
module "google-dns-managed-zone" {
source = "github.com/Eimert/terraform-google-dns-managed-zone"
# descriptive name for dns zone
dns_name = "cloud-zone"
# requires last dot. Ex.: prod.example.com.
dns_zone = "cloud.eimertvink.nl."
}
module "vm1" {
source = "github.com/Eimert/terraform-google-compute-engine-instance"
amount = 1
region = "europe-west4"
zone = "europe-west4-c"
# hostname format: name_prefix-amount
name_prefix = "vm"
machine_type = "n1-standard-2"
disk_type = "pd-ssd"
disk_size = "15"
disk_image = "centos-cloud/centos-7"
dns_name = "${module.google-dns-managed-zone.dns_name}"
dns_zone = "${module.google-dns-managed-zone.dns_zone}"
dns_record_name = "tower-dev"
user_data = "firestone-lab"
username = "eimert"
public_key_path = "~/.ssh/id_rsa.pub"
}
# module "gc2" {}
terraform init
terraform plan
Boom! Credentials file missing.- Add your google cloud credentials in a .json file. Getting started guide
Keep the Google Cloud credentials in a safe place. Don't push them to Git.
- Adapt the Terraform variables in
main.tf
to match your Google cloud project name, and VM requirements. All optional parameters can be found in variables.tf. - Let terraform fire up the VM's:
terraform apply
- Wait a few
minutesseconds. - Connect using SSH (private key auth):
ssh -i <private key> <username>@<ip from output>
. Or:ssh eimert@ansible-dev.cloud.eimertvink.nl
. - Break down the resources:
terraform destroy
machine_type
Overview of choices for variable machine_type.
f1-micro
g1-small
n1-standard-1
n1-standard-2
n1-standard-4
n1-standard-8
n1-highmem-2
n1-highmem-4
n1-highmem-8
n1-highcpu-2
n1-highcpu-4
n1-highcpu-8
Values are derived from Google cloud console REST API call examples. Click for CPU and memory details.
DNS
The subdomain cloud.eimertvink.nl is configured with Google' nameservers:
This terraform plan creates an DNS A record for VMs. When (VM) amount = 2
, this results in:
If you want a unique (sub-)subdomain for every VM, use multiple TF module calls:
module "gc1" {
(..)
dns_record_name = "ansible-dev"
(..)
}
module "gc2" {
(..)
dns_record_name = "ansible-tst"
(..)
}