create a terraform to AWS the service include VPC EC2 Load balancer and auto scalling
Here is an example Terraform code to deploy a VPC with EC2 instances, a load balancer, and auto scaling group in AWS:
# Define AWS provider
provider "aws" {
region = "us-west-2"
}
# Create a VPC with a public subnet and an internet gateway
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "my_vpc"
}
}
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
tags = {
Name = "public_subnet"
}
}
resource "aws_internet_gateway" "my_igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = "my_igw"
}
}
resource "aws_route_table" "public_route_table" {
vpc_id = aws_vpc.my_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_igw.id
}
tags = {
Name = "public_route_table"
}
}
resource "aws_route_table_association" "public_association" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_route_table.id
}
# Create a security group for the instances
resource "aws_security_group" "instance_sg" {
name_prefix = "instance_sg"
description = "Security group for instances"
vpc_id = aws_vpc.my_vpc.id
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
# Create an EC2 instance
resource "aws_instance" "my_ec2" {
ami = "ami-0c94855ba95c71c99"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.instance_sg.id]
subnet_id = aws_subnet.public_subnet.id
tags = {
Name = "my_ec2"
}
}
# Create a load balancer
resource "aws_lb" "my_lb" {
name = "my_lb"
load_balancer_type = "application"
subnets = [aws_subnet.public_subnet.id]
tags = {
Name = "my_lb"
}
}
# Add a listener to the load balancer
resource "aws_lb_listener" "my_lb_listener" {
load_balancer_arn = aws_lb.my_lb.arn
port = 80
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.my_target_group.arn
}
}
# Create a target group for the load balancer
resource "aws_lb_target_group" "my_target_group" {
name = "my_target_group"
port = 80
protocol = "HTTP"
vpc_id = aws_vpc.my_vpc.id
health_check {
healthy_threshold = 2
unhealthy_threshold = 2
timeout = 3
interval = 30
path = "/"
port = "traffic-port"
protocol = "HTTP"
}
}
# Register the EC2 instance with the target group
resource "aws_lb_target_group_attachment" "my_target_group_attachment" {
target_group_arn = aws_lb_target_group.my_target_group.arn
target_id = aws_instance.my_ec2.id
port = 80
}
# Create an auto scaling group
resource "aws_autoscaling_group" "my_asg" {
name = "my_asg"
vpc_zone_identifier = [aws_subnet.public_subnet.id]
launch_configuration = aws_launch_configuration.my_launch_config.name
max_size = 3
min_size = 1
desired_capacity = 2
tag {
key = "Name"
value = "my_asg"
propagate_at_launch = true
}
}
# Create a launch configuration for the auto scaling group
resource "aws_launch_configuration" "my_launch_config" {
name = "my_launch_config"
image_id = "ami-0c94855ba95c71c99"
instance_type = "t2.micro"
security_groups = [aws_security_group.instance_sg.id]
lifecycle {
create_before_destroy = true
}
}
This Terraform code will create a VPC with a public subnet and an internet gateway. It will also create a security group for the instances and launch an EC2 instance in the public subnet. A load balancer will be created with a target group and a listener. The EC2 instance will be registered with the target group. Finally, an auto scaling group will be created with a launch configuration and the EC2 instance will be launched in the auto scaling group.
原文地址: https://www.cveoy.top/t/topic/bDdp 著作权归作者所有。请勿转载和采集!