terragrunt icon indicating copy to clipboard operation
terragrunt copied to clipboard

backend s3 config: error within if condition - 'encrypt' expected type 'bool', got unconvertible type 'string'

Open cfir-atbay opened this issue 4 years ago • 8 comments

I'm trying to define a config block for two environments - local and cloud and I'm using the if/else condition but I got an error message for the encrypt attribute of the s3 bucket: 'encrypt' expected type 'bool', got unconvertible type 'string'. If I remove the if/else condition block then it worked but I need to choose between the two environments, so I've to use if/else condition. The config block code:

config = local.is_local_environment ? { # Local configuration path = "${path_relative_to_include()}/terraform.tfstate" } : { # Cloud configuration bucket = "my-bucket" key = "terraform/${path_relative_to_include()}/terraform.tfstate" region = local.region encrypt = true dynamodb_table = "terraform-lock" } }

cfir-atbay avatar Feb 21 '21 10:02 cfir-atbay

Could you update your post to use a fenced code block and proper indentation so it's easier to read?

Also, could you share the full log output, including the command you ran?

brikis98 avatar Feb 23 '21 09:02 brikis98

local backends don't expect any kind of config. Just use null

config = local.is_local_environment ? null : { 
  # Cloud configuration 
  bucket = "my-bucket" 
  key = "terraform/${path_relative_to_include()}/terraform.tfstate" 
  region = local.region 
  encrypt = true 
  dynamodb_table = "terraform-lock" 
} 
}

john-delivuk avatar Nov 23 '21 17:11 john-delivuk

@brikis98 I think you can close this issue, @john-delivuk has provided a solution / workaround.

korenyoni avatar Nov 21 '22 08:11 korenyoni

@brikis98 I think the issue still persists. Reproducer:

terragrunt.hcl:

remote_state {
  backend = "s3"
  config = true ? {
    bucket = "terraform-state-test"
    key = "test"
    region = "us-east-1"
    encrypt = true
  } : {}
}

main.tf:

terraform {
  backend "s3" {}
}

Does not look like a Terragrunt issue per se though.

GergelyKalmar avatar Dec 02 '22 12:12 GergelyKalmar

It works fine as long as the config map contains the same type of objects.

GergelyKalmar avatar Dec 02 '22 12:12 GergelyKalmar

Even though terraform ignores path in local configs, it is still a documented parameter

In my case, I'm using it to check that the state key won't collide when doing local plans.

  config = local.is_atlantis ? {
    region         = "us-east-1"
    bucket         = "xxxx"
    dynamodb_table = "xxxx"
    key            = "${local.service_state_key}/terraform.tfstate"
    encrypt        = true
    } : {
    path = "${local.service_state_key}/terraform.tfstate"
  }

It would be nice to add support for this.

h3adache avatar Aug 01 '23 14:08 h3adache

workaround

locals {
  ....
  remote_backend = local.use_local ? "local" : "s3"
  remote_state_config = {
    s3 = {
      region         = "us-east-1"
      bucket         = "xxxxx"
      dynamodb_table = "xxxx"
      key            = "${local.service_state_key}/terraform.tfstate"
      encrypt        = true
    }
    local = {
      path = "${local.service_state_key}/terraform.tfstate"
    }
  } 
}

remote_state {
  backend = local.remote_backend
  config  = local.remote_state_config[local.remote_backend]
  generate = {
    path      = "~backend.tf"
    if_exists = "overwrite_terragrunt"
  }
}

h3adache avatar Aug 01 '23 14:08 h3adache

I also encounter this bug. The workaround by @h3adache works for me.

jacekgajek avatar Aug 12 '24 09:08 jacekgajek

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for raising this issue.

github-actions[bot] avatar May 02 '25 02:05 github-actions[bot]