Giter Club home page Giter Club logo

api-gateway-options's Introduction

api-gateway-options

This is a Terraform version of what API Gateway creates when you "Enable CORS" for a resource (with a couple of configurable extras to boot).

Usage

resource "aws_route53_record" "site" {
  #
}

resource "aws_api_gateway_rest_api" "site" {
  #
}

locals {
  dependency_hashes = [
    md5(file("${path.module}/proxy.tf")),
  ]

  combined_hash = md5(join("\n", local.dependency_hashes))
}

resource "aws_api_gateway_deployment" "site" {
  depends_on = [
    aws_api_gateway_integration.proxy,
    module.options_proxy.id,
  ]

  rest_api_id       = aws_api_gateway_rest_api.site.id
  stage_name        = var.env
  stage_description = local.combined_hash  # This is a convenient way to force a fresh deployment if any of the underlying resources change.
}

resource "aws_api_gateway_method_settings" "site" {
  method_path = "*/*"
  rest_api_id = aws_api_gateway_rest_api.site.id

  settings {
    metrics_enabled    = true
    logging_level      = "INFO"
    data_trace_enabled = true
  }

  stage_name = aws_api_gateway_deployment.site.stage_name
}

resource "aws_api_gateway_resource" "proxy" {
  parent_id   = aws_api_gateway_rest_api.site.root_resource_id
  path_part   = "{proxy+}"
  rest_api_id = aws_api_gateway_rest_api.site.id
}

module "options_proxy" {
  source = "github.com/jdhollis/api-gateway-options"

  allow_headers = "'content-type,authorization,x-amz-date,x-api-key,x-amz-security-token,x-csrf-token'"
  allow_methods = "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'"
  allow_origin  = var.allow_origin_override != "" ? var.allow_origin_override : "https://${aws_route53_record.site.name}" # var.allow_origin_override makes it easier to work with API Gateway when in local development
  resource_id   = aws_api_gateway_resource.proxy.id
  rest_api_id   = aws_api_gateway_rest_api.site.id
}

api-gateway-options's People

Contributors

jdhollis avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.