Skip to content

Commit 65de76f

Browse files
committed
Merge branch 'master' of github.com:nficano/python-lambda
* 'master' of github.com:nficano/python-lambda: Bump version: 3.1.0 → 3.1.1 Bump version: 3.0.3 → 3.1.0 Add support for `--profile` command-line argument. fix for handling S3 lambda deployment arguments .gitignore: Ignore Vim swap files.
2 parents 52b1e5c + 3a05ca2 commit 65de76f

File tree

6 files changed

+112
-41
lines changed

6 files changed

+112
-41
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,6 @@ target/
6060

6161
# Jetbrains/PyCharm project files
6262
.idea/
63+
64+
# vim swap files
65+
.*.sw?

aws_lambda/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# flake8: noqa
33
__author__ = 'Nick Ficano'
44
__email__ = '[email protected]'
5-
__version__ = '3.0.3'
5+
__version__ = '3.1.1'
66

77
from .aws_lambda import deploy, deploy_s3, invoke, init, build, upload, cleanup_old_versions
88

aws_lambda/aws_lambda.py

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
log = logging.getLogger(__name__)
3434

3535

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+
):
3740
"""Deletes old deployed versions of the function in AWS Lambda.
3841
3942
Won't delete $Latest and any aliased version
@@ -48,13 +51,14 @@ def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):
4851
print("Won't delete all versions. Please do this manually")
4952
else:
5053
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)
5255

56+
profile_name = cfg.get('profile')
5357
aws_access_key_id = cfg.get('aws_access_key_id')
5458
aws_secret_access_key = cfg.get('aws_secret_access_key')
5559

5660
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,
5862
cfg.get('region'),
5963
)
6064

@@ -80,7 +84,7 @@ def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):
8084

8185
def deploy(
8286
src, use_requirements=False, local_package=None,
83-
config_file='config.yaml',
87+
config_file='config.yaml', profile_name=None,
8488
):
8589
"""Deploys a new function to AWS Lambda.
8690
@@ -93,7 +97,7 @@ def deploy(
9397
"""
9498
# Load and parse the config file.
9599
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)
97101

98102
# Copy all the pip dependencies required to run your code into a temporary
99103
# folder then add the handler file in the root of this directory.
@@ -112,7 +116,8 @@ def deploy(
112116

113117

114118
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,
116121
):
117122
"""Deploys a new function via AWS S3.
118123
@@ -125,7 +130,7 @@ def deploy_s3(
125130
"""
126131
# Load and parse the config file.
127132
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)
129134

130135
# Copy all the pip dependencies required to run your code into a temporary
131136
# folder then add the handler file in the root of this directory.
@@ -139,14 +144,14 @@ def deploy_s3(
139144
use_s3 = True
140145
s3_file = upload_s3(cfg, path_to_zip_file, use_s3)
141146
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)
143148
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)
145150

146151

147152
def upload(
148153
src, use_requirements=False, local_package=None,
149-
config_file='config.yaml',
154+
config_file='config.yaml', profile_name=None,
150155
):
151156
"""Uploads a new function to AWS S3.
152157
@@ -159,7 +164,7 @@ def upload(
159164
"""
160165
# Load and parse the config file.
161166
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)
163168

164169
# Copy all the pip dependencies required to run your code into a temporary
165170
# folder then add the handler file in the root of this directory.
@@ -174,7 +179,8 @@ def upload(
174179

175180

176181
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,
178184
verbose=False,
179185
):
180186
"""Simulates a call to your function.
@@ -189,7 +195,11 @@ def invoke(
189195
"""
190196
# Load and parse the config file.
191197
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
193203

194204
# Load environment variables from the config file into the actual
195205
# environment.
@@ -248,7 +258,8 @@ def init(src, minimal=False):
248258

249259

250260
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,
252263
):
253264
"""Builds the file bundle.
254265
@@ -261,7 +272,7 @@ def build(
261272
"""
262273
# Load and parse the config file.
263274
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)
265276

266277
# Get the absolute path to the output directory and create it if it doesn't
267278
# already exist.
@@ -439,44 +450,46 @@ def get_role_name(region, account_id, role):
439450
return 'arn:{0}:iam::{1}:role/{2}'.format(prefix, account_id, role)
440451

441452

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):
443454
"""Query STS for a users' account_id"""
444455
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,
446457
region,
447458
)
448459
return client.get_caller_identity().get('Account')
449460

450461

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):
452463
"""Shortcut for getting an initialized instance of the boto3 client."""
453464

454-
return boto3.client(
455-
client,
465+
boto3.setup_default_session(
466+
profile_name=profile_name,
456467
aws_access_key_id=aws_access_key_id,
457468
aws_secret_access_key=aws_secret_access_key,
458469
region_name=region,
459470
)
471+
return boto3.client(client)
460472

461473

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):
463475
"""Register and upload a function to AWS Lambda."""
464476

465477
print('Creating your new Lambda function')
466478
byte_stream = read(path_to_zip_file, binary_file=True)
479+
profile_name = cfg.get('profile')
467480
aws_access_key_id = cfg.get('aws_access_key_id')
468481
aws_secret_access_key = cfg.get('aws_secret_access_key')
469482

470483
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'),
472485
)
473486
role = get_role_name(
474487
cfg.get('region'), account_id,
475488
cfg.get('role', 'lambda_basic_execution'),
476489
)
477490

478491
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,
480493
cfg.get('region'),
481494
)
482495

@@ -531,24 +544,25 @@ def create_function(cfg, path_to_zip_file, *use_s3, **s3_file):
531544
client.create_function(**kwargs)
532545

533546

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):
535548
"""Updates the code of an existing Lambda function"""
536549

537550
print('Updating your Lambda function')
538551
byte_stream = read(path_to_zip_file, binary_file=True)
552+
profile_name = cfg.get('profile')
539553
aws_access_key_id = cfg.get('aws_access_key_id')
540554
aws_secret_access_key = cfg.get('aws_secret_access_key')
541555

542556
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'),
544558
)
545559
role = get_role_name(
546560
cfg.get('region'), account_id,
547561
cfg.get('role', 'lambda_basic_execution'),
548562
)
549563

550564
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,
552566
cfg.get('region'),
553567
)
554568

@@ -603,10 +617,11 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
603617
"""Upload a function to AWS S3."""
604618

605619
print('Uploading your new Lambda function')
620+
profile_name = cfg.get('profile')
606621
aws_access_key_id = cfg.get('aws_access_key_id')
607622
aws_secret_access_key = cfg.get('aws_secret_access_key')
608623
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,
610625
cfg.get('region'),
611626
)
612627
byte_stream = b''
@@ -641,10 +656,11 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
641656
def function_exists(cfg, function_name):
642657
"""Check whether a function exists or not"""
643658

659+
profile_name = cfg.get('profile')
644660
aws_access_key_id = cfg.get('aws_access_key_id')
645661
aws_secret_access_key = cfg.get('aws_secret_access_key')
646662
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,
648664
cfg.get('region'),
649665
)
650666

@@ -663,3 +679,12 @@ def function_exists(cfg, function_name):
663679
f['FunctionName'] for f in functions_resp.get('Functions', [])
664680
])
665681
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

Comments
 (0)