🎓 A1Learner
🔍
0 of 50 lessons completed

Identity: IAM Users and Roles

IAM is the service that decides who can do what in AWS. Users are people or programs, roles are identities a service can take on, and policies are the written rules.

A user, on its own

The user starts with no permissions. A policy must be attached before it can do anything.

resource "aws_iam_user" "reader" {
  name = "lesson-reader"
}

A small read-only policy

The policy allows two read actions and nothing else. That is least privilege in code.

resource "aws_iam_policy" "read_only" {
  name = "lesson-read-only"

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect   = "Allow"
        Action   = ["s3:ListBucket", "s3:GetObject"]
        Resource = "*"
      }
    ]
  })
}

A role a service can take on

The assume-role policy says EC2 may take on this role. The role then carries the permissions.

resource "aws_iam_role" "app" {
  name = "lesson-app"

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

Example

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "6.72.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true
  s3_use_path_style = true
  endpoints {
    iam = "http://localhost:5000"
    s3  = "http://localhost:5000"
  }
}

resource "aws_iam_user" "reader" {
  name = "lesson-reader"
}

resource "aws_iam_policy" "read_only" {
  name = "lesson-read-only"

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Effect   = "Allow"
        Action   = ["s3:ListBucket", "s3:GetObject"]
        Resource = "*"
      }
    ]
  })
}

resource "aws_iam_user_policy_attachment" "reader" {
  user       = aws_iam_user.reader.name
  policy_arn = aws_iam_policy.read_only.arn
}

resource "aws_iam_role" "app" {
  name = "lesson-app"

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

output "user_name" {
  value = aws_iam_user.reader.name
}

output "role_name" {
  value = aws_iam_role.app.name
}

Key points

  • An IAM user is an identity for a person or a program.
  • An IAM role is an identity a service can take on for a time.
  • A policy is a document that allows or denies actions.
  • A policy is attached to a user or a role to give it permissions.
  • Least privilege means the smallest set of permissions that works.
  • A role is safer than a long-lived key, because its credentials expire.

The configuration creates a user with a small read-only policy attached, plus a role an EC2 instance can assume. Both follow least privilege: the user can only read, and the role is the only way the application gets access. The user and role live in the mock AWS on this machine, not in a real account. A final destroy wipes them both.

Note

The user and role exist only in the emulator on this machine. Real AWS is never contacted.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What does least privilege mean?