我正忙于尝试了解有关Terraform的更多信息,但是遇到一个问题,我不知道如何解决/修复。
问题如下,在我的脚本中,我正在生成一个ec2实例(AWS),它带有一些附带的东西,例如en EIP和来自模块的安全组,都可以正常工作。但是我无法弄清楚如何将安全组附加到计算机上,现在已经创建了它。
代码如下:
data "aws_ami" "latest" {
most_recent = true
owners = [ "self"]
filter {
name = "name"
values = [ lookup(var.default_ami, var.ami) ]
}
}
module "aws_security_group" {
source = "./modules/services/Security groups/"
server_port = 443
}
resource "aws_instance" "test-ec2deployment" {
ami = data.aws_ami.latest.id
instance_type = var.instance_type
subnet_id = var.subnet_id
availability_zone = var.availability_zone
associate_public_ip_address = var.public_ip
root_block_device {
volume_type = "gp2"
volume_size = 60
delete_on_termination = true
}
tags = {
Name = "Testserver2viaTerraform"
}
}
resource "aws_eip" "ip" {
instance = aws_instance.test-ec2deployment.id
}
resource "aws_eip" "example" {
vpc = true
}
上面是主文件,我正在加载以下模块:
resource "aws_security_group" "my-webserver" {
name = "webserver"
description = "Allow HTTP from Anywhere"
vpc_id = "vpc-"
ingress {
from_port = 80
to_port = 80
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"]
}
tags = {
Name = "my-webserver"
Site = "my-web-site"
}
}
最后一步是将安全组附加到计算机,但是同样,该操作没有任何线索。我一直在阅读一些文档并尝试使用google,但是我似乎找不到答案或答案对我不起作用。所以希望你们能进一步帮助我。
感谢您的宝贵时间,不胜感激!
在aws_security_group模块中,您需要通过在./modules/services/Security groups // main.tf中添加以下内容来输出安全组ID。
output "securitygroup_id" {
value = aws_security_group.my-webserver.id
}
然后在您的主tf文件中将安全组附加到您的实例,如下所示:
resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id = module.aws_security_group.securitygroup_id
network_interface_id = aws_instance.test-ec2deployment.primary_network_interface_id
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句