Skip to content

Commit 0e12ddd

Browse files
committed
Initial commit
0 parents  commit 0e12ddd

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*~

main.tf

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
resource "aws_api_gateway_method" "cors_method" {
2+
rest_api_id = "${var.api}"
3+
resource_id = "${var.resource}"
4+
http_method = "OPTIONS"
5+
authorization = "NONE"
6+
}
7+
8+
resource "aws_api_gateway_integration" "cors_integration" {
9+
rest_api_id = "${var.api}"
10+
resource_id = "${var.resource}"
11+
http_method = "${aws_api_gateway_method.cors_method.http_method}"
12+
type = "MOCK"
13+
request_templates {
14+
"application/json" = <<EOF
15+
{ "statusCode": 200 }
16+
EOF
17+
}
18+
}
19+
20+
resource "aws_api_gateway_method_response" "cors_method_response" {
21+
rest_api_id = "${var.api}"
22+
resource_id = "${var.resource}"
23+
http_method = "${aws_api_gateway_method.cors_method.http_method}"
24+
25+
status_code = "200"
26+
27+
response_models {
28+
"application/json" = "Empty"
29+
}
30+
31+
response_parameters {
32+
"method.response.header.Access-Control-Allow-Headers" = true,
33+
"method.response.header.Access-Control-Allow-Methods" = true,
34+
"method.response.header.Access-Control-Allow-Origin" = true
35+
}
36+
}
37+
38+
resource "aws_api_gateway_integration_response" "cors_integration_response" {
39+
rest_api_id = "${var.api}"
40+
resource_id = "${aws_api_gateway_method.cors_method.resource_id}"
41+
http_method = "${aws_api_gateway_method.cors_method.http_method}"
42+
43+
status_code = "${aws_api_gateway_method_response.cors_method_response.status_code}"
44+
45+
response_parameters = {
46+
"method.response.header.Access-Control-Allow-Headers" = "'${local.headers}'",
47+
"method.response.header.Access-Control-Allow-Methods" = "'${local.methods}'",
48+
"method.response.header.Access-Control-Allow-Origin" = "'${local.origins}'"
49+
}
50+
}

variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
variable "api" {}
2+
3+
variable "resource" {}
4+
5+
variable "methods" {
6+
default = []
7+
}
8+
9+
variable "origins" {
10+
default = ["*"]
11+
}
12+
13+
variable "headers" {
14+
default = ["Content-Type", "X-Amz-Date", "Authorization", "X-Api-Key", "X-Amz-Security-Token"]
15+
}
16+
17+
locals {
18+
methodOptions = "OPTIONS"
19+
defaultHeaders = ["Content-Type", "X-Amz-Date", "Authorization", "X-Api-Key", "X-Amz-Security-Token"]
20+
21+
methods = "${join(",", distinct(concat(var.methods, list(local.methodOptions))))}"
22+
origins = "${join(",", var.origins)}"
23+
headers = "${join(",", distinct(concat(var.headers, local.defaultHeaders)))}"
24+
}

0 commit comments

Comments
 (0)