0% found this document useful (0 votes)
10 views

Terraform to attach a security group to an EC2 instance

The document provides a Terraform example for attaching a security group to an EC2 instance. It includes the necessary code to define a security group that allows traffic from a specific IP address and to create an EC2 instance with that security group. Users are instructed to replace placeholder values with their actual configuration details and to execute Terraform commands to apply the changes.

Uploaded by

saiakkina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Terraform to attach a security group to an EC2 instance

The document provides a Terraform example for attaching a security group to an EC2 instance. It includes the necessary code to define a security group that allows traffic from a specific IP address and to create an EC2 instance with that security group. Users are instructed to replace placeholder values with their actual configuration details and to execute Terraform commands to apply the changes.

Uploaded by

saiakkina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

To attach a security group to an EC2 instance using Terraform, you can use the

`aws_instance` resource along with the `vpc_security_group_ids` argument. Here's an


example Terraform code:

```hcl
provider "aws" {
region = "us-east-1" # Update with your desired region
}

resource "aws_security_group" "allow_only_specific_ips_sg" {


name = "allow-only-specific-ips-sg"
description = "Security group to allow only specific IP addresses"
vpc_id = "your-vpc-id" # Update with your VPC ID

// Inbound rule to allow traffic from specific IP address


ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["50.60.70.80/32"]
}

// Outbound rule to allow all traffic


egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

resource "aws_instance" "example" {


ami = "ami-12345678" # Update with your desired AMI ID
instance_type = "t2.micro" # Update with your desired instance type
subnet_id = "your-subnet-id" # Update with your subnet ID
key_name = "your-key-pair" # Update with your SSH key pair name
security_groups = [aws_security_group.allow_only_specific_ips_sg.id] # Attach
the security group to the instance
}
```

Make sure to replace `"your-vpc-id"`, `"your-subnet-id"`, and `"your-key-pair"`


with your actual VPC ID, subnet ID, and key pair name, respectively. Also, update
`"ami-12345678"` with the desired AMI ID and adjust the instance type
(`"t2.micro"`) if needed.

After saving the above code in a `.tf` file, you can run `terraform init`,
`terraform plan`, and `terraform apply` commands in the directory where the file is
located to create the EC2 instance with the specified security group attached.

You might also like