iam-role

iam-role

Features

  • Create IAM roles with customizable trust policies
  • Attach AWS managed or customer managed policies
  • Define inline policies for role-specific permissions
  • Optional instance profile creation for EC2 use cases
  • Permissions boundary support for delegated administration
  • Comprehensive input validation with helpful error messages

When to Use

Use this module when you need to:

  • Create execution roles for Lambda functions, ECS tasks, or Step Functions
  • Set up EC2 instance roles with automatic instance profile creation
  • Enable cross-account access with proper security controls
  • Implement GitHub Actions OIDC federation for keyless CI/CD
  • Delegate role creation to teams while enforcing permissions boundaries

Usage

Basic Usage

module "lambda_role" {
  source = "../../"

  name = "example-lambda-role"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect    = "Allow"
      Principal = { Service = "lambda.amazonaws.com" }
      Action    = "sts:AssumeRole"
    }]
  })

  managed_policy_arns = [
    "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
  ]

  tags = {
    Environment = "example"
  }
}

Complete Example

See examples/complete for a full-featured example demonstrating all module capabilities.

Inputs

NameTypeRequiredDefaultDescription
assume_role_policystringtruen/aJSON trust policy document that grants entities pe…
create_instance_profileboolfalsen/aWhether to create an EC2 instance profile for this…
descriptionstringfalsen/aHuman-readable description of the role’s purpose. …
force_detach_policiesboolfalsen/aWhether to force detach policies before destroying…
inline_policiesmap(string)false{}Map of inline policy names to JSON policy document…
instance_profile_namestringfalsen/aName of the instance profile. Defaults to the role…
managed_policy_arnsset(string)false[]Set of managed policy ARNs to attach to the role. …
max_session_durationnumberfalse3600Maximum session duration in seconds for role assum…
namestringtruen/aThe name of the IAM role. Must be unique within yo…
pathstringfalse/Path for the IAM role. Use paths to organize roles…
permissions_boundarystringfalsen/aARN of the policy to use as a permissions boundary…
tagsmap(string)false{}Map of tags to apply to all resources created by t…

For detailed input documentation including examples, see the registry page.

Outputs

NameDescription
create_dateTimestamp when the role was created (ISO 8601 format). Usef…
instance_profile_arnARN of the instance profile (if created). Only populated wh…
instance_profile_idUnique identifier for the instance profile (if created). On…
instance_profile_nameName of the instance profile (if created). Only populated w…
role_arnARN of the IAM role. Use this ARN when: - Configuring Lambd…
role_idStable and unique string identifying the role. This ID rema…
role_nameName of the IAM role. Use the name (not ARN) when: - Attach…
unique_idUnique identifier assigned by AWS (format: AROA…). This i…

Principle of Least Privilege

Always grant the minimum permissions required. Prefer specific resource ARNs over wildcards, and use inline policies for role-specific permissions that shouldn’t be reused.

Permissions Boundaries

Use permissions boundaries when delegating role creation to development teams. Boundaries set the maximum permissions a role can have, regardless of what policies are attached. This prevents privilege escalation in multi-tenant environments.

External IDs for Third-Party Access

Always use external IDs when granting cross-account access to third parties. This prevents the “confused deputy” problem where a malicious actor could trick a service into accessing your resources.

Session Duration

Use the shortest session duration that meets your needs. Shorter sessions reduce the window of exposure if credentials are compromised:

  • 1 hour (3600): Default, suitable for most automated tasks
  • 4 hours (14400): Long CI/CD pipelines
  • 8 hours (28800): Workday console sessions
  • 12 hours (43200): Maximum, rarely needed

OIDC Trust Policies

When using OIDC federation (e.g., GitHub Actions), always scope trust policies to specific repositories and branches. Never allow all repositories or all branches to assume production roles.

Common Trust Policy Patterns

ServicePrincipal
Lambdalambda.amazonaws.com
ECS Tasksecs-tasks.amazonaws.com
EC2ec2.amazonaws.com
Step Functionsstates.amazonaws.com
API Gatewayapigateway.amazonaws.com
CodeBuildcodebuild.amazonaws.com
CodePipelinecodepipeline.amazonaws.com
CloudWatch Eventsevents.amazonaws.com
SNSsns.amazonaws.com
SQSsqs.amazonaws.com

Condition Keys

Common condition keys for trust policies:

Condition KeyUse Case
sts:ExternalIdThird-party cross-account access
aws:MultiFactorAuthPresentRequire MFA for role assumption
aws:SourceIpRestrict by IP address
aws:SourceVpcRestrict to specific VPC
aws:PrincipalOrgIDRestrict to AWS Organization
sts:RoleSessionNameAudit/track who assumed the role

This README was generated from structured documentation. See spec.yaml for the complete module specification.