要使用Terraform构建AWS基础架构(基础教程)
选择供应商
terraform {
required_version = ">= 1.0.1"
required_providers {
aws = ">= 3.0"
}
}
provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
region = var.aws_region
}
定义变量
在terraform.tfvars文件中
aws_access_key="xxxxxxxx"
aws_secret_key="xxxxxxxx"
aws_region="xxxxxxxx"
在variables.tf文件中
variable "aws_access_key" {
type = string
}
variable "aws_secret_key" {
type = string
}
variable "aws_region" {
type = string
}
资源的定义
除此之外,例如创建一个名为aws_alb.tf的文件(虽然文件不分割也可以运行,但基本上分割文件更整洁且易于理解),然后进行资源定义。详细内容请查阅文档。
resource "aws_lb" "api_lb" {
name = "test-alb"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.test-group.id]
subnets = ["${aws_subnet.test_subnet.id}", "${aws_subnet.test_subnet2.id}"]
enable_deletion_protection = true
tags = {
"Environment" = terraform.workspace
"Application" = "api"
}
}
完成记述后,请执行下述操作:
terraform plan
terraform apply
每次修改后都执行terraform plan terraform apply。
如果您想暂时关闭资源的话
terraform destroy
根据以上笔记做记录