From c5181ae1627fed20b79a829bf2330f3ee9d8a0f6 Mon Sep 17 00:00:00 2001 From: Peter Mikus Date: Thu, 24 Mar 2022 10:48:15 +0100 Subject: [PATCH] feat(terraform): AWS VPC Signed-off-by: Peter Mikus Change-Id: I3acd41dc07c15eac0e87a4aedd75d53b30666cf8 --- fdio.infra.terraform/terraform-aws-vpc/README.md | 54 +++++++++++ .../terraform-aws-vpc/example/main.tf | 7 ++ .../terraform-aws-vpc/example/output.tf | 4 + .../terraform-aws-vpc/example/providers.tf | 3 + .../terraform-aws-vpc/example/variables.tf | 5 + .../terraform-aws-vpc/example/versions.tf | 9 ++ fdio.infra.terraform/terraform-aws-vpc/main.tf | 83 ++++++++++++++++ fdio.infra.terraform/terraform-aws-vpc/outputs.tf | 4 + .../terraform-aws-vpc/variables.tf | 107 +++++++++++++++++++++ fdio.infra.terraform/terraform-aws-vpc/versions.tf | 9 ++ 10 files changed, 285 insertions(+) create mode 100644 fdio.infra.terraform/terraform-aws-vpc/README.md create mode 100644 fdio.infra.terraform/terraform-aws-vpc/example/main.tf create mode 100644 fdio.infra.terraform/terraform-aws-vpc/example/output.tf create mode 100644 fdio.infra.terraform/terraform-aws-vpc/example/providers.tf create mode 100644 fdio.infra.terraform/terraform-aws-vpc/example/variables.tf create mode 100644 fdio.infra.terraform/terraform-aws-vpc/example/versions.tf create mode 100644 fdio.infra.terraform/terraform-aws-vpc/main.tf create mode 100644 fdio.infra.terraform/terraform-aws-vpc/outputs.tf create mode 100644 fdio.infra.terraform/terraform-aws-vpc/variables.tf create mode 100644 fdio.infra.terraform/terraform-aws-vpc/versions.tf diff --git a/fdio.infra.terraform/terraform-aws-vpc/README.md b/fdio.infra.terraform/terraform-aws-vpc/README.md new file mode 100644 index 0000000000..4903bcb36f --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/README.md @@ -0,0 +1,54 @@ + +## Requirements + +| Name | Version | +|------|---------| +| [terraform](#requirement\_terraform) | >= 1.1.4 | +| [aws](#requirement\_aws) | ~> 4.3.0 | + +## Providers + +| Name | Version | +|------|---------| +| [aws](#provider\_aws) | ~> 4.3.0 | + +## Modules + +No modules. + +## Resources + +| Name | Type | +|------|------| +| [aws_internet_gateway.internet_gateway](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/internet_gateway) | resource | +| [aws_route.route](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource | +| [aws_security_group.security_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource | +| [aws_subnet.subnet](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource | +| [aws_vpc.vpc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc) | resource | + +## Inputs + +| Name | Description | Type | Default | Required | +|------|-------------|------|---------|:--------:| +| [security\_group\_description](#input\_security\_group\_description) | Security group description. | `string` | `"Allow inbound/outbound traffic"` | no | +| [security\_group\_egress](#input\_security\_group\_egress) | Egress security group map. | `list(any)` |
[
{
"cidr_blocks": [
"0.0.0.0/0"
],
"from_port": 0,
"ipv6_cidr_blocks": [
"::/0"
],
"protocol": "-1",
"to_port": 0
}
]
| no | +| [security\_group\_ingress](#input\_security\_group\_ingress) | Ingress security group map. | `list(any)` |
[
{
"cidr_blocks": [
"0.0.0.0/0"
],
"from_port": 22,
"ipv6_cidr_blocks": [
"::/0"
],
"protocol": "tcp",
"to_port": 22
}
]
| no | +| [security\_group\_name](#input\_security\_group\_name) | Name of the security group. | `string` | n/a | yes | +| [security\_group\_revoke\_rules\_on\_delete](#input\_security\_group\_revoke\_rules\_on\_delete) | Instruct Terraform to revoke all of the Security Groups attached ingress and egress rules before deleting the rule itself. | `bool` | `false` | no | +| [subnet\_assign\_ipv6\_address\_on\_creation](#input\_subnet\_assign\_ipv6\_address\_on\_creation) | Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address. | `bool` | `false` | no | +| [subnet\_availability\_zone](#input\_subnet\_availability\_zone) | AWS availability zone | `string` | `"us-east-1a"` | no | +| [subnet\_map\_public\_ip\_on\_launch](#input\_subnet\_map\_public\_ip\_on\_launch) | Specify true to indicate that instances launched into the subnet should be assigned a public IP address. | `bool` | `false` | no | +| [tags\_environment](#input\_tags\_environment) | Environment used for tag. | `string` | `""` | no | +| [tags\_name](#input\_tags\_name) | Name used for tag. | `string` | `""` | no | +| [vpc\_assign\_generated\_ipv6\_cidr\_block](#input\_vpc\_assign\_generated\_ipv6\_cidr\_block) | Requests an Amazon-provided IPv6 CIDR block with a /56 prefix length for the VPC. | `bool` | `true` | no | +| [vpc\_cidr\_block](#input\_vpc\_cidr\_block) | The CIDR block for the association. | `string` | `"192.168.0.0/24"` | no | +| [vpc\_enable\_dns\_hostnames](#input\_vpc\_enable\_dns\_hostnames) | Whether or not the VPC has DNS hostname support. | `bool` | `true` | no | +| [vpc\_enable\_dns\_support](#input\_vpc\_enable\_dns\_support) | Whether or not the VPC has DNS support. | `bool` | `true` | no | +| [vpc\_instance\_tenancy](#input\_vpc\_instance\_tenancy) | The allowed tenancy of instances launched into the selected VPC. | `string` | `"default"` | no | + +## Outputs + +| Name | Description | +|------|-------------| +| [vpc\_id](#output\_vpc\_id) | The ID of the VPC | + \ No newline at end of file diff --git a/fdio.infra.terraform/terraform-aws-vpc/example/main.tf b/fdio.infra.terraform/terraform-aws-vpc/example/main.tf new file mode 100644 index 0000000000..9d9469754d --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/example/main.tf @@ -0,0 +1,7 @@ +module "vpc" { + source = "../" + security_group_name = "terraform-sg" + subnet_availability_zone = "us-east-1a" + tags_name = "terraform-vpc" + tags_environment = "terraform-vpc-environment" +} diff --git a/fdio.infra.terraform/terraform-aws-vpc/example/output.tf b/fdio.infra.terraform/terraform-aws-vpc/example/output.tf new file mode 100644 index 0000000000..c26bbaecb6 --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/example/output.tf @@ -0,0 +1,4 @@ +output "vpc_id" { + value = module.vpc.vpc_id + description = "The ID of the VPC" +} \ No newline at end of file diff --git a/fdio.infra.terraform/terraform-aws-vpc/example/providers.tf b/fdio.infra.terraform/terraform-aws-vpc/example/providers.tf new file mode 100644 index 0000000000..5ff54f0d65 --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/example/providers.tf @@ -0,0 +1,3 @@ +provider "aws" { + region = var.region +} \ No newline at end of file diff --git a/fdio.infra.terraform/terraform-aws-vpc/example/variables.tf b/fdio.infra.terraform/terraform-aws-vpc/example/variables.tf new file mode 100644 index 0000000000..f75f3d547f --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/example/variables.tf @@ -0,0 +1,5 @@ +variable "region" { + description = "AWS Region." + type = string + default = "us-east-1" +} diff --git a/fdio.infra.terraform/terraform-aws-vpc/example/versions.tf b/fdio.infra.terraform/terraform-aws-vpc/example/versions.tf new file mode 100644 index 0000000000..af1be4a4e1 --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/example/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = ">= 4.3.0" + } + } + required_version = ">= 1.1.4" +} diff --git a/fdio.infra.terraform/terraform-aws-vpc/main.tf b/fdio.infra.terraform/terraform-aws-vpc/main.tf new file mode 100644 index 0000000000..ed2e8715e7 --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/main.tf @@ -0,0 +1,83 @@ +locals { + tags = { + "Name" = "${var.tags_name}" + "Environment" = "${var.tags_environment}" + } +} + +# Create VPC +resource "aws_vpc" "vpc" { + assign_generated_ipv6_cidr_block = var.vpc_assign_generated_ipv6_cidr_block + cidr_block = var.vpc_cidr_block + enable_dns_hostnames = var.vpc_enable_dns_hostnames + enable_dns_support = var.vpc_enable_dns_support + instance_tenancy = var.vpc_instance_tenancy + tags = local.tags +} + +# Create Security Groups +resource "aws_security_group" "security_group" { + depends_on = [ + aws_vpc.vpc + ] + description = var.security_group_description + name = var.security_group_name + revoke_rules_on_delete = var.security_group_revoke_rules_on_delete + tags = local.tags + vpc_id = aws_vpc.vpc.id + + dynamic "ingress" { + for_each = var.security_group_ingress + content { + from_port = ingress.value["from_port"] + to_port = ingress.value["to_port"] + protocol = ingress.value["protocol"] + cidr_blocks = ingress.value["cidr_blocks"] + ipv6_cidr_blocks = ingress.value["ipv6_cidr_blocks"] + } + } + dynamic "egress" { + for_each = var.security_group_egress + content { + from_port = ingress.value["from_port"] + to_port = ingress.value["to_port"] + protocol = ingress.value["protocol"] + cidr_blocks = ingress.value["cidr_blocks"] + ipv6_cidr_blocks = ingress.value["ipv6_cidr_blocks"] + } + } +} + +# Create Gateway +resource "aws_internet_gateway" "internet_gateway" { + depends_on = [ + aws_vpc.vpc + ] + tags = local.tags + vpc_id = aws_vpc.vpc.id +} + +# Create Routes +resource "aws_route" "route" { + depends_on = [ + aws_vpc.vpc, + aws_internet_gateway.internet_gateway + ] + destination_cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.internet_gateway.id + route_table_id = aws_vpc.vpc.main_route_table_id +} + +# Create Subnets +resource "aws_subnet" "subnet" { + depends_on = [ + aws_vpc.vpc + ] + assign_ipv6_address_on_creation = var.subnet_assign_ipv6_address_on_creation + availability_zone = var.subnet_availability_zone + cidr_block = aws_vpc.vpc.cidr_block + ipv6_cidr_block = cidrsubnet(aws_vpc.vpc.ipv6_cidr_block, 8, 1) + map_public_ip_on_launch = var.subnet_map_public_ip_on_launch + tags = local.tags + vpc_id = aws_vpc.vpc.id +} diff --git a/fdio.infra.terraform/terraform-aws-vpc/outputs.tf b/fdio.infra.terraform/terraform-aws-vpc/outputs.tf new file mode 100644 index 0000000000..444cde448a --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/outputs.tf @@ -0,0 +1,4 @@ +output "vpc_id" { + value = aws_vpc.vpc.id + description = "The ID of the VPC" +} \ No newline at end of file diff --git a/fdio.infra.terraform/terraform-aws-vpc/variables.tf b/fdio.infra.terraform/terraform-aws-vpc/variables.tf new file mode 100644 index 0000000000..1e883eda78 --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/variables.tf @@ -0,0 +1,107 @@ +variable "tags_name" { + description = "Name used for tag." + type = string + default = "" +} + +variable "tags_environment" { + description = "Environment used for tag." + type = string + default = "" +} + +# Variables for elastic beanstalk VPC +variable "vpc_assign_generated_ipv6_cidr_block" { + description = "Requests an Amazon-provided IPv6 CIDR block with a /56 prefix length for the VPC." + type = bool + default = true +} + +variable "vpc_cidr_block" { + description = "The CIDR block for the association." + type = string + default = "192.168.0.0/24" +} + +variable "vpc_enable_dns_hostnames" { + description = "Whether or not the VPC has DNS hostname support." + type = bool + default = true +} + +variable "vpc_enable_dns_support" { + description = "Whether or not the VPC has DNS support." + type = bool + default = true +} + +variable "vpc_instance_tenancy" { + description = "The allowed tenancy of instances launched into the selected VPC." + type = string + default = "default" +} + +# Variables for Security Group +variable "security_group_description" { + description = "Security group description." + type = string + default = "Allow inbound/outbound traffic" +} + +variable "security_group_name" { + description = "Name of the security group." + type = string +} + +variable "security_group_revoke_rules_on_delete" { + description = "Instruct Terraform to revoke all of the Security Groups attached ingress and egress rules before deleting the rule itself." + type = bool + default = false +} + +variable "security_group_ingress" { + description = "Ingress security group map." + type = list(any) + default = [ + { + from_port = 22 + to_port = 22 + protocol = "tcp" + cidr_blocks = ["0.0.0.0/0"] + ipv6_cidr_blocks = ["::/0"] + }, + ] +} + +variable "security_group_egress" { + description = "Egress security group map." + type = list(any) + default = [ + { + from_port = 0 + to_port = 0 + protocol = "-1" + cidr_blocks = ["0.0.0.0/0"] + ipv6_cidr_blocks = ["::/0"] + }, + ] +} + +# Variables for elastic beanstalk Subnet +variable "subnet_assign_ipv6_address_on_creation" { + description = "Specify true to indicate that network interfaces created in the specified subnet should be assigned an IPv6 address." + type = bool + default = false +} + +variable "subnet_availability_zone" { + description = "AWS availability zone" + type = string + default = "us-east-1a" +} + +variable "subnet_map_public_ip_on_launch" { + description = "Specify true to indicate that instances launched into the subnet should be assigned a public IP address." + type = bool + default = false +} diff --git a/fdio.infra.terraform/terraform-aws-vpc/versions.tf b/fdio.infra.terraform/terraform-aws-vpc/versions.tf new file mode 100644 index 0000000000..66b9c17f9b --- /dev/null +++ b/fdio.infra.terraform/terraform-aws-vpc/versions.tf @@ -0,0 +1,9 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 4.3.0" + } + } + required_version = ">= 1.1.4" +} -- 2.16.6