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 @@ -60,3 +60,6 @@ target/

# Jetbrains/PyCharm project files
.idea/

# vim swap files
.*.sw?
73 changes: 49 additions & 24 deletions aws_lambda/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
log = logging.getLogger(__name__)


def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):
def cleanup_old_versions(
src, keep_last_versions,
config_file='config.yaml', profile_name=None,
):
"""Deletes old deployed versions of the function in AWS Lambda.

Won't delete $Latest and any aliased version
Expand All @@ -48,13 +51,14 @@ def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):
print("Won't delete all versions. Please do this manually")
else:
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)
cfg = read_cfg(path_to_config_file, profile_name)

profile_name = cfg.get('profile')
aws_access_key_id = cfg.get('aws_access_key_id')
aws_secret_access_key = cfg.get('aws_secret_access_key')

client = get_client(
'lambda', aws_access_key_id, aws_secret_access_key,
'lambda', profile_name, aws_access_key_id, aws_secret_access_key,
cfg.get('region'),
)

Expand All @@ -80,7 +84,7 @@ def cleanup_old_versions(src, keep_last_versions, config_file='config.yaml'):

def deploy(
src, use_requirements=False, local_package=None,
config_file='config.yaml',
config_file='config.yaml', profile_name=None,
):
"""Deploys a new function to AWS Lambda.

Expand All @@ -93,7 +97,7 @@ def deploy(
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)
cfg = read_cfg(path_to_config_file, profile_name)

