![[Quick Tip] I Tested How the IAM Role Names Automatically Generated by CloudFormation Get Truncated When the Stack Name or Logical ID Is Long](https://devio2024-media.developers.io/image/upload/f_auto,q_auto/c_limit,w_1200/v1790870737/eyecatch/acbedf6qbbklkr30bzky.png)
[Quick Tip] I Tested How the IAM Role Names Automatically Generated by CloudFormation Get Truncated When the Stack Name or Logical ID Is Long
This page has been translated by machine translation. View original
Hello, I'm Hayashi.
When you create an IAM role in CloudFormation without specifying RoleName, a name in the format <stack name>-<logical ID>-<random string> is generated automatically.
I checked how the name is generated when the stack name and logical ID are long and exceed the IAM role name limit of 64 characters.
Conclusion First
- The stack name and logical ID used in the role name total at most 50 characters
- If both the stack name and the logical ID exceed 25 characters, the first 25 characters of each are used
- If you specify
role/<stack name>-*in theResourceof an IAM policy, it will not match roles whose stack name has been truncated, so either keep the stack name to 25 characters or fewer, or specifyRoleName
What the Documentation Tells Us
The documentation states that the automatically generated name is a combination of the stack name, the logical ID, and a unique ID.
It also states that the limit for RoleName is 64 characters.
I could not confirm from the documentation which parts are used, and how many characters, when the limit is exceeded.
Verification Method
I tested AWS::IAM::Role in the Tokyo region.
I prepared a template defining five IAM roles without specifying RoleName.
| Logical ID | Characters |
|---|---|
Role |
4 |
OrderWorkerExecutionRole |
24 |
OrderApiTaskExecutionRole |
25 |
OrderBatchJobExecutionRole |
26 |
OrderApiPipelineDeployStageCodeBuildProjectServiceRole |
54 |
From this template, I created five stacks with different stack name lengths.
| Stack name | Characters |
|---|---|
dev-app-common |
14 |
stg-order-api-batch-base |
24 |
prod-order-api-monitoring |
25 |
prod-order-api-application |
26 |
stg-order-management-api-ecs-service-deploy-pipeline |
52 |
I checked the role names with describe-stack-resources.
aws cloudformation describe-stack-resources --stack-name dev-app-common \
--query 'StackResources[].[LogicalResourceId,PhysicalResourceId]' --output text
Results
All role names were in the format <stack name>-<logical ID>-<random 12 characters>.
Two hyphens and 12 random characters make 14 characters, so the stack name and logical ID can use at most 50 characters in total.
| Characters in stack name and logical ID | Truncation |
|---|---|
| Total is 50 characters or fewer | None |
| Total exceeds 50 characters, and one of them is 25 characters or fewer | The other is truncated to the remaining number of characters |
| Both exceed 25 characters | Each is truncated to its first 25 characters |
Results for each combination.
The truncated side and the number of characters after truncation are listed.
| Stack name | Logical ID 4 chars | 24 chars | 25 chars | 26 chars | 54 chars |
|---|---|---|---|---|---|
| 14 chars | - | - | - | - | Logical ID 36 |
| 24 chars | - | - | - | - | Logical ID 26 |
| 25 chars | - | - | - | Logical ID 25 | Logical ID 25 |
| 26 chars | - | - | Stack name 25 | Both 25 | Both 25 |
| 52 chars | Stack name 46 | Stack name 26 | Stack name 25 | Both 25 | Both 25 |
Examples of Truncated Role Names
With a 26-character stack name and a 25-character logical ID, the last character of the stack name was truncated.
スタック名: prod-order-api-application
ロール名: prod-order-api-applicatio-OrderApiTaskExecutionRole-9w02y8Ta3WrL
When the 25th character of the stack name is a hyphen, two hyphens appeared in a row.
スタック名: stg-order-management-api-ecs-service-deploy-pipeline
ロール名: stg-order-management-api--OrderApiTaskExecutionRole-IfDGwdvtuFnP
Caution When Specifying Roles with Wildcards in IAM Policies
When you want to allow operations only on roles created by a specific stack, you may specify a wildcard based on the stack name in the Resource of an IAM policy.
Here is an example allowing iam:PassRole.
{
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::123456789012:role/prod-order-api-application-*"
}
A role whose stack name has been truncated does not match this Resource, so it is not covered by the permission.
prod-order-api-applicatio-OrderApiTaskExecutionRole-9w02y8Ta3WrL
Specifying the first 25 characters of the stack name combined with a wildcard matched all the combinations in this test.
arn:aws:iam::123456789012:role/prod-order-api-applicatio*
This specification also matches roles of other stacks whose first 25 characters are the same.
To fix the role name, specify RoleName.
For a role with RoleName specified, updates that require replacement cannot be performed.
Summary
I checked how IAM role names automatically generated by CloudFormation are truncated.
- The stack name and logical ID used in the role name total at most 50 characters
- If both the stack name and the logical ID exceed 25 characters, the first 25 characters of each are used
- If you specify
role/<stack name>-*in theResourceof an IAM policy, it will not match roles whose stack name has been truncated, so either keep the stack name to 25 characters or fewer, or specifyRoleName
I hope this article is helpful to someone. Thank you for reading to the end!
References
