使用 terraform 启动 EC2 实例时出错
Posted
技术标签:
【中文标题】使用 terraform 启动 EC2 实例时出错【英文标题】:Error while launching EC2 instance using terraform 【发布时间】:2022-01-15 18:05:13 【问题描述】:我对 Terraform 还很陌生。我正在尝试使用以下 Terraform 代码来启动 EC2 实例:
provider "aws"
region = "ap-south-1"
access_key = "<Key>"
secret_key = "<secret>"
# Main VPC
resource "aws_vpc" "vpc_main"
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags =
Name = "Main VPC"
resource "aws_subnet" "public"
vpc_id = "$aws_vpc.vpc_main.id"
cidr_block = "10.0.0.0/16"
map_public_ip_on_launch = true
tags =
Name = "Public Subnet"
resource "aws_security_group" "allow_web"
name = "allow-web-traffic"
description = "Allow all inbound/outbound traffic on 80 443"
vpc_id = "$aws_vpc.vpc_main.id"
ingress
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
ingress
from_port = 443
to_port = 443
protocol = "tcp"
egress
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
resource "aws_security_group" "allow_ssh"
name = "allow-ssh-traffic"
description = "Allow ssh traffic on 22"
vpc_id = "$aws_vpc.vpc_main.id"
ingress
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
egress
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
resource "aws_instance" "proxy_server"
ami = "ami-026f33d38b6410e30" # RHEL 7.5 HVM SSD
instance_type = "t2.micro"
key_name = "EC2NewKey"
security_groups = ["allow_ssh","allow_web"]
vpc_security_group_ids = ["$aws_security_groups.allow_ssh.id","$aws_security_group.allow_web.id"] # this breaks it
subnet_id = "$aws_subnet.public.id"
但在执行“Terraform plan”时出错:“未在根模块中声明托管资源“aws_security_groups”“allow_ssh”。谁能告诉我我在代码中犯了什么基本错误 ?
【问题讨论】:
你打错了:资源是aws_security_group
而不是groups
。
【参考方案1】:
一个错字。
aws_security_groups.allow_ssh.id
应该变成:
aws_security_group.allow_ssh.id
【讨论】:
以上是关于使用 terraform 启动 EC2 实例时出错的主要内容,如果未能解决你的问题,请参考以下文章
如何通过 Terraform 启动没有密钥对的 EC2 实例?
在 terraform 中使用模块将多个安全组分配给 ec2 时出错
在使用 terraform cloud [aws-provider] 启动 ec2 实例时,既不能执行 user_data 脚本,也不能使用连接块执行 remote-exec