啥都不說先上代碼
目錄結構
├── cloud_init.sh # 開機執行腳本
├── instance.tf # 申請資源的主機
├── main.tf # 主配置文件
├── output.tf # 輸出變量
├── start.sh # terraform 命令
└── variables.tf # 輸入變量
輸入變量 一個AWS 帳號的基礎信息(PS 大佬告訴我應該用data模塊查詢,先硬編碼,後期再改)
variable "region" {
type = string
description = "請輸入創建的region"
}
variable "region_az" {
type = string
description = "請輸入創建資源所在的AZ"
}
variable "instance_type" {
type = string
default = "t3.nano"
#default = "m5.large"
description = "請輸入創建的實例類型"
}
variable "tag_name" {
type = string
description = "請輸入機器的名字"
}
variable "tag_project" {
type = string
description = "請輸入計費的tag標籤"
}
variable "storage_number" {
type = string
default = 1
description = "請輸入要創建的硬碟數量"
}
variable "key_name" {
type = string
default = "xxx"
description = "請輸入連接伺服器使用的ssh名字"
}
variable "iams" {
type = map
default = {
"us-east-2" = "ami-xxx"
"ap-northeast-1" = "ami-xxx"
}
}
variable "vpcs" {
type = map
default = {
"us-east-2" = "vpc-xxx"
"ap-northeast-1" = "vpc-xxx"
}
}
variable "subnetes" {
type = map
default = {
"us-east-2a" = "subnet-xxx"
"us-east-2b" = "subnet-xxx"
"us-east-2c" = "subnet-xxx"
"ap-northeast-1a" = "subnet-xxx"
"ap-northeast-1c" = "subnet-xxx"
"ap-northeast-1d" = "subnet-xxx"
}
}
variable "storage_dev" {
type = map
default = {
"0" = "/dev/sdh"
"1" = "/dev/sdi"
"2" = "/dev/sdj"
"3" = "/dev/sdk"
"4" = "/dev/sdl"
}
}
主配置文件,聲明使用的認證信息
provider "aws" {
region = var.region
shared_credentials_file = "/opt/terraform/aws/.creds/xxxx"
}
實例配置文件,聲明需要在AWS 上創建的資源
resource "aws_instance" "this_ec2" {
ami = var.iams[var.region]
instance_type = var.instance_type
subnet_id = var.subnetes[var.region_az]
availability_zone = var.region_az
key_name = var.key_name
user_data = "${file("cloud_init.sh")}"
tags = {
Name = var.tag_name
Project = var.tag_project
}
volume_tags = {
Name = var.tag_name
Project = var.tag_project
}
}
resource "aws_ebs_volume" "this_ebs" {
count = var.storage_number
availability_zone = var.region_az
size = 10
tags = {
Name = "${var.tag_name}-disk-${count.index}"
Project = var.tag_project
}
}
resource "aws_volume_attachment" "instance_att_sdb" {
count = var.storage_number
device_name = var.storage_dev[count.index]
instance_id = "${aws_instance.this_ec2.id}"
volume_id = "${aws_ebs_volume.this_ebs[count.index].id}"
}
resource "aws_eip" "this_elb" {
instance = "${aws_instance.this_ec2.id}"
vpc = true
}
輸出的參數,後面自動添加cmdb、jumpserver 等工具使用
output "tag_name" {
value = var.tag_name
}
output "instance_ip_addr" {
value = aws_instance.this_ec2.private_ip
}
output "instance_eip" {
value = aws_instance.this_ec2.public_ip
}
使用方法
terraform apply -var="region=us-east-2" -var="region_az=us-east-2a" -var="instance_type=m5.large" -var="tag_name=test" -var="tag_project=test" -var="storage_number=2"
參數說明:
region: EC2 所在的region
region-az: EC2 所在的AZ
instance_type: EC2 的實例類型
tag_name: EC2 的tag Key=Name
tag_project: EC2 的tag Key=Project
storage_number: EC2 附加的硬碟,最多附加五塊
開始拆一下
instance.tf
這裡面有四個動作,申請EC2,申請EBS,EC2與EBS綁定,申請EIP與EC2 綁定。好像沒啥好說的,寫完之後發現好簡單。每個方法在官網後面有個output,可以通過定義的方法名.output 的值取到輸出的值
emm.. 沒啥好講的,就是這麼簡單,tf 的難點在於規劃,如何把帳戶,秘鑰,配置,腳本,做好規劃。這個需要折騰一下。