Sub topic 8
Giving the role only the permissions it needs
This chapter assumes you can read an IAM policy document, you have created the github-deploy role from the earlier chapters, and you know the basic AWS CLI. The trust policy decides who can assume the role. The permissions policy, covered here, decides what the role can do once someone has assumed it.
Two policies, two questions
An IAM role has two kinds of policy, and they answer different questions.
- The trust policy answers "which identities may become this role?" You wrote it with
sts:AssumeRoleWithWebIdentityand conditions onaudandsub. - The permissions policy answers "what may a session of this role do?" It uses the same JSON grammar as any identity policy.
A tight trust policy with a broad permissions policy is still risky. Anyone who can run a job that matches the sub condition, including a compromised dependency or a malicious pull request merged to main, gets everything the permissions policy grants. Treat the permissions policy as the limit on the damage if the trust policy is ever bypassed.
Start from what the job actually calls
Work out the minimum from the workflow, not from guesses. The deployment workflow in this tutorial runs two AWS commands:
aws sts get-caller-identity, which needs no permission at all.aws s3 sync ./site s3://my-example-bucket --delete.
aws s3 sync is a high-level command that makes several API calls for you:
| What sync does | IAM action | Resource type |
|---|---|---|
| Lists the bucket to compare it with local files | `s3:ListBucket` | The bucket |
| Uploads new or changed files | `s3:PutObject` | Objects in the bucket |
| Removes remote files that no longer exist locally (`--delete`) | `s3:DeleteObject` | Objects in the bucket |
The table shows a detail that causes many errors. s3:ListBucket applies to the bucket ARN (arn:aws:s3:::my-example-bucket). s3:PutObject and s3:DeleteObject apply to object ARNs (arn:aws:s3:::my-example-bucket/*). If you put every action on one resource, some calls will be denied.
A least-privilege policy for the site deployment
Save this as deploy-policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListSiteBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-example-bucket"
},
{
"Sid": "WriteSiteObjects",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::my-example-bucket/*"
}
]
}The policy has no wildcard in Action. It names one bucket. It also does not grant s3:GetObject, because sync uploading from local disk does not read object contents. Attach it to the role as an inline policy:
aws iam put-role-policy \
--role-name github-deploy \
--policy-name deploy-site-bucket \
--policy-document file://deploy-policy.jsonAn inline policy lives and dies with the role, which suits a policy that exists only for this deployment. Use a customer managed policy instead if several roles will share the same permissions.
Adding permissions when the workflow grows
When you add a step, add only the action that step needs. Suppose the workflow then invalidates a CloudFront cache:
{
"Sid": "InvalidateSiteCache",
"Effect": "Allow",
"Action": "cloudfront:CreateInvalidation",
"Resource": "arn:aws:cloudfront::123456789012:distribution/EXAMPLEDISTID"
}Add this object to the Statement array and replace the distribution ID with your own. CloudFront supports resource-level permissions for this action, so scope it to one distribution. Not every AWS action supports resource-level scoping. For those actions the only valid Resource is *, and you should check the service's "Actions, resources, and condition keys" page in the IAM documentation before assuming you can narrow it.
Habits that keep the role small
- Do not attach
AdministratorAccessorPowerUserAccessto a CI role. They are convenient while debugging and rarely get removed. - Avoid
"Action": "s3:*". It includes bucket deletion and policy changes, which a static-site deploy never needs. - Keep one role per purpose. A role that deploys the site should not also manage IAM or databases. Split roles by workflow, then use the
subcondition to decide which workflow may assume which role. - Separate read-only from write roles. A pull request workflow that only runs
terraform planoraws s3 lscan assume a role with read access, while the deploy role is limited tomainor a protected environment. - Prefer explicit resource ARNs over
*. Where a name is predictable, such asmy-example-bucket, use it.
Optional guardrail: a permissions boundary
A permissions boundary is a separate policy that sets the maximum permissions a role can ever have. The role's effective permissions are the intersection of its own policies and the boundary. Teams that let developers create CI roles often require a boundary so no role can exceed an agreed ceiling, such as "S3 and CloudFront only". It is not needed for a single deployment role, but it is worth knowing when many roles are created.
Check the policy before relying on it
You can test the policy without running a deployment. The IAM policy simulator is available through the CLI:
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::123456789012:role/github-deploy \
--action-names s3:PutObject s3:DeleteObject \
--resource-arns arn:aws:s3:::my-example-bucket/index.html \
--query 'EvaluationResults[].[EvalActionName,EvalDecision]' \
--output tableBoth actions should return allowed. Repeat the command with another bucket's ARN and expect implicitDeny. Running the denial test is the evidence that the policy is narrow.
If you already have a working but over-broad role, IAM Access Analyzer can generate a policy from the actions recorded in CloudTrail for that role. Treat the result as a draft. It only knows about calls that happened during the period you analyze, so a rare step, such as a monthly cleanup, can be missing.
Common mistakes
AccessDeniedonListBucketonly. The action was placed on the object ARN (.../*) instead of the bucket ARN.sync --deleteuploads files but fails on removal.s3:DeleteObjectis missing.- Permissions work locally, fail in CI. Your local credentials belong to a different identity. Run
aws sts get-caller-identityin the workflow to see which role the job is using. - Policy edits do not seem to apply. IAM changes are usually effective within seconds, but sessions already in progress use the updated policy on their next request, so re-run the job before assuming the edit failed.
Exercise
A workflow runs aws s3 sync ./dist s3://shop-assets --delete, then aws cloudfront create-invalidation --distribution-id E123ABC --paths "/*". Write the statements needed in the role's permissions policy, using account ID 111122223333, and explain why s3:ListBucket and s3:PutObject cannot share one statement.
Show answer
Statement 1: Action s3:ListBucket on Resource arn:aws:s3:::shop-assets. Statement 2: Actions s3:PutObject and s3:DeleteObject on Resource arn:aws:s3:::shop-assets/*. Statement 3: Action cloudfront:CreateInvalidation on Resource arn:aws:cloudfront::111122223333:distribution/E123ABC. s3:ListBucket is a bucket-level action, so it only matches the bucket ARN. s3:PutObject is an object-level action, so it only matches object ARNs ending in /*. A single statement with one resource cannot match both types.
Exercise
A teammate proposes attaching AdministratorAccess to github-deploy because the trust policy already limits the role to the main branch. Give two reasons this is a poor idea.
Show answer
First, the trust policy only controls who can assume the role. Anyone who can run code on main, including through a compromised dependency or a merged malicious change, would get full account access. Second, administrator permissions let a job change IAM itself, for example by editing its own policy or creating new access keys, so a single mistake can become a persistent compromise. Scoping the permissions policy to the actions the deployment uses limits the damage in both cases.