原文转载自 「HelloDog」 ( https://wsgzao.github.io/post/terraform/ ) By wsgzao
预计阅读时间 0 分钟（共 0 个字， 0 张图片， 0 个链接）
相信 Infrastructure as Code (IaC)的 DevOps 理念已经深入人心，如果是 Ansible 是偏向 Infrastructure Configuration tool，那么 Terraform 就是 Infrastructure provision tool，两者本身并不冲突，而 Terraform 使用 declarative 声明式语法结合强大的生态让我们在多云 (AWS,Azure,GCP) 和容器化 (K8s)) 环境中可以更加从容应对新的挑战。
2020 年 09 月 14 日 - 初稿
Terraform 官方文档中 CTO 的介绍视频可以帮助你快速了解
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.
Configuration files describe to Terraform the components needed to run a single application or your entire datacenter. Terraform generates an execution plan describing what it will do to reach the desired state, and then executes it to build the described infrastructure. As the configuration changes, Terraform is able to determine what changed and create incremental execution plans which can be applied.
The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.
Advantages of Infrastructure as Code
关于 Terraform 开源版你需要知道的缺点，官方的介绍视频主要通过引入 Terraform Enterprise 企业版来解决以下问题
Terraform 的安装和基础命令并不复杂，需要重点学习和熟悉的是各个云厂商开放的 API 语法
安装 Terraform，找到与你系统 匹配的软件包 然后下载。Terraform 被打包为一个 zip 归档文件。
下载完 zip 文件以后，解压这个包。Terraform 是一个名为 terraform 的独立文件。包里其他所有的文件都可以安全删掉，Terraform 依然可以正常工作。
最后一步确保 terraform 二进制文件在 PATH 上可用。
# copy binary file to path
A Terraform configuration is a series of code blocks that define your intended infrastructure. You’ll run the
terraform command against this file to create an Nginx webserver and view the default Nginx web page.
First, open the
main.tf file in the text editor by clicking this link.
You don’t have to edit or even understand the code. It defines two resources: a Docker disk image that packages the Nginx webserver, and a Docker container that gives it a name and runs it on port 80.
All Terraform workflows start with the
init command. Terraform searches the configuration for both direct and indirect references to providers (such as Docker). Terraform then attempts to load the required plugins.
Now provision the webserver by running
You will be asked to confirm. Type
yes and press
ENTER. It may take up to 30 seconds. A message will display confirmation that it succeeded.
Visit this URL to view the default Nginx web page which is now live:
Alternatively, you can examine Docker’s process list. You will see the
tutorial container which is running Nginx.
To remove the Nginx webserver as defined in
main.tf, run the destroy command.
You will be prompted to confirm. Type
yes and press
You have now created and destroyed your first Terraform resources! Terraform supports hundreds of ecosystem providers, from major cloud resources to content delivery networks and more.
之前也提到了 Terraform 学起来很快，但更重要的是熟悉不同 Providers 之前的调用差异
Learn Terraform - New users can start here. Interactive guides to teach you how to use Terraform’s features. Begin with the Getting Started guide, then continue with task-specific advanced guides or go directly to the Terraform CLI docs.
Terraform is used to create, manage, and update infrastructure resources such as physical machines, VMs, network switches, containers, and more. Almost any infrastructure type can be represented as a resource in Terraform.
A provider is responsible for understanding API interactions and exposing resources. Most providers configure a specific infrastructure platform (either cloud or self-hosted). Providers can also offer local utilities for tasks like generating random numbers for unique resource names.
Terraform Documentation - The documentation is an in-depth reference guide to all the features of Terraform, including technical details about the internals of how Terraform operates.