Skip to content

Instantly share code, notes, and snippets.

@lgfa29
Created July 9, 2021 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgfa29/03519c68e54dde7eec0d46d7a47b60d9 to your computer and use it in GitHub Desktop.
Save lgfa29/03519c68e54dde7eec0d46d7a47b60d9 to your computer and use it in GitHub Desktop.
provider "google" {
project = "..."
region = "us-central1"
zone = "us-central1-a"
}
locals {
my_list_from_map = [for k, v in var.my_map : "${k}.${v}"]
}
resource "local_file" "my_file" {
count = length(local.my_list_from_map)
content = local.my_list_from_map[count.index]
filename = "${path.module}/abc_${count.index}.txt"
}
resource "local_file" "my_file_for_each" {
for_each = { for x in var.my_list : x => x }
content = each.value
filename = "${path.module}/${each.key}.txt"
}
data "template_file" "init" {
template = "${file("${path.module}/init.tpl")}"
vars = {
name = "Luiz"
}
}
resource "google_compute_instance" "default" {
name = "test"
machine_type = "f1-micro"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
dynamic "network_interface" {
for_each = var.my_list
content {
network = "default"
subnetwork = network_interface.value
}
}
}
variable "subnet" {
type = list(object({
name = string
ip_cidr_range = string
region = string
}))
}
output "subnet_ips" {
value = var.subnet[*]["ip_cidr_range"]
}
# Variables.
# Primitives.
variable "my_string" {
type = string
}
variable "my_number" {
type = number
}
variable "my_bool" {
type = bool
}
# Collections.
variable "my_list" {
type = list(string)
}
variable "my_map" {
type = map(string)
}
variable "my_set" {
type = set(string)
}
# Structured.
variable "my_object" {
type = object({
id = number
name = string
})
}
variable "my_tuple" {
type = tuple([string, number, bool])
}
# Outputs.
output "my_string" {
value = var.my_string
}
output "my_number" {
value = var.my_number
}
output "my_bool" {
value = var.my_bool
}
output "my_list" {
value = var.my_list
}
output "my_map" {
value = var.my_map
}
output "my_set" {
value = var.my_set
}
output "my_object" {
value = var.my_object
}
output "my_tuple" {
value = var.my_tuple
}
my_string = "abc"
my_number = 123
my_bool = false
my_list = ["x", "y", "z"]
my_set = ["b", "c", "d", "a"]
my_map = {
k3 = "v3"
k = "v"
}
my_object = {
id = 123
name = "abc"
}
my_tuple = ["abc", 123, false]
subnet = [
{
name = "private"
ip_cidr_range = "10.0.0.0/24"
region = "us-central1"
},
{
name = "public"
ip_cidr_range = "192.168.0.0/24"
region = "us-central1"
},
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment