I’m creating aws api gateway resources (not terraform resources) using this:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_resource
resource "aws_api_gateway_resource" "MyDemoResource" {
rest_api_id = aws_api_gateway_rest_api.MyDemoAPI.id
parent_id = aws_api_gateway_rest_api.MyDemoAPI.root_resource_id
path_part = "mydemoresource"
}
This works excellent when resources exist at the /
level, and the parent_id
is set to the api gw’s id. The problem I’m facing is how to handle the creation order for gw resources who’s parent_id
is the id
of another gw resource; this requires the latter resource to already be created. I’ll give a code example to make it easier to follow:
The resource being created is bar
from the path /foo/bar/char
/.
In this example:
path_part = bar
parent_id = foo
.
foo
is a api_gateway_resource that either has or has not already been created with a parent_id
equal to the api gw root id.
resource "aws_api_gateway_resource" "bar" {
rest_api_id = aws_api_gateway_rest_api.gw.id
parent_id = aws_api_gateway_resource.bar.id
path_part = "bar"
}
or if you’re using a for_each loop like i am:
resource "aws_api_gateway_resource" "generic_resource" {
for_each = var.resources
rest_api_id = aws_api_gateway_rest_api.gw.id
parent_id = aws_api_gateway_resource.[each.value].id
path_part = "bar"
}
where value=bar
.
Terraform will throw an error is the loop loops up the id for bar
and it hasn’t yet been created.
This is an addon to issue 53956 after I realized that not all path_part
’s sat at the root of the gateway and needed to modify the terraform resource block for api_gateway_resource. At present, I have a yaml file of the gw resources and the values terraform needs to create the aws_api_gateway_resource, aws_api_gateway_method, aws_api_gateway_method_response, aws_api_gateway_integration, and aws_api_gateway_integration_response with the resolved dependency issue from 53956.
Additional code for context:
# Resources and Method Response with Object Variables
resource "aws_api_gateway_resource" "resource" {
for_each = var.only_resources
path_part = each.key #apigw_path_parts
rest_api_id = aws_api_gateway_rest_api.iddw.id
parent_id = [each.value.parent_id].id
#parent_id = aws_api_gateway_resource.simple_resource[each.value.parent_id].id
}
/* resource "aws_api_gateway_resource" "slim_resource" {
for_each = var.slim_resources
path_part = each.key
rest_api_id = aws_api_gateway_rest_api.iddw.id
parent_id = aws_api_gateway_resource.root_resource[each.value.parent_id].id
} */
resource "aws_api_gateway_method" "method" {
for_each = var.resources_yaml
rest_api_id = aws_api_gateway_rest_api.iddw.id
resource_id = aws_api_gateway_resource.resource[each.key].id
http_method = each.value["httpMethod"]
authorization = each.value["authorizationType"]
request_models = try(each.value.requestModels, {})
request_parameters = try(each.value.requestParameters, {})
depends_on = [
aws_api_gateway_model.model,
]
}
resource "aws_api_gateway_integration" "integration" {
for_each = var.resources_yaml
rest_api_id = aws_api_gateway_rest_api.iddw.id
resource_id = aws_api_gateway_resource.resource[each.key].id #aws_api_gateway_resource.id
http_method = each.value.httpMethod
integration_http_method = each.value.methodIntegrationhttpMethod
type = each.value.methodIntegrationtype
connection_type = each.value.methodIntegrationconnectionType
uri = each.value.methodIntegrationuri
request_templates = try(each.value.methodIntegrationrequestTemplates, {})
request_parameters = each.value.methodIntegrationrequestParameters
depends_on = [
aws_api_gateway_resource.resource,
aws_api_gateway_method.method,
]
}
resource "aws_api_gateway_integration_response" "integration_response" {
for_each = var.resources_yaml
rest_api_id = aws_api_gateway_rest_api.iddw.id
resource_id = aws_api_gateway_resource.resource[each.key].id #aws_api_gateway_resource.id
http_method = each.value.methodIntegrationhttpMethod
status_code = each.value.methodIntegrationintegrationResponsesstatusCode
response_parameters = try(each.value.methodIntegrationintegrationResponseParameters, {})
# Unused fields. keeping code for historical notes
#content_handling = try(each.value.integration_response_content_handling, null) #unused resource
#response_templates = try(each.value.integration_response_templates, {}) unused resource
#selection_pattern = try(each.value.integration_response_selection_pattern, "") unused resource
depends_on = [
aws_api_gateway_integration.integration,
]
}
resource "aws_api_gateway_method_response" "method_response" {
for_each = var.resources_yaml
rest_api_id = aws_api_gateway_rest_api.iddw.id
resource_id = aws_api_gateway_resource.resource[each.key].id #aws_api_gateway_resource.id
http_method = each.value.httpMethod
status_code = each.value.methodResponsesstatusCode
response_models = each.value.methodResponsesresponseModels
#response_models = contains(var.models, endswith(response_models))
depends_on = [
aws_api_gateway_integration_response.integration_response,
aws_api_gateway_model.model,
]
}
# API Gateway Models
resource "aws_api_gateway_model" "model" {
for_each = var.models
rest_api_id = aws_api_gateway_rest_api.iddw.id
name = each.key
content_type = each.value.content_type
schema = each.value.schema
}