首页 > 其他分享 >用terraform在aws上部署vpc

用terraform在aws上部署vpc

时间:2023-02-26 03:11:06浏览次数:120  
标签:subnet vpc aws terraform VPC var id

用terraform在aws上部署vpc

VPC

AWS 虚拟私有云 (VPC) 是与您的 AWS 账户关联的虚拟网络。它代表一个与 AWS 公有云中的其他资源隔离的逻辑网络。 VPC 由几个独立的组件组成,被粗略地描述为在 AWS 中运行的“迷你数据中心”。在 VPC 中,组织托管 EC2 实例和其他 AWS 资源。 VPC 的管理通过 AWS 管理控制台完成,或者通过使用 Terraform 或 CloudFormation 的软件自动化完成。

VPC 的一些常见用例包括:

  • 托管网络应用程序

  • 托管网站或电子商务网站

  • 将工作负载迁移到 AWS 云

  • 将数据中心扩展到云端(混合云)

  • 备份或恢复

对于云网络,VPC 为帐户所有者提供了极大的灵活性以及对网络和安全环境的控制。这包括定义安全组、网络访问控制列表、创建 IP 子网、建立 IP 地址范围、配置路由表以及确定哪些 EC2 实例可公开访问的能力。可以在 VPC 中部署其他 AWS 服务(例如 Amazon S3),并且组织可以将 S3 访问限制为仅对 VPC 中的那些 EC2 实例进行访问。

  • VPC 的常见网络用例包括:

  • VPC 到 VPC 对等

  • VPC 到 On-premise 数据中心

  • 分支位置到 VPC 连接

  • 远程用户到基于 VPC 的应用程序

  • 多云对等(AWS VPC 到 Azure VNET 或谷歌云 VPC)

  • VPC 到 Internet 资源(VPC 出口流量)

最后,通过选择 VPC 选项通过 AWS 管理控制台直接设置 VPC。一旦选择,VPC 在(1)选择 IP 地址范围后开始运行; (2) 创建子网; (3) 创建到 Internet 的路由以及 (4) 授权进出 VPC 的流量。

 

terraform基础

一般文件包括:

  • main.tf: 主文件

  • variables.tf: 存储变量的文件

  • output.tf: 输出文件,一般用于module

tf的variables中default无法再用variable:利用module,赋值

一些结构:

variable "public_subnet_cidrs" {
   type = list(string)
   description = "public subnet cidrs"
   default="10.0.0.0/24"#没有default会让用户输入
}#采用 var.public_subnet_cidrs调用

data "aws_ami""webapp_ami" {
   filter {
     name = "name"
     values = ["csye6225_*"]
  }
   most_recent = true#因为名字设置的时间,可以找最新的一个
}#采用data.aws_ami.webapp_ami调用

 

 

代码参考

文件结构

modules

-webserver //webserver module

--main.tf

--output.tf //本次没用到,所以为空

--variables.tf

setups //运行的主程序,命令需要来到这个文件夹之下

-main.tf

-variables.tf

 

main.tf (modules/webserver/)

terraform {
 required_version = ">= 0.12"
}#版本要求

resource "aws_subnet" "public_subnets" {
count      = length(var.public_subnet_cidrs)
vpc_id     =  var.vpc_id
cidr_block = element(var.public_subnet_cidrs, count.index)
availability_zone = element(var.azs_public, count.index)
map_public_ip_on_launch = true
tags = {
  Name = "Public Subnet ${count.index + 1}"
}
}

resource "aws_subnet" "private_subnets" {
count      = length(var.private_subnet_cidrs)
vpc_id     =  var.vpc_id
cidr_block = element(var.private_subnet_cidrs, count.index)
availability_zone = element(var.azs_private, count.index)

tags = {
  Name = "Private Subnet ${count.index + 1}"
}
}

resource "aws_internet_gateway" "gw" {
vpc_id =  var.vpc_id
tags = {
  Name = "Project VPC IG"
}
}

resource "aws_route_table" "public" {
 vpc_id =  var.vpc_id
 route {
   cidr_block = "0.0.0.0/0"
   gateway_id = aws_internet_gateway.gw.id
}
 tags = {
   Name = "Public Route Table"
}
}

resource "aws_route_table_association" "public_subnet_asso" {
count = length(var.public_subnet_cidrs)
subnet_id      = element(aws_subnet.public_subnets[*].id, count.index)
route_table_id = aws_route_table.public.id
}

resource "aws_route_table" "private" {
 vpc_id = var.vpc_id
 route {
   cidr_block = "0.0.0.0/0"
   gateway_id = aws_internet_gateway.gw.id
}
 tags = {
   Name = "Private Route Table"
}
}

resource "aws_route_table_association" "private_subnet_asso" {
count = length(var.private_subnet_cidrs)
subnet_id      = element(aws_subnet.private_subnets[*].id, count.index)
route_table_id = aws_route_table.private.id
}

variables.tf (modules/webserver/)

variable "vpc_id" {
    type = string
    description = "vpc ID"
}

variable "public_subnet_cidrs" {
   type = list(string)
   description = "public subnet cidrs"
}

variable "private_subnet_cidrs" {
   type = list(string)
   description = "private subnet cidrs"
}


variable "azs_public" {
type        = list(string)
description = "Availability Zones"
}

variable "azs_private" {
type        = list(string)
description = "Availability Zones"
}

main.tf (setups/)

provider "aws" {
 region = var.region
}

resource "aws_vpc" "main" {
 #cidr_block = "10.0.0.0/16"
 cidr_block = "${var.cidr_block}.0.0/16"
 tags = {
   Name = "Project VPC"
}
}


module "will_webserver" {
 source = "../modules/webserver"
 vpc_id = aws_vpc.main.id
 public_subnet_cidrs = [
   "${var.cidr_block}.1.0/24",
   "${var.cidr_block}.2.0/24",
   #"${var.cidr_block}.3.0/24"
]
 private_subnet_cidrs = [
   "${var.cidr_block}.4.0/24",
   "${var.cidr_block}.5.0/24",
   #"${var.cidr_block}.6.0/24"
]

 azs_public  = ["${var.region}a",
                 "${var.region}b",
                # "${var.region}c"
                ]
 azs_private = ["${var.region}a",
                "${var.region}b",
                #"${var.region}c"
                ]
 #不是所有的region的可用az固定,按需求修改
}

variables.tf (setups/)

variable "region" {
 type        = string
 description = "Region name"
}

variable "cidr_block" {
 type        = string
 description = "CIDR block (X.X)"
}

前置条件

  • 安装terraform

  • 配置好aws configure

  • region确保都一致

运行命令

//需要到setups文件夹下
//格式化
terraform fmt

//初始化
terraform init

//验证正确
terraform validate

//模拟,非部署
terraform plan

//run
terraform apply
terraform apply --auto-approve

//摧毁
terraform destroy --auto-approve

参考资料



标签:subnet,vpc,aws,terraform,VPC,var,id
From: https://www.cnblogs.com/carrotmvp/p/17156053.html

相关文章