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