terraform-aws-modules / terraform-aws-autoscaling

Terraform module to create AWS Auto Scaling resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/autoscaling/aws
Apache License 2.0
289 stars 553 forks source link

Setting instance_market_options conditionally #208

Closed rysi3k closed 1 year ago

rysi3k commented 1 year ago

Description

In the current version of module "~> 6.5.1" it is not possible to use spot instances conditionally (or I do not know how to do this ;))

Versions

Reproduction Code [Required]

variable "spot_price" {
  type = number
  default = 0
}

module "prometheus_asg" {
  source  = "terraform-aws-modules/autoscaling/aws"
  version = "~> 6.5.1"

  name = "some-name"

instance_market_options = var.spot_price > 0 ? {
    market_type = "spot"
    spot_options = {
      max_price = var.spot_price
    }
  } : null

}

Above example cause error:

│   on .terraform/modules/.... line 140, in resource "aws_launch_template" "this":
│  140:     for_each = length(var.instance_market_options) > 0 ? [var.instance_market_options] : []
│     ├────────────────
│     │ var.instance_market_options is null
│ 
│ Invalid value for "value" parameter: argument must not be null.

Another try with:

instance_market_options = var.spot_price > 0 ? {
    market_type = "spot"
    spot_options = {
      max_price = var.spot_price
    }
  } : {}

Throws error

The true and false result expressions must have consistent types. The 'true' value includes object attribute "market_type", which is absent in the 'false' value.

Expected behavior

Possible to make condition in instance_market_options.

bryantbiggs commented 1 year ago

the easiest option is - don't wrap modules in modules (and don't use workspaces for environmental isolation)

rysi3k commented 1 year ago

Yeah, this is option, but I have many common options in each environment, in some cases, the only difference is spot/non-spot and name, but a bunch of options are the same. So I think it is not a good solution.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

bryantbiggs commented 1 year ago

this is not related to the module but related to how you are trying to mange terraform

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.