第 4 章:實例與負載均衡器設定
Reference Link
EC2 instance
EC2 instance
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = "t3.micro"
tags = {
Name = "HelloWorld"
}
}
Key pair
Key pair
resource "aws_key_pair" "deployer" {
key_name = "deployer-key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 email@example.com"
}
EBS
EBS
resource "aws_ebs_volume" "example" {
availability_zone = "us-west-2a"
size = 40
tags = {
Name = "HelloWorld"
}
}
- availability_zone (必選):指定 AZ
- iops(可選) :定義 IOPS,只有 io1, io2 or gp3 可選
- size(可選):定義 size (GiB)
- type(可選):可選擇 standard, gp2, gp3, io1, io2, sc1 or st1 (Default: gp2)
- throughput(可選)定義 throughput (MiB/s)只有 gp3 可選
關聯 EBS
resource "aws_volume_attachment" "ebs_att" {
device_name = "/dev/sdh"
volume_id = aws_ebs_volume.example.id
instance_id = aws_instance.web.id
}
resource "aws_instance" "web" {
ami = "ami-21f78e11"
availability_zone = "us-west-2a"
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
resource "aws_ebs_volume" "example" {
availability_zone = "us-west-2a"
size = 1
}
EIP
EIP
resource "aws_eip" "lb" {
instance = aws_instance.web.id
domain = "vpc"
}
resource "aws_vpc" "default" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
}
resource "aws_internet_gateway" "gw" {
vpc_id = aws_vpc.default.id
}
resource "aws_subnet" "tf_test_subnet" {
vpc_id = aws_vpc.default.id
cidr_block = "10.0.0.0/24"
map_public_ip_on_launch = true
depends_on = [aws_internet_gateway.gw]
}
resource "aws_instance" "foo" {
#us-west-2
ami = "ami-5189a661"
instance_type = "t2.micro"
private_ip = "10.0.0.12"
subnet_id = aws_subnet.tf_test_subnet.id
}
resource "aws_eip" "bar" {
domain = "vpc"
instance = aws_instance.foo.id
associate_with_private_ip = "10.0.0.12"
depends_on = [aws_internet_gateway.gw]
}
EIP association
關聯 EIP
resource "aws_eip_association" "eip_assoc" {
instance_id = aws_instance.web.id
allocation_id = aws_eip.example.id
}
resource "aws_instance" "web" {
ami = "ami-21f78e11"
availability_zone = "us-west-2a"
instance_type = "t2.micro"
tags = {
Name = "HelloWorld"
}
}
resource "aws_eip" "example" {
domain = "vpc"
}
Load balancer
Application Load Balancer
resource "aws_lb" "test" {
name = "test-lb-tf"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.lb_sg.id]
subnets = [for subnet in aws_subnet.public : subnet.id]
enable_deletion_protection = true
access_logs {
bucket = aws_s3_bucket.lb_logs.id
prefix = "test-lb"
enabled = true
}
tags = {
Environment = "production"
}
}
Network Load Balancer
resource "aws_lb" "test" {
name = "test-lb-tf"
internal = false
load_balancer_type = "network"
subnets = [for subnet in aws_subnet.public : subnet.id]
enable_deletion_protection = true
tags = {
Environment = "production"
}
}
Specifying Elastic IPs
resource "aws_lb" "example" {
name = "example"
load_balancer_type = "network"
subnet_mapping {
subnet_id = aws_subnet.example1.id
allocation_id = aws_eip.example1.id
}
subnet_mapping {
subnet_id = aws_subnet.example2.id
allocation_id = aws_eip.example2.id
}
}
Specifying private IP addresses for an internal-facing load balancer
resource "aws_lb" "example" {
name = "example"
load_balancer_type = "network"
subnet_mapping {
subnet_id = aws_subnet.example1.id
private_ipv4_address = "10.0.1.15"
}
subnet_mapping {
subnet_id = aws_subnet.example2.id
private_ipv4_address = "10.0.2.15"
}
}
Load balancer listener
ALB Listener
resource "aws_lb" "front_end" {
# ...
}
resource "aws_lb_target_group" "front_end" {
# ...
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.front_end.arn
port = "443"
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-2016-08"
certificate_arn = "arn:aws:iam::187416307283:server-certificate/test_cert_rab3wuqwgja25ct3n4jdj2tzu4"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.front_end.arn
}
}
NLB Listener
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.front_end.arn
port = "443"
protocol = "TLS"
certificate_arn = "arn:aws:iam::187416307283:server-certificate/test_cert_rab3wuqwgja25ct3n4jdj2tzu4"
alpn_policy = "HTTP2Preferred"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.front_end.arn
}
}
Listener with redirect action
resource "aws_lb" "front_end" {
# ...
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.front_end.arn
port = "80"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
Listener with fixed-response Action
resource "aws_lb" "front_end" {
# ...
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.front_end.arn
port = "80"
protocol = "HTTP"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "Fixed response content"
status_code = "200"
}
}
}
resource "aws_acm_certificate" "example" {
# ...
}
resource "aws_lb" "front_end" {
# ...
}
resource "aws_lb_listener" "front_end" {
# ...
}
Listener Certificate
resource "aws_lb_listener_certificate" "example" {
listener_arn = aws_lb_listener.front_end.arn
certificate_arn = aws_acm_certificate.example.arn
}
aws_lb_listener_rule
resource "aws_lb" "front_end" {
# ...
}
resource "aws_lb_listener" "front_end" {
# Other parameters
}
resource "aws_lb_listener_rule" "static" {
listener_arn = aws_lb_listener.front_end.arn
priority = 100
action {
type = "forward"
target_group_arn = aws_lb_target_group.static.arn
}
condition {
path_pattern {
values = ["/static/*"]
}
}
condition {
host_header {
values = ["example.com"]
}
}
}
Forward action
resource "aws_lb_listener_rule" "host_based_weighted_routing" {
listener_arn = aws_lb_listener.front_end.arn
priority = 99
action {
type = "forward"
target_group_arn = aws_lb_target_group.static.arn
}
condition {
host_header {
values = ["my-service.*.terraform.io"]
}
}
}
Weighted Forward action
resource "aws_lb_listener_rule" "host_based_routing" {
listener_arn = aws_lb_listener.front_end.arn
priority = 99
action {
type = "forward"
forward {
target_group {
arn = aws_lb_target_group.main.arn
weight = 80
}
target_group {
arn = aws_lb_target_group.canary.arn
weight = 20
}
stickiness {
enabled = true
duration = 600
}
}
}
condition {
host_header {
values = ["my-service.*.terraform.io"]
}
}
}
Redirect action
resource "aws_lb_listener_rule" "redirect_http_to_https" {
listener_arn = aws_lb_listener.front_end.arn
action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
condition {
http_header {
http_header_name = "X-Forwarded-For"
values = ["192.168.1.*"]
}
}
}
Fixed-response action
resource "aws_lb_listener_rule" "health_check" {
listener_arn = aws_lb_listener.front_end.arn
action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "HEALTHY"
status_code = "200"
}
}
condition {
query_string {
key = "health"
value = "check"
}
query_string {
value = "bar"
}
}
}
Target group
Instance Target Group
resource "aws_lb_target_group" "test" {
name = "tf-example-lb-tg"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.main.id
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
IP Target Group
resource "aws_lb_target_group" "ip-example" {
name = "tf-example-lb-tg"
port = 80
protocol = "HTTP"
target_type = "ip"
vpc_id = aws_vpc.main.id
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
Lambda Target Group
resource "aws_lb_target_group" "lambda-example" {
name = "tf-example-lb-tg"
target_type = "lambda"
}
ALB Target Group
resource "aws_lb_target_group" "alb-example" {
name = "tf-example-lb-alb-tg"
target_type = "alb"
port = 80
protocol = "TCP"
vpc_id = aws_vpc.main.id
}
Target Group Attachment
resource "aws_lb_target_group_attachment" "test" {
target_group_arn = aws_lb_target_group.test.arn
target_id = aws_instance.test.id
port = 80
}
resource "aws_lb_target_group" "test" {
# ... other configuration ...
}
resource "aws_instance" "test" {
# ... other configuration ...
}