-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathmain.tf
174 lines (142 loc) · 5.11 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright (c) HashiCorp, Inc.
# SPDX-License-Identifier: MPL-2.0
terraform {
required_version = ">= 0.12"
}
provider "aws" {
region = var.aws_region
}
# Create S3 Full Access Policy
resource "aws_iam_policy" "s3_policy" {
name = "s3-policy"
description = "Policy for allowing all S3 Actions"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": "*"
}
]
}
EOF
}
# Create API Gateway Role
resource "aws_iam_role" "s3_api_gateway_role" {
name = "s3-api-gateway-role"
# Create Trust Policy for API Gateway
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
# Attach S3 Access Policy to the API Gateway Role
resource "aws_iam_role_policy_attachment" "s3_policy_attach" {
role = aws_iam_role.s3_api_gateway_role.name
policy_arn = aws_iam_policy.s3_policy.arn
}
resource "aws_api_gateway_rest_api" "MyS3" {
name = "MyS3"
description = "API for S3 Integration"
}
resource "aws_api_gateway_resource" "Folder" {
rest_api_id = aws_api_gateway_rest_api.MyS3.id
parent_id = aws_api_gateway_rest_api.MyS3.root_resource_id
path_part = "{folder}"
}
resource "aws_api_gateway_resource" "Item" {
rest_api_id = aws_api_gateway_rest_api.MyS3.id
parent_id = aws_api_gateway_resource.Folder.id
path_part = "{item}"
}
resource "aws_api_gateway_method" "GetBuckets" {
rest_api_id = aws_api_gateway_rest_api.MyS3.id
resource_id = aws_api_gateway_rest_api.MyS3.root_resource_id
http_method = "GET"
authorization = "AWS_IAM"
}
resource "aws_api_gateway_integration" "S3Integration" {
rest_api_id = aws_api_gateway_rest_api.MyS3.id
resource_id = aws_api_gateway_rest_api.MyS3.root_resource_id
http_method = aws_api_gateway_method.GetBuckets.http_method
# Included because of this issue: https://github.com/hashicorp/terraform/issues/10501
integration_http_method = "GET"
type = "AWS"
# See uri description: https://docs.aws.amazon.com/apigateway/api-reference/resource/integration/
uri = "arn:aws:apigateway:${var.aws_region}:s3:path//"
credentials = aws_iam_role.s3_api_gateway_role.arn
}
resource "aws_api_gateway_method_response" "Status200" {
rest_api_id = aws_api_gateway_rest_api.MyS3.id
resource_id = aws_api_gateway_rest_api.MyS3.root_resource_id
http_method = aws_api_gateway_method.GetBuckets.http_method
status_code = "200"
response_parameters = {
"method.response.header.Timestamp" = true
"method.response.header.Content-Length" = true
"method.response.header.Content-Type" = true
}
response_models = {
"application/json" = "Empty"
}
}
resource "aws_api_gateway_method_response" "Status400" {
depends_on = [aws_api_gateway_integration.S3Integration]
rest_api_id = aws_api_gateway_rest_api.MyS3.id
resource_id = aws_api_gateway_rest_api.MyS3.root_resource_id
http_method = aws_api_gateway_method.GetBuckets.http_method
status_code = "400"
}
resource "aws_api_gateway_method_response" "Status500" {
depends_on = [aws_api_gateway_integration.S3Integration]
rest_api_id = aws_api_gateway_rest_api.MyS3.id
resource_id = aws_api_gateway_rest_api.MyS3.root_resource_id
http_method = aws_api_gateway_method.GetBuckets.http_method
status_code = "500"
}
resource "aws_api_gateway_integration_response" "IntegrationResponse200" {
depends_on = [aws_api_gateway_integration.S3Integration]
rest_api_id = aws_api_gateway_rest_api.MyS3.id
resource_id = aws_api_gateway_rest_api.MyS3.root_resource_id
http_method = aws_api_gateway_method.GetBuckets.http_method
status_code = aws_api_gateway_method_response.Status200.status_code
response_parameters = {
"method.response.header.Timestamp" = "integration.response.header.Date"
"method.response.header.Content-Length" = "integration.response.header.Content-Length"
"method.response.header.Content-Type" = "integration.response.header.Content-Type"
}
}
resource "aws_api_gateway_integration_response" "IntegrationResponse400" {
depends_on = [aws_api_gateway_integration.S3Integration]
rest_api_id = aws_api_gateway_rest_api.MyS3.id
resource_id = aws_api_gateway_rest_api.MyS3.root_resource_id
http_method = aws_api_gateway_method.GetBuckets.http_method
status_code = aws_api_gateway_method_response.Status400.status_code
selection_pattern = "4\\d{2}"
}
resource "aws_api_gateway_integration_response" "IntegrationResponse500" {
depends_on = [aws_api_gateway_integration.S3Integration]
rest_api_id = aws_api_gateway_rest_api.MyS3.id
resource_id = aws_api_gateway_rest_api.MyS3.root_resource_id
http_method = aws_api_gateway_method.GetBuckets.http_method
status_code = aws_api_gateway_method_response.Status500.status_code
selection_pattern = "5\\d{2}"
}
resource "aws_api_gateway_deployment" "S3APIDeployment" {
depends_on = [aws_api_gateway_integration.S3Integration]
rest_api_id = aws_api_gateway_rest_api.MyS3.id
stage_name = "MyS3"
}