あめがえるのITブログ

頑張りすぎない。ほどほどに頑張るブログ。

【AWS】APIGatewayメソッド GUI作成とTerraform作成の差分を確認してみた。


APIGatewayメソッド GUI作成とTerraform作成の差分を確認してみた。

GUI作成
メソッドを作成する際、「Lambdaプロキシ統合」にチェックを入れれば他は自動で作成してくれる。

Terraform作成
最小限APIとリソース、メソッド、統合リクエストを作成すれば動作するものが作成できる。

resource "aws_api_gateway_rest_api" "example" {
  name        = "${var.env["env"]}-example-api"
  description = "Example Public API With Cognito Authentication"
  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

resource "aws_api_gateway_resource" "example_resource" {
  rest_api_id = aws_api_gateway_rest_api.example.id
  parent_id   = aws_api_gateway_rest_api.example.root_resource_id
  path_part   = "example"
}

resource "aws_api_gateway_method" "example_method" {
  rest_api_id   = aws_api_gateway_rest_api.example.id
  resource_id   = aws_api_gateway_resource.example_resource.id
  http_method   = "GET"
  authorization = "NONE"
}

resource "aws_api_gateway_integration" "example_integration" {
  rest_api_id             = aws_api_gateway_rest_api.example.id
  resource_id             = aws_api_gateway_resource.example_resource.id
  http_method             = aws_api_gateway_method.example_method.http_method
  integration_http_method = "POST"
  type                    = "MOCK"

  request_templates = {
    "application/json" = <<EOF
    {
       "statusCode": 200,
       "message": "Operation was succcessful"
    }
    EOF
  }
}

resource "aws_api_gateway_deployment" "example_deployment" {
  rest_api_id = aws_api_gateway_rest_api.example.id
  stage_name  = "test"
}

resource "aws_api_gateway_stage" "example_stage" {
  rest_api_id   = aws_api_gateway_rest_api.example.id
  stage_name    = "prod"
  deployment_id = aws_api_gateway_deployment.example_deployment.id
}

AWS CLIで確認

APIGateway設定
GUI作成】

aws apigateway get-rest-api --rest-api-id h3aoheel20
{
    "id": "h3aoheel20",
    "name": "ProxyAPIGateway",
    "createdDate": "2025-02-25T21:07:44+00:00",
    "apiKeySource": "HEADER",
    "endpointConfiguration": {
        "types": [
            "REGIONAL"
        ]
    },
    "tags": {},
    "disableExecuteApiEndpoint": false,
    "rootResourceId": "5bjulv8z45"
}

【Terraform作成】

aws apigateway get-rest-api --rest-api-id ipc8dyde85
{
    "id": "ipc8dyde85",
    "name": "test-example-api",
    "description": "Example Public API With Cognito Authentication",
    "createdDate": "2025-03-01T12:00:51+00:00",
    "apiKeySource": "HEADER",
    "endpointConfiguration": {
        "types": [
            "REGIONAL"
        ]
    },
    "tags": {},
    "disableExecuteApiEndpoint": false,
    "rootResourceId": "d8bryqxzy4"
}

リソース
GUI作成】

aws apigateway get-resources --rest-api-id h3aoheel20
{
    "items": [
        {
            "id": "5bjulv8z45",
            "path": "/"
        },
        {
            "id": "9jvf94",
            "parentId": "5bjulv8z45",
            "pathPart": "hello",
            "path": "/hello",
            "resourceMethods": {
                "GET": {}
            }
        }
    ]
}

【Terraform作成】

aws apigateway get-resources --rest-api-id ipc8dyde85
{
    "items": [
        {
            "id": "d8bryqxzy4",
            "path": "/"
        },
        {
            "id": "m41d87",
            "parentId": "d8bryqxzy4",
            "pathPart": "example",
            "path": "/example",
            "resourceMethods": {
                "GET": {}
            }
        }
    ]
}

メソッド
GUI作成】

aws apigateway get-method --rest-api-id h3aoheel20 --resource-id 9jvf94 --http-method GET
{
    "httpMethod": "GET",
    "authorizationType": "NONE",
    "apiKeyRequired": false,
    "requestParameters": {},
    "requestModels": {},
    "methodResponses": {
        "200": {
            "statusCode": "200",
            "responseModels": {
                "application/json": "Empty"
            }
        }
    },
    "methodIntegration": {
        "type": "AWS_PROXY",
        "httpMethod": "POST",
        "uri": "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:790301748424:function:ProxyLambda/invocations",
        "passthroughBehavior": "WHEN_NO_MATCH",
        "contentHandling": "CONVERT_TO_TEXT",
        "timeoutInMillis": 29000,
        "cacheNamespace": "9jvf94",
        "cacheKeyParameters": [],
        "integrationResponses": {
            "200": {
                "statusCode": "200",
                "responseTemplates": {
                    "application/json": null
                }
            }
        }
    }
}

【Terraform作成】

aws apigateway get-method --rest-api-id ipc8dyde85 --resource-id m41d87 --http-method GET
{
    "httpMethod": "GET",
    "authorizationType": "NONE",
    "apiKeyRequired": false,
    "methodResponses": {
        "200": {
            "statusCode": "200",
            "responseModels": {
                "application/json": "Empty"
            }
        }
    },
    "methodIntegration": {
        "type": "MOCK",
        "requestTemplates": {
            "application/json": "    {\r\n       \"statusCode\": 200,\r\n       \"message\": \"Operation was succcessful\"\r\n    }\r\n"
        },
        "passthroughBehavior": "WHEN_NO_MATCH",
        "timeoutInMillis": 29000,
        "cacheNamespace": "m41d87",
        "cacheKeyParameters": [],
        "integrationResponses": {
            "200": {
                "statusCode": "200",
                "selectionPattern": ".*",
                "responseTemplates": {
                    "application/json": "    {\r\n       \"statusCode\": 200,\r\n       \"message\": \"Operation was successful\"\r\n    }\r\n"
                }
            }
        }
    }
}

統合リクエス
GUI作成】

aws apigateway get-integration --rest-api-id h3aoheel20 --resource-id 9jvf94 --http-method GET
{
    "type": "AWS_PROXY",
    "httpMethod": "POST",
    "uri": "arn:aws:apigateway:ap-northeast-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-northeast-1:790301748424:function:ProxyLambda/invocations",
    "passthroughBehavior": "WHEN_NO_MATCH",
    "contentHandling": "CONVERT_TO_TEXT",
    "timeoutInMillis": 29000,
    "cacheNamespace": "9jvf94",
    "cacheKeyParameters": [],
    "integrationResponses": {
        "200": {
            "statusCode": "200",
            "responseTemplates": {
                "application/json": null
            }
        }
    }
}

【Terraform作成】

aws apigateway get-integration --rest-api-id ipc8dyde85 --resource-id m41d87 --http-method GET
{
    "type": "MOCK",
    "requestTemplates": {
        "application/json": "    {\r\n       \"statusCode\": 200,\r\n       \"message\": \"Operation was succcessful\"\r\n    }\r\n"
    },
    "passthroughBehavior": "WHEN_NO_MATCH",
    "timeoutInMillis": 29000,
    "cacheNamespace": "m41d87",
    "cacheKeyParameters": [],
    "integrationResponses": {
        "200": {
            "statusCode": "200",
            "selectionPattern": ".*",
            "responseTemplates": {
                "application/json": "    {\r\n       \"statusCode\": 200,\r\n       \"message\": \"Operation was successful\"\r\n    }\r\n"
            }
        }
    }
}

統合レスポンス
GUI作成】

aws apigateway get-integration-response --rest-api-id h3aoheel20 --resource-id 9jvf94 --http-method GET --status-code 200
{
    "statusCode": "200",
    "responseTemplates": {
        "application/json": null
    }
}

【Terraform作成】

aws apigateway get-integration-response --rest-api-id ipc8dyde85 --resource-id m41d87 --http-method GET --status-code 200

An error occurred (NotFoundException) when calling the GetIntegrationResponse operation: Invalid Response status code specified

メソッドレスポンス
GUI作成】

aws apigateway get-method-response --rest-api-id h3aoheel20 --resource-id 9jvf94 --http-method GET --status-code 200
{
    "statusCode": "200",
    "responseModels": {
        "application/json": "Empty"
    }
}

【Terraform作成】

aws apigateway get-method-response --rest-api-id ipc8dyde85 --resource-id m41d87 --http-method GET --status-code 200

An error occurred (NotFoundException) when calling the GetMethodResponse operation: Invalid Response status code specified


統合レスポンスとメソッドレスポンスはTerraformでは明示的に指定しないと設定されないことがわかる。



感想

まぁ横着せずにコードに記載しましょう、という話