Implement Account Factory for Terraform (AFT) by using a bootstrap pipeline
Created by Vinicius Elias (AWS) and Edgar Costa Filho (AWS)
Summary
Notice: AWS CodeCommit is no longer available to new customers. Existing customers of AWS CodeCommit can continue to use the service as normal. Learn more
This pattern provides a simple and secure method for deploying AWS Control Tower Account Factory for Terraform (AFT) from the management account of AWS Organizations. The core of the solution is an AWS CloudFormation template that automates the AFT configuration by creating a Terraform pipeline, which is structured to be easily adaptable for initial deployment or subsequent updates.
Security and data integrity are top priorities at AWS, so the Terraform state file, which is a critical component that tracks the state of the managed infrastructure and configurations, is securely stored in an Amazon Simple Storage Service (Amazon S3) bucket. This bucket is configured with several security measures, including server-side encryption and policies to block public access, to help ensure that your Terraform state is safeguarded against unauthorized access and data breaches.
The management account orchestrates and oversees the entire environment, so it is a critical resource in AWS Control Tower. This pattern follows AWS best practices and ensures that the deployment process is not only efficient but also aligns with security and governance standards, to offer a comprehensive, secure, and efficient way to deploy AFT in your AWS environment.
For more information about AFT, see the AWS Control Tower documentation.
Prerequisites and limitations
Prerequisites
A basic AWS multi-account environment with the following accounts at the minimum: management account, Log Archive account, Audit account, and one additional account for AFT management.
An established AWS Control Tower environment. The management account should be properly configured, because the CloudFormation template will be deployed within it.
The necessary permissions in the AWS management account. You'll need sufficient permissions to create and manage resources such as S3 buckets, AWS Lambda functions, AWS Identity and Access Management (IAM) roles, and AWS CodePipeline projects.
Familiarity with Terraform. Understanding Terraform's core concepts and workflow is important because the deployment involves generating and managing Terraform configurations.
Limitations
Be aware of the AWS resource quotas in your account. The deployment might create multiple resources, and encountering service quotas could impede the deployment process.
The template is designed for specific versions of Terraform and AWS services. Upgrading or changing versions might require template modifications.
Product versions
Terraform version 1.5.7 or later
AFT version 1.11.1 or later
Architecture
Target technology stack
AWS CloudFormation
AWS CodeBuild
AWS CodeCommit
AWS CodePipeline
Amazon EventBridge
IAM
AWS Lambda
Amazon S3
Target architecture
The following diagram illustrates the implementation discussed in this pattern.
The workflow consists of three main tasks: creating the resources, generating the content, and running the pipeline.
Creating the resources
The CloudFormation template that's provided with this pattern
A CodeCommit repository to store the AFT Terraform bootstrap code
An S3 bucket to store the Terraform state file that's associated with the AFT implementation
A CodePipeline pipeline
Two CodeBuild projects to implement the Terraform plan and apply commands in different stages of the pipeline
IAM roles for CodeBuild and CodePipeline services
A second S3 bucket to store pipeline runtime artifacts
An EventBridge rule to capture CodeCommit repository changes on the
main
branchAnother IAM role for the EventBridge rule
Additionally, if you set the Generate AFT Files
parameter in the CloudFormation template to true
, the template creates these additional resources to generate the content:
An S3 bucket to store the generated content and to be used as the source of the CodeCommit repository
A Lambda function to process the given parameters and generate the appropriate content
An IAM function to run the Lambda function
A CloudFormation custom resource that runs the Lambda function when the template is deployed
Generating the content
To generate the AFT bootstrap files and their content, the solution uses a Lambda function and an S3 bucket. The function creates a folder in the bucket, and then creates two files inside the folder: main.tf
and backend.tf
. The function also processes the provided CloudFormation parameters and populates these files with predefined code, replacing the respective parameter values.
To view the code that's used as a template to generate the files, see the solution's GitHub repository
main.tf
module "aft" { source = "github.com/aws-ia/terraform-aws-control_tower_account_factory?ref=<aft_version>" # Required variables ct_management_account_id = "<ct_management_account_id>" log_archive_account_id = "<log_archive_account_id>" audit_account_id = "<audit_account_id>" aft_management_account_id = "<aft_management_account_id>" ct_home_region = "<ct_home_region>" # Optional variables tf_backend_secondary_region = "<tf_backend_secondary_region>" aft_metrics_reporting = "<false|true>" # AFT Feature flags aft_feature_cloudtrail_data_events = "<false|true>" aft_feature_enterprise_support = "<false|true>" aft_feature_delete_default_vpcs_enabled = "<false|true>" # Terraform variables terraform_version = "<terraform_version>" terraform_distribution = "<terraform_distribution>" }
backend.tf
terraform { backend "s3" { region = "<aft-main-region>" bucket = "<s3-bucket-name>" key = "aft-setup.tfstate" } }
During the CodeCommit repository creation, if you set the Generate AFT Files
parameter to true
, the template uses the S3 bucket with the generated content as the source of the main
branch to automatically populate the repository.
Running the pipeline
After the resources have been created and the bootstrap files have been configured, the pipeline runs. The first stage (Source) fetches the source code from the main branch of the repository, and the second stage (Build) runs the Terraform plan command and generates the results to be reviewed. In the third stage (Approval), the pipeline waits for a manual action to approve or reject the last stage (Deploy). At the last stage, the pipeline runs the Terraform apply
command by using the result of the previous Terraform plan
command as input. Finally, a cross-account role and the permissions in the management account are used to create the AFT resources in the AFT management account.
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 CodeBuild is a fully managed build service that helps you compile source code, run unit tests, and produce artifacts that are ready to deploy.
AWS CodeCommit is a version control service that helps you privately store and manage Git repositories without needing to manage your own source control system.
AWS CodePipeline helps you quickly model and configure the different stages of a software release and automate the steps required to release software changes continuously.
AWS Lambda is a compute service that runs your code in response to events and automatically manages compute resources, providing a fast way to create a modern, serverless application for production.
AWS SDK for Python (Boto3)
is a software development kit that helps you integrate your Python application, library, or script with AWS services.
Other tools
Terraform
is an infrastructure as code (IaC) tool that lets you build, change, and version infrastructure safely and efficiently. This includes low-level components such as compute instances, storage, and networking; and high-level components such as DNS entries and SaaS features. Python
is an easy to learn, powerful programming language. It has efficient high-level data structures and provides a simple but effective approach to object-oriented programming.
Code repository
The code for this pattern is available in the GitHub AFT bootstrap pipeline repository
For the official AFT repository, see AWS Control Tower Account Factory for Terraform
Best practices
When you deploy AFT by using the provided CloudFormation template, we recommend that you follow best practices to help ensure a secure, efficient, and successful implementation. Key guidelines and recommendations for implementing and operating the AFT include the following.
Thorough review of parameters: Carefully review and understand each parameter in the CloudFormation template. Accurate parameter configuration is crucial for the correct setup and functioning of AFT.
Regular template updates: Keep the template updated with the latest AWS features and Terraform versions. Regular updates help you take advantage of new functionality and maintain security.
Versioning: Pin your AFT module version and use a separate AFT deployment for testing if possible.
Scope: Use AFT only to deploy infrastructure guardrails and customizations. Do not use it to deploy your application.
Linting and validation: The AFT pipeline requires a linted and validated Terraform configuration. Run lint, validate, and test before pushing the configuration to AFT repositories.
Terraform modules: Build reusable Terraform code as modules, and always specify the Terraform and AWS provider versions to match your organization's requirements.
Epics
Task | Description | Skills required |
---|---|---|
Prepare the AWS Control Tower environment. | Set up and configure AWS Control Tower in your AWS environment to ensure centralized management and governance for your AWS accounts. For more information, see Getting started with AWS Control Tower in the AWS Control Tower documentation. | Cloud administrator |
Launch the AFT management account. | Use the AWS Control Tower Account Factory to launch a new AWS account to serve as your AFT management account. For more information, see Provision accounts with AWS Service Catalog Account Factory in the AWS Control Tower documentation. | Cloud administrator |
Task | Description | Skills required |
---|---|---|
Launch the CloudFormation template. | In this epic, you deploy the CloudFormation template provided with this solution to set up the AFT bootstrap pipeline in your AWS management account. The pipeline deploys the AFT solution in the AFT management account that you set up in the previous epic. Step 1: Open the AWS CloudFormation console
Step 2: Create a new stack
Step 3: Configure stack parameters
Step 4: Decide on file generation
Step 5: Fill in AWS Control Tower and AFT account details
Step 6: Configure AFT options
Step 7: Specify versions
Step 8: Review and create the stack
Step 9: Monitor stack creation
Step 10: Verify the deployment
| Cloud administrator |
Task | Description | Skills required |
---|---|---|
Populate the AFT bootstrap repository. | (Optional) After you deploy the CloudFormation template, you can populate or validate the content in the newly created AFT bootstrap repository, and test whether the pipeline has run successfully. If you set the Step 1: Populate the repository
Step 2: Commit and push your changes
| Cloud administrator |
Validate the AFT bootstrap pipeline. | Step 1: View the pipeline
Step 2: Approve the Terraform plan results
Step 3: Wait for the deployment
Step 4: Check created resources
| Cloud administrator |
Troubleshooting
Issue | Solution |
---|---|
The custom Lambda function included in the CloudFormation template fails during deployment. | Check the Amazon CloudWatch logs for the Lambda function to identify the error. The logs provide detailed information and can help pinpoint the specific issue. Confirm that the Lambda function has the necessary permissions and that the environment variables have been set correctly. |
You encounter failures in resource creation or management caused by inadequate permissions. | Review the IAM roles and policies that are attached to the Lambda function, CodeBuild, and other services involved in the deployment. Confirm that they have the necessary permissions. If there are permission issues, adjust the IAM policies to grant the required access. |
You’re using an outdated version of the CloudFormation template with newer AWS services or Terraform versions. | Regularly update the CloudFormation template to be compatible with the latest AWS and Terraform releases. Check the release notes or documentation for any version-specific changes or requirements. |
You reach AWS service quotas during deployment. | Before you deploy the pipeline, check AWS service quotas for resources such as S3 buckets, IAM roles, and Lambda functions. Request increases if necessary. For more information, see AWS service quotas on the AWS website. |
You encounter errors due to incorrect input parameters in the CloudFormation template. | Double-check all input parameters for typos or incorrect values. Confirm that resource identifiers, such as account IDs and Region names, are accurate. |
Related resources
To implement this pattern successfully, review the following resources. These resources provide additional information and guidance that can be invaluable in setting up and managing AFT by using AWS CloudFormation.
AWS documentation:
AWS Control Tower User Guide offers detailed information on setting up and managing AWS Control Tower.
AWS CloudFormation documentation provides insights into CloudFormation templates, stacks, and resource management.
IAM policies and best practices:
Security best practices in IAM explains how to help secure AWS resources by using IAM roles and policies.
Terraform on AWS:
Terraform AWS Provider documentation
provides comprehensive information about using Terraform with AWS.
AWS service quotas:
AWS service quotas provides information about how to view AWS service quotas and how to request increases.