【发布时间】:2023-04-02 04:34:01
【问题描述】:
我对 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。
标签: amazon-ec2 terraform