Skip to content

Commit 4fd64e9

Browse files
committed
Added an option to invoke() that allows you to specify an environment file to be loaded using python-dotenv. This will make it easier to support multiple configuration environments and parameterized environment variables in the config.yaml file
1 parent 4c4d6ab commit 4fd64e9

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

aws_lambda/aws_lambda.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import botocore
1919
import pip
2020
import yaml
21+
from dotenv import load_dotenv
2122

2223
from .helpers import archive
2324
from .helpers import get_environment_variable_value
@@ -183,16 +184,22 @@ def upload(
183184

184185
def invoke(
185186
src, event_file='event.json',
186-
config_file='config.yaml', profile_name=None,
187+
config_file='config.yaml', profile_name=None, environment_file=None,
187188
verbose=False,
188189
):
189190
"""Simulates a call to your function.
190191
191192
:param str src:
192193
The path to your Lambda ready project (folder must contain a valid
193194
config.yaml and handler module (e.g.: service.py).
194-
:param str alt_event:
195+
:param str event_file:
195196
An optional argument to override which event file to use.
197+
:param str config_file:
198+
An optional argument to override which config file to use
199+
:param str profile_name:
200+
An optional argument to specify which AWS profile name to use
201+
:param str environment_file:
202+
An optional argument to specify a .env style enviroment file to load
196203
:param bool verbose:
197204
Whether to print out verbose details.
198205
"""
@@ -204,6 +211,10 @@ def invoke(
204211
if profile_name:
205212
os.environ['AWS_PROFILE'] = profile_name
206213

214+
# Load an env file if specified to initialize the environment
215+
if environment_file:
216+
load_dotenv(environment_file)
217+
207218
# Load environment variables from the config file into the actual
208219
# environment.
209220
env_vars = cfg.get('environment_variables')

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ docutils==0.12
55
jmespath==0.9.0
66
pyaml==15.8.2
77
python-dateutil==2.5.3
8+
python-dotenv==0.8.2
89
PyYAML==3.11
910
six==1.10.0

scripts/lambda

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,19 @@ def build(requirements, local_package, config_file, profile):
8585
'--profile',
8686
help='AWS profile to use.',
8787
)
88+
@click.option(
89+
'--environment-file',
90+
default=None,
91+
help='.env file used to load environment before invocation'
92+
)
8893
@click.option('--verbose', '-v', is_flag=True)
89-
def invoke(event_file, config_file, profile, verbose):
94+
def invoke(event_file, config_file, profile, environment_file, verbose):
9095
aws_lambda.invoke(
9196
CURRENT_DIR,
9297
event_file=event_file,
9398
config_file=config_file,
9499
profile_name=profile,
100+
environment_file=environment_file,
95101
verbose=verbose,
96102
)
97103

0 commit comments

Comments
 (0)