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

  1. 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 be latchkey:// 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
  2. Ask for a session

    Terminal
    lk run --aws-session --env shop/development -- terraform plan

    Latchkey signs a request to AWS STS with the stored key. The command receives AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN and AWS_CREDENTIAL_EXPIRATION. The long-lived key never reaches it. You don't need the AWS CLI installed.

  3. Assume a role instead

    Pass --aws-role with 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
  4. Pick how long it lasts

    The credentials last 15 minutes unless --aws-for says 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

The CLI reference lists the options, and the security page says where protection ends.

Related