Terraform-provider-aws: `name_prefix` is almost useless for `aws_alb_target_group`

Created on 15 Sep 2017  路  8Comments  路  Source: hashicorp/terraform-provider-aws

Terraform v0.10.4
aws_alb_target_group -- name_prefix creates a 26 character random string after the prefix. However, AWS seems to only accept a max of 32 characters for this resource, leaving only 6 characters for prefix.

It may make sense to cut the random string to something more like 10-16 characters.

aws_alb_target_group.accounts-api: Error creating ALB Target Group: ValidationError: Target group name 'dev3-api00ab7d2fbb5e69734f62409381' cannot be longer than '32' characters

enhancement servicelbv2

Most helpful comment

Looks like this is stalling, can we please give this some love? the PR is there just waiting for it to be merged in.

@AlexRudd 's workaround is valid, however it prevents changes on the name attribute and if we need to rename it, we've got to manually comment out the life cycle block in a module.

All 8 comments

Hi @zero-below

Thanks for opening this issue.

A good thing could be to generate a string that is 32 - len(name_prefix), so if you have a prefix hello-world-foo-bar-, we would need to generate a string of 12 chars.

What do you think?

Work around I've been using:

resource "aws_alb_target_group" "tg" {
name  = "${substr(format("%s-%s", "${var.prefix}-tg", replace(uuid(), "-", "")), 0, 32)}"
...

lifecycle {
    create_before_destroy = true
    ignore_changes        = ["name"]
  }
}

@Ninir Sorry for the slow response, for some reason I didn't see your reply.

That solution seems to be good.

If you cap the random string at the current 26 characters, then it will also prevent any changes for people who previously had <6 char names.

As a side note, I've found a sprinkling of other resources that also have the 32 char limit, though I unfortunately didn't take notes. I bring this up because it's probably something that a number of AWS resources will end up needing to have implemented.

Looks like this is stalling, can we please give this some love? the PR is there just waiting for it to be merged in.

@AlexRudd 's workaround is valid, however it prevents changes on the name attribute and if we need to rename it, we've got to manually comment out the life cycle block in a module.

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

what's the status of this? Just got an error of

Error: "name_prefix" cannot be longer than 6 characters

Looks like there's still an issue with alb_target_groups?

I have copied the pull request to the new repo that is home to this functionality: https://github.com/hashicorp/terraform-plugin-sdk/pull/508

Work around I've been using:

resource "aws_alb_target_group" "tg" {
name  = "${substr(format("%s-%s", "${var.prefix}-tg", replace(uuid(), "-", "")), 0, 32)}"
...

lifecycle {
    create_before_destroy = true
    ignore_changes        = ["name"]
  }
}

Here's a better approach that avoids having to use the lifecycle block:

resource "random_uuid" "some_uuid" {}

resource "aws_alb_target_group" "tg" {
  name  = "${substr(format("%s-%s", "${var.prefix}-tg", replace(random_uuid.some_uuid.result, "-", "")), 0, 32)}"
Was this page helpful?
0 / 5 - 0 ratings