Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Hard-coded AZ zone identifiers not appropriate for all regions #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ resource "aws_volume_attachment" "game_volume_attachment" {
| Name | Description | Type | Default |
| --- | --- | --- | ---|
| region | The aws region. Choose the one closest to you: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions | `string` | |
| allowed_availability_zone_identifier | The allowed availability zone identify (the letter suffixing the region). Choose ones that allows you to request the desired instance as spot instance in your region. An availability zone will be selected at random and the instance will be booted in it. | `list(string)` | ["a", "b"] |
| allowed_availability_zone_identifier | The allowed availability zone identify (the letter suffixing the region). Choose ones that allows you to request the desired instance as spot instance in your region. An availability zone will be selected at random and the instance will be booted in it. | `list(string)` | `[]` (all available) |
| instance_type | The aws instance type, Choose one with a CPU/GPU that fits your need: https://aws.amazon.com/ec2/instance-types/#Accelerated_Computing | `string` | "g4dn.xlarge" |
| resource_name | Name with which to prefix resources in AWS | `string` | `cloud-gaming` |
| root_block_device_size_gb | The size of the root block device (C:\\ drive) attached to the instance | `number` | 120 |
Expand Down
10 changes: 8 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ data "external" "local_ip" {
program = ["curl", "https://api.ipify.org?format=json"]
}

data "aws_availability_zones" "available" {
state = "available"
}

locals {
availability_zone = "${var.region}${element(var.allowed_availability_zone_identifier, random_integer.az_id.result)}"
availability_zones = length(var.allowed_availability_zone_identifier) != 0 ? var.allowed_availability_zone_identifier : [for az in data.aws_availability_zones.available.names : substr(az, -1, 1)]
availability_zone_identifier = element(local.availability_zones, random_integer.az_id.result)
availability_zone = "${var.region}${local.availability_zone_identifier}"
}

resource "random_integer" "az_id" {
min = 0
max = length(var.allowed_availability_zone_identifier)
max = length(local.availability_zones)
}

resource "random_password" "password" {
Expand Down
4 changes: 2 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ variable "resource_name" {
}

variable "allowed_availability_zone_identifier" {
description = "The allowed availability zone identify (the letter suffixing the region). Choose ones that allows you to request the desired instance as spot instance in your region. An availability zone will be selected at random and the instance will be booted in it."
description = "The allowed availability zone identify (the letter suffixing the region). Choose ones that allows you to request the desired instance as spot instance in your region. If omitted, an availability zone will be selected at random and the instance will be booted in it."
type = list(string)
default = ["a", "b"]
default = []
}

variable "instance_type" {
Expand Down