Have you ever tried to maintain services between environments and got lost in all the variables? Or even broken things by accident? Today I'm going to give you some tips on how to speed up your workflow and make things easier. My focus will be mainly on the backend, as I work with it a lot.
Multiple Git configurations
There may be a situation where you need to switch between different git repositories, each of which has its own set of credentials (or ssh keys). Even if you only have one set of credentials for all repositories, at least for your personal git account(for side or open source projects), it is always good for security to have them different from the ones you use at work. We don't want our work to be compromised, do we? ๐
Let's start by setting up your Git profile, assuming we need to set up two profiles, one for work and one for personal use.
Custom Git configurations for individuals
Set up a personal profile by creating a file called .gitconfig-personal
in your home directory ($HOME) and filling it with the following content. Make sure you use your own username and email for your git account.
[user]
name = <put your personal username here>
email = <put your personal email here>
We will configure the profile for your work and save it under a different name, such as .gitconfig-work
, just as we did in the previous step. Also put it in your home directory like this $HOME/.gitconfig-work
.
[user]
name = <put your work username here>
email = <put your work email here>
Git global configurations
Last but not least, we will use the includeIf
sections to specify which profiles we want git to use in which directories. This will allow us to seamlessly switch between git profiles depending on your working directory or workspace. The only thing we would need to do is edit the following content with the appropriate values, depending on what you need, and put it in .gitconfig
in your home directory ($HOME). You are ready to go.
[includeIf "gitdir:~/personal/"]
path = ~/.gitconfig-personal
[includeIf "gitdir:~/work/"]
path = ~/.gitconfig-work
Assuming that we don't have any local level configurations for Git that would override the global level we've just set (to see more detail here), we can now try to verify that by running the command git config -l
.
$ cd ~/personal/personal_git_repo
$ git config -l | grep user
user.name=<personal username will appear here>
user.email=<personal email will appear here>
$ cd ~/work/work_git_repo
$ git config -l | grep user
user.name=<work username will appear here>
user.email=<work email will appear here>
Ta-da ๐ฅณ We can now switch between a number of different custom git configuration files, depending on the path of the git repository we are working with.
Named profiles for the AWS Command-line interface (CLI)
I always feel that if we can find a way to quickly move between different cloud-native environments, it can make our life a lot easier. It can also prevent us from accidentally breaking things.
What is a named profile?
A named profile is a collection of settings and credentials that we can apply to a AWS CLI command. When we specify a profile to run a command, the settings and credentials are used to run that command. Multiple named profiles can be stored in the config and credentials files.
In the following example we specify user1 except for the default profile one and save it as ~/.aws/config
[default]
region=eu-west-2
output=json
# Include the prefix "profile" only when configuring a named profile in the config file
[profile user1]
region=us-east-1
output=json
AWS credentials
Next thing is to configure credentials and allow the AWS CLI tool to know which credentials should be used for each specified profile. We save it as ~/.aws/credentials
[default]
aws_access_key_id=<put your access key id here>
aws_secret_access_key=<put your secret access key here>
[user1] # Do not use the prefix "profile" here in crendetials
aws_access_key_id=<put your access key id here>
aws_secret_access_key=<put your secret access key here>
Verify that the profile is successfully added by aws configure list-profiles
. You should now see 2 profiles like below:
default
user1
To switch between different AWS accounts, set the environment variable AWS_PROFILE
at the command line with export AWS_PROFILE=<put your profile name here>
. Setting the env variable will change the default profile until the end of your shell session or until you set the variable to a different value.
Use the aws configure list
command to list your current profile and show the details of it as shown below:
Name Value Type Location
---- ----- ---- --------
profile user1 env ['AWS_PROFILE', 'AWS_DEFAULT_PROFILE']
access_key ****************FPFQ shared-credentials-file
secret_key ****************a5K9 shared-credentials-file
region us-east-1 env ['AWS_REGION', 'AWS_DEFAULT_REGION']
We can go further by running aws sts get-caller-identity
to get the identity of the caller and make sure you are using the correct identity to run the command.
Account: 'aws_account_id'
Arn: arn:aws:iam::aws_account_id:user/user1
UserId: ****************KPM7
Great! Now you can switch between the different name profiles with ease ๐
Conclusion
This article has given us some tips on how to optimise the tools we use to speed up our workflow.
Hopefully you found something useful and can apply it to your work.
Thank you very much for reading.