Skip to content

Commit 65f35f0

Browse files
committed
Adding private environment variable values for deploy - supports non-hard coded values in the config
1 parent 75a0539 commit 65f35f0

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

README.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ python-λ
1313
Python-lambda is a toolset for developing and deploying *serverless* Python code in AWS Lambda.
1414

1515
NOTE: CHANGES FROM BASE REPOSITORY
16-
============================
16+
==================================
1717

1818
* 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.
1920

2021

2122
A call for contributors
@@ -167,6 +168,13 @@ Lambda functions support environment variables. In order to set environment vari
167168
env1: foo
168169
env2: baz
169170
171+
You can also keep "secrets" out of your config file by using the following syntax ``${}`` to read the values from your current/local environment.
172+
173+
.. code:: yaml
174+
175+
environment_variables:
176+
env3: ${LOCAL_ENVIRONMENT_VARIABLE_NAME}
177+
170178
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.
171179

172180

aws_lambda/aws_lambda.py

Lines changed: 3 additions & 2 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__)
@@ -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
}
@@ -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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import zipfile
44
import datetime as dt
5-
5+
import re
66

77
def mkdir(path):
88
if not os.path.exists(path):
@@ -31,3 +31,12 @@ def archive(src, dest, filename):
3131
def timestamp(fmt='%Y-%m-%d-%H%M%S'):
3232
now = dt.datetime.utcnow()
3333
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

0 commit comments

Comments
 (0)