Workload identity for EC2, Lambda and CI

Intermediate · 17 min

Give software AWS permissions without stored keys: instance profiles, execution and task roles, Pod identity, GitHub Actions OIDC and PassRole.

Before thisAssuming roles with STSPolicy documents and evaluation logic

Integrations Ninjas · 17 min

Workload identity for EC2, Lambda and CI

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Integrations NinjasReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Software on AWS should hold no access keys. The platform proves what is running and hands it temporary role credentials, which the SDK picks up and refreshes.
  • EC2 uses an instance profile, Lambda an execution role, ECS a task role, and EKS Pod Identity or IRSA for Pods.
  • Code outside AWS with an OpenID Connect token, such as a CI job, exchanges it with AssumeRoleWithWebIdentity.
  • In a GitHub trust policy always restrict sub to your repository and a branch or environment, and check aud.
  • iam:PassRole decides which roles someone may hand to a service. Unrestricted, it is a route to privilege escalation.

The secret that shouldn’t exist

Your application needs to read from S3. The tempting solution is to make an access key and put it in a config file. Now there is a secret in your repository, image or server, valid until someone remembers to rotate it.

The better question is: can the platform vouch for the software? AWS knows which instance, function or task it is running. So it can hand that workload temporary credentials for a role, refreshed automatically. Nothing to store, nothing to leak for long. Your code creates a client with no keys, and the credential chain finds them.

Think of a contractor whose building pass is issued at reception each morning, instead of a copied key that lives in their glovebox.

On AWS compute

Where the code runs What you configure Role trusts
EC2 instance profile ec2.amazonaws.com
Lambda execution role lambda.amazonaws.com
ECS task role (separate from the task execution role) ecs-tasks.amazonaws.com
EKS Pod Identity association, or IRSA pods.eks.amazonaws.com, or the cluster’s OIDC provider

EC2 needs one extra object. An instance profile is a container that holds exactly one role, and it is what you attach to an instance. The console hides this. The CLI doesn’t:

aws iam create-instance-profile --instance-profile-name web-app
aws iam add-role-to-instance-profile --instance-profile-name web-app --role-name web-app

Adding a second role fails. The credentials are served by the instance metadata service, so any process that can reach it has the role. Treat one instance as one trust level.

Lambda places the execution role’s credentials in the function’s environment. ECS and EKS give each task or Pod its own role, so neighbours on a host don’t share. The task execution role in ECS is different: it is what the agent uses to pull the image and send logs.

Outside AWS: federation

A CI job runs on someone else’s machine. It does have an identity: the CI system can issue it a signed OpenID Connect token saying which repository and branch it is. You register the issuer in IAM once, and write a role trusting it. The job calls sts:AssumeRoleWithWebIdentity with the token. This flow, from the pipeline’s side, is covered for GitHub Actions and Terraform. The AWS side is the trust policy:

{"Version":"2012-10-17","Statement":[{
  "Effect":"Allow",
  "Principal":{"Federated":"arn:aws:iam::123456789012: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:acme@1234/shop@5678:environment:prod"
  }}}]}

sub is the lock that matters. GitHub’s issuer serves every repository on GitHub, so a policy without a tight sub trusts strangers. IAM refuses to save a GitHub trust policy where sub is missing or only a wildcard.

Check the exact format of your sub. GitHub documents that repositories created after 15 July 2026 (and any that opt in) use immutable subjects that include numeric IDs, as above. Older ones use repo:acme/shop:environment:prod. Copying an old tutorial into a new repository gives an AccessDenied that looks mysterious.

For servers you run elsewhere with certificates, IAM Roles Anywhere does the same exchange using X.509. For the reverse direction, AWS workloads proving themselves to outside services, STS can now issue tokens through outbound identity federation.

PassRole: the permission behind the permission

When you launch an instance or create a function with a role, you are passing that role to a service. AWS checks that you have iam:PassRole for it. Without that check, anyone who could create a function could give it an administrator role and run whatever they liked.

{"Effect":"Allow","Action":"iam:PassRole",
 "Resource":"arn:aws:iam::123456789012:role/web-app",
 "Condition":{"StringEquals":{"iam:PassedToService":"ec2.amazonaws.com"}}}

Name the roles. "Resource": "*" here is one of the most common escalation paths in real accounts.

When it goes wrong

Run aws sts get-caller-identity from inside the workload. If it names a different role, or fails to find credentials, look at the attachment first. Leftover AWS_ACCESS_KEY_ID variables override the role silently.

About the video

The embed is Integrations Ninjas’ “Authenticate GitHub Actions with AWS using OIDC”, June 2025, 17 minutes. Its chapters explain the flow, add the identity provider in IAM, create the role and trust policy, write the workflow and verify the identity. It covers only the CI case, and it predates immutable subjects. For EC2, see the short video in deeper. I judged both from chapters and descriptions, since transcripts couldn’t be fetched.

What was run: the instance profile commands ran against moto, a local emulator, which also rejected a second role. No workload or CI job ran, so no token was exchanged. Trust policies, the role table and GitHub’s subject format are written from AWS and GitHub documentation, checked in November 2026.

Where this fits

That is identity for software. People need the same thing, temporary credentials from one sign-in: IAM Identity Center and federation.

Check yourself

  1. 01Two applications run on one EC2 instance. Can the instance profile give them different permissions?

    No. A profile holds one role and everything on the instance that can reach the metadata service gets it. Run them separately, or use containers with task or Pod roles.

  2. 02A GitHub trust policy checks only that aud is sts.amazonaws.com. Why is that dangerous?

    Every repository on GitHub can obtain a token with that audience from the same issuer. Without a sub condition, other people's workflows could assume the role. IAM now rejects such a trust policy for GitHub.

  3. 03A developer may create Lambda functions and has iam:PassRole on all roles. What can they do?

    Attach a more powerful role, such as an administrator role trusting Lambda, to a function they write, then run code with those permissions. Limit PassRole to specific role ARNs.

  4. 04Your on-premises server needs to write to S3 and has no OIDC token. What are the options besides an access key?

    IAM Roles Anywhere, which exchanges an X.509 certificate from a certificate authority you register for temporary role credentials.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark