Skip to content

Commit 38d43ad

Browse files
authored
Merge pull request nficano#53 from asaolabs/master
Support for Python 3.6 & removing requirement to store secrets/environment values in the config file
2 parents 7ebee04 + 946367f commit 38d43ad

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ docs/_build/
5757

5858
# PyBuilder
5959
target/
60+
61+
# Jetbrains/PyCharm project files
62+
.idea/

README.rst

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ python-λ
1212

1313
Python-lambda is a toolset for developing and deploying *serverless* Python code in AWS Lambda.
1414

15+
NOTE: CHANGES FROM BASE REPOSITORY
16+
==================================
17+
18+
* Adding Python 3.6 support for the local environment & the lambda runtime
19+
* Supports "secret" environment variable values by reading them from the local environment during deploy instead of using hard-coded ones in the config file.
20+
* You can install this version via pip with ``pip install --editable git+https://github.com/asaolabs/python-lambda#egg=python-lambda``
21+
22+
1523
A call for contributors
1624
=======================
1725
With python-lambda and `pytube <https://github.com/nficano/pytube/>`_ both continuing to gain momentum, I'm calling for contributors to help build out new features, review pull requests, fix bugs, and maintain overall code quality. If you're interested, please email me at nficano[at]gmail.com.
@@ -28,7 +36,7 @@ The *Python-Lambda* library takes away the guess work of developing your Python-
2836
Requirements
2937
============
3038

31-
* Python 2.7 (At the time of writing this, AWS Lambda only supports Python 2.7).
39+
* Python 2.7 & 3.6 (At the time of writing this, AWS Lambda only supports Python 2.7/3.6).
3240
* Pip (~8.1.1)
3341
* Virtualenv (~15.0.0)
3442
* Virtualenvwrapper (~4.7.1)
@@ -161,6 +169,13 @@ Lambda functions support environment variables. In order to set environment vari
161169
env1: foo
162170
env2: baz
163171
172+
You can also keep "secrets" out of your config file by using the following syntax ``${}`` to read the values from your current/local environment.
173+
174+
.. code:: yaml
175+
176+
environment_variables:
177+
env3: ${LOCAL_ENVIRONMENT_VARIABLE_NAME}
178+
164179
This would create environment variables in the lambda instance upon deploy. If your functions don't need environment variables, simply leave this section out of your config.
165180

166181

aws_lambda/aws_lambda.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from .helpers import mkdir
2121
from .helpers import read
2222
from .helpers import timestamp
23+
from .helpers import get_environment_variable_value
2324

2425

2526
log = logging.getLogger(__name__)
@@ -344,7 +345,7 @@ def create_function(cfg, path_to_zip_file):
344345
"""Register and upload a function to AWS Lambda."""
345346

346347
print('Creating your new Lambda function')
347-
byte_stream = read(path_to_zip_file)
348+
byte_stream = read(path_to_zip_file, binary_file=True)
348349
aws_access_key_id = cfg.get('aws_access_key_id')
349350
aws_secret_access_key = cfg.get('aws_secret_access_key')
350351

@@ -375,7 +376,7 @@ def create_function(cfg, path_to_zip_file):
375376
kwargs.update(
376377
Environment={
377378
'Variables': {
378-
key: value
379+
key: get_environment_variable_value(value)
379380
for key, value
380381
in cfg.get('environment_variables').items()
381382
}
@@ -389,7 +390,7 @@ def update_function(cfg, path_to_zip_file):
389390
"""Updates the code of an existing Lambda function"""
390391

391392
print('Updating your Lambda function')
392-
byte_stream = read(path_to_zip_file)
393+
byte_stream = read(path_to_zip_file, binary_file=True)
393394
aws_access_key_id = cfg.get('aws_access_key_id')
394395
aws_secret_access_key = cfg.get('aws_secret_access_key')
395396

@@ -422,7 +423,7 @@ def update_function(cfg, path_to_zip_file):
422423
kwargs.update(
423424
Environment={
424425
'Variables': {
425-
key: value
426+
key: get_environment_variable_value(value)
426427
for key, value
427428
in cfg.get('environment_variables').items()
428429
}

aws_lambda/helpers.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
# -*- coding: utf-8 -*-
2-
import datetime as dt
32
import os
43
import zipfile
5-
4+
import datetime as dt
5+
import re
66

77
def mkdir(path):
88
if not os.path.exists(path):
99
os.makedirs(path)
1010

1111

12-
def read(path, loader=None):
13-
with open(path) as fh:
12+
def read(path, loader=None, binary_file=False):
13+
open_mode = 'rb' if binary_file else 'r'
14+
with open(path, mode=open_mode) as fh:
1415
if not loader:
1516
return fh.read()
1617
return loader(fh.read())
@@ -30,3 +31,12 @@ def archive(src, dest, filename):
3031
def timestamp(fmt='%Y-%m-%d-%H%M%S'):
3132
now = dt.datetime.utcnow()
3233
return now.strftime(fmt)
34+
35+
36+
def get_environment_variable_value(val):
37+
env_val = val
38+
if val is not None and isinstance(val, str):
39+
match = re.search(r'^\${(?P<environment_key_name>\w+)*}$', val)
40+
if match is not None:
41+
env_val = os.environ.get(match.group('environment_key_name'))
42+
return env_val

aws_lambda/project_templates/config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ function_name: my_lambda_function
44
handler: service.handler
55
# role: lambda_basic_execution
66
description: My first lambda function
7+
runtime: python2.7
78

89
# if access key and secret are left blank, boto will use the credentials
910
# defined in the [default] section of ~/.aws/credentials.
@@ -14,6 +15,7 @@ aws_secret_access_key:
1415
# timeout: 15
1516
# memory_size: 512
1617
#
18+
1719
# Experimental Environment variables
1820
environment_variables:
1921
env_1: foo

0 commit comments

Comments
 (0)