Deploy and manage AWS Control Tower controls by using Terraform - AWS Prescriptive Guidance

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.

Architecture diagram of controls deployed across all AWS accounts in the organizational unit.

AWS Control Tower controls are categorized according to their behavior and their guidance.

There are three primary types of control behaviors:

  1. 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.

  2. 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.

  3. 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 repository.

Best practices

Epics

TaskDescriptionSkills 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 repository from GitHub.

git clone https://github.com/aws-samples/aws-control-tower-controls-terraform.git
DevOps engineer

Edit the Terraform backend configuration file.

  1. In the cloned repository, open the backend.tf file.

  2. Edit the file to set the Terraform backend configuration. The configuration you define in this file depends on your environment. For more information, see Backend configuration (Terraform documentation).

  3. Save and close the backend.tf file.

DevOps engineer, Terraform

Edit the Terraform provider configuration file.

  1. In the cloned repository, open the provider.tf file.

  2. Edit the file to set the Terraform provider configuration. For more information, see Provider configuration (Terraform documentation). Set the AWS Region as the Region where the AWS Control Tower API is available.

  3. Save and close the provider.tf file.

DevOps engineer, Terraform

Edit the configuration file.

  1. In the cloned repository, open the variables.tfvars file.

  2. In the controls section, in the control_names parameter, enter the control API identifier. Each control has a unique API identifier for each Region in which AWS Control Tower is available. To find the control identifier, do the following:

    1. In Tables of control metadata, locate the control you want to enable.

    2. In the Control API identifiers, by Region column, locate the API identifier for the Region in which you are making the API call, such as arn:aws:controltower:us-east-1::control/AWS-GR_AUDIT_BUCKET_ENCRYPTION_ENABLED.

    3. Extract the control identifier from the Regional identifier, such as AWS-GR_AUDIT_BUCKET_ENCRYPTION_ENABLED.

  3. In the controls section, in the organizational_unit_ids parameter, enter the ID of the organizational unit where you want to enable the control, such as ou-1111-11111111. Enter the ID in double quotation marks, and separate multiple IDs with commas. For more information about how to retrieve OU IDs, see Viewing the details of an OU.

  4. Save and close the variables.tfvars file. For an example of an updated variables.tfvars file, see the Additional information section of this pattern.

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.

  1. Enter the following command to initialize Terraform.

    $ terraform init -upgrade
  2. Enter the following command to preview the changes compared the current state.

    $ terraform plan -var-file="variables.tfvars"
  3. Review the configuration changes in the Terraform plan and confirm that you want to implement these changes in the organization.

  4. Enter the following command to deploy the resources.

    $ terraform apply -var-file="variables.tfvars"
DevOps engineer, General AWS, Terraform
TaskDescriptionSkills required

Run the destroy command.

Enter the following command to remove the resources deployed by this pattern.

$ terraform destroy -var-file="variables.tfvars"
DevOps engineer, General AWS, Terraform

Troubleshooting

IssueSolution

Error: creating ControlTower Control ValidationException: Guardrail <control ID> is already enabled on organizational unit <OU ID> error

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 apply command, Terraform will skip this resource. Do the following to import the resource to the current Terraform state:

  1. In the AWS Control Tower management account, enter the following command to retrieve a list of the Amazon Resource Names (ARNs) for the OUs, where <root-ID> is the organization root. For more information about retrieving this ID, see Viewing the details of the root.

    aws organizations list-organizational-units-for-parent --parent-id <root-ID>
  2. For each OU returned in the previous step, enter the following command, where <OU-ARN> is the ARN of the OU.

    aws controltower list-enabled-controls --target-identifier <OU-ARN>
  3. Copy the ARNs and perform the Terraform import in the required module so that it is included in the Terraform state. For instructions, see Import (Terraform documentation).

  4. Repeat the steps in Deploy the configuration in the Epics section.

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

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": "*" } ] }