本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
在本地验证 Account Factory for Terraform (AFT) 代码
由 Alexandru Pop (AWS) 和 Michal Gorniak (AWS) 编写
环境:生产 | 技术:基础设施; DevOps;现代化; DevelopmentAndTesting | 工作负载:开源 |
Amazon Web Services:AWS Control Tower |
Summary
此模式显示了如何在本地测试由 AWS Control HashiCorp Tower Account Factory for Terraform (AFT) 管理的 Terraform 代码。Terraform 是一种开源的基础设施即代码(IaC)工具,可帮助您使用代码来预置和管理云基础结构和资源。AFT 构建了 Terraform 管道,帮助您在 AWS Control Tower 中配置和自定义多个 Amazon Web Services account。
在代码开发过程中,它可能有助于在 AFT 管道之外在本地测试 Terraform 基础设施即代码(IaC)。该模式说明了如何执行以下操作:
检索存储在您的 AFT 管理账户的 AWS 存储 CodeCommit 库中的 Terraform 代码的本地副本。
使用检索代码在本地模拟 AFT 管道。
此过程还可以用于运行不属于普通 AFT 管道的 Terraform 命令。例如,您可使用此方法来运行 terraform validate
、terraform plan
、terraform destroy
和 terraform import
等命令。
先决条件和限制
先决条件
使用 AWS Control Tower
的有效 AWS 多账户环境 全面部署 AFT 环境
用于代码提交的 AWS CLI 凭证助手,已安装并配置
Python 3.x
Git
,已在本地计算机上安装并配置 git-remote-commit 实用程序,已安装并配置
Terraform
,已安装和配置(本地 Terraform 软件包版本必须与 AFT 部署中使用的版本相匹配)
限制
这种模式不包含 AWS Control Tower、AFT 或任何特定 Terraform 模块所需部署步骤。
在此过程中本地生成的输出不保存在 AFT 管道运行时日志中。
架构
目标技术堆栈
AFT 基础设施部署至 AWS Control Tower 部署中
Terraform
Git
AWS CLI 版本 2
自动化和扩缩
此模式显示了如何在单个 AFT 托管 Amazon Web Services account 中本地调用 Terraform 代码进行 AFT 全球账户自定义。验证您的 Terraform 代码后,您可将其应用于多账户环境中的其余账户。有关更多信息,请参阅 AWS Control Tower 文档中的重新调用自定义。
您还可使用类似的过程在本地终端中运行 AFT 账户自定义。要从 AFT 账户自定义项中本地调用 Terraform 代码,请在 AFT 管理账户中克隆aft-account-customizations 存储库,而不是从 AFT aft-global-account-customizations管理账户 CodeCommit 中克隆存储库。
工具
Amazon Web Services
AWS Control Tower 可帮您按照规范性最佳实践设置和管理 AWS 多账户环境。
AWS 命令行界面(AWS CLI)是一种开源工具,它可帮助您通过命令行 Shell 中的命令与 Amazon Web Services 交互。
其他服务
HashiCorp Terraform
是一种开源基础设施即代码 (IaC) 工具,可帮助您使用代码来配置和管理云基础架构和资源。 Git
是开源分布式版本控制系统。
代码
以下是 bash 脚本示例,可在本地运行由 AFT 管理的 Terraform 代码。若要使用此脚本,请按照此模式操作说明部分中的说明操作。
#! /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 "$@"
操作说明
任务 | 描述 | 所需技能 |
---|---|---|
将示例代码保存为本地文件。 |
| AWS 管理员 |
使示例代码运行。 | 通过执行以下操作之一,打开终端窗口并通过验证您的 AWS AFT 管理账户:
注意:您的组织可能还有一个自定义工具,用于向您的 AWS 环境提供身份验证凭证。 | AWS 管理员 |
在正确的 AWS 区域验证对 AFT 管理账户的访问权限。 | 重要提示:请务必使用与 AFT 管理账户进行身份验证的终端会话相同的终端会话。
| AWS 管理员 |
创建新的本地目录来存储 AFT 存储库代码。 | 从相同的终端会话中,运行以下两个命令:
| AWS 管理员 |
克隆远程 AFT 存储库代码。 |
| AWS 管理员 |
任务 | 描述 | 所需技能 |
---|---|---|
打开之前运行的 AFT 管道,将 Terraform 配置文件复制中本地文件夹。 | 注意:要让 AFT 管道在本地运行,需要在此操作说明中创建的 backend.tf 和 aft-providers.tf 配置文件。这些文件是在基于云 AFT 管道中自动创建,但必须手动创建才能使管道在本地运行。本地运行 AFT 管道需要一组文件,这些文件表示在单个 Amazon Web Services account 中运行管道。
自动生成 backend.tf 语句示例
注意: | AWS 管理员 |
任务 | 描述 | 所需技能 |
---|---|---|
实施要验证的 Terraform 配置更改。 |
| AWS 管理员 |
运行 ct_terraform.sh 脚本并查看输出。 |
重要提示:
| AWS 管理员 |
任务 | 描述 | 所需技能 |
---|---|---|
将对 backend.tf 和 aft-providers.tf 文件的引用添加至 .gitignore 文件中。 | 通过运行以下命令将您创建的
注意:将文件移动到 | AWS 管理员 |
提交您的代码更改并将其推送至远程 AFT 存储库。 |
重要提示:在此之前,您通过执行此过程引入的代码更改仅适用于一个 Amazon Web Services account。 | AWS 管理员 |
任务 | 描述 | 所需技能 |
---|---|---|
将更改发布至您由 AFT 管理的所有账户。 | 要对由 AFT 管理的多个 Amazon Web Services account 执行更改,请按照 AWS Control Tower 文档中重新调用自定义项中的说明进行操作。 | AWS 管理员 |