Skip to content

Commit 9c2ebd1

Browse files
committed
Merge branch 'master' of github.com:nficano/python-lambda
* 'master' of github.com:nficano/python-lambda: Cleaned up some verbiage in the env. variables section Cleaning up the documentation now that the pull was done. Updated documentation with install info Adding private environment variable values for deploy - supports non-hard coded values in the config Adding Python3.6 support for local and runtime environments Bump version: 0.8.2 → 0.8.3 Bump version: 0.8.1 → 0.8.2
2 parents 6796d14 + 3f6ad0c commit 9c2ebd1

File tree

8 files changed

+31
-13
lines changed

8 files changed

+31
-13
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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The *Python-Lambda* library takes away the guess work of developing your Python-
2828
Requirements
2929
============
3030

31-
* Python 2.7 (At the time of writing this, AWS Lambda only supports Python 2.7).
31+
* Python 2.7 & 3.6 (At the time of writing this, AWS Lambda only supports Python 2.7/3.6).
3232
* Pip (~8.1.1)
3333
* Virtualenv (~15.0.0)
3434
* Virtualenvwrapper (~4.7.1)
@@ -153,13 +153,15 @@ Now try and run:
153153
154154
Environment Variables
155155
=====================
156-
Lambda functions support environment variables. In order to set environment variables for your deployed code to use, you can configure them in ``config.yaml``
156+
Lambda functions support environment variables. In order to set environment variables for your deployed code to use, you can configure them in ``config.yaml``. To load the
157+
value for the environment variable at the time of deployment (instead of hard coding them in your configuration file), you can use local environment values (see 'env3' in example code below).
157158

158159
.. code:: yaml
159160
160161
environment_variables:
161162
env1: foo
162163
env2: baz
164+
env3: ${LOCAL_ENVIRONMENT_VARIABLE_NAME}
163165
164166
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.
165167

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__ = '0.8.1'
5+
__version__ = '0.8.3'
66

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

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

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[bumpversion]
22
commit = True
33
tag = True
4-
current_version = 0.8.1
4+
current_version = 0.8.3
55
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\-(?P<release>[a-z]+))?
66
serialize =
77
{major}.{minor}.{patch}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
setup(
2020
name='python-lambda',
21-
version='0.8.1',
21+
version='0.8.3',
2222
description='The bare minimum for a Python app running on Amazon Lambda.',
2323
long_description=readme,
2424
author='Nick Ficano',

0 commit comments

Comments
 (0)