123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- terraform {
- required_providers {
- aws = {
- source = "hashicorp/aws"
- version = "~> 1.0.4"
- }
- }
- }
- variable "aws_region" {}
- variable "base_cidr_block" {
- description = "A /16 CIDR range definition, such as 10.1.0.0/16, that the VPC will use"
- default = "10.1.0.0/16"
- }
- variable "availability_zones" {
- description = "A list of availability zones in which to create subnets"
- type = list(string)
- }
- provider "aws" {
- region = var.aws_region
- }
- resource "aws_vpc" "main" {
- # Referencing the base_cidr_block variable allows the network address
- # to be changed without modifying the configuration.
- cidr_block = var.base_cidr_block
- }
- resource "aws_subnet" "az" {
- # Create one subnet for each given availability zone.
- count = length(var.availability_zones)
- # For each subnet, use one of the specified availability zones.
- availability_zone = var.availability_zones[count.index]
- # By referencing the aws_vpc.main object, Terraform knows that the subnet
- # must be created only after the VPC is created.
- vpc_id = aws_vpc.main.id
- # Built-in functions and operators can be used for simple transformations of
- # values, such as computing a subnet address. Here we create a /20 prefix for
- # each subnet, using consecutive addresses for each availability zone,
- # such as 10.1.16.0/20 .
- cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index+1)
- }
|