为 Terraformer 在 Docker 环境中提供轻松运行的准备条件
首先
我们提供了一个可以轻松设置和使用的Terraformer环境,但还需要一定程度了解Terraform,因为它不仅仅是Terraformer的工具。我们将Terraformer的步骤和内容整理在一起,是因为我们想要在Docker中方便地运行Terraformer。
关于Terraformer的信息
用中文将以下内容翻译且重新表达:Terraformer 是一种能将已构建基础设施转换为 tf 文件的工具。
请参考以下文章了解Terraformer的使用方法。
准备一个Dockerfile。
请在Dockerfile中参考README.md中提供的安装步骤中的Linux > 发行版 > Shell,进行记录。
export PROVIDER={all,google,aws,kubernetes}
curl -LO https://github.com/GoogleCloudPlatform/terraformer/releases/download/$(curl -s https://api.github.com/repos/GoogleCloudPlatform/terraformer/releases/latest | grep tag_name | cut -d '"' -f 4)/terraformer-${PROVIDER}-linux-amd64
chmod +x terraformer-${PROVIDER}-linux-amd64
sudo mv terraformer-${PROVIDER}-linux-amd64 /usr/local/bin/terraformer
在 README.md 文件中的示例中,执行 export PROVIDER={all,google,aws,kubernetes} 命令可以获取 AWS、GCP 和 k8s 等所有执行文件,但只需获取 all 即可,无需问题。
Dockerfile 创建如下:
FROM hashicorp/terraform:0.13.7
RUN apk update --no-cache && \
apk add curl bash
RUN curl -LO https://github.com/GoogleCloudPlatform/terraformer/releases/download/$(curl -s https://api.github.com/repos/GoogleCloudPlatform/terraformer/releases/latest | grep tag_name | cut -d '"' -f 4)/terraformer-all-linux-amd64
RUN chmod +x terraformer-all-linux-amd64 && \
mv terraformer-all-linux-amd64 /usr/local/bin/terraformer
WORKDIR /app/terraform
ENTRYPOINT ["/usr/local/bin/terraformer"]
CMD ["--help"]
准备 docker-compose.yml 文件
为了方便执行,接下来创建一个 docker-compose.yml 文件。
version: "3"
services:
terraformer:
build:
context: .
dockerfile: ./Dockerfile
volumes:
- ./:/app/terraform
运行容器
因为准备了Dockerfile和docker-compose.yml,所以创建容器并运行Terraformer的–help选项。
如果显示了如下所示的Terraformer帮助信息,则表示运行成功。
% docker-compose run --rm terraformer
Creating terraformer_terraformer_run ... done
Usage:
[command]
Available Commands:
help Help about any command
import Import current state to Terraform configuration
plan Plan to import current state to Terraform configuration
version Print the version number of Terraformer
Flags:
-h, --help help for this command
-v, --version version for this command
Use " [command] --help" for more information about a command.
此外,由于使用了 Terraform 这个基础镜像来构建 Terraformer 容器,因此可以通过重写 entrypoint 来执行 terraform 命令。
mziyut% docker-compose run --rm --entrypoint terraform terraformer
Creating terraformer_terraformer_run ... done
Usage: terraform [-version] [-help] <command> [args]
The available commands for execution are listed below.
The most common, useful commands are shown first, followed by
less common or more advanced commands. If you're just getting
started with Terraform, stick with the common commands. For the
other commands, please read the help and docs before usage.
Common commands:
apply Builds or changes infrastructure
console Interactive console for Terraform interpolations
destroy Destroy Terraform-managed infrastructure
env Workspace management
fmt Rewrites config files to canonical format
get Download and install modules for the configuration
graph Create a visual graph of Terraform resources
import Import existing infrastructure into Terraform
init Initialize a Terraform working directory
login Obtain and save credentials for a remote host
logout Remove locally-stored credentials for a remote host
output Read an output from a state file
plan Generate and show an execution plan
providers Prints a tree of the providers used in the configuration
refresh Update local state file against real resources
show Inspect Terraform state or plan
taint Manually mark a resource for recreation
untaint Manually unmark a resource as tainted
validate Validates the Terraform files
version Prints the Terraform version
workspace Workspace management
All other commands:
0.12upgrade Rewrites pre-0.12 module source code for v0.12
0.13upgrade Rewrites pre-0.13 module source code for v0.13
debug Debug output management (experimental)
force-unlock Manually unlock the terraform state
push Obsolete command for Terraform Enterprise legacy (v1)
state Advanced state management
最后
现在可以准备好Dockerfile和docker-compose.yml,并轻松地运行Terraformer。
要执行Terraformer,需要设置terraform等配置,请参考官方文档进行设置。