Deploy and manage AWS Control Tower controls by using Terraform
Created by Iker Reina Fuente (AWS) and Ivan Girardi (AWS)
Code repository: Deploy and manage AWS Control Tower controls by using Terraform | Environment: Production | Technologies: Security, identity, compliance; CloudNative; Infrastructure; Management & governance |
Workload: Open-source | AWS services: AWS Control Tower; AWS Organizations |
Summary
This pattern describes how to use AWS Control Tower controls, HashiCorp Terraform, and infrastructure as code (IaC) to implement and administer preventive, detective, and proactive security controls. A control (also known as a guardrail) is a high-level rule that provides ongoing governance for your overall AWS Control Tower environment. For example, you can use controls to require logging for your AWS accounts and then configure automatic notifications if specific security-related events occur.
AWS Control Tower helps you implement preventive, detective, and proactive controls that govern your AWS resources and monitor compliance across multiple AWS accounts. Each control enforces a single rule. In this pattern, you use a provided IaC template to specify which controls you want to deploy in your environment.
AWS Control Tower controls apply to an entire organizational unit (OU), and the control affects every AWS account within the OU. Therefore, when users perform any action in any account in your landing zone, the action is subject to the controls that govern the OU.
Implementing AWS Control Tower controls helps establish a strong security foundation for your AWS landing zone. By using this pattern to deploy the controls as IaC through Terraform, you can standardize the controls in your landing zone and more efficiently deploy and manage them.
To deploy AWS Control Tower controls as IaC, you can also use AWS Cloud Development Kit (AWS CDK) instead of Terraform. For more information, see Deploy and manage AWS Control Tower controls by using AWS CDK and AWS CloudFormation.
Target audience
This pattern is recommended for users who have experience with AWS Control Tower, Terraform, and AWSÂ Organizations.
Prerequisites and limitations
Prerequisites
Active AWS accounts managed as an organization in AWS Organizations and an AWS Control Tower landing zone. For instructions, see Create an account structure
(AWS Well-Architected Labs). AWS Command Line Interface (AWS CLI), installed and configured.
An AWS Identity and Access Management (IAM) role in the management account that has permissions to deploy this pattern. For more information about the required permissions and a sample policy, see Least privilege permissions for the IAM role in the Additional information section of this pattern.
Permissions to assume the IAM role in the management account.
Apply the service control policy (SCP)-based control with the identifier CT.CLOUDFORMATION.PR.1. This SCP must be activated to deploy proactive controls. For instructions, see Disallow management of resource types, modules, and hooks within the AWS CloudFormation registry.
Terraform CLI, installed
(Terraform documentation). Terraform AWS Provider, configured
(Terraform documentation). Terraform backend, configured
(Terraform documentation).
Product versions
AWS Control Tower version 3.0 or later
Terraform version 1.5 or later
Terraform AWS Provider version 4.67 or later
Architecture
Target architecture
This section provides a high-level overview of this solution and the architecture established by the sample code. The following diagram shows controls deployed across the various accounts in the OU.
AWS Control Tower controls are categorized according to their behavior and their guidance.
There are three primary types of control behaviors:
Preventive controls are designed to prevent actions from occurring. These are implemented with service control policies (SCPs) in AWS Organizations. The status of a preventive control is either enforced or not enabled. Preventive controls are supported in all AWS Regions.
Detective controls are designed to detect specific events when they occur and log the action in CloudTrail. These are implemented with AWS Config rules. The status of a detective control is either clear, in violation, or not enabled. Detective controls apply only in those AWS Regions supported by AWS Control Tower.
Proactive controls scan resources that would be provisioned by AWS CloudFormation and check whether they are compliant with your company policies and objectives. Resources that are not compliant will not be provisioned. These are implemented with AWS CloudFormation hooks. The status of a proactive control is PASS, FAIL, or SKIP.
Control guidance is the recommended practice for how to apply each control to your OUs. AWS Control Tower provides three categories of guidance: mandatory, strongly recommended, and elective. The guidance of a control is independent of its behavior. For more information, see Control behavior and guidance.
Tools
AWS services
AWS CloudFormation helps you set up AWS resources, provision them quickly and consistently, and manage them throughout their lifecycle across AWS accounts and Regions.
AWS Config provides a detailed view of the resources in your AWS account and how they’re configured. It helps you identify how resources are related to one another and how their configurations have changed over time.
AWS Control Tower helps you set up and govern an AWS multi-account environment, following prescriptive best practices.
AWS Organizations is an account management service that helps you consolidate multiple AWS accounts into an organization that you create and centrally manage.
Other tools
HashiCorp Terraform
is an open-source infrastructure as code (IaC) tool that helps you use code to provision and manage cloud infrastructure and resources.
Code repository
The code for this pattern is available in the GitHub Deploy and manage AWS Control Tower controls by using Terraform
Best practices
The IAM role used to deploy this solution should adhere to the principle of least-privilege (IAM documentation).
Follow the Best practices for AWS Control Tower administrators (AWS Control Tower documentation).
Epics
Task | Description | Skills required |
---|---|---|
Clone the repository. | In a bash shell, enter the following command. This clones the Deploy and manage AWS Control Tower controls by using Terraform
| DevOps engineer |
Edit the Terraform backend configuration file. |
| DevOps engineer, Terraform |
Edit the Terraform provider configuration file. |
| DevOps engineer, Terraform |
Edit the configuration file. |
| DevOps engineer, General AWS, Terraform |
Assume the IAM role in the management account. | In the management account, assume the IAM role that has permissions to deploy the Terraform configuration file. For more information about the permissions required and a sample policy, see Least privilege permissions for the IAM role in the Additional information section. For more information about assuming an IAM role in the AWS CLI, see Use an IAM role in the AWS CLI. | DevOps engineer, General AWS |
Deploy the configuration file. |
| DevOps engineer, General AWS, Terraform |
Task | Description | Skills required |
---|---|---|
Run the destroy command. | Enter the following command to remove the resources deployed by this pattern.
| DevOps engineer, General AWS, Terraform |
Troubleshooting
Issue | Solution |
---|---|
| The control you are trying to enable is already enabled in the target OU. This error can occur if a user manually enabled the control through the AWS Management Console, through AWS Control Tower or through AWS Organizations. To deploy the Terraform configuration file, you can use either of the following options. Option 1: Update the Terraform current state file You can import the resource to the Terraform current state file. When you rerun the
Option 2: Disable the control If you are working in a non-production environment, you can disable the control in the console. Reenable it by repeating the steps in Deploy the configuration in the Epics section. This approach is not recommended for production environments because there is a period of time when the control will be disabled. If you want to use this option in a production environment, you can implement temporary controls, such as temporarily applying a SCP in AWS Organizations. |
Related resources
AWS documentation
About controls (AWS Control Tower documentation)
Controls library (AWS Control Tower documentation)
Deploy and manage AWS Control Tower controls by using AWS CDK and AWS CloudFormation (AWS Prescriptive Guidance)
Other resources
Additional information
Example variables.tfvars file
The following is an example of an updated variables.tfvars file.
controls = [ { control_names = [ "AWS-GR_ENCRYPTED_VOLUMES", ... ], organizational_unit_ids = ["ou-1111-11111111", "ou-2222-22222222"...], }, { control_names = [ "AWS-GR_SUBNET_AUTO_ASSIGN_PUBLIC_IP_DISABLED", ... ], organizational_unit_ids = ["ou-1111-11111111"...], }, ]
Least privilege permissions for the IAM role
This APG pattern requires that you assume an IAM role in the management account. Best practice is to assume a role with temporary permissions and limit the permissions according to the principle of least privilege. The following sample policy allows the minimum actions required to enable or disable AWS Control Tower controls.
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "controltower:EnableControl", "controltower:DisableControl", "controltower:GetControlOperation", "controltower:ListEnabledControls", "organizations:AttachPolicy", "organizations:CreatePolicy", "organizations:DeletePolicy", "organizations:DescribeOrganization", "organizations:DetachPolicy", "organizations:ListAccounts", "organizations:ListAWSServiceAccessForOrganization", "organizations:ListChildren", "organizations:ListOrganizationalUnitsForParent", "organizations:ListParents", "organizations:ListPoliciesForTarget", "organizations:ListRoots", "organizations:UpdatePolicy" ], "Resource": "*" } ] }