Terraform 是一种安全有效地构建、更改和版本控制基础设施的工具(基础架构自动化的编排工具)。它的目标是 "Write, Plan, and create Infrastructure as Code", 基础架构即代码。Terraform 几乎可以支持所有市面上能见到的云服务。具体的说就是可以用代码来管理维护 IT 资源,把之前需要手动操作的一部分任务通过程序来自动化的完成,这样的做的结果非常明显:高效、不易出错。
Terraform 提供了对资源和提供者的灵活抽象。该模型允许表示从物理硬件、虚拟机和容器到电子邮件和 DNS 提供者的所有内容。由于这种灵活性,Terraform 可以用来解决许多不同的问题。这意味着有许多现有的工具与Terraform 的功能重叠。但是需要注意的是,Terraform 与其他系统并不相互排斥。它可以用于管理小到单个应用程序或达到整个数据中心的不同对象。
关于Terraform
Terraform以单个二进制文件的形式分布。通过解压缩并将其移动到包含在系统路径中的目录中来安装Terraform.
下载安装
Terraform的官方网站是https://www.terraform.io , 在这个网站中,可以轻松的找到的下载地址, https://www.terraform.io/downloads.html
准备环境
注:我装的是Linux版的环境,其他环境可以由自己测试。
一:首先检查一下你的python环境
默认的环境是2.7.8,这个版本太低,需要升级到3.6.8
二:安装Terraform
解压Terraform文件后Terraform是个文件,配置Terraform 环境变量
三:安装aws-cli
总结
此时环境全部准备好了,下面就开始进入Terraform的世界了。
使用Terraform 创建,删改,修改 S3 Bucket
(1).每个 Terraform 项目需要自己单独的目录空间
(2).目录下的所有 *.tf 文件都会被 Terraform 加载,在初始化 Terraform 工作空间之前必须至少要有一个 *.tf 文件。
Terraform 配置的语法是该公司 HashiCorp 独创的 HCL(HashiCorp configuration language), 它可以兼容 JSON 格式。
provider "aws" 部分,它指定选用什么 provider, 以及验证信息。
(3)resource "aws_s3_bucket" "s3_bucket" 部分
如果 bucket-test-bucket 不存在的话,运行 terraform apply 将会创建它,否则试图更新该 bucket。
(4)初始化工作目录
在初始化 Terraform 工作目录之前, 其他命令如 apply, plan 多是不可用的,提示需要初始化工作目录,命令是 terraform init
(5). 执行 Terraform 管理命令
有了前面的准备之后,终于可以开始运行 Terraform 的管理命令了。Terraform 在正式执行之前提供了预览执行计划的机会,让我们清楚的了解将要做什么
terraform plan
terraform apply
总结
- 关于Terraform的基本操作也就这几条,很简单吧。
- 创建 IAM Role, 该 Role 要能操作 Lambda,S3和 CloudWatch
- 创建IAM Policy策略,可以操作ec2的所有权限。
- 将托管的IAM策略附加到角色。
- 创建一个Lambda函数。
- 创建一个cloudwatch event rule定时触发Lambda函数。
- 创建Lambda权限,允许外部源调用Lambda函数。
Terraform is a tool for safely and efficiently building, changing, and versioning infrastructure (infrastructure automation orchestration tool). Its goal is "Write, Plan, and create Infrastructure as Code". Terraform can support almost all cloud services available on the market. Specifically, it allows you to manage and maintain IT resources through code, automating tasks that previously required manual operations. The results are obvious: efficient and error-free.
Terraform provides flexible abstraction of resources and providers. This model allows representation of everything from physical hardware, virtual machines, and containers to email and DNS providers. Due to this flexibility, Terraform can be used to solve many different problems. This means there are many existing tools with overlapping functionality with Terraform. However, note that Terraform is not mutually exclusive with other systems. It can be used to manage different objects ranging from a single application to an entire data center.
About Terraform
Terraform is distributed as a single binary file. Install Terraform by extracting it and moving it to a directory included in the system path.
Download and Install
Terraform's official website is https://www.terraform.io, where you can easily find the download address: https://www.terraform.io/downloads.html
Prepare Environment
Note: I installed the Linux version. Other environments can be tested by yourself.
1: First check your Python environment
The default environment is 2.7.8, which is too low and needs to be upgraded to 3.6.8
2: Install Terraform
After extracting the Terraform file, it's a single file. Configure Terraform environment variables
3: Install aws-cli
Summary
At this point, the environment is ready. Let's enter the world of Terraform.
Create, Delete, and Modify S3 Bucket Using Terraform
(1). Each Terraform project needs its own separate directory space
(2). All *.tf files in the directory will be loaded by Terraform. Before initializing the Terraform workspace, there must be at least one *.tf file.
Terraform's configuration syntax is HashiCorp's proprietary HCL (HashiCorp configuration language), which is compatible with JSON format.
The provider "aws" section specifies which provider to use and authentication information.
(3) The resource "aws_s3_bucket" "s3_bucket" section
If bucket-test-bucket doesn't exist, running terraform apply will create it; otherwise, it will try to update the bucket.
(4) Initialize working directory
Before initializing the Terraform working directory, other commands like apply, plan are mostly unavailable, prompting you to initialize the working directory with the command: terraform init
(5). Execute Terraform management commands
With the previous preparations complete, you can finally start running Terraform management commands. Terraform provides an opportunity to preview the execution plan before actual execution, letting us clearly understand what will be done.
terraform plan
terraform apply
Summary
- The basic operations of Terraform are just these few steps, very simple.
- Create IAM Role that can operate Lambda, S3, and CloudWatch
- Create IAM Policy with all EC2 operation permissions
- Attach managed IAM policy to role
- Create a Lambda function
- Create a CloudWatch event rule to trigger Lambda function on schedule
- Create Lambda permission to allow external sources to invoke Lambda function