-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-aws-rest-api-to-s3.sh
executable file
·253 lines (212 loc) · 8.32 KB
/
create-aws-rest-api-to-s3.sh
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env bash
set -o errexit
set -o nounset
##################################################
# Designed by genzouw ( https://genzouw.com ) from Japan
# * Twitter : @genzouw ( https://twitter.com/genzouw )
# * Facebook : genzouw ( https://www.facebook.com/genzouw )
# * Gmail : [email protected]
#
# Please feel free to contact us if you have any questions,
# request for friends, consultation, assistance with development, etc.
##################################################
#-Configuration------------------------------------
# Before execution, change the following variables to your preferred values.
#
# Alternatively, add the environment variable settings at the beginning of the command as follows:
#
# IAM_ROLE_NAME='value1' \
# S3_BUCKET_NAME='value2' \
# APIGATEWAY_RESTAPI_NAME='value2' \
# create-aws-rest-api-to-s3.sh
#
IAM_ROLE_NAME="${IAM_ROLE_NAME:-ANY_IAM_ROLE_NAME}"
S3_BUCKET_NAME=${S3_BUCKET_NAME:-ANY_S3_BUCKET_NAME}
APIGATEWAY_RESTAPI_NAME=${APIGATEWAY_RESTAPI_NAME:-ANY_APIGATEWAY_RESTAPI_NAME}
AWS_DEFAULT_REGION=${AWS_DEFAULT_REGION:-ap-northeast-1}
cat <<EOF
The following tools are required to run this script:
* [aws-cli](https://aws.amazon.com/jp/cli/)
* [jq](https://stedolan.github.io/jq/)
Run the script with the following environment variable values:
* IAM_ROLE_NAME="${IAM_ROLE_NAME}"
* S3_BUCKET_NAME="${S3_BUCKET_NAME}"
* APIGATEWAY_RESTAPI_NAME="${APIGATEWAY_RESTAPI_NAME}"
* AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"
EOF
#----IAM-------------------------------------------
# First create the initial policy file of "API Gateway"
cat <<'EOF' >/tmp/${IAM_ROLE_NAME}-policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
# Create a role based on the created policy file. At the same time, save the IAM ROLE ID.
IAM_ROLE_ID=$(
aws iam create-role \
--role-name "${IAM_ROLE_NAME}" \
--assume-role-policy-document file:///tmp/${IAM_ROLE_NAME}-policy.json | jq -r '.Role.RoleId'
)
# Check
if [[ -z $IAM_ROLE_ID ]]; then
echo 'Could not get ${IAM_ROLE_ID}.'
exit 255
fi
cat <<EOF
The following AWS objects have been created.
* IAM_ROLE_ID="${IAM_ROLE_ID}"
EOF
aws iam attach-role-policy \
--role-name "${IAM_ROLE_NAME}" \
--policy-arn arn:aws:iam::aws:policy/AmazonS3FullAccess
#---S3---------------------------------------------
# Create S3 bucket
aws s3 mb s3://${S3_BUCKET_NAME}
#---API Gateway[REST API]--------------------------
# Save the REST API ID
APIGATEWAY_REST_API_ID=$(
aws apigateway create-rest-api \
--name "${APIGATEWAY_RESTAPI_NAME}" | jq -r '.id'
)
# Check
if [[ -z $APIGATEWAY_REST_API_ID ]]; then
echo 'Could not get ${APIGATEWAY_REST_API_ID}.'
exit 255
fi
cat <<EOF
The following AWS objects have been created.
* APIGATEWAY_REST_API_ID="${APIGATEWAY_REST_API_ID}"
EOF
#---API Gateway[Resource]--------------------------
# Save "Root Resource ID"
APIGATEWAY_RESOURCE_ID_ROOT=$(
aws apigateway get-resources \
--rest-api-id "$APIGATEWAY_REST_API_ID" | jq -r '.items[0].id'
)
# Save "Resource ID"
APIGATEWAY_RESOURCE_ID_S3_BUCKET_NAME=$(
aws apigateway create-resource \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--parent-id "${APIGATEWAY_RESOURCE_ID_ROOT}" \
--path-part '{s3_bucket_name}' | jq -r '.id'
)
# Save "Resource ID"
APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY=$(
aws apigateway create-resource \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--parent-id "${APIGATEWAY_RESOURCE_ID_S3_BUCKET_NAME}" \
--path-part '{s3_object_key}' | jq -r '.id'
)
# Check
if [[ -z $APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY ]]; then
echo 'Could not get ${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}.'
exit 255
fi
cat <<EOF
The following AWS objects have been created.
* APIGATEWAY_RESOURCE_ID_S3_BUCKET_NAME="${APIGATEWAY_RESOURCE_ID_S3_BUCKET_NAME}"
EOF
cat <<EOF
The following AWS objects have been created.
* APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY="${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}"
EOF
#---API Gateway[Method]----------------------------
# Enable GET for REST API Resources
aws apigateway put-method \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--resource-id "${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}" \
--http-method GET \
--authorization-type NONE \
--request-parameters "method.request.path.s3_bucket_name=true,method.request.path.s3_object_key=true" \
--api-key-required \
;
#---API Gateway[Method Request/Response]-----------
aws apigateway put-method-response \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--resource-id "${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}" \
--http-method GET \
--status-code 200 \
--response-models '{"application/json": "Empty"}'
aws apigateway update-method-response \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--resource-id "${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}" \
--http-method GET \
--status-code 200 \
--patch-operations op="add",path="/responseParameters/method.response.header.Content-Type",value="false"
aws apigateway put-method-response \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--resource-id "${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}" \
--http-method GET \
--status-code 400 \
--response-models '{"application/json": "Empty"}'
aws apigateway put-method-response \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--resource-id "${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}" \
--http-method GET \
--status-code 500 \
--response-models '{"application/json": "Empty"}'
aws apigateway put-integration \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--resource-id "${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}" \
--http-method GET \
--type AWS \
--integration-http-method GET \
--uri "arn:aws:apigateway:${AWS_DEFAULT_REGION}:s3:path/{s3_bucket_name}/{s3_object_key}" \
--credentials $(
aws iam get-role \
--role-name "${IAM_ROLE_NAME}" | jq -r '.Role.Arn'
) \
--request-parameters 'integration.request.path.s3_bucket_name=method.request.path.s3_bucket_name,integration.request.path.s3_object_key=method.request.path.s3_object_key'
aws apigateway put-integration-response \
--rest-api-id "${APIGATEWAY_REST_API_ID}" \
--resource-id "${APIGATEWAY_RESOURCE_ID_S3_OBJECT_KEY}" \
--http-method GET \
--status-code 200 \
--response-templates '{"application/json": ""}'
cat <<EOF
+----------------------------+
| Process Successful !!! |
+----------------------------+
If you want to deploy, please execute the following command.
----------------------------------------
aws apigateway create-deployment \\
--rest-api-id "${APIGATEWAY_REST_API_ID}" \\
--stage-name "prod" \\
--stage-description 'Production' \\
--description 'Production'
----------------------------------------
If you want to all, please execute the following command.
----------------------------------------
aws apigateway delete-rest-api \\
--rest-api-id "${APIGATEWAY_REST_API_ID}"
aws s3 rb \\
--force "s3://${S3_BUCKET_NAME}"
aws iam detach-role-policy \\
--role-name ${IAM_ROLE_NAME} \\
--policy-arn "arn:aws:iam::aws:policy/AmazonS3FullAccess"
aws iam delete-role \\
--role-name \\
"${IAM_ROLE_NAME}"
----------------------------------------
Let's have a nice day!
EOF
#=================================================
# The MIT License
#
# Copyright (c) {year} {copyright holders}
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#=================================================