When you build data pipelines, the path of least resistance is often granting broad s3:GetObject or dynamodb:PutItem permissions to ensure jobs don't fail during initial development. Over time, these roles accumulate "permission creep," where they retain access to buckets, tables, or APIs they no longer touch. This represents a significant risk in regulated environments: if a pipeline component is compromised, the blast radius is proportional to the permissions attached to its execution role.

To solve this, you need a way to shift from manual, guess-based policy trimming to evidence-based pruning. AWS IAM Access Analyzer provides an automated way to compare the actual actions performed against the actions defined in your IAM policies. In this article, you will implement an automated workflow that identifies unused permissions for your pipeline roles and generates a refined policy template, keeping your infrastructure lean and secure. This guide covers IAM Access Analyzer policies for roles and assumes you are managing your infrastructure via AWS CLI or Terraform.

What you'll build

You will create an IAM Access Analyzer policy generation job that analyzes CloudTrail logs to determine the actual usage of an existing pipeline role. You will then use the resulting policy to update your Infrastructure as Code (IaC) definition, effectively enforcing least privilege.

flowchart LR Role["Pipeline IAM Role"] --> Actions["Actual API Calls"] CloudTrail["AWS CloudTrail"] --> Analyzer["IAM Access Analyzer"] Analyzer --> Report["Generated Policy"] Report --> Terraform["IaC (Terraform/CDK)"]

Prerequisites

  • CloudTrail: You must have a trail enabled in the account and region where the pipeline role operates. Access Analyzer requires at least 7 days of CloudTrail data to generate reliable recommendations.
  • Permissions: You need iam:GenerateCredentialReport, iam:CreatePolicyVersion, and access-analyzer:CreateAnalyzer permissions.
  • Infrastructure: An existing IAM role used by your Spark or Lambda job that you suspect is over-privileged.

Step 1: Enable IAM Access Analyzer

Access Analyzer monitors resource access, but for policy generation, you need to ensure the service is active in your region. Enabling it allows the service to begin observing access patterns across your accounts.

Run the following command to create an analyzer:

aws accessanalyzer create-analyzer \ --analyzer-name "pipeline-security-analyzer" \ --type "ACCOUNT" 

Verify the creation by listing your analyzers:

aws accessanalyzer list-analyzers 

Look for the status field to confirm it is ACTIVE.

Step 2: Generate a policy based on access activity

Now, you will trigger the policy generation. This feature uses the access history captured in CloudTrail to identify which actions the role actually performed.

Execute the following command, replacing the role ARN with your specific pipeline role:

aws accessanalyzer start-policy-generation \ --policy-generation-details '{"principalArn": "arn:aws:iam::<account-id>:role/your-pipeline-role"}' 

The output will provide a jobId. Copy this ID to poll for the results.

Step 3: Retrieve and review the recommendations

The generation process is asynchronous. Wait a few moments and then poll for the status using the jobId you received in the previous step:

aws accessanalyzer get-generated-policy \ --job-id <your-job-id> 

When the status field returns SUCCEEDED, the generatedPolicyResult field will contain the JSON-formatted policy that includes only the actions the role has actually used. Review this output against your business requirements; it is common to see that your role was using 20% of its defined permissions, allowing you to safely deprecate the remaining 80%.

Step 4: Update your IaC definition

With the generated JSON in hand, update your Terraform or AWS CDK code. Do not manually apply this policy via the console; doing so creates configuration drift.

In your Terraform definition, replace the existing inline_policy or managed_policy_arns with the refined policy document:

resource "aws_iam_role_policy" "pipeline_policy" { name = "refined-pipeline-policy" role = aws_iam_role.pipeline_role.id policy = <<EOF { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "s3:GetObject", "s3:ListBucket" ], "Resource": "*" } ] } EOF } 

Apply your infrastructure changes using your standard CI/CD pipeline. Verify the update by checking the role in the IAM console and ensuring the effective policy matches your new, tightened definition.

Design decisions

  • Automated Generation, not Manual Audit. I prefer using Access Analyzer over manual log analysis (like searching CloudTrail manually) because Access Analyzer accounts for the complex logic of IAM evaluation, such as resource-based policy interactions.
  • Policy Versioning, not Policy Replacement. Always use IAM policy versions when updating roles. If the generated policy is missing a permission required for an edge case (e.g., a once-a-month reconciliation job), you can immediately roll back to the previous policy version without redeploying your entire IaC stack.
  • Account-level Analysis, not Global. Keep your analyzers scoped to the Account level. Scoping to the Organization level can lead to excessive noise and makes it difficult to pinpoint which specific pipeline role is the source of an over-permissioned policy.

Troubleshooting

  • Symptom: The generated policy is empty or missing expected actions.
    • Cause: The role has not performed the required actions within the 7-day window tracked by CloudTrail.
    • Fix: Ensure your pipeline job has completed a full run (including error handling paths) during the observation period.
  • Symptom: AccessDenied errors immediately after applying the new policy.
    • Cause: The Access Analyzer observation period missed a rare or conditional event.
    • Fix: Revert to the previous policy version, analyze CloudTrail logs for the specific AccessDenied events, add the missing permissions, and apply the policy again.
  • Symptom: PolicyGenerationLimitExceeded error.
    • Cause: You are running too many generation jobs simultaneously.
    • Fix: Wait for existing jobs to complete before triggering new ones.

Clean up

If you created the analyzer solely for this audit and no longer require continuous monitoring of policy generation, delete the analyzer:

aws accessanalyzer delete-analyzer --analyzer-name "pipeline-security-analyzer" 

Conclusion

You now have a repeatable process to transform bloated IAM roles into lean, least-privilege configurations. By basing your security posture on observed data rather than initial assumptions, you significantly reduce the potential impact of credential compromise in your data platform.

Try it: Identify your most critical pipeline role today and run a generation job to see how much of its current policy is actually required for its daily operation.

Tags: #aws #security #dataengineering

About the examples: automated checks confirmed that 5 AWS CLI commands match the AWS service API models (operation and parameter names). The examples have not been run end to end, so test them in a non-production environment before relying on them.

Cover photo by Sajad Nori on Unsplash.

This article was drafted with AI assistance and is intended for technical review and fact-checking before publication.