同时启动多台 EC2 实例
知识点
实战演习/说明讲解
- 修改 EC2 资源配置, 设置启动数量
- 修改 Outputs 输出定义(由于生成多台EC2)
- 运行模版确认动作
操作步骤
目录结构
| Bash |
|---|
| .
├── ec2-aws_ami.tf
├── ec2-sg.tf
├── ec2-variables.tf
├── ec2.tf
├── main.tf
└── ec2-outputs.tf
|
编写 EC2 AMI 数据源文件, 始终保持使用最新的 AMI
ec2-aws_ami.tf
| Terraform |
|---|
| data "aws_ami" "myami" {
# 最新使用
most_recent = true
# amazon官方认证
owners = ["amazon"]
# 镜像文件名称过滤
filter {
name = "name"
values = ["amzn-ami-hvm-*-x86_64-gp2"]
}
# 根设备过滤条件
filter {
name = "root-device-type"
values = ["ebs"]
}
}
|
编写安全组定义文件
ec2-sg.tf
| Terraform |
|---|
| # Create Security Group
resource "aws_security_group" "learnaws-sg-web-ssh" {
# 安全组的名称
name = "learnaws-sg-web-ssh"
description = "Web and SSH"
# 安全组的入口描述
ingress {
description = "Allow Port 80"
from_port = 80
to_port = 80
protocol = "tcp"
# 允许范围,这里是所有ip都可访问
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Allow Port 443"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "Allow Port 22"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
# cidr_blocks = ["12.15.55.32/32"]
}
# 安全组的出口描述
egress {
description = "Allow all ip and ports outbound"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "learnaws-sg-web-ssh"
}
}
|
定义 EC2 启动环境映射
ec2-variables.tf
| Terraform |
|---|
| # AWS Region
variable "aws_region" {
description = "AWS Region"
type = string
default = "ap-northeast-1"
}
# AWS EC2 Instance Env
variable "instance_type_env" {
description = "EC2 Instnace Type Env"
type = map(string)
default = {
"dev" = "t2.micro"
"prod" = "t2.large"
}
}
|
修改 EC2 资源配置, 设置启动数量
ec2.tf
| Terraform |
|---|
| ###########################################################
# EC2 资源设置
resource "aws_instance" "myweb_server2" {
ami = data.aws_ami.myami.id
instance_type = var.instance_type_env["dev"]
vpc_security_group_ids = [aws_security_group.learnaws-sg-web-ssh.id]
# 设置启动台数
count = 3
tags = {
# 为生成的 EC2 加入标签索引
Name = "learnaws-tf-ec2-${count.index}"
}
}
|
编写 TF 项目描述文件
main.tf
| Terraform |
|---|
| ###########################################################
# Terraform 基本设置
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.71"
}
}
required_version = ">= 1.1.3"
}
###########################################################
# 提供商设置(云平台)
provider "aws" {
profile = "learnaws"
region = var.aws_region
}
|
修改 Outputs 输出定义(由于生成多台EC2)
ec2-outputs.tf
| Terraform |
|---|
| output "instance_instance_type" {
# EC2 实例类型,因为所有的EC2都是一样的,取第一个输出即可
value = aws_instance.myweb_server2[0].instance_type
}
output "instance_ami" {
# 使用的 AMI,因为所有的EC2都是一样的,取第一个输出即可
value = aws_instance.myweb_server2[0].ami
}
# 输出所有 EC2 ips
output "instance_publicip" {
description = "EC2 ips"
value = [for instance in aws_instance.myweb_server2 : instance.public_ip]
}
# 输出所有 EC2 部署的可用区
output "instance_availability_zone" {
value = aws_instance.myweb_server2[*].availability_zone
}
# 通过 map 方式输出内网ip
output "instance_privateip_map" {
value = { for instance in aws_instance.myweb_server2 : instance.id => instance.private_ip }
}
# 通过 map 方式输出 EC2 TagName
output "instance_tagName" {
value = { for instance in aws_instance.myweb_server2 : instance.id => lookup(instance.tags, "Name") }
}
# 最后输出的样式如下:
# instance_ami = "ami-0204e434dddffa5cf"
# instance_availability_zone = [
# "ap-northeast-1c",
# "ap-northeast-1c",
# "ap-northeast-1c",
# ]
# instance_instance_type = "t2.micro"
# instance_privateip_map = {
# "i-03c0f40ee9e65b268" = "172.31.5.141"
# "i-0b4b99fa322384d4e" = "172.31.14.184"
# "i-0f4535c49c555b4c2" = "172.31.14.125"
# }
# instance_publicip = [
# "18.183.163.231",
# "3.115.19.20",
# "35.78.86.26",
# ]
# instance_tagName = {
# "i-03c0f40ee9e65b268" = "learnaws-tf-ec2-0"
# "i-0b4b99fa322384d4e" = "learnaws-tf-ec2-2"
# "i-0f4535c49c555b4c2" = "learnaws-tf-ec2-1"
# }
|
EC2资源属性参照
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance
运行模版确认动作
| Bash |
|---|
| # 目录初始化
$ terraform init
# 检验 tf 文件
$ terraform validate
# 实施计划, 准备资源
$ terraform plan
# 应用部署
$ terraform apply
$ terraform apply -auto-approve
# 摧毁系统
$ terraform destroy
$ terraform destroy -auto-approve
|
最后更新:
February 17, 2023
创建日期:
February 17, 2023