This resource attaches a security group to an Elastic Network Interface (ENI). It can be used to attach a security group to any existing ENI, be it a secondary ENI or one attached as the primary interface on an instance.
The following provides a very basic example of setting up an instance (provided
by instance
) in the default security group, creating a security group
(provided by sg
) and then attaching the security group to the instance's
primary network interface via the aws_network_interface_sg_attachment
resource,
named sg_attachment
:
data "aws_ami" "ami" {
most_recent = true
filter {
name = "name"
values = ["amzn-ami-hvm-*"]
}
owners = ["amazon"]
}
resource "aws_instance" "instance" {
instance_type = "t2.micro"
ami = data.aws_ami.ami.id
tags = {
type = "terraform-test-instance"
}
}
resource "aws_security_group" "sg" {
tags = {
type = "terraform-test-security-group"
}
}
resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id = aws_security_group.sg.id
network_interface_id = aws_instance.instance.primary_network_interface_id
}
In this example, instance
is provided by the aws_instance
data source,
fetching an external instance, possibly not managed by Terraform.
sg_attachment
then attaches to the output instance's network_interface_id
:
data "aws_instance" "instance" {
instance_id = "i-1234567890abcdef0"
}
resource "aws_security_group" "sg" {
tags = {
type = "terraform-test-security-group"
}
}
resource "aws_network_interface_sg_attachment" "sg_attachment" {
security_group_id = aws_security_group.sg.id
network_interface_id = data.aws_instance.instance.network_interface_id
}
security_group_id
- (Required) The ID of the security group.network_interface_id
- (Required) The ID of the network interface to attach to.This resource exports no additional attributes.
create
- (Default 3m
)read
- (Default 3m
)delete
- (Default 3m
)In Terraform v1.5.0 and later, use an import
block to import Network Interface Security Group attachments using the associated network interface ID and security group ID, separated by an underscore (_
). For example:
import {
to = aws_network_interface_sg_attachment.sg_attachment
id = "eni-1234567890abcdef0_sg-1234567890abcdef0"
}
Using terraform import
, import Network Interface Security Group attachments using the associated network interface ID and security group ID, separated by an underscore (_
). For example:
% terraform import aws_network_interface_sg_attachment.sg_attachment eni-1234567890abcdef0_sg-1234567890abcdef0