sample.hcl.txt 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. terraform {
  2. required_providers {
  3. aws = {
  4. source = "hashicorp/aws"
  5. version = "~> 1.0.4"
  6. }
  7. }
  8. }
  9. variable "aws_region" {}
  10. variable "base_cidr_block" {
  11. description = "A /16 CIDR range definition, such as 10.1.0.0/16, that the VPC will use"
  12. default = "10.1.0.0/16"
  13. }
  14. variable "availability_zones" {
  15. description = "A list of availability zones in which to create subnets"
  16. type = list(string)
  17. }
  18. provider "aws" {
  19. region = var.aws_region
  20. }
  21. resource "aws_vpc" "main" {
  22. # Referencing the base_cidr_block variable allows the network address
  23. # to be changed without modifying the configuration.
  24. cidr_block = var.base_cidr_block
  25. }
  26. resource "aws_subnet" "az" {
  27. # Create one subnet for each given availability zone.
  28. count = length(var.availability_zones)
  29. # For each subnet, use one of the specified availability zones.
  30. availability_zone = var.availability_zones[count.index]
  31. # By referencing the aws_vpc.main object, Terraform knows that the subnet
  32. # must be created only after the VPC is created.
  33. vpc_id = aws_vpc.main.id
  34. # Built-in functions and operators can be used for simple transformations of
  35. # values, such as computing a subnet address. Here we create a /20 prefix for
  36. # each subnet, using consecutive addresses for each availability zone,
  37. # such as 10.1.16.0/20 .
  38. cidr_block = cidrsubnet(aws_vpc.main.cidr_block, 4, count.index+1)
  39. }