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:
ami
specifies the Amazon Machine Image (AMI) ID to use for the instance.instance_type
defines the type of instance (e.g.,t2.micro
).key_name
specifies 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_name
specifies the name of the key pair.public_key
points 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:
ingress
specifies the inbound rules for the security group.egress
specifies the outbound rules for the security group.cidr_blocks
determines 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_zone
specifies the Availability Zone.size
determines the volume size in GB.type
specifies 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_name
specifies the device name to mount the volume.volume_id
is the ID of the EBS volume.instance_id
is 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:
name
specifies the launch configuration name.image_id
is the AMI ID.instance_type
defines the instance type.lifecycle
block 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_configuration
refers to the launch configuration for the ASG.min_size
,max_size
, anddesired_capacity
define the scaling policies.vpc_zone_identifier
specifies 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:
instance
associates 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_price
specifies 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_options
configures 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.🚀