我正忙于学习更多关于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"
}
}
最后一步是将安全组连接到机器上,但同样,没有关于如何做到这一点的线索。我已经阅读了几篇文档,并试图在谷歌上搜索,但我似乎找不到答案,或者答案对我不起作用。所以希望你们能进一步帮助我。
谢谢你抽出时间,非常感谢!
在aws_security_group模块中,您需要通过在中添加以下内容来输出安全组id/模块/服务/安全组//main.tf
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
}