-
Notifications
You must be signed in to change notification settings - Fork 0
/
fabfile.py
125 lines (99 loc) · 3.11 KB
/
fabfile.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
from fabric.api import env, local, task
from fabric.context_managers import quiet
env.appname = env.get('appname', 'thanksforfeedback')
env.arch = env.get('arch', 'linux/arm64')
env.profile = env.get('profile', 'devsoc-serverless')
env.stage = env.get('stage', 'production')
@task
def demo():
"""
Load demo environment settings.
"""
env.stage = 'demo'
def aws_vault(command, capture=False, shell=None):
"""
Run aws-vault locally with the given profile.
"""
vault_command = 'aws-vault exec {} -- {}'.format(env.profile, command)
return local(vault_command, capture=capture, shell=shell)
@task
def deploy():
"""
Deploy to AWS.
"""
service_name = '{}-{}'.format(env.appname, env.stage)
with quiet():
aws_vault(
'aws ecr create-repository --repository-name {}'.format(service_name),
)
image_url = aws_vault(
'aws ecr describe-repositories '
'--repository-name {} '
'--output=text '
'--query=repositories[0].repositoryUri'.format(
service_name
),
capture=True,
)
repository_hostname = image_url.split('/')[0]
aws_vault(
'aws ecr get-login-password | docker login --username AWS --password-stdin {}'.format(
repository_hostname,
)
)
# Build with a fresh environment to avoid uncommitted files or cruft
sentry_release = local("git rev-parse HEAD", capture=True)
local(
'git archive HEAD | '
'docker buildx build --build-arg SENTRY_RELEASE={} --push --platform={} --tag={} -'.format(
sentry_release, env.arch, image_url,
)
)
aws_vault('npm run serverless -- deploy --stage {}'.format(env.stage))
@task
def lambda_env(name=None, value=None):
"""
Update SSM environment variables.
Setting a variable:
fab lambda_env:name=ENV_VAR_NAME,value=env_var_value
Getting a single variable:
fab lambda_env:name=ENV_VAR_NAME
Listing all variables:
fab lambda_env
"""
service_name = '{}-{}'.format(env.appname, env.stage)
if name is None and value is None:
# List parameters
aws_vault(
'aws ssm get-parameters-by-path '
'--region eu-west-2 '
'--path "/serverless/{}/" '
'--with-decryption'.format(service_name)
)
elif value is None:
# Get parameter
aws_vault(
'aws ssm get-parameter '
'--region eu-west-2 '
'--name "/serverless/{}/{}" '
'--with-decryption'.format(service_name, name)
)
else:
# Set parameter
aws_vault(
'aws ssm put-parameter '
'--region eu-west-2 '
'--name "/serverless/{}/{}" '
'--type SecureString '
'--value "{}" '
'--overwrite'.format(service_name, name, value)
)
@task
def invoke(name):
"""
Invoke a lambda function.
Examples:
fab invoke:function_name
fab invoke:name=function_name
"""
aws_vault('npm run serverless -- invoke --stage {} --function {}'.format(env.stage, name))