Guide · AWS
Use short-lived AWS credentials locally
A long-lived AWS access key in your shell or an env file works until someone deletes it. A command that only needs to run for a few minutes should get credentials that stop working on their own.
In shortRun lk run --aws-session --env shop/development -- terraform plan. Latchkey signs a request to AWS STS with your stored access key and gives the command the temporary credentials that come back, valid for 15 minutes by default. Add --aws-role ARN to assume a role instead.
Why the long-lived key is the problem
Put a long-lived access key in ~/.zshrc, a .env file or a CI log and it keeps working, for anyone who has a copy, until you rotate it. Most local commands, a Terraform plan or a deploy script, run for minutes. A credential that outlives them is more than they need.
The alternative is temporary credentials from AWS STS. Latchkey requests them for you and hands them to one command, so the long-lived key can stay in iCloud Keychain. Each run gets a fresh set, so there is nothing to refresh by hand and nothing to clean up afterwards.
Set it up
-
Store the long-lived key as references
The two halves of your access key are items in an environment, referenced by the names AWS tools look for. Import them from an env file, or save each one with
lk set. Both variables must belatchkey://references..env.local AWS_ACCESS_KEY_ID=latchkey://shop/development/AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY=latchkey://shop/development/AWS_SECRET_ACCESS_KEY -
Ask for a session
Terminal lk run --aws-session --env shop/development -- terraform planLatchkey signs a request to AWS STS with the stored key. The command receives
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKENandAWS_CREDENTIAL_EXPIRATION. The long-lived key never reaches it. You don't need the AWS CLI installed. -
Assume a role instead
Pass
--aws-rolewith the role's ARN to get that role's credentials. The ARN below is an example.Terminal lk run --aws-role arn:aws:iam::123456789012:role/dev --env shop/development -- terraform plan -
Pick how long it lasts
The credentials last 15 minutes unless
--aws-forsays otherwise, from 15 minutes to 12 hours; a shorter time is raised to 15 minutes. A role's own maximum session duration still applies, and AWS refuses a longer request. Use the shortest time that covers the command.Terminal lk run --aws-role arn:aws:iam::123456789012:role/dev --aws-for 1h --env shop/development -- terraform plan
With an agent
An agent that runs your infrastructure commands is a good fit for this. If the credentials end up in its output, a file or a transcript, they stop working when the time is up. The long-lived key was never in the command, so there is nothing long-lived to rotate afterwards. Items that aren't agent-safe or covered by a grant ask you first, as with any other run. See Give coding agents API keys safely for approvals and grants.
What this doesn't do
- Short-lived is not the same as narrow. The credentials can do what the key, or the role, can do until they expire. Limit the role in AWS if the command needs less.
- Without a role, the credentials come from AWS's GetSessionToken, and AWS doesn't let those call IAM operations without MFA. If the command manages IAM, assume a role with
--aws-role. - MFA isn't supported yet.
- Latchkey uses AWS's global STS endpoint, so it doesn't cover accounts in the China or GovCloud partitions.
- Within the time window, a command that holds the credentials can still print, write or send them. Masking only hides exact copies in its output.
- Latchkey doesn't change your AWS setup. The access key and the role must already exist.
The CLI reference lists the options, and the security page says where protection ends.