Many organizations restrict permissions to create and manage AWS Identity and Access Management (IAM) resources to a group of privileged users or a central team. This post explains how also granting these permissions to builders, that is the people who are developing, testing, launching, and managing cloud infrastructure, can speed up your development, increase your agility, and improve your application security. In addition, you will use an example application stack to see how IAM permissions boundaries can help establish a secure, yet agile, work environment for builders.
An example application stack
Defining and creating IAM resources within the application stack allows your builders to craft policies and roles that grant least privilege to application resources. When builders can assume identities with both IAM and application permissions, it is efficient for them to scope policies to specific resources by referencing them directly in a single template.
To illustrate our point, we’ll use a simple “hello world” serverless application. The application includes an AWS Step Functions state machine that, once executed, will invoke a “Hello World!” AWS Lambda function. We will use this example application along with some IAM policies and roles to illustrate how permissions boundaries can change the way IAM permissions are managed.
In this example application template, the Resource element in MyStateMachineExecutionRole which is specified as the role for MyStateMachine, includes a reference to the Amazon Resource Name (ARN) of MyLambdaFunction. This is a great example of the principle of least privilege: MyStateMachine will only have permissions to invoke MyLambdaFunction. Scoping this relationship is straightforward because the IAM, Step Functions, and Lambda resources are defined together in the same template.
Example application template
In an organization that centrally manages IAM resources, a builder trying to deploy this example application may get an error. This is because the roles the builders are granted prohibit most IAM actions, particularly actions like iam:CreateRole and iam:UpdateRole. The stack will fail to deploy because it tries to create IAM resources and the builder lacks the permissions to do so. This creates three key challenges:
- Builders must rely on someone else to create IAM resources, or to launch an application stack that contains the IAM resources they need. This approach either slows down development while builders wait for the roles and policies to be created, or it encourages the central IAM team to grant overly-broad permissions so that they don’t have to be involved in day-to-day changes.
- IAM resources must be created before the rest of the stack, which makes it much more difficult to create least-privilege policies and roles.
- The code for IAM and application infrastructure is managed separately, which requires extra coordination when creating and updating workloads.
Removing these challenges can simplify your cloud development, and lower the amount of overhead and coordination required across your organization.
When IAM resources are created in a separately managed stack before the rest of the application, it encourages organizations to either overuse wildcards (“*”) in policies, or to specify custom physical names for resources.
Sometimes pre-determined names make sense, but managing names of resources between teams adds a layer of configuration management across code that would otherwise be unnecessary. There are also cases where specifying a name prevents updates that require replacing the named resource (for example the Lambda function FunctionName). If you don’t manage custom names, your resources could be partially specified using the predictable part of the resulting name. For example, ARNs for Lambda functions created using AWS CloudFormation include a function name according to the pattern
Note: the reason a wildcard is used in place of
is because that field is truncated when the character limit is reached, so it is not a reliable field to match.
It is possible to update IAM policies with the known resource names after stack creation, but in practice, that is difficult to manage at scale. Also, naming schemas vary across infrastructure as code (IaC) tools, so allowing IAM resources to be created in the application stack makes configuration management more efficient for teams.
Fortunately, by granting builders IAM permissions, your organization will be able to create more secure policies and improve your DevOps posture.
Use permissions boundaries to enforce security perimeters
Organizations that exclude IAM permissions from all but privileged users are trying to enforce security perimeters around identities and resources. So, how can your organization permit actions like iam:CreateRole and iam:UpdateRole in builder policies, without allowing builders to create roles with elevated permissions that go beyond those perimeters? This is where permissions boundaries come in. You can use a managed policy attached to a builder role as a permissions boundary, which will effectively set the maximum permissions that a builder could be granted.
Following the steps in this section, we will create a permissions boundary around our builders. This will allow them to create IAM roles for their resources (like Lambda functions or EC2 instances) with IAM policies attached. But they will only deploy successfully if the policies the create are inside the permissions boundaries.
Create the builder policy and trust policy
In the first procedure, you create a policy that grants builders both the application-related permissions they need, and the permissions to create and manage policies, roles, and instance-profiles.
Note: This procedure assumes the partition in all ARNs is aws, and the URL suffix in principals is amazonaws.com. If you are deploying in another partition, you will need to make the appropriate substitutions.
The builders can be granted these permissions if they meet two conditions:
- The name of the resource starts with the word builder.
- Roles have a builder-boundary policy applied as a permissions boundary.
To create the builder policy
- Open or install the AWS Command Line Interface (CLI) and assume a role that has permissions to create IAM resources.
- Download the builder policy from an S3 bucket to your local machine using the following command:
- Open the builder-policy.json policy and replace
with your account number.
- Create the builder policy using the following command:
Create the permissions boundary
In this procedure, you create a builder boundary policy that can be used as the permissions boundary. This example uses several statements and policy elements to deny permissions both for specific services (such as AWS Organizations and AWS Account Management) and specific actions (such as AWS CloudTrail DeleteTrail), and allows everything else.
Note: In this case, you use a wildcard for the Resource element because you want to deny these permissions for all resources related to these services and actions.
To create the permissions boundary
- Download the builder boundary policy from an S3 bucket to a file named builder-boundary.json by using the following command:
- Create the boundary policy by using the following command:
Create the builder role and set the permissions boundary
Finally, you can create a builder-role, set a builder-trust as the trust policy and a builder-boundary as the permissions boundary, and attach the builder-policy as the permissions policy. The effective permissions for the builder-role will then be the intersection of the two policies: a permission granted in the permissions policy must also be granted in the permissions boundary, or else it will be implicitly denied according to the policy evaluation logic.
To create the builder role and set the permissions boundary
- Download the trust policy from an S3 bucket to a file named builder-trust.json by using the following command:
This policy allows anyone in the account, as well as the CloudFormation and Amazon Elastic Compute Cloud (EC2) services, to assume the builder-role on an Amazon EC2 instance. You can use a combination of the Principal and Condition attributes to reduce its scope.
- Create the role and trust, and set the permissions boundary by using the following command, replacing
with your account number:
- Attach the builder-policy to the role by using the following command, replacing
with your account number:
Test the permissions boundary
In this procedure, you will demonstrate that the permissions boundaries work. You test the new builder-role by trying to launch a stack that includes IAM resources — first without the required permissions boundary attached, and then retrying it after the permissions boundary has been attached.
To test the permissions boundary
- To test, assume the builder-role and try to deploy an example application. Download the example application template shown above and save it as builder-application.yml. Do not make any changes yet — you want to test without it first, to confirm that the guardrails you have put in place are working. Use the following command.
- Assume the builder-role by using the following command:
- Check that the you have now assumed the builder-role by using the following command:
- Launch the stack with CloudFormation by using the following command:
- If things are set up correctly, the stack will fail. To confirm, check the StackStatus by using the following command– it will show ROLLBACK_IN_PROGRESS or ROLLBACK_COMPLETE.
- The stack failed to create because the builder-role you assumed does not have permissions to create the two execution roles without satisfying the conditions of the guardrails you created. To see the details, use the following command:
Look for the section with LogicalResourceId: MyLambdaFunctionExecutionRole and review the output for ResourceStatusReason.
- Because the original stack did not create successfully, you will not be able to update it, so you will need to delete it instead by using the following command:
- To fix your stack, modify the builder-application.yml file by uncommenting the lines with the PermissionsBoundary and RoleName properties in the MyLambdaFunctionExecutionRole and MyStateMachineExecutionRole resources.
For example, the MyLambdaFunctionExecutionRole resource should look like this with your account number for
- Launch a stack with your updated template by using the following command:
- Confirm the StackStatus is CREATE_COMPLETE by using the following command:
The builder-role you assumed does not have permissions to view the AWS Management Console and verify the application you deployed is working. However, you may do so by logging in to the console using another role with the appropriate permissions.
To verify the application you deployed is working
- Log in to the AWS Management Console using another role with the appropriate permissions.
- Navigate to the Step Functions console, choose State machines.
- Choose the state machine you just created, choose Start execution, choose Start execution again, and view the output of the Lambda function.
Now builders can safely assume the new builder-role and launch application stacks that include IAM resources.
After you have finished testing, clean up the resources created in this example. Because the builder-role does not have permissions to delete IAM policies, you will need to assume a different role that can manage IAM resources to complete steps 3-6 below.
To clean up
- Delete the builder-application stack by using the following command:
- Detach the builder-policy from builder-role by using the following command, replacing
with your account number:
- Delete the builder-role by using the following command:
- Delete the builder-policy by using the following command, replacing
with your account number:
- Delete the builder-boundary by using the following command, replacing
with your account number:
Service control policies
Permissions boundaries are applied to individual IAM users or roles within an account. So, if your organization has multiple accounts, you must create and maintain these boundaries in each account. But what if you’d like to apply a set of rules across some or all of your accounts? In this case, you could use service control policies (SCPs), which are a feature of AWS Organizations, to provide central control over the maximum available permissions for multiple accounts in your organization. Like permissions boundaries, SCPs use the same IAM policy language with fine-grained control, but do not actually grant any permissions.
SCPs provide a way for you to simplify control and management of permissions across multiple accounts. And like permissions boundaries, different SCPs can be crafted to be more or less restrictive depending on the account type (for example, dev, test, or prod), and can serve as another guardrail to provide builders the capabilities they need while restricting access to specific services, features, and actions. To learn more about creating SCPs, see the example service control policies section in the AWS Organizations User Guide.
Creating and managing tightly scoped policies and roles is an ongoing process that requires a lot of thought and attention to detail. AWS IAM enables fine-grained access control to AWS services, and permissions boundaries are an advanced feature. Managing IAM permissions boundaries, roles, and policies has its own development, testing, and deployment lifecycle. We recommend learning to use the IAM Policy Simulator to test policies and determine whether or not specific actions are allowed for a given user, group, or role. In addition, you can create and update policies using the Visual editor or JSON tab in the IAM console where you will get insights, warnings, and suggestions related to security and errors in IAM policies.
After policies have been created, it is important to continuously monitor and audit them. Two tools you can use for this are:
- Access Advisor – to review when services and actions were last accessed.
- Access Analyzer – to identify resources that are shared with an external identity.
Lastly, the AWS Cloud Development Kit (CDK) has built-in convenience methods to help you follow best practices, including the ability to generate least-privilege policies for cloud applications with a single line of code.
In this post, you learned how to put policies and guardrails in place that will allow your organization to grant IAM permissions to builders. These changes will enable your builders to develop and deploy cloud infrastructure and applications more rapidly, and will help strengthen your organization’s security culture by extending the responsibility to a broader group. To learn more about creating, testing, and refining IAM policies, see Creating IAM policies, Testing IAM policies, and Refining permissions using access information in the IAM User Guide.
If you have feedback about this post, submit comments in the Comments section below. If you have questions about this post, contact AWS Support.
Want more AWS Security news? Follow us on Twitter.