# Copy all the pip dependencies required to run your code into a temporary
# folder then add the handler file in the root of this directory.
Expand All @@ -112,7 +116,8 @@ def deploy(


def deploy_s3(
src, use_requirements=False, local_package=None, config_file='config.yaml',
src, use_requirements=False, local_package=None,
config_file='config.yaml', profile_name=None,
):
"""Deploys a new function via AWS S3.

Expand All @@ -125,7 +130,7 @@ def deploy_s3(
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)
cfg = read_cfg(path_to_config_file, profile_name)

# Copy all the pip dependencies required to run your code into a temporary
# folder then add the handler file in the root of this directory.
Expand All @@ -146,7 +151,7 @@ def deploy_s3(

def upload(
src, use_requirements=False, local_package=None,
config_file='config.yaml',
config_file='config.yaml', profile_name=None,
):
"""Uploads a new function to AWS S3.

Expand All @@ -159,7 +164,7 @@ def upload(
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)
cfg = read_cfg(path_to_config_file, profile_name)

# Copy all the pip dependencies required to run your code into a temporary
# folder then add the handler file in the root of this directory.
Expand All @@ -174,7 +179,8 @@ def upload(


def invoke(
src, event_file='event.json', config_file='config.yaml',
src, event_file='event.json',
config_file='config.yaml', profile_name=None,
verbose=False,
):
"""Simulates a call to your function.
Expand All @@ -189,7 +195,11 @@ def invoke(
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)
cfg = read_cfg(path_to_config_file, profile_name)

# Set AWS_PROFILE environment variable based on `--profile` option.
if profile_name:
os.environ['AWS_PROFILE'] = profile_name

# Load environment variables from the config file into the actual
# environment.
Expand Down Expand Up @@ -248,7 +258,8 @@ def init(src, minimal=False):


def build(
src, use_requirements=False, local_package=None, config_file='config.yaml',
src, use_requirements=False, local_package=None,
config_file='config.yaml', profile_name=None,
):
"""Builds the file bundle.

Expand All @@ -261,7 +272,7 @@ def build(
"""
# Load and parse the config file.
path_to_config_file = os.path.join(src, config_file)
cfg = read(path_to_config_file, loader=yaml.load)
cfg = read_cfg(path_to_config_file, profile_name)

# Get the absolute path to the output directory and create it if it doesn't
# already exist.
Expand Down Expand Up @@ -439,44 +450,46 @@ def get_role_name(region, account_id, role):
return 'arn:{0}:iam::{1}:role/{2}'.format(prefix, account_id, role)


def get_account_id(aws_access_key_id, aws_secret_access_key, region=None):
def get_account_id(profile_name, aws_access_key_id, aws_secret_access_key, region=None):
"""Query STS for a users' account_id"""
client = get_client(
'sts', aws_access_key_id, aws_secret_access_key,
'sts', profile_name, aws_access_key_id, aws_secret_access_key,
region,
)
return client.get_caller_identity().get('Account')


def get_client(client, aws_access_key_id, aws_secret_access_key, region=None):
def get_client(client, profile_name, aws_access_key_id, aws_secret_access_key, region=None):
"""Shortcut for getting an initialized instance of the boto3 client."""

return boto3.client(
client,
boto3.setup_default_session(
profile_name=profile_name,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a profile name is set, should secret\access keys still be set?

aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
region_name=region,
)
return boto3.client(client)


def create_function(cfg, path_to_zip_file, *use_s3, **s3_file):
"""Register and upload a function to AWS Lambda."""

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

account_id = get_account_id(
aws_access_key_id, aws_secret_access_key, cfg.get('region'),
profile_name, aws_access_key_id, aws_secret_access_key, cfg.get('region'),
)
role = get_role_name(
cfg.get('region'), account_id,
cfg.get('role', 'lambda_basic_execution'),
)

client = get_client(
'lambda', aws_access_key_id, aws_secret_access_key,
'lambda', profile_name, aws_access_key_id, aws_secret_access_key,
cfg.get('region'),
)

Expand Down Expand Up @@ -536,19 +549,20 @@ def update_function(cfg, path_to_zip_file, *use_s3, **s3_file):

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

account_id = get_account_id(
aws_access_key_id, aws_secret_access_key, cfg.get('region'),
profile_name, aws_access_key_id, aws_secret_access_key, cfg.get('region'),
)
role = get_role_name(
cfg.get('region'), account_id,
cfg.get('role', 'lambda_basic_execution'),
)

client = get_client(
'lambda', aws_access_key_id, aws_secret_access_key,
'lambda', profile_name, aws_access_key_id, aws_secret_access_key,
cfg.get('region'),
)

Expand Down Expand Up @@ -603,10 +617,11 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
"""Upload a function to AWS S3."""

print('Uploading your new Lambda function')
profile_name = cfg.get('profile')
aws_access_key_id = cfg.get('aws_access_key_id')
aws_secret_access_key = cfg.get('aws_secret_access_key')
client = get_client(
's3', aws_access_key_id, aws_secret_access_key,
's3', profile_name, aws_access_key_id, aws_secret_access_key,
cfg.get('region'),
)
byte_stream = b''
Expand Down Expand Up @@ -641,10 +656,11 @@ def upload_s3(cfg, path_to_zip_file, *use_s3):
def function_exists(cfg, function_name):
"""Check whether a function exists or not"""

profile_name = cfg.get('profile')
aws_access_key_id = cfg.get('aws_access_key_id')
aws_secret_access_key = cfg.get('aws_secret_access_key')
client = get_client(
'lambda', aws_access_key_id, aws_secret_access_key,
'lambda', profile_name, aws_access_key_id, aws_secret_access_key,
cfg.get('region'),
)

Expand All @@ -663,3 +679,12 @@ def function_exists(cfg, function_name):
f['FunctionName'] for f in functions_resp.get('Functions', [])
])
return function_name in functions


def read_cfg(path_to_config_file, profile_name):
cfg = read(path_to_config_file, loader=yaml.load)
if profile_name is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of:
if profile_name is not None:

Couldn't you just do:
if profile_name:

cfg['profile'] = profile_name
elif 'AWS_PROFILE' in os.environ:
cfg['profile'] = os.environ['AWS_PROFILE']
return cfg
Loading