33
33
log = logging .getLogger (__name__ )
34
34
35
35
36
- def cleanup_old_versions (src , keep_last_versions , config_file = 'config.yaml' ):
36
+ def cleanup_old_versions (
37
+ src , keep_last_versions ,
38
+ config_file = 'config.yaml' , profile_name = None ,
39
+ ):
37
40
"""Deletes old deployed versions of the function in AWS Lambda.
38
41
39
42
Won't delete $Latest and any aliased version
@@ -48,13 +51,14 @@ def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):
48
51
print ("Won't delete all versions. Please do this manually" )
49
52
else :
50
53
path_to_config_file = os .path .join (src , config_file )
51
- cfg = read (path_to_config_file , loader = yaml . load )
54
+ cfg = read_cfg (path_to_config_file , profile_name )
52
55
56
+ profile_name = cfg .get ('profile' )
53
57
aws_access_key_id = cfg .get ('aws_access_key_id' )
54
58
aws_secret_access_key = cfg .get ('aws_secret_access_key' )
55
59
56
60
client = get_client (
57
- 'lambda' , aws_access_key_id , aws_secret_access_key ,
61
+ 'lambda' , profile_name , aws_access_key_id , aws_secret_access_key ,
58
62
cfg .get ('region' ),
59
63
)
60
64
@@ -80,7 +84,7 @@ def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):
80
84
81
85
def deploy (
82
86
src , use_requirements = False , local_package = None ,
83
- config_file = 'config.yaml' ,
87
+ config_file = 'config.yaml' , profile_name = None ,
84
88
):
85
89
"""Deploys a new function to AWS Lambda.
86
90
@@ -93,7 +97,7 @@ def deploy(
93
97
"""
94
98
# Load and parse the config file.
95
99
path_to_config_file = os .path .join (src , config_file )
96
- cfg = read (path_to_config_file , loader = yaml . load )
100
+ cfg = read_cfg (path_to_config_file , profile_name )
97
101
98
102
# Copy all the pip dependencies required to run your code into a temporary
99
103
# folder then add the handler file in the root of this directory.
@@ -112,7 +116,8 @@ def deploy(
112
116
113
117
114
118
def deploy_s3 (
115
- src , use_requirements = False , local_package = None , config_file = 'config.yaml' ,
119
+ src , use_requirements = False , local_package = None ,
120
+ config_file = 'config.yaml' , profile_name = None ,
116
121
):
117
122
"""Deploys a new function via AWS S3.
118
123
@@ -125,7 +130,7 @@ def deploy_s3(
125
130
"""
126
131
# Load and parse the config file.
127
132
path_to_config_file = os .path .join (src , config_file )
128
- cfg = read (path_to_config_file , loader = yaml . load )
133
+ cfg = read_cfg (path_to_config_file , profile_name )
129
134
130
135
# Copy all the pip dependencies required to run your code into a temporary
131
136
# folder then add the handler file in the root of this directory.
@@ -139,14 +144,14 @@ def deploy_s3(
139
144
use_s3 = True
140
145
s3_file = upload_s3 (cfg , path_to_zip_file , use_s3 )
141
146
if function_exists (cfg , cfg .get ('function_name' )):
142
- update_function (cfg , path_to_zip_file , use_s3 , s3_file )
147
+ update_function (cfg , path_to_zip_file , use_s3 = use_s3 , s3_file = s3_file )
143
148
else :
144
- create_function (cfg , path_to_zip_file , use_s3 , s3_file )
149
+ create_function (cfg , path_to_zip_file , use_s3 = use_s3 , s3_file = s3_file )
145
150
146
151
147
152
def upload (
148
153
src , use_requirements = False , local_package = None ,
149
- config_file = 'config.yaml' ,
154
+ config_file = 'config.yaml' , profile_name = None ,
150
155
):
151
156
"""Uploads a new function to AWS S3.
152
157
@@ -159,7 +164,7 @@ def upload(
159
164
"""
160
165
# Load and parse the config file.
161
166
path_to_config_file = os .path .join (src , config_file )
162
- cfg = read (path_to_config_file , loader = yaml . load )
167
+ cfg = read_cfg (path_to_config_file , profile_name )
163
168
164
169
# Copy all the pip dependencies required to run your code into a temporary
165
170
# folder then add the handler file in the root of this directory.
@@ -174,7 +179,8 @@ def upload(
174
179
175
180
176
181
def invoke (
177
- src , event_file = 'event.json' , config_file = 'config.yaml' ,
182
+ src , event_file = 'event.json' ,
183
+ config_file = 'config.yaml' , profile_name = None ,
178
184
verbose = False ,
179
185
):
180
186
"""Simulates a call to your function.
@@ -189,7 +195,11 @@ def invoke(
189
195
"""
190
196
# Load and parse the config file.
191
197
path_to_config_file = os .path .join (src , config_file )
192
- cfg = read (path_to_config_file , loader = yaml .load )
198
+ cfg = read_cfg (path_to_config_file , profile_name )
199
+
200
+ # Set AWS_PROFILE environment variable based on `--profile` option.
201
+ if profile_name :
202
+ os .environ ['AWS_PROFILE' ] = profile_name
193
203
194
204
# Load environment variables from the config file into the actual
195
205
# environment.
@@ -248,7 +258,8 @@ def init(src, minimal=False):
248
258
249
259
250
260
def build (
251
- src , use_requirements = False , local_package = None , config_file = 'config.yaml' ,
261
+ src , use_requirements = False , local_package = None ,
262
+ config_file = 'config.yaml' , profile_name = None ,
252
263
):
253
264
"""Builds the file bundle.
254
265
@@ -261,7 +272,7 @@ def build(
261
272
"""
262
273
# Load and parse the config file.
263
274
path_to_config_file = os .path .join (src , config_file )
264
- cfg = read (path_to_config_file , loader = yaml . load )
275
+ cfg = read_cfg (path_to_config_file , profile_name )
265
276
266
277
# Get the absolute path to the output directory and create it if it doesn't
267
278
# already exist.
@@ -439,44 +450,46 @@ def get_role_name(region, account_id, role):
439
450
return 'arn:{0}:iam::{1}:role/{2}' .format (prefix , account_id , role )
440
451
441
452
442
- def get_account_id (aws_access_key_id , aws_secret_access_key , region = None ):
453
+ def get_account_id (profile_name , aws_access_key_id , aws_secret_access_key , region = None ):
443
454
"""Query STS for a users' account_id"""
444
455
client = get_client (
445
- 'sts' , aws_access_key_id , aws_secret_access_key ,
456
+ 'sts' , profile_name , aws_access_key_id , aws_secret_access_key ,
446
457
region ,
447
458
)
448
459
return client .get_caller_identity ().get ('Account' )
449
460
450
461
451
- def get_client (client , aws_access_key_id , aws_secret_access_key , region = None ):
462
+ def get_client (client , profile_name , aws_access_key_id , aws_secret_access_key , region = None ):
452
463
"""Shortcut for getting an initialized instance of the boto3 client."""
453
464
454
- return boto3 .client (
455
- client ,
465
+ boto3 .setup_default_session (
466
+ profile_name = profile_name ,
456
467
aws_access_key_id = aws_access_key_id ,
457
468
aws_secret_access_key = aws_secret_access_key ,
458
469
region_name = region ,
459
470
)
471
+ return boto3 .client (client )
460
472
461
473
462
- def create_function (cfg , path_to_zip_file , * use_s3 , ** s3_file ):
474
+ def create_function (cfg , path_to_zip_file , use_s3 = False , s3_file = None ):
463
475
"""Register and upload a function to AWS Lambda."""
464
476
465
477
print ('Creating your new Lambda function' )
466
478
byte_stream = read (path_to_zip_file , binary_file = True )
479
+ profile_name = cfg .get ('profile' )
467
480
aws_access_key_id = cfg .get ('aws_access_key_id' )
468
481
aws_secret_access_key = cfg .get ('aws_secret_access_key' )
469
482
470
483
account_id = get_account_id (
471
- aws_access_key_id , aws_secret_access_key , cfg .get ('region' ),
484
+ profile_name , aws_access_key_id , aws_secret_access_key , cfg .get ('region' ),
472
485
)
473
486
role = get_role_name (
474
487
cfg .get ('region' ), account_id ,
475
488
cfg .get ('role' , 'lambda_basic_execution' ),
476
489
)
477
490
478
491
client = get_client (
479
- 'lambda' , aws_access_key_id , aws_secret_access_key ,
492
+ 'lambda' , profile_name , aws_access_key_id , aws_secret_access_key ,
480
493
cfg .get ('region' ),
481
494
)
482
495
@@ -531,24 +544,25 @@ def create_function(cfg, path_to_zip_file, *use_s3, **s3_file):
531
544
client .create_function (** kwargs )
532
545
533
546
534
- def update_function (cfg , path_to_zip_file , * use_s3 , ** s3_file ):
547
+ def update_function (cfg , path_to_zip_file , use_s3 = False , s3_file = None ):
535
548
"""Updates the code of an existing Lambda function"""
536
549
537
550
print ('Updating your Lambda function' )
538
551
byte_stream = read (path_to_zip_file , binary_file = True )
552
+ profile_name = cfg .get ('profile' )
539
553
aws_access_key_id = cfg .get ('aws_access_key_id' )
540
554
aws_secret_access_key = cfg .get ('aws_secret_access_key' )
541
555
542
556
account_id = get_account_id (
543
- aws_access_key_id , aws_secret_access_key , cfg .get ('region' ),
557
+ profile_name , aws_access_key_id , aws_secret_access_key , cfg .get ('region' ),
544
558
)
545
559
role = get_role_name (
546
560
cfg .get ('region' ), account_id ,
547
561
cfg .get ('role' , 'lambda_basic_execution' ),
548
562
)
549
563
550
564
client = get_client (
551
- 'lambda' , aws_access_key_id , aws_secret_access_key ,
565
+ 'lambda' , profile_name , aws_access_key_id , aws_secret_access_key ,
552
566
cfg .get ('region' ),
553
567
)
554
568
@@ -603,10 +617,11 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
603
617
"""Upload a function to AWS S3."""
604
618
605
619
print ('Uploading your new Lambda function' )
620
+ profile_name = cfg .get ('profile' )
606
621
aws_access_key_id = cfg .get ('aws_access_key_id' )
607
622
aws_secret_access_key = cfg .get ('aws_secret_access_key' )
608
623
client = get_client (
609
- 's3' , aws_access_key_id , aws_secret_access_key ,
624
+ 's3' , profile_name , aws_access_key_id , aws_secret_access_key ,
610
625
cfg .get ('region' ),
611
626
)
612
627
byte_stream = b''
@@ -641,10 +656,11 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
641
656
def function_exists (cfg , function_name ):
642
657
"""Check whether a function exists or not"""
643
658
659
+ profile_name = cfg .get ('profile' )
644
660
aws_access_key_id = cfg .get ('aws_access_key_id' )
645
661
aws_secret_access_key = cfg .get ('aws_secret_access_key' )
646
662
client = get_client (
647
- 'lambda' , aws_access_key_id , aws_secret_access_key ,
663
+ 'lambda' , profile_name , aws_access_key_id , aws_secret_access_key ,
648
664
cfg .get ('region' ),
649
665
)
650
666
@@ -663,3 +679,12 @@ def function_exists(cfg, function_name):
663
679
f ['FunctionName' ] for f in functions_resp .get ('Functions' , [])
664
680
])
665
681
return function_name in functions
682
+
683
+
684
+ def read_cfg (path_to_config_file , profile_name ):
685
+ cfg = read (path_to_config_file , loader = yaml .load )
686
+ if profile_name is not None :
687
+ cfg ['profile' ] = profile_name
688
+ elif 'AWS_PROFILE' in os .environ :
689
+ cfg ['profile' ] = os .environ ['AWS_PROFILE' ]
690
+ return cfg
0 commit comments