发布于 2026-01-06 4 阅读
0

使用 Terraform 构建 AWS 系列。DEV 全球展示挑战赛,由 Mux 呈现:展示你的项目!

使用 Terraform 构建 AWS 架构系列。

由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!

基础设施即代码 (IaC) 彻底改变了开发人员和运维团队配置和管理基础设施的方式,实现了自动化、可重复性和可扩展性。最近,我完成了一个重要的项目,涉及将工作负载从本地迁移到 AWS 云。在此过程中,我学到了很多东西,也掌握了一些技能,我很乐意与大家分享。

为了记录和反思这些经验教训,我决定推出一个新系列,重点介绍通过这些项目获得的关键见解和技能。

项目 #01:使用 Terraform 在 AWS 上部署 NGINX 服务器

这是一个简单的项目,旨在演示如何使用 Terraform 在 AWS 上轻松便捷地部署 NGINX 服务器。它展示了自动化和可重复流程如何简化基础设施配置,同时确保一致性和可靠性。

VPC图

项目步骤

以下是基础设施建设过程的详细说明:

1. 创建网络基础设施

  • 已设置一个VPC10.0.0.0/16 ,其 CIDR 块为。
  • VPC 内的公共子网允许实例连接到互联网。
  • VPC 连接了一个互联网网关以实现外部连接。
  • 创建了一个路由表并将其与子网关联,以便通过互联网网关路由流量。

2. 配置安全组

  • 定义了一个安全组,允许:
    • 端口 80 上的入站流量(HTTP)。
    • 端口 443 上的入站流量(HTTPS)。
  • 为了与外部资源进行通信,出站流量不受限制。

3. 部署 EC2 实例

  • 使用公开可用的Ubuntu AMI启动了t2.micro实例
  • 该实例配置了公共 IP 地址,以允许外部访问。
  • 用户数据脚本自动完成了 NGINX Web 服务器的安装和设置:
  #!/bin/bash
  sudo apt-get update -y
  sudo apt-get install -y nginx
  sudo systemctl start nginx
  sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

4. 验证部署

  • Terraform 输出结果中包含了 EC2 实例的公共 IP 地址。
  • 可以通过在浏览器中访问公共 IP 地址来访问 NGINX 的默认页面。

5. 清理资源

  • Terraformdestroy命令确保所有资源在不再需要时都能安全、系统地删除。

关键项目文件

  1. providers.tf已配置 AWS 提供商并定义了 Terraform 版本:
terraform {
   required_version = ">= 1.7.5"

   required_providers {
     aws = {
       source  = "hashicorp/aws"
       version = "~> 5.0"
     }
   }
 }

 provider "aws" {
  region = "us-east-1"
 }
Enter fullscreen mode Exit fullscreen mode
  1. network.tf定义了 VPC、子网、互联网网关、路由表和关联关系:
locals {
  common_tags = {
    project = "project01"
  }
}

resource "aws_vpc" "project01-vpc" {
  cidr_block = "10.0.0.0/16"
  tags = merge(local.common_tags, {
    Name = "project01-vpc"
  })
}

resource "aws_subnet" "public-subnet" {
  vpc_id     = aws_vpc.project01-vpc.id
  cidr_block = "10.0.0.0/24"
  tags = merge(local.common_tags, {
    Name = "public-subnet"
  })
}

resource "aws_internet_gateway" "project01-igw" {
  vpc_id = aws_vpc.project01-vpc.id

  tags = merge(local.common_tags, {
    Name = "project01-igw"
  })
}

resource "aws_route_table" "project01-rtb" {
  vpc_id = aws_vpc.project01-vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.project01-igw.id
  }

  tags = merge(local.common_tags, {
    Name = "project01-rtb"
  })
}

resource "aws_route_table_association" "public" {
  subnet_id      = aws_subnet.public-subnet.id
  route_table_id = aws_route_table.project01-rtb.id
}

Enter fullscreen mode Exit fullscreen mode
  1. compute.tf已配置 EC2 实例和安全组:

resource "aws_security_group" "nginx-server-sg" {
  description = "Security group allowing HTTP(port 80) and HTTPS(port 443)"
  name        = "nginx-server-sg"
  vpc_id      = aws_vpc.project01-vpc.id

  # Allow inbound HTTP traffic on port 80
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  # Allow inbound HTTP traffic on port 443
  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  # Allow all outbound traffic
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]

  }

  tags = merge(local.common_tags, {
    Name = "nginx-server-sg"
  })

}

resource "aws_instance" "nginx-server" {
  ami                         = "ami-0866a3c8686eaeeba"
  associate_public_ip_address = true
  instance_type               = "t2.micro"
  subnet_id                   = aws_subnet.public-subnet.id
  vpc_security_group_ids      = [aws_security_group.nginx-server-sg.id]


  root_block_device {
    delete_on_termination = true
    volume_size           = 10
    volume_type           = "gp3"
  }

  user_data = <<-EOF
                #!/bin/bash
                sudo apt-get update -y
                sudo apt-get install -y nginx
                sudo systemctl start nginx
                sudo systemctl enable nginx
                EOF

  tags = merge(local.common_tags, {
    Name = "project01-nginx-server"
  })

  lifecycle {
    create_before_destroy = true
  }
}

Enter fullscreen mode Exit fullscreen mode

结论

本项目旨在展示基础设施即代码 (IaC) 工具(例如 Terraform)的功能。虽然它并不全面或完美,但它为探索 IaC 如何简化和自动化基础设施管理奠定了基础。我希望本项目能够激发您的灵感,并鼓励您更深入地研究 IaC 实践。

您的反馈非常宝贵,请随时分享您的想法。我期待您的回复!


自己动手试试

  • 克隆仓库:
git clone https://github.com/bokal2/terraform-projects.git
cd project01
Enter fullscreen mode Exit fullscreen mode
  • 请按照 README 中的步骤部署您自己的 NGINX 服务器。

祝您搭建愉快!

文章来源:https://dev.to/bokal/architecting-aws-with-terraform-series-373l