Skip to content

Instantly share code, notes, and snippets.

@medwig
Last active July 18, 2019 14:26
Show Gist options
  • Save medwig/09ad7dc08e50237e69696672e728c05c to your computer and use it in GitHub Desktop.
Save medwig/09ad7dc08e50237e69696672e728c05c to your computer and use it in GitHub Desktop.
Bash alias run sls with --aws-profile
# Set default aws-profile
AWS_PROFILE=myprofile
# Run sls with an aws-profile
sls_with_aws_profile() {
if [ -z ${AWS_PROFILE+x} ];
then printf "\e[1;31mAWS_PROFILE is not set\e[0m\n$ export AWS_PROFILE=profile_name\n" \
&& return;
fi
export AWS_SDK_LOAD_CONFIG=true;
(>&2 printf "profile:\e[32m $AWS_PROFILE\e[0m\n\n";);
\sls --aws-profile $AWS_PROFILE "$@";
}
alias sls="sls_with_aws_profile"
@medwig
Copy link
Author

medwig commented Apr 15, 2019

Easily deploy to multiple AWS accounts with serverless framework. Set the AWS_PROFILE variable to the desired account name in ~/.aws/config (or ~/.aws/credentials) by running:
$ export AWS_PROFILE=target_profile

A default profile can be set in .bashrc if desired (line 2).

Commands can then be run as usual in serverless:
$ sls info
and it will be expanded to
$ sls --aws-profile info

The profile will be printed to stderr before each execution to make it clear what profile the account is running on. Printing to stderr means it will not interfere with piped output, for example to jq:
$ sls invoke -f fooFunc | jq
This call will print the profile, and the stdout of the call will be passes to jq, everything will work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment