Sub topic 6
Granting least-privilege access with IAM
This chapter assumes you know what IAM roles and policies are, can read JSON, and have created a parameter and a secret as in the earlier chapters. It covers how to write an identity policy that lets an application read only what it needs.
Three services, three permission checks
Reading one value can involve up to three services. Each service checks permissions separately.
| You read... | Service action | Also needed |
|---|---|---|
| A String or StringList parameter | `ssm:GetParameter`, `ssm:GetParameters`, `ssm:GetParametersByPath` | Nothing else |
| A SecureString parameter | The same `ssm:` actions | `kms:Decrypt` on the key, if the key is a customer managed key |
| A Secrets Manager secret | `secretsmanager:GetSecretValue` | `kms:Decrypt` on the key, if the key is a customer managed key |
If you use the AWS managed keys (alias/aws/ssm or aws/secretsmanager), you do not need to grant kms:Decrypt in the identity policy of a principal in the same account. A customer managed key needs an explicit grant, either in the key policy or in the identity policy.
Scope by resource
Parameter ARNs follow the hierarchy you designed. A wildcard at the end of a path limits access to one environment.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["ssm:GetParameter", "ssm:GetParameters", "ssm:GetParametersByPath"],
"Resource": "arn:aws:ssm:us-east-1:111122223333:parameter/myapp/prod/*"
},
{
"Effect": "Allow",
"Action": "secretsmanager:GetSecretValue",
"Resource": "arn:aws:secretsmanager:us-east-1:111122223333:secret:myapp/prod/db-*"
},
{
"Effect": "Allow",
"Action": "kms:Decrypt",
"Resource": "arn:aws:kms:us-east-1:111122223333:key/EXAMPLE-KEY-ID"
}
]
}Notes on this policy:
- In a parameter ARN, the leading slash of the name is not repeated. The name
/myapp/prod/db/hostbecomesparameter/myapp/prod/db/host. A parameter with no leading slash is writtenparameter/name. - Secrets Manager appends a random six-character suffix to every secret ARN. The trailing
-*matches it. Without it, the statement matches nothing. GetParametersByPathis authorized against the path you request. A policy forparameter/myapp/prod/*allows listing/myapp/prodand everything below it.- The key ARN is the key itself, not the alias.
How the decision is made
For a request in the same account, IAM allows it if any identity policy or resource policy allows it and nothing denies it. An explicit Deny always wins. A KMS key is the exception to the same-account shortcut. Its key policy must either allow the principal directly or delegate to IAM, which the default key policy does by trusting the account's root.
Common errors
AccessDeniedExceptionnamingssm:GetParameter: The resource ARN in your policy does not match the parameter. Check for a doubled slash, a wrong Region, or a wrong account ID.AccessDeniedExceptionnamingkms:Decrypt: Thessmorsecretsmanagerpermission is fine. Add the key to the policy, or confirm the key policy allows the role.AccessDeniedExceptionon a secret that exists: The-*suffix is missing from the secret ARN.- Works in the console, fails in code: The console uses your user's permissions, and the application uses its role's.
Exercise
A Lambda function's role has the policy above. It calls GetSecretValue on myapp/prod/db and gets an access denied error that names kms:Decrypt. The secret policy statement is correct. What are two likely causes?
Show answer
The secret is encrypted with a different customer managed key than the one in the policy's kms:Decrypt statement, or the key policy does not allow the role (and does not delegate to IAM). Updating the identity policy to name the right key ARN, or allowing the role in the key policy, fixes it.