为什么我得到资源的路径部分只允许 a-zA-Z0-9._-: 尝试使用 Terraform 创建 API 网关资源?



是否可以使用Terraform从根目录创建子目录和AWS API网关资源?

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"
}

我得到错误:

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."

我发现了这个GitHub票证和其他一些类似的。我不确定这是一个bug还是一个设计的解决方案。

此外,如果我在AWS控制台上手动创建它,然后导入它,则完整路径正确地出现在属性path中,而不是在输入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"
}    

path_part最后一个API网关资源的路径段,而不是完整路径。这反映了您在AWS控制台中看到的树状结构。

完整路径被导出,是的,但不作为参数接受。这就是为什么您在导入的Terraform状态中拥有完整的路径作为path,而最后一个路径段—work—作为path_part

当前,您没有传递最后一个路径段;你正在传递完整的路径。

你需要做的是为路径段的不同部分指定3个不同的aws_api_gateway_resource,使用正确指定的父id来创建你需要的结构:

  1. 父ID为aws_api_gateway_rest_api.{REST-API-NAME}.root_resource_idwill
  2. 父ID为aws_api_gateway_resource.will.idthis
  3. work,父ID为aws_api_gateway_resource.this.id

可视化表示:

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

最新更新