## 安装Terraform ## 1. https://www.terraform.io/downloads.html 2. 设置path=D:\tools\tf\terraform 3. cmd 运行terraform --version ``` D:\>terraform --version Terraform v0.14.5 ``` 4. 下载: https://docs.aws.amazon.com/cli/latest/userguide/install-windows.html AWSCLI64.msi 并安装 ``` aws configure --profile course 输入access_key和secret_key和resion ``` ## 用一个最简单的例子创建一个linux主机 ## 1. main.tf ``` provider "aws" { profile = "course" region = "ap-southeast-1" } resource "aws_vpc" "my_vpc" { cidr_block = "10.0.0.0/16" } resource "aws_subnet" "my_subnet" { vpc_id = aws_vpc.my_vpc.id cidr_block = "10.0.1.0/24" availability_zone_id = "apse1-az1" } resource "aws_instance" "my_instance" { ami = "ami-0adfdaea54d40922b" instance_type = "t2.micro" associate_public_ip_address = true subnet_id = aws_subnet.my_subnet.id private_ip = "10.0.1.100" credit_specification { cpu_credits = "unlimited" } } ``` 2. 运行terraform init /plan /apply 输入yes D:\course\example\1.02>terraform init 3. 查看AWS后台,发现一台EC2主机已经被创建 4. 执行terraform destroy 输入yes 销毁主机以及关联资源 5. 查看当前terraform生成的文件 ``` D:\course\example\1.02>dir Volume in drive D is UserProfile Volume Serial Number is 8A16-AC57 Directory of D:\course\example\1.02 2021/05/06 16:29 <DIR> . 2021/05/06 16:29 <DIR> .. 2021/05/06 14:26 <DIR> .terraform 2021/05/06 16:17 1,083 .terraform.lock.hcl 2021/05/06 16:10 1,341 main.tf 2021/05/06 16:29 157 terraform.tfstate 2021/05/06 16:28 7,463 terraform.tfstate.backup 4 File(s) 10,044 bytes 3 Dir(s) 97,182,687,232 bytes free ```