本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
由 Alexandru Pop (AWS) 和 Michal Gorniak (AWS) 编写
摘要
注意: AWS CodeCommit 不再向新客户开放。的现有客户 AWS CodeCommit 可以继续照常使用该服务。了解更多
此模式显示了如何在本地测试由 Acco AWS Control Tower unt Fact HashiCorp ory for Terraform(AFT)管理的 Terraform 代码。Terraform 是一种开源的基础设施即代码(IaC)工具,可帮助您使用代码来预置和管理云基础结构和资源。AFT 设置了一个 Terraform 管道,可以帮助你在其中配置和自定义多个 AWS 账户 管道。 AWS Control Tower
在代码开发过程中,它可能有助于在 AFT 管道之外在本地测试 Terraform 基础设施即代码(IaC)。该模式说明了如何执行以下操作:
检索存储在您的 AFT 管理账户存储 AWS CodeCommit 库中的 Terraform 代码的本地副本。
使用检索代码在本地模拟 AFT 管道。
此过程还可以用于运行不属于普通 AFT 管道的 Terraform 命令。例如,您可使用此方法来运行 terraform validate
、terraform plan
、terraform destroy
和 terraform import
等命令。
先决条件和限制
先决条件
一个活跃的 AWS 多账户环境,使用 AWS Control Tower
全面部署 AFT 环境
AWS CLI 已安装并@@ 配置的 AWS CodeCommit凭证助手
Python 3.x
Git
,已在本地计算机上安装并配置 git-remote-commit
实用程序,已安装并配置Terraform
,已安装和配置(本地 Terraform 软件包版本必须与 AFT 部署中使用的版本相匹配)
限制
此模式不包括 AFT 或任何特定 Terraform 模块所需的 AWS Control Tower部署步骤。
在此过程中本地生成的输出不保存在 AFT 管道运行时日志中。
架构
目标技术堆栈
部署在部署中部署的 AFT 基础架构 AWS Control Tower
Terraform
Git
AWS CLI 第 2 版
自动化和扩缩
此模式显示了如何在单个 AFT 管理的单个 AFT 管理中本地调用 Terraform 代码进行 AFT 全局账户自定义。 AWS 账户验证您的 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 Command Line Interface (AWS CLI) 是一个开源工具,可帮助您 AWS 服务 通过命令行外壳中的命令进行交互。
其他服务
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 "$@"
操作说明
Task | 描述 | 所需技能 |
---|---|---|
将示例代码保存为本地文件。 |
| AWS 管理员 |
使示例代码运行。 | 打开终端窗口,通过执行以下任一操作 AWS 在您的 AFT 管理账户中进行身份验证:
注意您的组织可能还有一个自定义工具,可以为您的 AWS 环境提供身份验证凭证。 | AWS 管理员 |
验证对 AFT 管理账户的访问权限是否正确 AWS 区域。 | 重要确保使用与 AFT 管理账户进行身份验证时使用的终端会话相同。
| AWS 管理员 |
创建新的本地目录来存储 AFT 存储库代码。 | 从相同的终端会话中,运行以下两个命令:
| AWS 管理员 |
克隆远程 AFT 存储库代码。 |
| AWS 管理员 |
Task | 描述 | 所需技能 |
---|---|---|
打开之前运行的 AFT 管道,将 Terraform 配置文件复制中本地文件夹。 | 注意要让 AFT 管道在本地运行,需要在此长篇故事中创建的
自动生成的 backend.tf 语句示例
注意
| AWS 管理员 |
Task | 描述 | 所需技能 |
---|---|---|
实施要验证的 Terraform 配置更改。 |
| AWS 管理员 |
运行 |
重要
| AWS 管理员 |
Task | 描述 | 所需技能 |
---|---|---|
将对 | 通过运行以下命令将您创建的
注意将文件移动到 | AWS 管理员 |
提交您的代码更改并将其推送至远程 AFT 存储库。 |
重要在此之前,您通过执行此过程引入的代码更改 AWS 账户 仅适用于一个代码更改。 | AWS 管理员 |
Task | 描述 | 所需技能 |
---|---|---|
对所有由 AFT 管理的账户进行更改。 | 要将更改推广到由 AFT 管理 AWS 账户 的多个项目,请按照文档中 “重新调用自定义” 中的 AWS Control Tower 说明进行操作。 | AWS 管理员 |