【问题标题】:Using data for existing resource (security group), getting: A managed resource has not been declared in the root module使用现有资源(安全组)的数据,获取:未在根模块中声明托管资源
【发布时间】:2021-04-26 02:14:28
【问题描述】:

学习 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.aws_security_groups.my-tib-sg.id
  • @ydaetskcoR 我怎么会错过呢?也许写一个答案,所以我可以接受它..
  • 我很确定有一个现有的答案,这是一个副本,但我根本找不到它,所以我会在这里回答,然后在将来尝试将其用作重复目标如果它再次出现。

标签: amazon-web-services terraform terraform-provider-aws


【解决方案1】:

引用数据源时,需要在地址前加上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"
    ]
  }
}

【讨论】:

    猜你喜欢
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    相关资源
    最近更新 更多