Separate access for people, workloads and CI on AWS
Give people permission sets, give services workload roles, give CI a federated role, and keep root for root-only tasks.
By Invicly Tech, , in AWS Cloud Engineering. About 12 minutes to read.
In short
Different callers need different kinds of identity. People sign in through IAM Identity Center and get a permission set per account; services running on AWS use roles attached to the Lambda function, ECS task or EC2 instance; CI systems outside AWS swap a signed OIDC token for a short role session. Root is kept for the few tasks only root can do, and IAM users with access keys are retired.
Think of it like this: Staff carry personal badges, delivery robots get a fixed key for one door, and the visiting contractor gets a one-day pass after showing ID at reception.
Explained simply
Not every caller is a person. In the sample service, you sign in to look at logs, a Lambda function reads uploaded files, and GitHub Actions deploys new versions. Each of these needs access, but each should get it in a different way.
People use IAM Identity Center. You create users or groups, define permission sets such as ReadOnly or Admin, and assign them to accounts. Signing in gives a short session, and removing an assignment takes access away everywhere at once.
Code running on AWS uses roles that are attached to the compute. A Lambda function has an execution role, an ECS task has a task role, and an EC2 instance has an instance profile. AWS delivers fresh temporary credentials to the code automatically, so you never paste keys into code or environment variables.
CI systems outside AWS, like GitHub Actions, use federation. GitHub signs a short token that says which repository and branch is running. AWS checks the signature and conditions in the role's trust policy, then hands back a short role session.
Root stays locked away for the handful of jobs only root can do. Old IAM users with access keys are moved to one of the three patterns above, and then their keys are deleted.
Press Play on a diagram to walk through it one step at a time.
- IAM Identity Center user or group
- Permission set assigned per account
- Sign-in with MFA, short sessions
- Roles named AWSReservedSSO_...
- Lambda execution role
- ECS task role (plus execution role)
- EC2 instance profile
- Credentials delivered automatically
- IAM OIDC identity provider
- Role trusts the provider's tokens
- Conditions pin repo and branch
- No stored secret in the CI system
- People (Identity Center)
- Developers group: Developer in dev
- Operators group: ReadOnly in prod
- Admins group: Admin in sandbox
- Workloads (roles)
- filesvc-worker Lambda execution role
- filesvc-api ECS task role
- filesvc-api ECS task execution role
- CI (OIDC)
- filesvc-deploy-dev role, branch main
- filesvc-deploy-prod role, environment prod
- Root
- Locked away, MFA, root-only tasks
In depth
Permission sets in IAM Identity Center
A permission set is a template made of AWS managed policies, customer managed policy references, an inline policy and optionally a permissions boundary. When you assign a user or group to an account with a permission set, Identity Center creates a role in that account whose name is AWSReservedSSO_, then the permission set name and a random suffix, and lets the user assume it from the access portal or CLI.
Keep the list short and job-based: ReadOnly for most people, Developer for the dev account, Admin only for a few people and preferably only in sandbox and dev. Assign groups rather than individuals so joiners and leavers are handled in one place. The session duration is set per permission set, from one hour by default up to twelve. Do not edit the AWSReservedSSO roles directly; change the permission set and let Identity Center update every account.
- Assign groups, not individuals
- One permission set per job, not per person
- Shorter sessions for powerful sets
- Edit the permission set, never the generated role
Roles for Lambda, ECS and EC2
Each compute service gets credentials to your code in its own way, but the idea is the same: the service assumes a role you chose and the SDK picks up the credentials. A Lambda function's execution role must trust lambda.amazonaws.com; logs need the AWSLambdaBasicExecutionRole managed policy or equivalent. An ECS task has two roles. The task execution role is used by ECS itself to pull images from ECR and send logs to CloudWatch. The task role is used by your containers for application calls such as SQS or DynamoDB.
On EC2, a role is attached through an instance profile and the SDK reads credentials from the instance metadata service. Require IMDSv2, which needs a session token, to reduce the risk of credential theft through server-side request forgery. In every case, one workload gets one role scoped to its own resources.
- Used by the ECS agent and Fargate
- Pulls images from ECR
- Writes container logs to CloudWatch
- Reads secrets injected at start
- Used by your application code
- Calls SQS, S3, DynamoDB and so on
- Scoped to this service's resources
- Seen by the SDK as container credentials
Federating GitHub Actions with OIDC
First, create an IAM OIDC identity provider for https://token.actions.githubusercontent.com with audience sts.amazonaws.com. Next, create a deploy role whose trust policy allows sts:AssumeRoleWithWebIdentity from that provider, with conditions on the aud claim and the sub claim, such as repo:example-org/filesvc:ref:refs/heads/main or repo:example-org/filesvc:environment:prod. Then give the role only the deployment permissions it needs.
In the workflow, grant permissions id-token: write, and use the aws-actions/configure-aws-credentials action with role-to-assume and aws-region. The action requests a token from GitHub, calls STS and exports temporary credentials for the next steps. No AWS secret is stored in GitHub. Pin the action to a release tag or, better, a full commit SHA.
- Workflow job to GitHub OIDC issuer: Request ID token (audience sts.amazonaws.com)1Request ID token (audience sts.amazonaws.com)
- GitHub OIDC issuer to Workflow job: Signed JWT with sub repo:example-org/filesvc:ref:refs/heads/main2Signed JWT with sub repo:example-org/filesvc:ref:refs/heads/main
- Workflow job to AWS STS: AssumeRoleWithWebIdentity(role, token)3AssumeRoleWithWebIdentity(role, token)
- AWS STS to Workflow job: Temporary credentials if trust conditions match4Temporary credentials if trust conditions match
- Workflow job to AWS services: Deploy calls signed with the session5Deploy calls signed with the session
Root-only tasks and retiring IAM user keys
A few tasks still need the root user: changing a standalone account's root email, password or root access keys; closing or reopening a standalone account; restoring IAM permissions when every administrator has been locked out; activating IAM access to the Billing console; enabling S3 MFA delete; editing an S3 bucket policy or SQS queue policy that denies all principals; signing up for AWS GovCloud (US); and registering as a seller in the Reserved Instance Marketplace. In an organization, several of these can be done centrally from the management account instead.
To retire IAM users with keys, list keys and when they were last used, find what uses each one, move that caller to Identity Center, a workload role or OIDC, deactivate the key, wait to see if anything breaks, then delete it.
- aws iam list-access-keys --user-name NAME
- aws iam get-access-key-last-used --access-key-id ID
- Deactivate before deleting
- Credential report: aws iam generate-credential-report
Learn it in 5 short steps
About 30m each. Track them with a timer in your plan.
- 1Assign Identity Center permission sets~30m
- 2Attach roles to Lambda, ECS and EC2~30m
- 3Federate CI with OIDC~30m
- 4List the root-only tasks~30m
- 5Retire IAM users with access keys~30m
Key points
- A permission set is a template; Identity Center creates a matching role in each account it is assigned to.
- Give each workload its own role with only the actions and resources it needs.
- For ECS, the task role is what your code uses; the task execution role is what ECS uses to pull images and write logs.
- GitHub Actions can assume an AWS role with OIDC, so no AWS keys live in repository secrets.
- Replace IAM users with access keys by moving people to Identity Center and code to roles, then delete the keys.
Key terms
Tap a card to flip it. 0/5 known
Examples
{
"Effect": "Allow",
"Principal": { "Federated": "arn:aws:iam::111122223333:oidc-provider/token.actions.githubusercontent.com" },
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"token.actions.githubusercontent.com:aud": "sts.amazonaws.com",
"token.actions.githubusercontent.com:sub": "repo:example-org/filesvc:ref:refs/heads/main"
}
}
}Trust policy statement for a CI deploy role: only workflows on main in one repository can assume it.
Giving the Lambda worker its own role
The worker reads uploads from S3, consumes the SQS queue and writes results to DynamoDB. Step 1: create a role that trusts lambda.amazonaws.com. Step 2: attach AWSLambdaBasicExecutionRole for logs. Step 3: add an inline policy with s3:GetObject on the in/ prefix, the SQS consume actions on the one queue, and dynamodb:PutItem on the one table. Step 4: set the role on the function. Evidence: the function's CloudWatch logs show successful processing, and an attempt to read another bucket fails with AccessDenied. Cleanup: delete the role when the lab stack is removed.
aws iam create-role --role-name filesvc-worker \
--assume-role-policy-document file://trust-lambda.json
aws iam attach-role-policy --role-name filesvc-worker \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam put-role-policy --role-name filesvc-worker \
--policy-name filesvc-worker-data --policy-document file://worker-data.json
aws lambda update-function-configuration --function-name filesvc-worker \
--role arn:aws:iam::111122223333:role/filesvc-workerA GitHub workflow that deploys with OIDC
You want pushes to main to deploy to the dev account without stored keys. Step 1: create the OIDC provider and the filesvc-deploy-dev role with the trust conditions shown in this topic's explainer. Step 2: add the workflow below. Step 3: push to main. Evidence: the job log shows the get-caller-identity output with assumed-role/filesvc-deploy-dev, and a push from another branch fails at the credentials step because the sub claim does not match. Cleanup: delete the role and provider when the sandbox is retired.
on: { push: { branches: [main] } }
permissions:
id-token: write
contents: read
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4 # pin a commit SHA in real use
with:
role-to-assume: arn:aws:iam::111122223333:role/filesvc-deploy-dev
aws-region: eu-west-1
- run: aws sts get-caller-identityQuick reference
- People: Identity Center permission sets. Workloads: attached roles. CI: OIDC federation
- Permission set becomes an AWSReservedSSO_* role in each assigned account
- Lambda: execution role trusts lambda.amazonaws.com
- ECS: task role for your code, task execution role for image pulls and logs
- EC2: instance profile plus IMDSv2 required
- GitHub OIDC: provider token.actions.githubusercontent.com, aud sts.amazonaws.com, pin sub
- Workflow needs permissions id-token: write
- Retire keys: find last use, move caller, deactivate, then delete
Common mistakes
- Using a wildcard such as repo:example-org/* in the OIDC sub condition, which lets any repository in the organisation deploy.
- Giving the ECS task execution role the application's permissions, or the task role the image-pull permissions.
- Sharing one broad role across every Lambda function, so a bug in one can touch all data.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Find every IAM user in the sandbox that still has an access key and when each key was last used.
- Exercise 2medium
Create a ReadOnly permission set, assign it to a Developers group in the sandbox account, and sign in from the CLI with it.
- Exercise 3hard
Set up GitHub OIDC for a test repository so only the main branch can assume a role that may run aws s3 ls, then prove a feature branch is refused.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Can I use IAM Identity Center without AWS Organizations?
- An account instance of Identity Center exists for some application use cases, but assigning permission sets to AWS accounts needs the organization instance, which uses AWS Organizations.
- Does GitHub OIDC work with other CI systems?
- Yes, the same pattern works for GitLab, Bitbucket and others that issue OIDC tokens. Only the provider URL, audience and claim names change.
- Is it bad to give a Lambda function AdministratorAccess in a sandbox?
- It works, but it hides mistakes that will break later in dev and prod, and any bug or injected code gets full control. Practise least privilege from the start.