云计算服务器组的搭建通常涉及到以下几个基础概念:
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "web" {
count = 3
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "web-server-${count.index}"
}
}
resource "aws_lb" "web_lb" {
name = "web-load-balancer"
internal = false
load_balancer_type = "application"
security_groups = [aws_security_group.lb_sg.id]
subnets = ["subnet-12345678", "subnet-87654321"]
}
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.web_lb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "404: page not found"
status_code = 404
}
}
}
resource "aws_lb_target_group" "web_tg" {
name = "web-target-group"
port = 80
protocol = "HTTP"
vpc_id = "vpc-12345678"
}
resource "aws_lb_listener_rule" "http_rule" {
listener_arn = aws_lb_listener.http.arn
priority = 100
condition {
path_pattern {
values = ["*"]
}
}
action {
type = "forward"
target_group_arn = aws_lb_target_group.web_tg.arn
}
}
resource "aws_security_group" "lb_sg" {
name = "lb-sg"
description = "Allow HTTP inbound traffic"
vpc_id = "vpc-12345678"
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
通过以上步骤和示例代码,可以快速搭建一个基本的云计算服务器组,并根据具体需求进行调整和优化。
没有搜到相关的文章