Matt W
Matt W

Reputation: 12423

Terraform error creating API Gateway Resource complains another resource with the same parent already has this name

I am creating an API Gateway resource called "lang":

resource "aws_api_gateway_resource" "lang" {
  path_part   = "lang"
  parent_id   = aws_api_gateway_resource.api.id
  rest_api_id = aws_api_gateway_rest_api.root_api.id
}

Having successfully created the API, when running terraform apply subsequent times, I get this error:

Error: Error creating API Gateway Resource: ConflictException: Another resource with the same parent already has this name: lang

The resource block above is absolutely, definitely the only one with that name. I've googled enough on this to realise that terraform is complaining that the resource called "lang" already exists and can't be recreated. What I don't know is how to get it destroyed first.

I have tried adding this to my script:

resource "aws_api_gateway_deployment" "deployment" {
  rest_api_id   = aws_api_gateway_rest_api.root_api.id
  stage_name    = var.envName
  
  triggers = {
    redeployment = sha1(jsonencode(aws_api_gateway_rest_api.root_api.body))
  }

  lifecycle {
    create_before_destroy = true
  }
}

...to no avail.

Should the create_before_destroy property not essentially destroy the API and cause it to be recreated or only the stage?

How can I make changes to the "lang" resource if the resource block can't do it? This seems a fundamental circular problem to me.

Upvotes: 2

Views: 3801

Answers (1)

Matt W
Matt W

Reputation: 12423

Annoyingly, this was a late night typo...

I had (deliberately) copy-pasted the resource "aws_api_gateway_resource" "lang" block and forgotten to rename the copy when going through and updating all the copies for their own purposes.

Upvotes: 1

Related Questions