首页 > 解决方案 > Why do I get Resource's path part only allow a-zA-Z0-9._-: when trying to create an API gateway resource using Terraform?

问题描述

Is it possible to create and AWS API gateway resources with subdirectories from the root with Terraform?

When doing

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   = "/will/this/work"
}

I get the error:

aws_api_gateway_resource.MyDemoResource: 1 error occurred: aws_api_gateway_resource.MyDemoResource: Error creating API Gateway Resource: BadRequestException: Resource's path part only allow a-zA-Z0-9._-: or a valid greedy path variable and curly braces at the beginning and the end."

I've found this GitHub ticket and a few others like it. I'm not sure if this is a bug or by design with a workaround.

Additionally, If I create this manually in the AWS console and then import it, the full path appears in the attribute path properly but not in the input path_part.

"aws_api_gateway_resource.MyDemoResource": {
                    "type": "aws_api_gateway_resource",
                    "depends_on": [],
                    "primary": {
                        "id": "foo",
                        "attributes": {
                            "id": "foo",
                            "parent_id": "foo",
                            **"path": "/will/this/work"**,
                            **"path_part": "work"**,
                            "rest_api_id": "foo"
                        }    

标签: amazon-web-servicesterraformaws-api-gateway

解决方案


The path_part is the last path segment of an API Gateway resource, not the full path. This mirrors the tree-like structure that you see in the AWS Console.

The full path is exported, yes, but is not taken in as an argument. That's why you have the full path in the imported Terraform state as path and the last path segment - work - as the path_part.

Currently, you're not passing the last path segment; you're passing the full path.

What you need to do is specify 3 different aws_api_gateway_resource each for the different parts of the path segment, with the parent IDs correctly specified to create the structure you need:

  1. will with the parent ID of aws_api_gateway_rest_api.{REST-API-NAME}.root_resource_id
  2. this with the parent ID of aws_api_gateway_resource.will.id
  3. work with the parent ID of aws_api_gateway_resource.this.id

Visual representation:

MyAPI
├─ /will
│  ├─ /this
│  │  ├─ /work

推荐阅读