Amazon EC2 Resources in Terraform
1. EC2 Instance
Resource Block:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with your AMI ID
instance_type = "t2.micro"
key_name = aws_key_pair.example.key_name
tags = {
Name = "example-instance"
}
}Notes:
amispecifies the Amazon Machine Image (AMI) ID to use for the instance.instance_typedefines the type of instance (e.g.,t2.micro).key_namespecifies the key pair for SSH access.
2. EC2 Key Pair
Resource Block:
resource "aws_key_pair" "example" {
key_name = "example-key"
public_key = file("~/.ssh/id_rsa.pub") # Path to your public key file
}Notes:
key_namespecifies the name of the key pair.public_keypoints to the public key file.
3. EC2 Security Group
Resource Block:
resource "aws_security_group" "example" {
name_prefix = "example-sg-"
description = "Security group for example EC2 instance"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
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"
}
}Notes:
ingressspecifies the inbound rules for the security group.egressspecifies the outbound rules for the security group.cidr_blocksdetermines the allowed IP addresses or ranges.
4. EC2 EBS Volume
Resource Block:
resource "aws_ebs_volume" "example" {
availability_zone = "us-west-2a" # Replace with your AZ
size = 10 # Size in GB
type = "gp2"
tags = {
Name = "example-ebs-volume"
}
}Notes:
availability_zonespecifies the Availability Zone.sizedetermines the volume size in GB.typespecifies the volume type (e.g.,gp2).
5. EC2 EBS Volume Attachment
Resource Block:
resource "aws_volume_attachment" "example" {
device_name = "/dev/xvdf"
volume_id = aws_ebs_volume.example.id
instance_id = aws_instance.example.id
}Notes:
device_namespecifies the device name to mount the volume.volume_idis the ID of the EBS volume.instance_idis the ID of the EC2 instance to attach the volume.
6. EC2 Launch Configuration
Resource Block:
resource "aws_launch_configuration" "example" {
name = "example-launch-configuration"
image_id = "ami-0c55b159cbfafe1f0" # Replace with your AMI ID
instance_type = "t2.micro"
key_name = aws_key_pair.example.key_name
lifecycle {
create_before_destroy = true
}
}Notes:
namespecifies the launch configuration name.image_idis the AMI ID.instance_typedefines the instance type.lifecycleblock ensures that the new launch configuration is created before the old one is destroyed.
7. EC2 Auto Scaling Group
Resource Block:
resource "aws_autoscaling_group" "example" {
launch_configuration = aws_launch_configuration.example.id
min_size = 1
max_size = 3
desired_capacity = 2
vpc_zone_identifier = ["subnet-0bb1c79de4EXAMPLE"] # Replace with your subnet ID
tag {
key = "Name"
value = "example-asg-instance"
propagate_at_launch = true
}
}Notes:
launch_configurationrefers to the launch configuration for the ASG.min_size,max_size, anddesired_capacitydefine the scaling policies.vpc_zone_identifierspecifies the subnet IDs for the ASG.
8. EC2 Elastic IP
Resource Block:
resource "aws_eip" "example" {
instance = aws_instance.example.id
tags = {
Name = "example-eip"
}
}Notes:
instanceassociates the EIP with an EC2 instance.
9. EC2 Spot Instance
Resource Block:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with your AMI ID
instance_type = "t2.micro"
spot_price = "0.03" # Maximum price you're willing to pay per hour
key_name = aws_key_pair.example.key_name
tags = {
Name = "example-spot-instance"
}
}Notes:
spot_pricespecifies the maximum price you're willing to pay for the spot instance.
10. EC2 Instance Metadata Options
Resource Block:
resource "aws_instance" "example" {
ami = "ami-0c55b159cbfafe1f0" # Replace with your AMI ID
instance_type = "t2.micro"
key_name = aws_key_pair.example.key_name
metadata_options {
http_tokens = "required"
http_put_responseHopLimit = 2
}
tags = {
Name = "example-instance-with-metadata"
}
}Notes:
metadata_optionsconfigures the instance metadata options.
Summary
This guide provides a comprehensive overview of managing EC2 resources using Terraform. You can use these configurations to deploy and manage EC2 instances and associated resources efficiently. Adjust the parameters and resource configurations as needed for your specific use case.🚀