Skip to content
This repository was archived by the owner on Mar 15, 2026. It is now read-only.

Commit ad8f277

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 365f1e9 commit ad8f277

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
@@ -180,16 +181,22 @@ def upload(
180181

181182
def invoke(
182183
src, event_file='event.json',
183-
config_file='config.yaml', profile_name=None,
184+
config_file='config.yaml', profile_name=None, environment_file=None,
184185
verbose=False,
185186
):
186187
"""Simulates a call to your function.
187188
188189
:param str src:
189190
The path to your Lambda ready project (folder must contain a valid
190191
config.yaml and handler module (e.g.: service.py).
191-
:param str alt_event:
192+
:param str event_file:
192193
An optional argument to override which event file to use.
194+
:param str config_file:
195+
An optional argument to override which config file to use
196+
:param str profile_name:
197+
An optional argument to specify which AWS profile name to use
198+
:param str environment_file:
199+
An optional argument to specify a .env style enviroment file to load
193200
:param bool verbose:
194201
Whether to print out verbose details.
195202
"""
@@ -201,6 +208,10 @@ def invoke(
201208
if profile_name:
202209
os.environ['AWS_PROFILE'] = profile_name
203210

211+
# Load an env file if specified to initialize the environment
212+
if environment_file:
213+
load_dotenv(environment_file)
214+
204215
# Load environment variables from the config file into the actual
205216
# environment.
206217
env_vars = cfg.get('environment_variables')

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ futures==3.0.5
66
jmespath==0.9.0
77
pyaml==15.8.2
88
python-dateutil==2.5.3
9+
python-dotenv==0.8.2
910
PyYAML==3.11
1011
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(use_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)