Managing exposed AWS access keys Part 1: Prevention — Stop AWS Access Keys from being exposed
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.
- Replace keys on EC2 instances with IAM roles for Amazon EC2
- Replace keys for Lambda functions with AWS Lambda execution roles
- Replace keys for ECS tasks with IAM roles for Amazon ECS tasks
- Replace keys for human users with AWS IAM Identity Center (SSO)
- Replace keys for cross-account access with IAM roles with AWS STS AssumeRole
- Replace keys for external/on-premises workloads with IAM Roles Anywhere
- Replace keys for CI/CD pipelines (GitHub, etc.) with IAM OIDC Identity Providers
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.
-
Store the secret:
aws secretsmanager create-secret \ --name my-app/credentials \ --secret-string '{"accessKeyId":"...","secretAccessKey":"..."}' -
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.
- 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.
- Click Get Started
- Enable GuardDuty.
- 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.
- Open IAM Console → Users.
- For each user: Security credentials tab → Assign MFA device.
- 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:
-
Open EventBridge Console.
-
Create a rule with this event pattern:
{ "source": ["aws.iam"], "detail-type": ["AWS API Call via CloudTrail"], "detail": { "eventName": ["CreateAccessKey", "CreateUser", "AttachUserPolicy"] } } -
Set target to an SNS topic that notifies your security team.
-
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" } }
}]
}
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
- Open AWS Config Console.
- Add the managed rule: access-keys-rotated.
- Set maxAccessKeyAge to 90 days.
- 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.
- Remove unused permissions
- Set maximum permission boundaries
- Separate accounts per environment
- Use short session durations
Prevention Checklist
Use this list to track completed steps and spot gaps in your prevention coverage.
| # | Action | Priority | Done |
|---|---|---|---|
| 1 | Replace long-lived keys with IAM Roles | Critical | ☐ |
| 2 | Install git-secrets on all repositories | High | ☐ |
| 3 | Enable MFA for all IAM users and root | Critical | ☐ |
| 4 | Store remaining keys in Secrets Manager with rotation | High | ☐ |
| 5 | Enable GuardDuty | High | ☐ |
| 6 | Set up EventBridge alerts for IAM changes | Medium | ☐ |
| 7 | Apply SCPs to restrict regions and high-risk actions | Medium | ☐ |
| 8 | Enable AWS Config key rotation rule | Medium | ☐ |
| 9 | Review IAM policies for least privilege | Medium | ☐ |
| 10 | Use separate accounts per environment | Medium | ☐ |
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:
-
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.
-
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.
-
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.
- Language
- English

Relevant content
asked 4 years ago
AWS OFFICIALUpdated 7 months ago