-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathdeploy-everything.sh
executable file
·179 lines (152 loc) · 4.88 KB
/
deploy-everything.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
#! /bin/bash
# You'll need to configure these settings, they must be unique.
# Be sure to also change the bucket name in `app.js`!
export bucket_name="youtube-mp3-downloader"
export role_name="YoutubeMp3DownloaderRole"
export policy_name="YoutubeMp3DownloaderPolicy"
export transcoder_function_name="YoutubeMp3TranscoderFunction"
export downloader_function_name="YoutubeMp3DownloaderFunction"
export downloader_api_name="YoutubeDownloaderApi"
# Make a new S3 bucket.
aws s3 mb "s3://${bucket_name}"
# Create a new role.
read -r -d '' role_policy_document <<'EOF'
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"apigateway.amazonaws.com",
"lambda.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
EOF
response="$(aws iam create-role \
--role-name "${role_name}" \
--assume-role-policy-document "${role_policy_document}")"
echo "${response}"
role_arn="$(jq -r .Role.Arn <<< "${response}")"
# Assign a role to the policy.
read -r -d '' policy_document <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"apigateway:*"
],
"Resource": "arn:aws:apigateway:*::/*"
},
{
"Effect": "Allow",
"Action": [
"execute-api:Invoke"
],
"Resource": "arn:aws:execute-api:*:*:*"
},
{
"Effect": "Allow",
"Action": [
"lambda:*"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "s3:ListAllMyBuckets",
"Resource": "arn:aws:s3:::*"
},
{
"Effect": "Allow",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::${bucket_name}",
"arn:aws:s3:::${bucket_name}/*"
]
}
]
}
EOF
aws iam put-role-policy \
--role-name "${role_name}" \
--policy-name "${policy_name}" \
--policy-document "${policy_document}"
# Create the transcoder Lambda function.
yarn add aws-sdk request tempy
rm -f youtube-mp3-transcoder.zip
zip youtube-mp3-transcoder.zip transcoder.js
aws lambda create-function \
--function-name "${transcoder_function_name}" \
--zip-file fileb://youtube-mp3-transcoder.zip \
--handler transcoder.handler \
--runtime nodejs6.10 \
--timeout 300 \
--role "${role_arn}"
rm -f youtube-mp3-transcoder.zip
zip --symlinks --recurse-paths youtube-mp3-transcoder.zip \
transcoder.js package.json node_modules/ exodus
aws s3 cp youtube-mp3-transcoder.zip "s3://${bucket_name}/"
aws lambda update-function-code \
--function-name "${transcoder_function_name}" \
--s3-bucket "${bucket_name}" \
--s3-key youtube-mp3-transcoder.zip
# Create the function for the downloader app's API.
yarn add aws-sdk aws-serverless-express express nunjucks ytdl-core
zip --symlinks --recurse-paths youtube-mp3-downloader.zip \
app.js download.html lambda.js package.json node_modules/
response="$(aws lambda create-function \
--function-name "${downloader_function_name}" \
--zip-file fileb://youtube-mp3-downloader.zip \
--handler lambda.handler \
--runtime nodejs6.10 \
--timeout 29 \
--role "${role_arn}")"
echo "${response}"
downloader_function_arn="$(jq -r .FunctionArn <<< "${response}")"
# Create the REST API.
response="$(aws apigateway create-rest-api \
--name "${downloader_api_name}" \
--endpoint-configuration types=REGIONAL)"
echo "${response}"
api_id="$(jq -r .id <<< "${response}")"
# Query the resources for this API.
response="$(aws apigateway get-resources \
--rest-api-id "${api_id}")"
echo "${response}"
root_resource_id="$(jq -r .items[0].id <<< "${response}")"
# Create a child resource.
response="$(aws apigateway create-resource \
--rest-api-id "${api_id}" \
--parent-id "${root_resource_id}" \
--path-part '{proxy+}')"
echo "${response}"
proxy_resource_id="$(jq -r .id <<< "${response}")"
# Allow GET requests for the proxy resource.
aws apigateway put-method \
--rest-api-id "${api_id}" \
--resource-id "${proxy_resource_id}" \
--http-method GET \
--authorization-type NONE
# Integrate the proxy resource with the downloader Lambda function.
aws apigateway put-integration \
--rest-api-id "${api_id}" \
--resource-id "${proxy_resource_id}" \
--http-method GET \
--integration-http-method POST \
--type AWS_PROXY \
--uri "arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/${downloader_function_arn}/invocations" \
--credentials "${role_arn}"
# Deploy the API with a stage name of "v1".
aws apigateway create-deployment \
--rest-api-id "${api_id}" \
--stage-name v1
# Echo out the bookmarklet.
echo "Now just create a bookmarklet with the following contents!"
echo "javascript:window.open(\`https://${api_id}.execute-api.us-east-2.amazonaws.com/v1/\${window.location.href\`);"