Validate Account Factory for Terraform (AFT) code locally
Created by Alexandru Pop (AWS) and Michal Gorniak (AWS)
Summary
This pattern shows how to locally test HashiCorp Terraform code that’s managed by AWS Control Tower Account Factory for Terraform (AFT). Terraform is an open-source infrastructure as code (IaC) tool that helps you use code to provision and manage cloud infrastructure and resources. AFT sets up a Terraform pipeline that helps you provision and customize multiple AWS accounts in AWS Control Tower.
During code development, it can be helpful to test your Terraform infrastructure as code (IaC) locally, outside of the AFT pipeline. This pattern shows how to do the following:
Retrieve a local copy of the Terraform code that’s stored in the AWS CodeCommit repositories in your AFT management account.
Simulate the AFT pipeline locally by using the retrieved code.
This procedure can also be used to run Terraform commands that aren’t part of the normal AFT pipeline. For example, you can use this method to run commands such as terraform validate
, terraform plan
, terraform destroy
, and terraform import
.
Prerequisites and limitations
Prerequisites
An active AWS multi-account environment that uses AWS Control Tower
A fully deployed of AFT environment
AWS Command Line Interface (AWS CLI), installed and configured
AWS CLI credential helper for Code Commit, installed and configured
Python 3.x
Git
, installed and configured on your local machine git-remote-commit utility, installed and configured
Terraform
, installed and configured (the local Terraform package version must match the version that’s used in the AFT deployment)
Limitations
This pattern doesn’t cover the deployment steps required for AWS Control Tower, AFT, or any specific Terraform modules.
The output that’s generated locally during this procedure isn’t saved in the AFT pipeline runtime logs.
Architecture
Target technology stack
AFT infrastructure deployed within an AWS Control Tower deployment
Terraform
Git
AWS CLI version 2
Automation and scale
This pattern shows how to locally invoke Terraform code for AFT global account customizations in a single AFT-managed AWS account. After your Terraform code is validated, you can apply it to the remaining accounts in your multi-account environment. For more information, see Re-invoke customizations in the AWS Control Tower documentation.
You can also use a similar process to run AFT account customizations in a local terminal. To locally invoke Terraform code from AFT account customizations, clone the aft-account-customizations repository instead of aft-global-account-customizations repository from CodeCommit in your AFT management account.
Tools
AWS services
AWS Control Tower helps you set up and govern an AWS multi-account environment, following prescriptive best practices.
AWS Command Line Interface (AWS CLI) is an open-source tool that helps you interact with AWS services through commands in your command-line shell.
Other services
HashiCorp Terraform
is an open-source infrastructure as code (IaC) tool that helps you use code to provision and manage cloud infrastructure and resources. Git
is an open-source, distributed version control system.
Code
The following is an example bash script that can be used to locally run Terraform code that’s managed by AFT. To use the script, follow the instructions in the Epics section of this pattern.
#! /bin/bash # Version: 1.1 2022-06-24 Unsetting AWS_PROFILE since, when set, it interferes with script operation # 1.0 2022-02-02 Initial Version # # Purpose: For use with AFT: This script runs the local copy of TF code as if it were running within AFT pipeline. # * Facilitates testing of what the AFT pipline will do # * Provides the ability to run terraform with custom arguments (like 'plan' or 'move') which are currently not supported within the pipeline. # # © 2021 Amazon Web Services, Inc. or its affiliates. All Rights Reserved. # This AWS Content is provided subject to the terms of the AWS Customer Agreement # available at http://aws.amazon.com/agreement or other written agreement between # Customer and either Amazon Web Services, Inc. or Amazon Web Services EMEA SARL or both. # # Note: Arguments to this script are passed directly to 'terraform' without parsing nor validation by this script. # # Prerequisites: # 1. local copy of ct GIT repositories # 2. local backend.tf and aft-providers.tf filled with data for the target account on which terraform is to be run # Hint: The contents of above files can be obtain from the logs of a previous execution of the AFT pipeline for the target account. # 3. 'terraform' binary is available in local PATH # 4. Recommended: .gitignore file containing 'backend.tf', 'aft_providers.tf' so the local copy of these files are not pushed back to git readonly credentials=$(aws sts assume-role \ --role-arn arn:aws:iam::$(aws sts get-caller-identity --query "Account" --output text ):role/AWSAFTAdmin \ --role-session-name AWSAFT-Session \ --query Credentials ) unset AWS_PROFILE export AWS_ACCESS_KEY_ID=$(echo $credentials | jq -r '.AccessKeyId') export AWS_SECRET_ACCESS_KEY=$(echo $credentials | jq -r '.SecretAccessKey') export AWS_SESSION_TOKEN=$(echo $credentials | jq -r '.SessionToken') terraform "$@"
Epics
Task | Description | Skills required |
---|---|---|
Save the example code as a local file. |
| AWS administrator |
Make the example code runnable. | Open a terminal window and authenticate into your AWS AFT management account by doing one of the following:
NoteYour organization might also have a custom tool to provide authentication credentials to your AWS environment. | AWS administrator |
Verify access to AFT management account in the correct AWS Region. | ImportantMake sure that you use the same terminal session that you authenticated into your AFT management account with.
| AWS administrator |
Create a new, local directory to store the AFT repository code. | In the same terminal session, run the following commands:
| AWS administrator |
Clone the remote AFT repository code. |
| AWS administrator |
Task | Description | Skills required |
---|---|---|
Open a previously run AFT pipeline and copy the Terraform configuration files to a local folder. | NoteThe backend.tf and aft-providers.tf configuration files that are created in this epic are needed for the AFT pipeline to run locally. These files are created automatically within the cloud-based AFT pipeline, but must be created manually for the pipeline to run locally. Running the AFT pipeline locally requires one set of files that represents running the pipeline within a single AWS account.
Example autogenerated backend.tf statement
NoteThe | AWS administrator |
Task | Description | Skills required |
---|---|---|
Implement the Terraform configuration changes that you want to validate. |
| AWS administrator |
Run the ct_terraform.sh script and review the output. |
Important
| AWS administrator |
Task | Description | Skills required |
---|---|---|
Add references to the backend.tf and aft-providers.tf files to a .gitignore file. | Add the
NoteMoving the files to the | AWS administrator |
Commit and push your code changes to the remote AFT repository. |
ImportantThe code changes that you introduce by following this procedure up until this point are applied to one AWS account only. | AWS administrator |
Task | Description | Skills required |
---|---|---|
Roll out the changes to all of your accounts managed by AFT. | To roll out the changes to multiple AWS accounts managed by AFT, follow the instructions in Re-invoke customizations in the AWS Control Tower documentation. | AWS administrator |