Terraform教程02 - 创建AWS EC2实例


首先创建一个目录,比如demo,然后进入该目录。

创建文件variables.tf

variable "access_key" {
  description = "AWS access key"
  type = string
  default = "XXXXXXXXXXXX"
}

variable "secret_key" {
  description = "AWS secret key"
  type = string
  default = "XXXXXXXXXXXX"
}

variable "region" {
  description = "AWS region"
  type = string
  default = "eu-west-1"
}

创建文件instance.tf

provider "aws" {
  access_key = var.access_key
  secret_key = var.secret_key
  region     = var.region
}

resource "aws_instance" "example" {
  ami           = "ami-0d75513e7706cf2d9"
  instance_type = "t2.micro"
}

其中的image ID可以从AWS Console获取,也可以从这里获取:https://cloud-images.ubuntu.com/locator/ec2/

初始化并安装插件

运行如下命令来初始化terraform,并安装相应的插件:

terraform init

这一步的目的就是下载并安装、配置terraform aws插件。比如针对上面的配置,会安装:terraform-provider-aws_v4.31.0_x5.exe。之后针对AWS的资源配置,都会有这个插件来负责创建对应的AWS资源。

其运行结果如下所示:

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v4.21.0...
- Installed hashicorp/aws v4.21.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

terraform plan命令

运行terraform plan命令可以查看一下terraform计划要做的事情。说白了就是当前的terraform定义和真正的系统之间有何差异,如果应用的话会创建何种资源:

terraform plan

这时可以使用如下命令组合将当前状态保存在一个文件中,之后再将文件中内容进行应用:

terraform plan -out current.terraform
terraform apply current.terraform

其实,直接运行terraform apply就相当于执行了三步操作:

  • terraform plan -out tmp.terraform
  • terraform apply tmp.terraform
  • rm -rf tmp.terraform

terraform apply

通过这个命令可以创建对应的云资源:

terraform apply

其运行结果如下所示:

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_instance.example: Creating...
aws_instance.example: Still creating... [10s elapsed]
aws_instance.example: Still creating... [20s elapsed]
aws_instance.example: Still creating... [30s elapsed]
aws_instance.example: Still creating... [40s elapsed]
aws_instance.example: Creation complete after 42s [id=i-0f5440f3e9f3da719]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

之后如果再次运行’terraform apply’,将不会创建任何新的资源。因为所有需要的AWS资源都已经被创建了。在.tf文件中已经明确说明了我们希望的系统最终状态,现在已经达到了这个状态,自然就无需做任何更改了。

Terraform会在当前目录下创建一个文件terraform.tfstate,并将相关资源的状态保存在该文件中。

更新资源

如果main.tf发生了变化,比如新增了tags:

resource "aws_instance" "ec2_test" {
  ami = "ami-0d75513e7706cf2d9" # ubuntu 22.04 LTS
  instance_type = "t2.micro"
  tags = {
    Name = "demo"
  }
}

再次运行terraform apply,则会更新AWS资源并添加tags。

删除资源

可以在main.tf中删除相关资源的定义,然后运行terraform apply就会删除该资源。

也可以同时删除所有的资源:

terraform destroy --auto-approve

输出资源相关信息

在创建了相关资源之后,可以在Terraform输出相关信息,这时可以使用output:

output "server_public_ip" {
  value = aws_eip.myip.public_ip
}

在运行apply之后,就可以看到类似的信息:

Outputs:

server_public_ip = xx.xxx.xxx.xxx

文章作者: 逻思
版权声明: 本博客所有文章除特別声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明来源 逻思 !
  目录