Sub topic 6
Granting least-privilege access with IAM
This chapter assumes you know what IAM users, roles, and policies are, and that you can read JSON. You should also have created a parameter and a secret, as in the earlier chapters. Here you will write the policies that decide who can read them.
Why access control is the main safeguard
Encryption protects stored values. IAM decides who gets the plaintext. An application role that can read every parameter and every secret in an account turns one compromised container into a full credential leak. Least privilege means each identity gets only the actions and resources its job needs, and nothing else.
Three services take part in a read:
- Parameter Store, through
ssm:actions. - Secrets Manager, through
secretsmanager:actions. - AWS KMS, through
kms:actions, when the value is encrypted with a customer managed key.
A caller needs permission on each service involved. A missing KMS permission is the most common cause of an AccessDeniedException that looks like it comes from Parameter Store or Secrets Manager.
An identity policy for an application role
The following policy limits a role to one environment's parameters, one secret, and one KMS key.
{
"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"
}
]
}Each statement does one job, so you can review and change them independently.
Parameter ARNs
A parameter ARN has the form arn:aws:ssm:REGION:ACCOUNT:parameter/PATH. The leading slash of the parameter name is not repeated, so the name /myapp/prod/db/host becomes parameter/myapp/prod/db/host. This is a common source of typos. A parameter whose name has no leading slash, such as myapp-flag, becomes parameter/myapp-flag.
The path hierarchy you designed earlier now pays off. The wildcard parameter/myapp/prod/* covers every parameter under that path at any depth. A role for the staging environment would use parameter/myapp/staging/*, and neither role can read the other's values.
Note that ssm:GetParametersByPath is authorized against the path, not against each child. The Resource entry must match the path ARN itself. For --path /myapp/prod, the ARN is parameter/myapp/prod, and the pattern parameter/myapp/prod/* does not match it. If you call this API, include both forms:
{
"Effect": "Allow",
"Action": "ssm:GetParametersByPath",
"Resource": [
"arn:aws:ssm:us-east-1:111122223333:parameter/myapp/prod",
"arn:aws:ssm:us-east-1:111122223333:parameter/myapp/prod/*"
]
}Also be aware that a recursive call returns only the children the caller may read through ssm:GetParameter style permissions. If the path ARN is allowed but some children are not, expect an access error rather than a partial result. Test your policy with the exact call your code makes.
Secret ARNs and the random suffix
Secrets Manager appends a dash and six random characters to every secret ARN, for example secret:myapp/prod/db-AbCdEf. You cannot know the suffix before you create the secret, and it changes if you delete and recreate the secret with the same name. Writing myapp/prod/db-* in the policy matches any suffix.
There is one edge case. The pattern myapp/prod/db-* also matches a different secret named myapp/prod/db-replica, because the wildcard matches any characters after the dash. If you have names that share a prefix, use myapp/prod/db-??????, which matches exactly six characters.
Granting the KMS permission
Two kinds of keys behave differently:
| Key | Who can use it | What you must do |
|---|---|---|
| AWS managed key (`alias/aws/ssm`, `alias/aws/secretsmanager`) | Any principal in the account that is allowed to call the service | Nothing extra. The key policy already allows use through the service. |
| Customer managed key | Principals named by the key policy or by IAM policies, when the key policy permits it | Grant `kms:Decrypt` in the role, and make sure the key policy allows it. |
For writers, add kms:GenerateDataKey for Secrets Manager, and kms:Encrypt for Parameter Store. Readers need only kms:Decrypt.
You can narrow the KMS grant further with a condition that ties key use to the service:
{
"Effect": "Allow",
"Action": "kms:Decrypt",
"Resource": "arn:aws:kms:us-east-1:111122223333:key/EXAMPLE-KEY-ID",
"Condition": {
"StringEquals": {
"kms:ViaService": "ssm.us-east-1.amazonaws.com"
}
}
}With this condition the role can decrypt with that key only when the request comes through Parameter Store. A stolen credential then cannot call KMS directly to decrypt other data encrypted with the same key.
Separating readers from writers
Most roles should read only. Keep write and administrative actions in a separate role, used by a pipeline or an operator.
| Task | Parameter Store action | Secrets Manager action |
|---|---|---|
| Read one value | `ssm:GetParameter`, `ssm:GetParameters` | `secretsmanager:GetSecretValue` |
| Read a whole path | `ssm:GetParametersByPath` | Not available |
| Create or update | `ssm:PutParameter` | `secretsmanager:CreateSecret`, `secretsmanager:PutSecretValue` |
| Delete | `ssm:DeleteParameter` | `secretsmanager:DeleteSecret` |
| Change who can access | `ssm:AddTagsToResource` and tag-based policies | `secretsmanager:PutResourcePolicy` |
Avoid ssm:* and secretsmanager:* on Resource: "*" in any application policy. Also avoid granting secretsmanager:ListSecrets. Listing exposes names and metadata, which often reveal system layout, and applications that know their secret name never need it.
Tag-based access control
When many teams share an account, per-ARN statements become hard to maintain. Both services support tags, so you can write one policy that matches a tag on the caller and a tag on the resource:
{
"Effect": "Allow",
"Action": ["ssm:GetParameter", "ssm:GetParameters"],
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:ResourceTag/team": "${aws:PrincipalTag/team}"
}
}
}A role tagged team=payments can then read only parameters tagged team=payments. The trade-off is that anyone allowed to change tags can change access. Restrict ssm:AddTagsToResource, ssm:RemoveTagsFromResource, and secretsmanager:TagResource as tightly as you restrict the reads.
Resource policies for secrets
Secrets Manager secrets can carry a resource-based policy. Parameters cannot, except for advanced-tier parameters shared through AWS Resource Access Manager. A resource policy matters mainly for cross-account access, covered in a later chapter. Within one account, an identity policy or a resource policy alone is enough to grant access. Across accounts, both sides must allow the request.
You can also use a resource policy as a guardrail, for example to deny everyone except a named role:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Deny",
"Principal": "*",
"Action": "secretsmanager:GetSecretValue",
"Resource": "*",
"Condition": {
"StringNotEquals": {
"aws:PrincipalArn": "arn:aws:iam::111122223333:role/myapp-prod"
}
}
}
]
}This denies every principal except that role, including administrators, so they cannot read the value without first editing the policy. Test it carefully, because a mistake can lock out people you meant to keep.
Common errors
AccessDeniedExceptionon a SecureString even thoughssm:GetParameteris allowed. The role lackskms:Decrypton the customer managed key, or the key policy does not allow it.- Access works for
GetParameterbut fails forGetParametersByPath. TheResourcedoes not include the path ARN itself. - Policy works in one Region only. ARNs contain the Region. Use the correct Region, or
*in that field if the role must work in several Regions. - Policy stops working after a secret is recreated. If your policy names the full ARN with the suffix, the new random suffix no longer matches. Use
-*or-??????. - A role cannot read a secret it created. Creating and reading are separate actions. Grant
GetSecretValueexplicitly.
You can check a policy without running the application. The IAM policy simulator, or the CLI command below, evaluates the policy for a role and an action:
aws iam simulate-principal-policy \
--policy-source-arn arn:aws:iam::111122223333:role/myapp-prod \
--action-names ssm:GetParameter \
--resource-arns arn:aws:ssm:us-east-1:111122223333:parameter/myapp/staging/db/hostThe result for this staging parameter should be implicitDeny. If it shows allowed, your policy is wider than you intended.
Exercise
A role has ssm:GetParameter allowed on arn:aws:ssm:us-east-1:111122223333:parameter/myapp/prod/*. A call to read the SecureString /myapp/prod/db/password with decryption fails with AccessDeniedException. The parameter uses a customer managed KMS key. Which permission is most likely missing, and how would you scope it?
Show answer
The role is missing kms:Decrypt on that customer managed key (and the key policy must also permit it). Scope it to the key's ARN in the Resource field, and optionally add a kms:ViaService condition set to ssm.us-east-1.amazonaws.com so the key can be used only through Parameter Store.
Exercise
You have two secrets, myapp/prod/db and myapp/prod/db-replica. You want a role to read only the first. Why is secret:myapp/prod/db-* a poor choice of resource, and what pattern is better?
Show answer
Secrets Manager adds a dash and six random characters to each ARN, and -* also matches db-replica. Use secret:myapp/prod/db-??????, which matches exactly six characters after the dash and therefore excludes db-replica.