简体   繁体   中英

AWS API Gateway Resource ID mapping with Terraform

I've a module of AWS API Gateway which were created by Terraform , Gateway creating without any issues, however when I tried to create a nested api gateway resource , the resources are taking a wrong ID , it suppose to take a parent ID, but instead it somehow takes an ID of different parent, seems it takes by alphabetical order.


Here the code it self:

resource "aws_api_gateway_resource" "parent" {
   for_each    = { for key, value in var.restapi.resource : key => value }
   path_part   = lookup(each.value, "path", null)
   parent_id   = element([ for key, value in aws_api_gateway_rest_api.managed : value.root_resource_id ], each.key)
   rest_api_id = element([ for key, value in aws_api_gateway_rest_api.managed : value.id ], each.key)
}


resource "aws_api_gateway_resource" "childs" {
   for_each    = { for key, value in var.restapi.resource.childs : key => value }
   depends_on  = [ aws_api_gateway_resource.parent ]
   path_part   = lookup(each.value, "path", null)
   parent_id   = element([ for key, value in aws_api_gateway_resource.managed : value.id ], each.key)
   rest_api_id = element([ for key, value in aws_api_gateway_rest_api.managed : value.id ], each.key)
}

The problem in aws_api_gateway_resource. childs aws_api_gateway_resource. childs is parent_id , as I mentioned above it takes wrong parent ID , here the terraform plan result:

# module.restapi.aws_api_gateway_resource.parent["1"] will be created
+ resource "aws_api_gateway_resource" "parent" {
    + id          = (known after apply)
    + parent_id   = "j3pt41ko7f"
    + path        = (known after apply)
    + path_part   = "saml-store"
    + rest_api_id = "mtu4b34wn4"
  }

# module.restapi.aws_api_gateway_resource.childs["1"] will be created
+ resource "aws_api_gateway_resource" "childs" {
    + id          = (known after apply)
    + parent_id   = (known after apply)
    + path        = (known after apply)
    + path_part   = "saml-store/enable"
    + rest_api_id = "mtu4b34wn4"
  }

# module.restapi.aws_api_gateway_resource.childs["2"] will be created
+ resource "aws_api_gateway_resource" "childs" {
    + id          = (known after apply)
    + parent_id   = "b72571"
    + path        = (known after apply)
    + path_part   = "block/disable"
    + rest_api_id = "mtu4b34wn4"
  }

module.restapi.aws_api_gateway_resource.childs["2"] taking a wrong ID , it should take the same ID of module.restapi.aws_api_gateway_resource.childs["1"] , which is unknown and will be known only after apply , so the path_part for module.restapi.aws_api_gateway_resource.childs["2"] must be "saml-store/enable" instead of "block/disable", how can I solve this issue?

It looks like a copy-paste issue in your child resource (you are referencing managed instead of parent):

resource "aws_api_gateway_resource" "childs" {
   for_each    = { for key, value in var.restapi.resource.childs : key => value }
   depends_on  = [ aws_api_gateway_resource.parent ]
   path_part   = lookup(each.value, "path", null)
   parent_id   = element([ for key, value in aws_api_gateway_resource.managed : value.id ], each.key)
   rest_api_id = element([ for key, value in aws_api_gateway_rest_api.managed : value.id ], each.key)
}

You should change it to:

resource "aws_api_gateway_resource" "childs" {
   for_each    = { for key, value in var.restapi.resource.childs : key => value }
   depends_on  = [ aws_api_gateway_resource.parent ]
   path_part   = lookup(each.value, "path", null)
   parent_id   = element([ for key, value in aws_api_gateway_resource.parent : value.id ], each.key)
   rest_api_id = element([ for key, value in aws_api_gateway_rest_api.managed : value.id ], each.key)
}

I suggest to you use a data statement Data Source: aws_api_gateway_resource , here you can exactly specify the path according to your code, it will be the solution, at the end it will return the exact id

data "aws_api_gateway_resource" "my_resource" {
  rest_api_id = "your_rest_api_id"
  path        = "/endpoint/path"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM