feat(terraform): AWS VPC
[csit.git] / fdio.infra.terraform / terraform-aws-vpc / main.tf
1 locals {
2   tags = {
3     "Name"        = "${var.tags_name}"
4     "Environment" = "${var.tags_environment}"
5   }
6 }
7
8 # Create VPC
9 resource "aws_vpc" "vpc" {
10   assign_generated_ipv6_cidr_block = var.vpc_assign_generated_ipv6_cidr_block
11   cidr_block                       = var.vpc_cidr_block
12   enable_dns_hostnames             = var.vpc_enable_dns_hostnames
13   enable_dns_support               = var.vpc_enable_dns_support
14   instance_tenancy                 = var.vpc_instance_tenancy
15   tags                             = local.tags
16 }
17
18 # Create Security Groups
19 resource "aws_security_group" "security_group" {
20   depends_on = [
21     aws_vpc.vpc
22   ]
23   description            = var.security_group_description
24   name                   = var.security_group_name
25   revoke_rules_on_delete = var.security_group_revoke_rules_on_delete
26   tags                   = local.tags
27   vpc_id                 = aws_vpc.vpc.id
28
29   dynamic "ingress" {
30     for_each = var.security_group_ingress
31     content {
32       from_port        = ingress.value["from_port"]
33       to_port          = ingress.value["to_port"]
34       protocol         = ingress.value["protocol"]
35       cidr_blocks      = ingress.value["cidr_blocks"]
36       ipv6_cidr_blocks = ingress.value["ipv6_cidr_blocks"]
37     }
38   }
39   dynamic "egress" {
40     for_each = var.security_group_egress
41     content {
42       from_port        = ingress.value["from_port"]
43       to_port          = ingress.value["to_port"]
44       protocol         = ingress.value["protocol"]
45       cidr_blocks      = ingress.value["cidr_blocks"]
46       ipv6_cidr_blocks = ingress.value["ipv6_cidr_blocks"]
47     }
48   }
49 }
50
51 # Create Gateway
52 resource "aws_internet_gateway" "internet_gateway" {
53   depends_on = [
54     aws_vpc.vpc
55   ]
56   tags   = local.tags
57   vpc_id = aws_vpc.vpc.id
58 }
59
60 # Create Routes
61 resource "aws_route" "route" {
62   depends_on = [
63     aws_vpc.vpc,
64     aws_internet_gateway.internet_gateway
65   ]
66   destination_cidr_block      = "0.0.0.0/0"
67   gateway_id                  = aws_internet_gateway.internet_gateway.id
68   route_table_id              = aws_vpc.vpc.main_route_table_id
69 }
70
71 # Create Subnets
72 resource "aws_subnet" "subnet" {
73   depends_on = [
74     aws_vpc.vpc
75   ]
76   assign_ipv6_address_on_creation = var.subnet_assign_ipv6_address_on_creation
77   availability_zone               = var.subnet_availability_zone
78   cidr_block                      = aws_vpc.vpc.cidr_block
79   ipv6_cidr_block                 = cidrsubnet(aws_vpc.vpc.ipv6_cidr_block, 8, 1)
80   map_public_ip_on_launch         = var.subnet_map_public_ip_on_launch
81   tags                            = local.tags
82   vpc_id                          = aws_vpc.vpc.id
83 }