What should I do if an API key or AWS access key leaks?

A calm, step-by-step response plan for a leaked AWS access key or API key: revoke, rotate, investigate, clean up and prevent it from happening again.

Anadoris Digital · · 4 min read

Faucet
Photo: Andreas Lischka / Wikimedia Commons (CC0)

Keys leak in ordinary ways: pushed to a public GitHub repository, pasted into a chat or support ticket, left in a shared spreadsheet, included in a mobile app or front-end bundle, or sitting on a laptop that was lost. Automated scanners search public code constantly, so a leaked AWS key can be found and misused very quickly, often to launch servers for crypto mining or to read your data.

Panic helps nobody, but speed matters. Having a short, rehearsed plan means the person who discovers the leak can act immediately rather than waiting to find out who is in charge.

Immediate steps: contain the damage

  • Deactivate or delete the leaked key at the provider: in the AWS IAM console for AWS keys, or the provider's dashboard for payment, courier, email or Shopify app credentials.
  • Create a replacement key with the narrowest permissions needed.
  • Update every system that used the old key, and confirm they work.
  • Tell your technical lead and a founder what happened and what you have done.
  • If AWS has emailed you about an exposed key, follow the steps in that notice.

Removing the key from the code or file where it leaked is necessary but not sufficient. Git history, forks, caches and scanners may already hold copies. Treat the key as compromised from the moment it was exposed.

Investigate what happened

Next, find out whether the key was used. For AWS, review CloudTrail events for that key: what actions were taken, from where and when. Check every region, not just your usual one, because attackers often launch resources in regions you do not watch. Look at Cost Explorer for sudden increases, and at GuardDuty findings if it is turned on. For other providers, check their activity logs or ask their support team.

  • List actions taken with the key since it was exposed.
  • Look for new users, roles, keys or permissions created by the attacker.
  • Check every region for unexpected servers, functions or containers.
  • Look for data access, such as reads from S3 buckets or database snapshots shared outside the account.
  • Note the time window so you can judge what data may have been exposed.

Clean up thoroughly

Remove anything the attacker created: servers, users, keys, roles and policies. Attackers often create a backdoor, such as an extra user or access key, so that they can return after you revoke the original. Rotate other secrets that the leaked key could have read, such as database passwords stored in a secrets manager. Then contact AWS support or the relevant provider, explain what happened and ask about any charges caused by the misuse.

Decide whether you need to notify anyone

If personal data may have been accessed, you may have obligations under the Digital Personal Data Protection Act and its rules, and CERT-In directions require certain cyber incidents to be reported within a short timeframe. Partners such as payment gateways may also require notification under their agreements. Take qualified advice quickly; obligations depend on what data was involved and what actually happened.

Prevent the next leak

  • Use IAM roles and short-lived credentials instead of long-lived keys wherever possible.
  • Store secrets in AWS Secrets Manager or Parameter Store, never in code.
  • Turn on secret scanning and push protection in GitHub or your code host.
  • Add a pre-commit hook that blocks commits containing secrets.
  • Never put secret keys in front-end code or mobile apps; only public, restricted keys belong there.
  • Limit every key's permissions and, where the provider allows, restrict it by IP address or domain.
  • Keep a list of all keys, their owners and where they are used, so rotation is quick.

Leaks you might not notice

Not every leak is obvious. A key may be embedded in an old version of a mobile app, a front-end bundle, a screenshot shared in a vendor ticket, or a backup copied to a personal drive. Some providers detect their own keys in public code and notify you, and AWS may restrict an exposed key automatically, but you should not rely on that.

Watch for indirect signs: unexpected charges, emails from providers about unusual activity, failed login alerts, new resources in regions you do not use, or customers reporting messages you did not send. Any of these should trigger the same response as a confirmed leak until you have proven otherwise. A habit of periodic key rotation also limits how long a quietly leaked key remains useful to anyone who finds it.

Where to start

Write a half-page leaked-key runbook now, before you need it: how to revoke keys at each provider you use, who to tell, and where to check for misuse. Then turn on secret scanning in your code host, enable GuardDuty and CloudTrail in all regions, and replace any long-lived AWS access keys used by apps with IAM roles.

If a leak has already happened and you need help investigating, Anadoris Digital can assist with the response and clean-up.

Related service: AWS & Cloud Management · More on this topic: AWS & Cloud Management articles

Common questions

Is deleting the key from GitHub enough?

No. Once a key has been public, copies may exist in Git history, forks, caches and the databases of automated scanners. Revoke the key at the provider first, replace it, and then clean up the repository history as a secondary step.

Will AWS refund charges from a leaked key?

AWS support may review charges caused by unauthorised use, especially if you act quickly and show you have secured the account. A refund is never assured, so contact support promptly, document what happened and set up controls to prevent a repeat.

How do attackers find leaked keys so fast?

Automated tools continuously scan public code repositories, package registries and paste sites for patterns that look like keys. A key pushed to a public repository can be found very quickly, which is why revoking immediately matters more than anything else.

What is secret scanning?

Secret scanning is a feature in code hosts such as GitHub that detects credentials in repositories. With push protection, it can block a commit containing a secret before it is stored. Some providers also receive alerts about their exposed keys and notify customers.

Your next chapter

Let’s build what comes next.

A new website. A better system. A simpler way to work. Tell us where you want to begin.

Discuss your project