Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_sns_topic: Name validation fails if the topic name has hyphen #19416

Open
timoguin opened this issue May 17, 2021 · 6 comments
Open

aws_sns_topic: Name validation fails if the topic name has hyphen #19416

timoguin opened this issue May 17, 2021 · 6 comments
Labels
service/sns Issues and PRs that pertain to the sns service.

Comments

@timoguin
Copy link
Contributor

timoguin commented May 17, 2021

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform CLI and Terraform AWS Provider Version

$ terraform -v
Terraform v0.15.3
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.40.0

Affected Resource(s)

  • aws_sns_topic

Terraform Configuration Files

resource "aws_sns_topic" "main" {
  name = "topic-with-hyphens"
}

Debug Output

None

Panic Output

None

Expected Behavior

The topic should be created.

Actual Behavior

The name of the topic fails validation during plan.

Steps to Reproduce

  1. terraform plan

Important Factoids

None

References

#15828 added support for Fifo topics (issue #15805), as well as some additional logic for the name validation. It looks like that's the source of the error, so this bug affects v3.37.0+.

Here is the offending code: https://github.com/hashicorp/terraform-provider-aws/blob/main/aws/resource_aws_sns_topic.go#L586-L594

		if fifoTopic {
			re = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,251}\.fifo$`)
		} else {
			re = regexp.MustCompile(`^[a-zA-Z0-9_-]{1,256}$`)
		}
		if !re.MatchString(name) {
			return fmt.Errorf("invalid topic name: %s", name)
		}

I think the hyphen in each character class needs to be escaped to fix this.

@ghost ghost added the service/sns Issues and PRs that pertain to the sns service. label May 17, 2021
@github-actions github-actions bot added the needs-triage Waiting for first response or review from a maintainer. label May 17, 2021
@RobertKeyser
Copy link

RobertKeyser commented May 21, 2021

I'm getting the same error, but with or without the hyphens. Same for underscores. In other words, nothing is working for me:

   │ Error: invalid topic name: topicwithouthyphens
   │
   │   on sns.tf line 28, in resource "aws_sns_topic" "sns":28: resource "aws_sns_topic" "sns" {

-- edit --

never mind 😢 it was my config (I was inadvertently forcing FIFO )

@slichtenthal
Copy link

slichtenthal commented May 21, 2021

this is also an issue for fifo topics and data aws_sns_topic- it doesn't like the period in it that is in the required ".fifo"

data "aws_sns_topic" "my_fifo_topic" { name = "topic-name.fifo" }

Error: "name" must match regex '^[A-Za-z0-9_-]+$'

  on ..\module\lambda-trigger\sqs.tf line 45, in data "aws_sns_topic" "my_fifo_topic":

@YakDriver
Copy link
Member

@timoguin Thanks for raising this! However, I'm unable to duplicate the problem.

Test config:

resource "aws_sns_topic" "test" {
  name = "topic-with-hyphens"
}

data "aws_sns_topic" "test" {
  name = aws_sns_topic.test.name
}

resource "aws_sns_topic" "test-fifo" {
  name       = "topic-name.fifo"
  fifo_topic = true
}

data "aws_sns_topic" "test-fifo" {
  name = aws_sns_topic.test-fifo.name
}

The results:

% terraform -version
Terraform v0.15.4
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.42.0
% terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  + create
 <= read (data resources)

Terraform will perform the following actions:

  # data.aws_sns_topic.test will be read during apply
  # (config refers to values not yet known)
 <= data "aws_sns_topic" "test"  {
      + arn  = (known after apply)
      + id   = (known after apply)
      + name = "topic-with-hyphens"
    }

  # data.aws_sns_topic.test-fifo will be read during apply
  # (config refers to values not yet known)
 <= data "aws_sns_topic" "test-fifo"  {
      + arn  = (known after apply)
      + id   = (known after apply)
      + name = "topic-name.fifo"
    }

  # aws_sns_topic.test will be created
  + resource "aws_sns_topic" "test" {
      + arn                         = (known after apply)
      + content_based_deduplication = false
      + fifo_topic                  = false
      + id                          = (known after apply)
      + name                        = "topic-with-hyphens"
      + name_prefix                 = (known after apply)
      + policy                      = (known after apply)
      + tags_all                    = (known after apply)
    }

  # aws_sns_topic.test-fifo will be created
  + resource "aws_sns_topic" "test-fifo" {
      + arn                         = (known after apply)
      + content_based_deduplication = false
      + fifo_topic                  = true
      + id                          = (known after apply)
      + name                        = "topic-name.fifo"
      + name_prefix                 = (known after apply)
      + policy                      = (known after apply)
      + tags_all                    = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_sns_topic.test-fifo: Creating...
aws_sns_topic.test: Creating...
aws_sns_topic.test: Creation complete after 2s [id=arn:aws:sns:us-west-2:067819342479:topic-with-hyphens]
data.aws_sns_topic.test: Reading...
aws_sns_topic.test-fifo: Creation complete after 2s [id=arn:aws:sns:us-west-2:067819342479:topic-name.fifo]
data.aws_sns_topic.test-fifo: Reading...
data.aws_sns_topic.test: Read complete after 0s [id=arn:aws:sns:us-west-2:067819342479:topic-with-hyphens]
data.aws_sns_topic.test-fifo: Read complete after 0s [id=arn:aws:sns:us-west-2:067819342479:topic-name.fifo]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

@YakDriver YakDriver removed the needs-triage Waiting for first response or review from a maintainer. label May 21, 2021
@slichtenthal
Copy link

slichtenthal commented May 21, 2021

@timoguin Thanks for raising this! However, I'm unable to duplicate the problem.

Test config:

resource "aws_sns_topic" "test" {
  name = "topic-with-hyphens"
}

data "aws_sns_topic" "test" {
  name = aws_sns_topic.test.name
}

resource "aws_sns_topic" "test-fifo" {
  name       = "topic-name.fifo"
  fifo_topic = true
}

data "aws_sns_topic" "test-fifo" {
  name = aws_sns_topic.test-fifo.name
}

The results:

% terraform -version
Terraform v0.15.4
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v3.42.0
% terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
  + create
 <= read (data resources)

Terraform will perform the following actions:

  # data.aws_sns_topic.test will be read during apply
  # (config refers to values not yet known)
 <= data "aws_sns_topic" "test"  {
      + arn  = (known after apply)
      + id   = (known after apply)
      + name = "topic-with-hyphens"
    }

  # data.aws_sns_topic.test-fifo will be read during apply
  # (config refers to values not yet known)
 <= data "aws_sns_topic" "test-fifo"  {
      + arn  = (known after apply)
      + id   = (known after apply)
      + name = "topic-name.fifo"
    }

  # aws_sns_topic.test will be created
  + resource "aws_sns_topic" "test" {
      + arn                         = (known after apply)
      + content_based_deduplication = false
      + fifo_topic                  = false
      + id                          = (known after apply)
      + name                        = "topic-with-hyphens"
      + name_prefix                 = (known after apply)
      + policy                      = (known after apply)
      + tags_all                    = (known after apply)
    }

  # aws_sns_topic.test-fifo will be created
  + resource "aws_sns_topic" "test-fifo" {
      + arn                         = (known after apply)
      + content_based_deduplication = false
      + fifo_topic                  = true
      + id                          = (known after apply)
      + name                        = "topic-name.fifo"
      + name_prefix                 = (known after apply)
      + policy                      = (known after apply)
      + tags_all                    = (known after apply)
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_sns_topic.test-fifo: Creating...
aws_sns_topic.test: Creating...
aws_sns_topic.test: Creation complete after 2s [id=arn:aws:sns:us-west-2:067819342479:topic-with-hyphens]
data.aws_sns_topic.test: Reading...
aws_sns_topic.test-fifo: Creation complete after 2s [id=arn:aws:sns:us-west-2:067819342479:topic-name.fifo]
data.aws_sns_topic.test-fifo: Reading...
data.aws_sns_topic.test: Read complete after 0s [id=arn:aws:sns:us-west-2:067819342479:topic-with-hyphens]
data.aws_sns_topic.test-fifo: Read complete after 0s [id=arn:aws:sns:us-west-2:067819342479:topic-name.fifo]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

I think the difference is in the code you provided, terraform is creating the FIFO SNS resource. In my case I’m trying to grab the ARN of an existing one not managed by TF to setup a subscription to it.

@timoguin
Copy link
Contributor Author

Is this old guy still an issue anymore? 😊

@shaheerkhan12605
Copy link

I faced the same issue, while create sns-topic

If you are using fifo_topic = true your sns topic must contain .fifo suffix. and if fifo_topic = false then your sns topic shouldn't contain .fifo suffix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service/sns Issues and PRs that pertain to the sns service.
Projects
None yet
Development

No branches or pull requests

5 participants