-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
executable file
·136 lines (114 loc) · 3.66 KB
/
deploy.py
File metadata and controls
executable file
·136 lines (114 loc) · 3.66 KB
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
import argparse
import boto3
import os
def create_deployment_bucket(bucket, profile, region):
session = boto3.Session(profile_name=profile)
s3 = session.client('s3')
try:
s3.head_bucket(Bucket=bucket)
return
except:
if region == "us-east-1":
response = s3.create_bucket(Bucket=bucket)
else:
response = s3.create_bucket(Bucket=bucket,CreateBucketConfiguration={'LocationConstraint': region})
return response
def build():
# Build the Lambda deployment packages
os.system('sam build -b build/')
def package(bucket, profile):
# generate next stage yaml file
os.system('sam package \
--template-file build/template.yaml \
--output-template-file build/output.yaml \
--s3-bucket {bucket} \
--profile {profile}'.format(bucket=bucket, profile=profile)
)
def deploy(project, profile, region, source_email):
# the actual deployment step
os.system('sam deploy \
--template-file build/output.yaml \
--stack-name {project} \
--capabilities CAPABILITY_NAMED_IAM \
--profile {profile} \
--region {region} \
--parameter-overrides SourceEmail={source_email}'.format(
project=project,
profile=profile,
region=region,
source_email=source_email
)
)
def parse_args(args=None):
des = "Deploys the File Uploaded Email Notification Lambda"
parser = argparse.ArgumentParser(description=des)
parser.add_argument(
'--profile',
'-p',
help="Specify an AWS profile",
type=str,
default='default'
)
parser.add_argument(
'--region',
'-r',
help="Specify the AWS region",
type=str,
default='us-east-1'
)
parser.add_argument(
'--email',
help="Specify the sender email address",
type=str,
default='no-reply@example.com'
)
parser.add_argument(
'--project',
help="Specify the project name",
type=str,
default='email-notification-lambda'
)
parser.add_argument(
'--codebucket',
help="Specify the deployment S3 bucket",
type=str,
default='email-notification-lambda-sam'
)
return parser.parse_args(args)
def clean_build(target):
if os.path.exists(target):
for d in os.listdir(target):
try:
clean_build('{}/{}'.format(target, d))
except OSError:
os.remove('{}/{}'.format(target, d))
os.rmdir(target)
def main(args=None):
args = parse_args(args)
PROFILE = args.profile
REGION = args.region
SOURCE_EMAIL = args.email
PROJECT = args.project
# Bucket used to stage lambda code
BUCKET = args.codebucket
print("REGION: {}".format(REGION))
print("PROFILE: {}".format(PROFILE))
print("SOURCE_EMAIL: {}".format(SOURCE_EMAIL))
print("PROJECT: {}".format(PROJECT))
print("BUCKET: {}".format(BUCKET))
# Create the deployment bucket if it does not exist
create_deployment_bucket(BUCKET, PROFILE, REGION)
# Clean and recreate the build directory
clean_build('build')
os.mkdir('build')
print('Building...')
build()
print('Build complete...')
print('Packaging...')
package(BUCKET, PROFILE)
print('Packaging complete...')
print('Deploying...')
deploy(PROJECT, PROFILE, REGION, SOURCE_EMAIL)
print('Deployment complete.')
if __name__ == "__main__":
main()