-
Notifications
You must be signed in to change notification settings - Fork 226
Add support for --profile command-line argument
#93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,3 +60,6 @@ target/ | |
|
|
||
| # Jetbrains/PyCharm project files | ||
| .idea/ | ||
|
|
||
| # vim swap files | ||
| .*.sw? | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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'), | ||
| ) | ||
|
|
||
|
|
@@ -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. | ||
|
|
||
|
|
@@ -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. | ||
|
|
@@ -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. | ||
|
|
||
|
|
@@ -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. | ||
|
|
@@ -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. | ||
|
|
||
|
|
@@ -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. | ||
|
|
@@ -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. | ||
|
|
@@ -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. | ||
|
|
@@ -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. | ||
|
|
||
|
|
@@ -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. | ||
|
|
@@ -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, | ||
| 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'), | ||
| ) | ||
|
|
||
|
|
@@ -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'), | ||
| ) | ||
|
|
||
|
|
@@ -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'' | ||
|
|
@@ -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'), | ||
| ) | ||
|
|
||
|
|
@@ -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: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of: Couldn't you just do: |
||
| cfg['profile'] = profile_name | ||
| elif 'AWS_PROFILE' in os.environ: | ||
| cfg['profile'] = os.environ['AWS_PROFILE'] | ||
| return cfg | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?