Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add vpc with example #3

Merged
merged 6 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions .github/workflows/tflint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ jobs:
GITHUB_TOKEN: ${{ github.token }}

- name: Run TFLint
run: |
tflint -f compact --chdir .
tflint -f compact --chdir modules/cloudfront-app
tflint -f compact --chdir modules/cloudfront-deployment-policy
tflint -f compact --chdir modules/cloudfront-s3-origin-bucket-policy
run: tflint --recursive -f compact

- run: echo ${{ steps.tflint.outputs.stdout }}
- run: echo ${{ steps.tflint.outputs.stderr }}
Expand Down
44 changes: 44 additions & 0 deletions examples/vpc/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/vpc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 4.0 |

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_database_subnet"></a> [database\_subnet](#module\_database\_subnet) | ../../modules/vpc-private-subnet | n/a |
| <a name="module_private_subnet"></a> [private\_subnet](#module\_private\_subnet) | ../../modules/vpc-private-subnet | n/a |
| <a name="module_public_subnet"></a> [public\_subnet](#module\_public\_subnet) | ../../modules/vpc-public-subnet | n/a |
| <a name="module_vpc"></a> [vpc](#module\_vpc) | ../../modules/vpc | n/a |
<!-- END_TF_DOCS -->
71 changes: 71 additions & 0 deletions examples/vpc/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
locals {
context = {
namespace = "selleo"
stage = "test"
name = "networking"
}
}

module "vpc" {
source = "../../modules/vpc"

name = "test"
cidr = "10.0.0.0/16"

context = local.context
}

module "public_subnet" {
source = "../../modules/vpc-public-subnet"

context = local.context
vpc_id = module.vpc.id
internet_gateway_id = module.vpc.internet_gateway_id

config = {
"a" = {
az = "eu-central-1a"
cidr = "10.0.1.0/24"
nat = true
}
"b" = {
az = "eu-central-1b"
cidr = "10.0.2.0/24"
nat = false
}
}
}

module "private_subnet" {
source = "../../modules/vpc-private-subnet"

context = local.context
vpc_id = module.vpc.id

config = {
"a" = {
az = "eu-central-1a"
cidr = "10.0.51.0/24"
}
}

nat_gateway_routing = {
"a" = module.public_subnet.nat_gateways["eu-central-1a"]
}
}

module "database_subnet" {
source = "../../modules/vpc-private-subnet"

context = local.context
vpc_id = module.vpc.id

config = {
"db-a" = {
az = "eu-central-1a"
cidr = "10.0.101.0/24"
}
}

database_subnet = true
}
15 changes: 15 additions & 0 deletions examples/vpc/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_version = "~> 1.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
}
}

provider "aws" {
region = "eu-central-1"
}

6 changes: 3 additions & 3 deletions modules/ecs-service/versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ terraform {
}

random = {
source = "hashicorp/random"
version = "~> 3.0"
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
}
51 changes: 51 additions & 0 deletions modules/vpc-private-subnet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# subnet

## Usage

## Output

<!-- BEGIN_TF_DOCS -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | ~> 1.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 4.0 |
| <a name="requirement_random"></a> [random](#requirement\_random) | ~> 3.0 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | ~> 4.0 |
| <a name="provider_random"></a> [random](#provider\_random) | ~> 3.0 |

## Resources

| Name | Type |
|------|------|
| [aws_db_subnet_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_subnet_group) | resource |
| [aws_route.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route) | resource |
| [aws_route_table.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table) | resource |
| [aws_route_table_association.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table_association) | resource |
| [aws_subnet.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/subnet) | resource |
| [random_id.this](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_config"></a> [config](#input\_config) | Subnet configuration | <pre>map(object({<br> az = string<br> cidr = string<br> }))</pre> | n/a | yes |
| <a name="input_context"></a> [context](#input\_context) | Context | <pre>object({<br> namespace = string<br> stage = string<br> name = string<br> })</pre> | <pre>{<br> "name": "default",<br> "namespace": "default",<br> "stage": "default"<br>}</pre> | no |
| <a name="input_database_subnet"></a> [database\_subnet](#input\_database\_subnet) | Database subnet group | `bool` | `false` | no |
| <a name="input_nat_gateway_routing"></a> [nat\_gateway\_routing](#input\_nat\_gateway\_routing) | NAT Gateway routing | `map(string)` | `{}` | no |
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | VPC ID | `string` | n/a | yes |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_database_subnet_group_id"></a> [database\_subnet\_group\_id](#output\_database\_subnet\_group\_id) | Database subnet group ID |
| <a name="output_ids"></a> [ids](#output\_ids) | Subnet IDs sorted by key |
| <a name="output_subnets"></a> [subnets](#output\_subnets) | Subnet IDs by AZ |
<!-- END_TF_DOCS -->
66 changes: 66 additions & 0 deletions modules/vpc-private-subnet/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
locals {
tags = merge({
"terraform.module" = "Selleo/terraform-aws-vpc"
"terraform.submodule" = "private-subnet"
"context.namespace" = var.context.namespace
"context.stage" = var.context.stage
"context.name" = var.context.name
"resource.group" = "network"
})
}

resource "aws_subnet" "this" {
for_each = var.config

vpc_id = var.vpc_id
availability_zone = each.value.az
cidr_block = each.value.cidr
map_public_ip_on_launch = false

tags = merge(local.tags, {
Name = "private-${random_id.this.hex}-${each.key}--${each.value.az}"
})
}

resource "aws_route_table" "this" {
for_each = var.config

vpc_id = var.vpc_id

tags = merge(local.tags, {
Name = "private-${random_id.this.hex}-${each.key}--${each.value.az}"
})
}

resource "aws_route_table_association" "this" {
for_each = var.config

subnet_id = aws_subnet.this[each.key].id
route_table_id = aws_route_table.this[each.key].id
}

resource "aws_route" "this" {
for_each = var.nat_gateway_routing

route_table_id = aws_route_table.this[each.key].id
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = each.value

timeouts {
create = "5m"
}
}

resource "aws_db_subnet_group" "this" {
for_each = var.database_subnet ? { "db" : true } : {}

name = "db-private-${random_id.this.hex}"
description = "Database subnet group for RDS"
subnet_ids = [for key in sort(keys(var.config)) : aws_subnet.this[key].id]

tags = merge(local.tags, { "resource.group" = "network" })
}

resource "random_id" "this" {
byte_length = 2
}
18 changes: 18 additions & 0 deletions modules/vpc-private-subnet/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
output "subnets" {
value = {
for key, config in var.config :
config.az => aws_subnet.this[key].id
}
description = "Subnet IDs by AZ"
}

output "ids" {
# ensure consistent ordering
value = [for key in sort(keys(var.config)) : aws_subnet.this[key].id]
description = "Subnet IDs sorted by key"
}

output "database_subnet_group_id" {
value = try(aws_db_subnet_group.this["db"].id, null)
description = "Database subnet group ID"
}
38 changes: 38 additions & 0 deletions modules/vpc-private-subnet/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
variable "vpc_id" {
type = string
description = "VPC ID"
}

variable "config" {
type = map(object({
az = string
cidr = string
}))
description = "Subnet configuration"
}

variable "nat_gateway_routing" {
type = map(string)
description = "NAT Gateway routing"
default = {}
}

variable "context" {
type = object({
namespace = string
stage = string
name = string
})
description = "Context"
default = {
namespace = "default"
stage = "default"
name = "default"
}
}

variable "database_subnet" {
type = bool
description = "Database subnet group "
default = false
}
15 changes: 15 additions & 0 deletions modules/vpc-private-subnet/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_version = "~> 1.0"

required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}

random = {
source = "hashicorp/random"
version = "~> 3.0"
}
}
}
Loading