Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@ docs/_build/

# PyBuilder
target/

# Jetbrains/PyCharm project files
.idea/
17 changes: 16 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ python-λ

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

NOTE: CHANGES FROM BASE REPOSITORY
==================================

* Adding Python 3.6 support for the local environment & the lambda runtime
* Supports "secret" environment variable values by reading them from the local environment during deploy instead of using hard-coded ones in the config file.
* You can install this version via pip with ``pip install --editable git+https://github.com/asaolabs/python-lambda#egg=python-lambda``


A call for contributors
=======================
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.
Expand All @@ -28,7 +36,7 @@ The *Python-Lambda* library takes away the guess work of developing your Python-
Requirements
============

* Python 2.7 (At the time of writing this, AWS Lambda only supports Python 2.7).
* Python 2.7 & 3.6 (At the time of writing this, AWS Lambda only supports Python 2.7/3.6).
* Pip (~8.1.1)
* Virtualenv (~15.0.0)
* Virtualenvwrapper (~4.7.1)
Expand Down Expand Up @@ -161,6 +169,13 @@ Lambda functions support environment variables. In order to set environment vari
env1: foo
env2: baz

You can also keep "secrets" out of your config file by using the following syntax ``${}`` to read the values from your current/local environment.

.. code:: yaml

environment_variables:
env3: ${LOCAL_ENVIRONMENT_VARIABLE_NAME}

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.


Expand Down
9 changes: 5 additions & 4 deletions aws_lambda/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .helpers import mkdir
from .helpers import read
from .helpers import timestamp
from .helpers import get_environment_variable_value


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

print('Creating your new Lambda function')
byte_stream = read(path_to_zip_file)
byte_stream = read(path_to_zip_file, binary_file=True)
aws_access_key_id = cfg.get('aws_access_key_id')
aws_secret_access_key = cfg.get('aws_secret_access_key')

Expand Down Expand Up @@ -375,7 +376,7 @@ def create_function(cfg, path_to_zip_file):
kwargs.update(
Environment={
'Variables': {
key: value
key: get_environment_variable_value(value)
for key, value
in cfg.get('environment_variables').items()
}
Expand All @@ -389,7 +390,7 @@ def update_function(cfg, path_to_zip_file):
"""Updates the code of an existing Lambda function"""

print('Updating your Lambda function')
byte_stream = read(path_to_zip_file)
byte_stream = read(path_to_zip_file, binary_file=True)
aws_access_key_id = cfg.get('aws_access_key_id')
aws_secret_access_key = cfg.get('aws_secret_access_key')

Expand Down Expand Up @@ -422,7 +423,7 @@ def update_function(cfg, path_to_zip_file):
kwargs.update(
Environment={
'Variables': {
key: value
key: get_environment_variable_value(value)
for key, value
in cfg.get('environment_variables').items()
}
Expand Down
18 changes: 14 additions & 4 deletions aws_lambda/helpers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# -*- coding: utf-8 -*-
import datetime as dt
import os
import zipfile

import datetime as dt
import re

def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)


def read(path, loader=None):
with open(path) as fh:
def read(path, loader=None, binary_file=False):
open_mode = 'rb' if binary_file else 'r'
with open(path, mode=open_mode) as fh:
if not loader:
return fh.read()
return loader(fh.read())
Expand All @@ -30,3 +31,12 @@ def archive(src, dest, filename):
def timestamp(fmt='%Y-%m-%d-%H%M%S'):
now = dt.datetime.utcnow()
return now.strftime(fmt)


def get_environment_variable_value(val):
env_val = val
if val is not None and isinstance(val, str):
match = re.search(r'^\${(?P<environment_key_name>\w+)*}$', val)
if match is not None:
env_val = os.environ.get(match.group('environment_key_name'))
return env_val
2 changes: 2 additions & 0 deletions aws_lambda/project_templates/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function_name: my_lambda_function
handler: service.handler
# role: lambda_basic_execution
description: My first lambda function
runtime: python2.7

# if access key and secret are left blank, boto will use the credentials
# defined in the [default] section of ~/.aws/credentials.
Expand All @@ -14,6 +15,7 @@ aws_secret_access_key:
# timeout: 15
# memory_size: 512
#

# Experimental Environment variables
environment_variables:
env_1: foo
Expand Down