Skip to content

Managing exposed AWS access keys Part 1: Prevention — Stop AWS Access Keys from being exposed

9 minute read
Content level: Intermediate
0

This is the first of three articles on managing exposed AWS access keys, starting with prevention, then evaluating the risk when a key is exposed, and recovering after a compromise. See the end of this post to view the other articles.

Overview

Exposed access keys are one of the top drivers of cloud security incidents. When long-lived credentials are accidentally committed to a public repository, embedded in application code, or shared insecurely, attackers can discover and use them after they become public— launching unauthorized resources, exfiltrating data, or escalating privileges across your account. By following the steps in this guide, you reduce the likelihood of key exposure, limit the blast radius if exposure does occur, and help your team detect and respond to credential compromise sooner. Whether you manage a single account or an enterprise-scale organization, these practices offer a strong baseline security posture AWS environments should strive to maintain.

Take the following proactive steps to reduce the risk of key exposure. Follow the steps in order. Each one builds on the ones before it — the earlier steps reduce the number of keys you need to protect, and the later steps add detection and containment for the keys that remain. Use the Prevention Checklist at the end to track what you've completed.

Step 1: Replace long-lived access keys where possible

The strongest form of prevention is removing the need for static keys where you can.

Action: Review the access keys in your account and, for each one, evaluate whether it can be replaced with temporary credentials from an IAM role.

Step 2: Install pre-commit scanning (git-secrets)

Install and use git-secrets to help prevent keys from being inappropriately committed to repositories.

Install

brew install git-secrets # macOS

-or-

git clone https://github.com/awslabs/git-secrets.git && cd git-secrets && make install # Linux

Configure per repository

cd /path/to/your/repo
git secrets --install
git secrets --register-aws

Scan existing commit history for exposed keys

git secrets --scan-history

Once configured, git-secrets checks each commit for AWS credential patterns and blocks the commit when a pattern matches.

Step 3: Store remaining keys in AWS Secrets Manager

For keys that cannot be replaced, store them centrally in AWS Secrets Manager and configure rotation on a schedule.

  1. Store the secret:

    aws secretsmanager create-secret \
    --name my-app/credentials \
    --secret-string '{"accessKeyId":"...","secretAccessKey":"..."}'
    
  2. Configure rotation on a schedule (recommended: every 30–90 days):

    • Open Secrets Manager Console.
    • Select your secret → Rotation configuration → Enable rotation.

Note: Rotating IAM access keys requires an AWS Lambda rotation function — Secrets Manager doesn't rotate IAM keys the same way it does for AWS-managed database credentials. See Rotating your secrets for further guidance.

  1. Update applications to retrieve credentials at runtime via the Secrets Manager API instead of hardcoding them.

Step 4: Enable GuardDuty

Amazon GuardDuty is a threat detection service that includes finding types focused on credential compromise, alongside broader detections across your AWS environment. Open GuardDuty Console.

  1. Click Get Started
  2. Enable GuardDuty.
  3. For AWS Organizations: enable delegated administrator for centralized management.

Key finding types for credential issues:

  • UnauthorizedAccess: IAMUser/InstanceCredentialExfiltration
  • Recon: IAMUser/MaliciousIPCaller
  • Persistence: IAMUser/AnomalousBehavior

For more information, see Amazon GuardDuty documentation.

Step 5: Enable MFA on all users and root

A second factor on human sign-ins prevents an exposed password from being used to create new access keys.

  1. Open IAM Console → Users.
  2. For each user: Security credentials tab → Assign MFA device.
  3. For root account: Security Credentials → Activate MFA.

For more information, see AWS Multi-factor authentication in IAM.

Step 6: Set Up Real-Time Alerting

Create EventBridge rules to alert on suspicious IAM activity:

  1. Open EventBridge Console.

  2. Create a rule with this event pattern:

    {
      "source": ["aws.iam"],
      "detail-type": ["AWS API Call via CloudTrail"],
      "detail": {
        "eventName": ["CreateAccessKey", "CreateUser", "AttachUserPolicy"]
      }
    }
    
  3. Set target to an SNS topic that notifies your security team.

  4. Tune the event pattern to your account's normal activity so the alerts stay actionable.

Additional events to monitor: ConsoleLogin from new IPs, StopLogging, RunInstances in unusual regions, CreateRole, AttachRolePolicy.

Step 7: Apply service control policies (SCPs) to limit blast radius

Use AWS Organizations SCPs to narrow the set of actions available in your accounts, so that compromised credentials have a smaller blast radius.

