-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeploy.py
156 lines (121 loc) · 4.28 KB
/
deploy.py
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
"""Deployment script
This script deploys the app to AWS Lambda.
Below are the steps for this deployment script:
1. Set the Youtube parameters in AWS SSM Parameter Store
2. Set the Spotify parameters in AWS SSM Parameter Store
3. Create a S3 bucket to store the AWS Lambda app configuration
4. If the user specifies the "--layer" argument in the command line, the script
will build the helper layer zip
5. Use the SAM cli to package and deploy to AWS Lambda
"""
import os, json, argparse, shutil
from distutils.dir_util import copy_tree
import boto3
import src.constants as constants
parser = argparse.ArgumentParser(
description='Deploy Youtube to Spotify Playlist to AWS Lambda.'
)
parser.add_argument(
'--layer',
action="store_true",
default=False,
help='Build the helper layer zip (default: %(default)s)'
)
args = parser.parse_args()
ssm_client = boto3.client('ssm')
def write_parameter(prefix, key, value):
"""
Writes the provided parameter to AWS SSM Parameter Store.
"""
try:
ssm_client.put_parameter(
Name=f'/YoutubeToSpotify/Prod/{prefix}/{key}',
Value=value,
Type='SecureString',
Overwrite=True
)
except:
print(f"[ERROR] Error setting parameter ({prefix}/{key}).")
return
def set_up_youtube_secrets():
"""
Writes the local Youtube parameters to AWS SSM Parameter Store.
"""
print("[NOTICE] Setting Youtube secrets.")
if not os.path.isfile(constants.YOUTUBE_SECRETS):
print(
"[ERROR] The Youtube secrets file does exist. " \
"Please run the set up script (set_up.py) first."
)
return
with open(constants.YOUTUBE_SECRETS) as f:
youtube_secrets = json.load(f)
for key, value in youtube_secrets.items():
write_parameter("Youtube", key, value)
def set_up_spotify_secrets():
"""
Writes the local Spotify parameters to AWS SSM Parameter Store.
"""
print("[NOTICE] Setting Spotify secrets.")
if not os.path.isfile(constants.SPOTIFY_SECRETS):
print(
"[ERROR] The Spotify secrets file does exist. " \
"Please run the set up script (set_up.py) first."
)
return
with open(constants.SPOTIFY_SECRETS) as f:
spotify_secrets = json.load(f)
for key, value in spotify_secrets.items():
write_parameter("Spotify", key, value)
def create_s3_bucket():
"""
Creates the S3 bucket to store the AWS Lambda app configuration (if needed)
"""
print("[NOTICE] Creating S3 bucket.")
s3_client = boto3.client('s3')
bucket_name = "youtube-to-spotify"
s3_client.create_bucket(
Bucket=bucket_name
)
def build_layer():
"""
Builds the helper layer zip.
This adheres to the AWS Lambda layer requirement of the zip being structured
as follows:
"python/lib/python3.x/site-packages/..."
"""
print("[NOTICE] Building helper layer.")
src = "YoutubeToSpotifyEnv/lib/python3.7/site-packages"
dest = "layers/youtube-to-spotify-helper-layer/python/lib/python3.7/site-packages/"
try:
# Create the directory to adhere to AWS Lambda layer structure requirement
os.makedirs(src)
except:
# The directory may already exist
pass
# Copy virtual env packages to directory
copy_tree(src, dest)
# Create the zip
shutil.make_archive(
"layers/youtube-to-spotify-helper-layer/function",
'zip',
"layers/youtube-to-spotify-helper-layer/",
"python"
)
def deploy_lambda():
"""
Uses the SAM cli to package and deploy to AWS Lambda
"""
print("[NOTICE] Deploy Lambda function.")
os.system("sam package --template-file template.yaml --s3-bucket youtube-to-spotify --s3-prefix lambda-templates/youtube-to-spotify --output-template-file packaged.yaml")
os.system("sam deploy --template-file packaged.yaml --stack-name YoutubeToSpotify --s3-bucket youtube-to-spotify --capabilities 'CAPABILITY_IAM'")
def main():
set_up_youtube_secrets()
set_up_spotify_secrets()
create_s3_bucket()
if args.layer:
# If the user specifies the "--layer" argument, build the helper layer
build_layer()
deploy_lambda()
if __name__ == "__main__":
main()