使用现有资源(安全组)的数据,获取:未在根模块中声明托管资源 [英] Using data for existing resource (security group), getting: A managed resource has not been declared in the root module
本文介绍了使用现有资源(安全组)的数据,获取:未在根模块中声明托管资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
学习 Terraform,我正在尝试启动一个 EC2 实例,重用现有的安全组(标记为 my-tib-sg
).
Learning Terraform, I'm trying to bring up an EC2 instance, reusing existing security group (tagged my-tib-sg
).
我收到以下错误,但不确定我做错了什么:
I'm getting the following error, and not sure what I'm doing wrong:
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.
,以区分数据源和资源.
When referring to data sources you need to prefix the address with data.
to differentiate between data sources and resources.
因此,在您的情况下,您应该像这样使用 data.aws_security_groups.my-tib-sg.id
:
So in your case you should use data.aws_security_groups.my-tib-sg.id
like so:
##################################################################################
# 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"
]
}
}
这篇关于使用现有资源(安全组)的数据,获取:未在根模块中声明托管资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文