Search code examples
amazon-web-servicesterraformaws-api-gatewayterraform-provider-aws

Terraform AWS API Gateway aws_api_gateway_model issue


I am new to terraform and trying to create API Gateway via terraform.

Here is my code

resource "aws_api_gateway_rest_api" "demo_services_apig" {
  name = "demo-services-apigateway-${var.environment}"
  description = "Demo service APIs to handle Snow events and User Authentication"
  api_key_source = "HEADER"
  endpoint_configuration {
    types = ["REGIONAL"]
  }
}


resource "aws_api_gateway_model" "demo_services_apig_model" {
  rest_api_id = aws_api_gateway_rest_api.demo_services_apig.id
  name = "UserOauthAPIResponseModel"
  description = "a JSON schema"
  content_type = "application/json"

  schema = jsonencode({
    type = "${file("api_gateway/json_files/oauth_response.json")}"
  })
}

Here is schema JSON under api_gateway/json_files/* which works fine if I configure it from console.

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "UserOAuthUserConfig",
    "type": "object",
    "properties": {
        "Role": {
            "type": "string"
        },
        "Policy": {
            "type": "string"
        },
        "HomeDirectory": {
            "type": "string"
        },
        "PublicKeys": {
            "type": "array",
            "items": {
                "type": "string"
            }
        }
    }
}

I am getting below error in Terraform and not able to figure out what's wrong here.

│ Error: creating API Gateway Model (UserOauthAPIResponseModel): BadRequestException: Invalid model specified: Validation Result: warnings : [], errors : [Invalid model schema specified]
│ 
│   with module.api_gateway.aws_api_gateway_model.demo_services_apig_model,
│   on api_gateway/main.tf line 15, in resource "aws_api_gateway_model" "demo_services_apig_model":
│   15: resource "aws_api_gateway_model" "demo_services_apig_model" {

Solution

  • Created model manually in console and use get-model CLI command to check format. use same format in terraform.

    resource "aws_api_gateway_model" "demo_services_apig_model" {
      rest_api_id = aws_api_gateway_rest_api.demo_services_apig.id
      name = "UserOauthAPIResponseModel"
      description = "a JSON schema"
      content_type = "application/json"
      schema = "{\"$schema\":\"http://json-schema.org/draft-04/schema#\",\"title\":\"UserUserConfig\",\"type\":\"object\",\"properties\":{\"Role\":{\"type\":\"string\"},\"Policy\":{\"type\":\"string\"},\"HomeDirectory\":{\"type\":\"string\"},\"PublicKeys\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}}}"
    }