Security Groups
1. Introduction
Security Groups act as virtual firewalls for your EC2 instances to control inbound and outbound traffic.
2. Creating a Security Group
Example:
resource "aws_security_group" "example" {
name = "example-sg"
description = "Example security group"
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 = "example-sg"
}
}
Explanation:
ingress
: Defines inbound traffic rules. For example, allowing HTTP traffic on port 80 from any IP.egress
: Defines outbound traffic rules. Allows all outbound traffic in this case.tags
: Optional. Tags to apply to the security group.
3. Applying Security Group to EC2 Instance
Example:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
security_groups = [aws_security_group.example.name]
tags = {
Name = "example-instance"
}
}
Explanation:
security_groups
: Applies the created security group to the EC2 instance.
SSH Keys
1. Introduction
SSH Keys are used to securely connect to your EC2 instances.
2. Creating an SSH Key Pair
Example:
resource "aws_key_pair" "example" {
key_name = "example-key"
public_key = file("~/.ssh/id_rsa.pub") # Path to your public key file
tags = {
Name = "example-key"
}
}
Explanation:
key_name
: The name of the key pair.public_key
: Path to the public key file on your local machine.
3. Using the SSH Key with an EC2 Instance
Example:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
key_name = aws_key_pair.example.key_name
tags = {
Name = "example-instance"
}
}
Explanation:
key_name
: Associates the SSH key with the EC2 instance.
Amazon Machine Images (AMIs)
1. Introduction
AMIs are used to create EC2 instances with pre-configured operating systems and applications.
2. Creating an AMI
Creating AMIs is usually done through the AWS Console or CLI. For Terraform, you can use existing AMIs or create your own manually.
Using Existing AMI:
Example:
data "aws_ami" "latest_amazon_linux" {
owners = ["amazon"]
most_recent = true
filters {
name = "name"
values = ["amzn2-ami-hvm-*-x86_64-gp2"]
}
}
Explanation:
data "aws_ami"
: Fetches details about the latest Amazon Linux 2 AMI.
3. Launching an EC2 Instance with AMI
Example:
resource "aws_instance" "example" {
ami = data.aws_ami.latest_amazon_linux.id
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
Snapshots
1. Introduction
Snapshots are backups of your EBS volumes that can be used to restore data.
2. Creating a Snapshot
Example:
Creating a snapshot is typically done through the AWS Console or CLI. For Terraform, you can automate the process of taking snapshots of volumes.
Example:
resource "aws_ebs_snapshot" "example" {
volume_id = "vol-12345678"
description = "Example snapshot"
}
Explanation:
volume_id
: The ID of the EBS volume you want to snapshot.description
: A description of the snapshot.
3. Restoring from a Snapshot
Example:
resource "aws_ebs_volume" "example" {
availability_zone = "us-west-2a"
size = 8
snapshot_id = aws_ebs_snapshot.example.id
}
Explanation:
snapshot_id
: The ID of the snapshot to restore from.
Elastic IPs
1. Introduction
Elastic IPs are static IP addresses designed for dynamic cloud computing.
2. Allocating an Elastic IP
Example:
resource "aws_eip" "example" {
instance = aws_instance.example.id
tags = {
Name = "example-eip"
}
}
Explanation:
instance
: Associates the Elastic IP with an EC2 instance.tags
: Optional. Tags to apply to the Elastic IP.
3. Using an Elastic IP
Example:
Allocate and associate an Elastic IP with an EC2 instance:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "example-instance"
}
}
resource "aws_eip" "example" {
instance = aws_instance.example.id
tags = {
Name = "example-eip"
}
}
This guide provides a basic overview of managing Security Groups, SSH Keys, AMIs, Snapshots, and Elastic IPs with Terraform. For more advanced configurations or additional resources, refer to the Terraform AWS Provider Documentation (opens in a new tab). If you need further assistance, feel free to ask! 🚀