Home > Blockchain >  Can I skip the cpu_option block in terraform?
Can I skip the cpu_option block in terraform?

Time:10-28

In AWS we are currently running our application in three environments: dev, staging and prod.

For our staging and prod environment we run our app on a x2gd.2xlarge instances. But, for our dev environment we only run on a x2gd.medium instance.

In our terraform we have a launch template block within which we configure cpu_options.

cpu_options {
  core_count = var.core_count
  threads_per_core = var.threads_per_core
}

Setting cpu_options works perfectly on our staging and prod x2gd.2xlarge instances. However, with the x2gd.medium in our dev environment we get this error message: "The x2gd.medium instance type does not support specifying CpuOptions.". This error is correct, the x2gd.medium instance only comes with 1 CPU and cant be configured with cpu_options.

All larger instance within the x2gd family can be configured with cpu_options. But, I don't really want to increase the size of the instance in our dev environment to x2gd.large as we don't need it. So, I'm wondering if there is anyway we can skip the cpu_options block when we run terraform for our dev environment?

CodePudding user response:

Expanding on the comment (h/t: @luk2302), you would need to use the dynamic block [1] with the for_each meta-argument [2]. The code block would then become:

dynamic "cpu_options" {
  for_each   = var.environment == "dev" ? [] : [1]
  content {
    core_count       = var.core_count
    threads_per_core = var.threads_per_core
  }
}

[1] https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks

[2] https://developer.hashicorp.com/terraform/language/meta-arguments/for_each

  • Related