Skip to content

aws_connect_phone_number attempts to claim the same phone number for multiple resources #45572

@mspoehr

Description

@mspoehr

Terraform and AWS Provider Version

Terraform v1.14.0
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v6.26.0
+ provider registry.terraform.io/hashicorp/awscc v1.66.0

Affected Resource(s) or Data Source(s)

  • aws_connect_phone_number resource

Expected Behavior

The resource claims any valid phone number matching the provided criteria.

Actual Behavior

The resource often fails to claim a phone number. The following behavior is observed:

  • If a phone number happens to be claimed in the time between SearchAvailablePhoneNumbers and ClaimPhoneNumber API calls, the creation fails.
  • If multiple phone number resources are being created (such as with a for_each), all phone number resources attempt to claim the same number. The first resource will be created successfully, and all other resources fail.

Relevant Error/Panic Output

Output from terraform apply:

│ Error: claiming Connect Phone Number (arn:aws:connect:ca-central-1:111122223333:instance/d214e486-f7dc-42a0-84bf-6de8c38b6133,+15047846302): operation error Connect: ClaimPhoneNumber, https response error StatusCode: 400, RequestID: bb8722c2-e2b2-4c97-a5b4-a9d1469ebe48, InvalidParameterException: Phone number not available: +15047846302. Try again with a different available number

│   with aws_connect_phone_number.this["phone_number_2"],
│   on main.tf line 33, in resource "aws_connect_phone_number" "this":
│   33: resource "aws_connect_phone_number" "this" {



│ Error: claiming Connect Phone Number (arn:aws:connect:ca-central-1:111122223333:instance/d214e486-f7dc-42a0-84bf-6de8c38b6133,+15047846302): operation error Connect: ClaimPhoneNumber, https response error StatusCode: 400, RequestID: a2fc5b7f-95d9-4fcc-9cf9-db2beb14765e, InvalidParameterException: Phone number not available: +15047846302. Try again with a different available number

│   with aws_connect_phone_number.this["phone_number_3"],
│   on main.tf line 33, in resource "aws_connect_phone_number" "this":
│   33: resource "aws_connect_phone_number" "this" {

Subsequent terraform state show 'aws_connect_phone_number.this["phone_number_1"]':

# aws_connect_phone_number.this["phone_number_1"]:
resource "aws_connect_phone_number" "this" {
    arn          = "arn:aws:connect:ca-central-1:111122223333:phone-number/6f1952b0-cdf7-4eaf-8b92-c6b6ccd97e23"
    country_code = "US"
    description  = "Phone number phone_number_1"
    id           = "6f1952b0-cdf7-4eaf-8b92-c6b6ccd97e23"
    phone_number = "+15047846302"
    region       = "ca-central-1"
    status       = [
        {
            message = null
            status  = "CLAIMED"
        },
    ]
    tags_all     = {}
    target_arn   = "arn:aws:connect:ca-central-1:111122223333:instance/d214e486-f7dc-42a0-84bf-6de8c38b6133"
    type         = "DID"
}

(note the phone number is the same in all three cases)

Sample Terraform Configuration

Click to expand configuration
locals {
  phone_numbers = [
    "phone_number_1",
    "phone_number_2",
    "phone_number_3"
  ]
}

resource "aws_connect_phone_number" "this" {
  for_each = toset(local.phone_numbers)

  type         = "DID"
  target_arn   = "<INSTANCE_ID>"
  country_code = "US"
  description  = "Phone number ${each.value}"
}

Steps to Reproduce

  1. Create a Connect instance and note the instance ID.
  2. Create terraform configuration which will result in multiple phone numbers being claimed, such as in the example above. Fill in the connect instance ID.
  3. Apply the configuration in a single terraform apply.

Debug Logging

See attached logs which shows a subset of the terraform apply command with TF_LOG=trace set. Note that the result of SearchAvailablePhoneNumbers is identical for all three resources. Perhaps the aws provider should implement a mechanism to retrieve different numbers, or retry with a different number if the attempted number is already claimed.

tf_apply_output.log

GenAI / LLM Assisted Development

n/a

Important Facts and References

  • This was tested in the ca-central-1 region only.
  • Creation of phone numbers with the Cloud Control API works as expected. An easy workaround is to use the awscc_connect_phone_number resource which has identical arguments.

Would you like to implement a fix?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugAddresses a defect in current functionality.needs-triageWaiting for first response or review from a maintainer.service/connectIssues and PRs that pertain to the connect service.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions