amazon-web-services - 使用现有资源(安全组)的数据,获取:未在根模块中声明托管资源
问题描述
学习 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"
]
}
}
推荐阅读
- spring - 在 Spring Boot 应用程序中从 Amazon S3 存储桶中的某个文件夹下载文件时获取 403
- javascript - 使用 VueJS 递归嵌套组件
- php - Woocommerce:根据运输等级向产品价格添加固定费用
- angular - 为什么(可观察
) => 可观察的 解析为 OperatorFunction - android - Koltin 函数参考:此表达式未使用
- python - VSCode:使用非本地导致变量类型“从不”
- variables - Sass 检查是否将值传递给 mixin
- python - tensorflow 2.5.0 arm64 mac的性能
- laravel - laravel Auth::attempt() 总是返回 false
- for-loop - ffmpeg/for 循环:如何使 ffmpeg 或 cmd 循环在发现错误后停止?