Created
October 12, 2021 10:19
-
-
Save saranshdhingra/b690bb67f586e2a6aa5d38a404b2c8a8 to your computer and use it in GitHub Desktop.
Terraform script to create multiple Google Cloud VM instances with different machine types and a custom docker container.
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
# | |
# Copyright 201 Google Inc. All Rights Reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
# | |
# The aim of this script is to create multiple GCE instances which deploy a custom docker image | |
# Currently we use a 'Container Optimised OS' provided by Google Cloud. | |
# Perhaps we can try the same with debian instances and installing docker as a startup script | |
# or find a marketplace OS which has docker installed. | |
terraform { | |
required_providers { | |
google = { | |
source = "hashicorp/google" | |
version = "3.87.0" | |
} | |
} | |
} | |
# variables | |
# We want our image to be deployed for these machine types | |
variable "machine_types" { | |
description = "Different machine types" | |
type = list(string) | |
default = ["f1-micro", "g1-small", "n1-standard-1", "n1-standard-2", "n1-standard-4", "n1-standard-8", "n1-highmem-2", "n1-highcpu-2", "e2-micro", "e2-small", "e2-medium", "e2-standard-2", "e2-standard-4", "e2-highmem-2", "e2-highcpu-2", "n2-standard-2", "n2-standard-4", "n2-highmem-2", "n2-highcpu-2"] | |
} | |
variable "id_template" { | |
description = "Just a placeholder, that will make it easier to identify resources" | |
type = string | |
default = "issue-2338" | |
} | |
# The custom GCR image url that you want to deploy | |
# ex: gcr.io/YOUR-PROJECT-ID/IMAGE-ID | |
variable "gcr_img_url" { | |
description = "The image path of the gcr.io image" | |
type = string | |
default = "" | |
} | |
# The service account that will be associated to the deployed compute instances | |
# This should have permissions to pull the GCR image | |
variable "service_account_email" { | |
description = "The email of the Service Account used by the Compute Instances" | |
type = string | |
default = "" | |
} | |
# We can get most of this from variables | |
provider "google" { | |
# this service account should have the permission to create the resources required | |
credentials = file("SERVICE-ACCOUNT-FILE-PATH") | |
project = "YOUR-PROJECT-ID" | |
region = "us-central1" | |
zone = "us-central1-c" | |
} | |
resource "google_compute_instance" "default" { | |
for_each = toset(var.machine_types) | |
name = "${var.id_template}-${each.value}" | |
machine_type = each.value | |
zone = "us-central1-c" | |
tags = ["${var.id_template}-machine"] | |
boot_disk { | |
# container optimized image to deploy a container image | |
# helps with boilerplate to install docker etc | |
initialize_params { | |
image = "projects/cos-cloud/global/images/cos-stable-89-16108-534-9" | |
} | |
} | |
metadata = { | |
# This has been taken from the Google Cloud UI | |
gce-container-declaration = "spec:\n containers:\n - name: instance-1\n image: ${var.gcr_img_url} \n securityContext:\n privileged: false\n stdin: false\n tty: false\n restartPolicy: Always\n# This container declaration format is not public API and may change without notice. Please\n# use gcloud command-line tool or Google Cloud Console to run Containers on Google Compute Engine." | |
} | |
network_interface { | |
network = "default" | |
access_config { | |
} | |
} | |
service_account { | |
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles. | |
email = "${var.service_account_email}" | |
scopes = ["cloud-platform"] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment