使用现有资源(安全组)的数据,获取:未在根模块中声明的托管资源



学习Terraform,我试图提出一个EC2实例,重用现有的安全组(标记为my-tib-sg)。

我得到以下错误,不确定我做错了什么:

Error: Reference to undeclared resource
on module_three.tf line 62, in resource "aws_instance" "nginx":
62:   vpc_security_group_ids = [aws_security_groups.my-tib-sg.id]
A managed resource "aws_security_groups" "my-tib-sg" has not been declared in
the root module.

代码如下:

##################################################################################
# VARIABLES
##################################################################################
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "private_key_path" {}
variable "key_name" {}
variable "region" {
default = "us-east-1"
}
##################################################################################
# PROVIDERS
##################################################################################
provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
region     = var.region
}
##################################################################################
# DATA
##################################################################################
data "aws_ami" "aws-linux" {
most_recent = true
owners      = ["amazon"]
filter {
name   = "name"
values = ["amzn-ami-hvm*"]
}
filter {
name   = "root-device-type"
values = ["ebs"]
}
filter {
name   = "virtualization-type"
values = ["hvm"]
}
}
data "aws_security_groups" "my-tib-sg" {
tags = {
Name = "my-tib-sg"
}
}

##################################################################################
# RESOURCES
##################################################################################
resource "aws_instance" "nginx" {
ami                    = data.aws_ami.aws-linux.id
instance_type          = "t2.micro"
key_name               = var.key_name
vpc_security_group_ids = [aws_security_groups.my-tib-sg.id]
connection {
type        = "ssh"
host        = self.public_ip
user        = "ec2-user"
private_key = file(var.private_key_path)
}
provisioner "remote-exec" {
inline = [
"sudo yum install nginx -y",
"sudo service nginx start"
]
}
}
##################################################################################
# OUTPUT
##################################################################################
output "aws_instance_public_dns" {
value = aws_instance.nginx.public_dns
}

引用数据源时,需要在地址前加上data.,以区分数据源和资源。

所以在你的情况下,你应该像这样使用data.aws_security_groups.my-tib-sg.id:

##################################################################################
# VARIABLES
##################################################################################
variable "aws_access_key" {}
variable "aws_secret_key" {}
variable "private_key_path" {}
variable "key_name" {}
variable "region" {
default = "us-east-1"
}
##################################################################################
# PROVIDERS
##################################################################################
provider "aws" {
access_key = var.aws_access_key
secret_key = var.aws_secret_key
region     = var.region
}
##################################################################################
# DATA
##################################################################################
data "aws_ami" "aws-linux" {
most_recent = true
owners      = ["amazon"]
filter {
name   = "name"
values = ["amzn-ami-hvm*"]
}
filter {
name   = "root-device-type"
values = ["ebs"]
}
filter {
name   = "virtualization-type"
values = ["hvm"]
}
}
data "aws_security_groups" "my-tib-sg" {
tags = {
Name = "my-tib-sg"
}
}

##################################################################################
# RESOURCES
##################################################################################
resource "aws_instance" "nginx" {
ami                    = data.aws_ami.aws-linux.id
instance_type          = "t2.micro"
key_name               = var.key_name
vpc_security_group_ids = [data.aws_security_groups.my-tib-sg.id]
connection {
type        = "ssh"
host        = self.public_ip
user        = "ec2-user"
private_key = file(var.private_key_path)
}
provisioner "remote-exec" {
inline = [
"sudo yum install nginx -y",
"sudo service nginx start"
]
}
}

最新更新