Skip to content

Commit d6ef173

Browse files
Feature/allow to deploy in existing vpc (#12)
* Added creating eks in existing VPC * added public_subnet variable * fixed outputs * terraform fmt * terraform-docs: automated action --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent f66c16e commit d6ef173

File tree

5 files changed

+48
-13
lines changed

5 files changed

+48
-13
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ No requirements.
1818

1919
| Name | Version |
2020
|------|---------|
21-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.48.0 |
22-
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | 2.16.1 |
21+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.55.0 |
22+
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | 2.18.1 |
2323

2424
## Modules
2525

@@ -37,12 +37,14 @@ No requirements.
3737
| [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source |
3838
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
3939
| [aws_partition.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/partition) | data source |
40+
| [aws_vpc.selected](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc) | data source |
4041

4142
## Inputs
4243

4344
| Name | Description | Type | Default | Required |
4445
|------|-------------|------|---------|:--------:|
4546
| <a name="input_additional_tags"></a> [additional\_tags](#input\_additional\_tags) | Additional tags to include | `map(string)` | `{}` | no |
47+
| <a name="input_create_vpc"></a> [create\_vpc](#input\_create\_vpc) | Specifies if new VPC be created, if not `vpc_id` and `subnet_ids` variables need to be provided | `bool` | `true` | no |
4648
| <a name="input_eks_additional_cluster_addons"></a> [eks\_additional\_cluster\_addons](#input\_eks\_additional\_cluster\_addons) | Map of additional cluster addon configurations to enable for the cluster. | `any` | `{}` | no |
4749
| <a name="input_eks_cluster_auth_role"></a> [eks\_cluster\_auth\_role](#input\_eks\_cluster\_auth\_role) | AWS roles with access permission to EKS cluster | <pre>list(object({<br> rolearn : string<br> username : string<br> groups = list(string)<br> }))</pre> | `[]` | no |
4850
| <a name="input_eks_cluster_auth_user"></a> [eks\_cluster\_auth\_user](#input\_eks\_cluster\_auth\_user) | AWS users with access permission to EKS cluster | <pre>list(object({<br> userarn : string<br> username : string<br> groups = list(string)<br> }))</pre> | `[]` | no |
@@ -61,8 +63,11 @@ No requirements.
6163
| <a name="input_environment"></a> [environment](#input\_environment) | Environment name | `string` | n/a | yes |
6264
| <a name="input_logs_retention_days"></a> [logs\_retention\_days](#input\_logs\_retention\_days) | Log retention in days | `number` | `14` | no |
6365
| <a name="input_org"></a> [org](#input\_org) | Organization name - part of other resource names | `string` | `"terraform"` | no |
66+
| <a name="input_private_subnet_ids"></a> [private\_subnet\_ids](#input\_private\_subnet\_ids) | List of IDs of existing private subnets, only used when `create_vpc` is set to `false` | `list(string)` | `[]` | no |
67+
| <a name="input_public_subnet_ids"></a> [public\_subnet\_ids](#input\_public\_subnet\_ids) | List of IDs of existing public subnets, only used when `create_vpc` is set to `false` | `list(string)` | `[]` | no |
6468
| <a name="input_region"></a> [region](#input\_region) | n/a | `string` | `"eu-central-1"` | no |
6569
| <a name="input_vpc_cidr"></a> [vpc\_cidr](#input\_vpc\_cidr) | VPC CIDR address | `string` | `"10.0.0.0/16"` | no |
70+
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | ID of existing VPC, only used when `create_vpc` is set to `false` | `string` | `""` | no |
6671
| <a name="input_vpc_nat_setting"></a> [vpc\_nat\_setting](#input\_vpc\_nat\_setting) | Enable NAT Gateway | <pre>object({<br> enable_nat_gateway : bool<br> multi_az_nat_gateway : bool<br> })</pre> | <pre>{<br> "enable_nat_gateway": true,<br> "multi_az_nat_gateway": false<br>}</pre> | no |
6772

6873
## Outputs

locals.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ locals {
88
azs_count = length(local.azs_names)
99
public_subnet_cidrs = [for step in range(local.azs_count) : cidrsubnet(var.vpc_cidr, 5, step)]
1010
private_subnet_cidrs = [for step in range(local.azs_count) : cidrsubnet(var.vpc_cidr, 5, step + local.azs_count)]
11-
eks_managed_node_group_defaults = var.eks_single_az ? merge(var.eks_cluster_node_groups_default_configuration, { subnet_ids = slice(module.vpc.private_subnets, 0, 1) }) : merge(var.eks_cluster_node_groups_default_configuration, { subnet_ids = module.vpc.private_subnets })
11+
private_subnets = var.create_vpc ? module.vpc[0].private_subnets : var.private_subnet_ids
12+
eks_managed_node_group_defaults = var.eks_single_az ? merge(var.eks_cluster_node_groups_default_configuration, { subnet_ids = slice(local.private_subnets, 0, 1) }) : merge(var.eks_cluster_node_groups_default_configuration, { subnet_ids = local.private_subnets })
1213

1314
tags = merge({
1415
environment = var.environment

main.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module "vpc" {
2+
count = var.create_vpc ? 1 : 0
23
source = "terraform-aws-modules/vpc/aws"
34
version = "~> 3.0"
45

@@ -30,6 +31,11 @@ module "vpc" {
3031
tags = local.tags
3132
}
3233

34+
data "aws_vpc" "selected" {
35+
count = var.create_vpc ? 0 : 1
36+
id = var.vpc_id
37+
}
38+
3339
module "kubernetes_secrets_encryption_key" {
3440
source = "./modules/encryption"
3541
org = var.org
@@ -45,8 +51,8 @@ module "eks" {
4551
cluster_name = var.eks_cluster_name
4652
cluster_version = var.eks_cluster_version
4753

48-
vpc_id = module.vpc.vpc_id
49-
subnet_ids = concat(module.vpc.public_subnets, module.vpc.private_subnets)
54+
vpc_id = var.create_vpc ? module.vpc[0].vpc_id : var.vpc_id
55+
subnet_ids = var.create_vpc ? concat(module.vpc[0].public_subnets, module.vpc[0].private_subnets) : concat(var.public_subnet_ids, var.private_subnet_ids)
5056

5157
cluster_endpoint_private_access = var.eks_cluster_endpoint_access.enable_private_access
5258
cluster_endpoint_public_access = var.eks_cluster_endpoint_access.enable_public_access

outputs.tf

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,37 @@
33
############################
44

55
output "vpc_name" {
6-
value = module.vpc.name
6+
value = try(module.vpc.name, null)
77
description = "The name of the VPC"
88
}
99

1010
output "vpc_id" {
11-
value = module.vpc.vpc_id
11+
value = try(module.vpc.vpc_id, null)
1212
description = "The VPC ID"
1313
}
1414

1515
output "vpc_public_subnets_ids" {
16-
value = module.vpc.public_subnets
17-
16+
value = try(module.vpc.public_subnets, null)
1817
description = "The list of public subnets IDs associated with the VPC"
1918
}
2019

2120
output "vpc_private_subnets_ids" {
22-
value = module.vpc.private_subnets
21+
value = try(module.vpc.private_subnets, null)
2322
description = "The list of private subnets IDs associated with the VPC"
2423
}
2524

2625
output "vpc_nats_ids" {
27-
value = module.vpc.nat_ids
26+
value = try(module.vpc.nat_ids, null)
2827
description = "The list of allocation ID for Elastic IPs"
2928
}
3029

3130
output "vpc_public_route_table_ids" {
32-
value = module.vpc.public_route_table_ids
31+
value = try(module.vpc.public_route_table_ids, null)
3332
description = "The list of IDs of public route tables"
3433
}
3534

3635
output "vpc_private_route_table_ids" {
37-
value = module.vpc.private_route_table_ids
36+
value = try(module.vpc.private_route_table_ids, null)
3837
description = "The list of IDs of private route tables"
3938
}
4039

variables.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,27 @@ variable "eks_single_az" {
185185
type = bool
186186
default = false
187187
}
188+
189+
variable "create_vpc" {
190+
description = "Specifies if new VPC be created, if not `vpc_id` and `subnet_ids` variables need to be provided"
191+
type = bool
192+
default = true
193+
}
194+
195+
variable "vpc_id" {
196+
description = "ID of existing VPC, only used when `create_vpc` is set to `false`"
197+
type = string
198+
default = ""
199+
}
200+
201+
variable "private_subnet_ids" {
202+
description = "List of IDs of existing private subnets, only used when `create_vpc` is set to `false`"
203+
type = list(string)
204+
default = []
205+
}
206+
207+
variable "public_subnet_ids" {
208+
description = "List of IDs of existing public subnets, only used when `create_vpc` is set to `false`"
209+
type = list(string)
210+
default = []
211+
}

0 commit comments

Comments
 (0)