Creates an AWS AppSync GraphQL API with an HTTP data source and a function to handle requests and responses.

Terraform Template

resource "aws_appsync_datasource" "example" {
  api_id = aws_appsync_graphql_api.example.id

  http_config {
    endpoint = "http://example.com"
  }
  name = "example"
  type = "HTTP"
}

resource "aws_appsync_function" "example" {
  api_id = aws_appsync_graphql_api.example.id
  data_source = aws_appsync_datasource.example.name
  name = "example"
  request_mapping_template = <<-EOF
    {
     "version": "2018-05-29",
     "method": "GET",
     "resourcePath": "/",
     "params":{
     "headers": $utils.http.copyheaders($ctx.request.headers)
     }
    }
    
  EOF
  response_mapping_template = <<-EOF
    #if($ctx.result.statusCode == 200)
     $ctx.result.body
    #else
     $utils.appendError($ctx.result.body, $ctx.result.statusCode)
    #end
    
  EOF
}

resource "aws_appsync_graphql_api" "example" {
  authentication_type = "API_KEY"
  name = "example"
  schema = <<-EOF
    type Mutation {
     putPost(id: ID!, title: String!): Post
    }
    
    type Post {
     id: ID!
     title: String!
    }
    
    type Query {
     singlePost(id: ID!): Post
    }
    
    schema {
     query: Query
     mutation: Mutation
    }
    
  EOF
}