Deny actions outside approved regions:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "DenyUnapprovedRegions",
    "Effect": "Deny",
    "Action": "*",
    "Resource": "*",
    "Condition": {
      "StringNotEquals": {
        "aws:RequestedRegion": ["us-east-1", "us-west-2", "eu-west-1"]
      }
    }
  }]
}

Deny high-risk actions without MFA:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "DenyDangerousActionsWithoutMFA",
    "Effect": "Deny",
    "Action": ["iam:CreateUser", "iam:CreateAccessKey", "iam:AttachUserPolicy", "ec2:RunInstances"],
    "Resource": "*",
    "Condition": { "BoolIfExists": { "aws:MultiFactorAuthPresent": "false" } }
  }]
}

See Service control policies

Step 8: Enforce automated key rotation with AWS Config

Automated rotation limits how long an exposed key stays valid, even when the exposure is unknown

  1. Open AWS Config Console.
  2. Add the managed rule: access-keys-rotated.
  3. Set maxAccessKeyAge to 90 days.
  4. Non-compliant keys appear in the dashboard.

Documentation: access-keys-rotated Rule

Step 9: Apply least privilege

Tight permissions on each key limit what a third party could do with it if exposed.

Prevention Checklist

Use this list to track completed steps and spot gaps in your prevention coverage.

#ActionPriorityDone
1Replace long-lived keys with IAM RolesCritical☐
2Install git-secrets on all repositoriesHigh☐
3Enable MFA for all IAM users and rootCritical☐
4Store remaining keys in Secrets Manager with rotationHigh☐
5Enable GuardDutyHigh☐
6Set up EventBridge alerts for IAM changesMedium☐
7Apply SCPs to restrict regions and high-risk actionsMedium☐
8Enable AWS Config key rotation ruleMedium☐
9Review IAM policies for least privilegeMedium☐
10Use separate accounts per environmentMedium☐

Need Help? Sign Up for AWS Security Incident Response

Even with strong preventive controls in place, security incidents can still happen. When they do, having a plan for expert support in advance can shorten your time to containment.

AWS Security Incident Response is a managed service that helps you prepare for, respond to, and recover from security events in your AWS environment. The service provides:

  • 24/7 access to AWS security experts who can guide you through active incidents, including credential compromise, unauthorized access, and data exposure.
  • Automated triage and investigation that accelerates response times by surfacing relevant findings from GuardDuty, Security Hub, and other detection services into a centralized case management workflow.
  • Pre-built response playbooks for common incident types, so your team isn't starting from scratch when an event occurs.
  • Coordinated remediation support to help you contain threats, remove attacker persistence, and restore your environment to a known-good state.
  • Proactive readiness features including incident simulations and response planning to ensure your team is prepared before an incident strikes.

Whether you have a dedicated security operations team or a small team wearing multiple hats, AWS Security Incident Response gives you the expertise and tooling to handle security events confidently and efficiently.

Learn more about AWS Security Incident Response

Conclusion

Prevention works best in layers. The steps above build toward a posture where fewer keys exist, the keys that do are rotated and scoped tightly, and unauthorized use is detected early. Review the Prevention Checklist, close any gaps, and continue to Managing exposed AWS access keys Part 2: Risk assessment—Evaluating an exposed AWS access key when you're ready.

Managing Exposed Access Keys

See the blogs part of the managing exposed Access keys series:

  1. Managing exposed AWS access keys Part 1: Prevention — Stop AWS Access Keys from being exposed

    Stop AWS access keys from being exposed before they become a liability. This first article in our three-part series on managing exposed AWS access keys focuses on prevention, with nine practical steps—from replacing long-lived keys with IAM roles and enabling MFA to setting up GuardDuty, real-time alerting, and least-privilege controls—plus a checklist to help your team reduce the risk and blast radius of credential compromise.

  2. Managing exposed AWS access keys Part 2: Risk assessment—Evaluating an exposed AWS access key

    Discovered an exposed AWS access key? Before you rotate anything, size the risk. This eight-step guide walks you through mapping the key's permissions and exposure window, classifying the severity, checking CloudTrail for unauthorized activity, and deciding whether business needs justify a documented, time-bounded delay in rotation—Part 2 of our three-part series on managing exposed AWS access keys.

  3. Managing exposed AWS access keys Part 3: Recovery—Responding to an exposed AWS access key

    Discovered an exposed AWS access key? This third installment in our series is a step-by-step recovery playbook for containing the damage—from locking down the compromised key and rooting out unauthorized activity to safely rotating credentials and hardening your account against a repeat incident.