It’s a good idea to rotate your AWS Access Keys regularly, here is a simple script that makes the process easy and painless. For a lot of engineers it’s a compliance requirement to rotate keys on a regular cadence, it also takes a little effort and requires us to remember.
The idea behind the script is simple:
Use current AWS Access Key to retrieve key(s) for current user, write them to a local file
Request a new key
Setup the new key on the local machine
Delete the old access key
Cleanup
To avoid having to remember to rotate regularly, or before I get the compliance naughty email I also have a cronjob configured to run weekly.
# This script will automate the rotation of AWS access keys, which should be rotated regularly for security and compliance reasons
# TO USE:
# Update the following variables: # - USER - Your AWS user name # - LOCAL_BASE_PATH - The location of the .aws folder for your user. Will contain a config and credentials file
# Set some variables USER='' LOCAL_BASE_PATH=''
AWS="docker run --rm -it -v $LOCAL_BASE_PATH:/root/.aws amazon/aws-cli"