generated from opsd-io/terraform-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* vars * cleanup * roles * code * some data resources * add region output * add auth config vars * outputs * optionals * fargate * vars * aws-auth * tools * source_security_group_ids * workers * vars docs * encryption_key_arn * encryption_key_arn * node_group_name_prefix * cleanups
- Loading branch information
Showing
20 changed files
with
820 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,5 @@ terraform 1.5.5 | |
terraform-docs 0.16.0 | ||
tflint 0.47.0 | ||
pre-commit 3.3.3 | ||
kubectl 1.28.2 | ||
yq 4.34.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
locals { | ||
account_map = { | ||
current = data.aws_caller_identity.current.account_id | ||
} | ||
|
||
nodes_role_arns = concat(var.nodes_role_arns, [aws_iam_role.node.arn]) | ||
nodes_map = [for arn in local.nodes_role_arns : { | ||
rolearn = arn | ||
username = "system:node:{{EC2PrivateDNSName}}" | ||
groups = [ | ||
"system:bootstrappers", | ||
"system:nodes", | ||
] | ||
}] | ||
|
||
fargate_role_arns = concat(var.fargate_role_arns, [aws_iam_role.fargate.arn]) | ||
fargate_map = [for arn in local.fargate_role_arns : { | ||
rolearn = arn | ||
username = "system:node:{{SessionName}}" | ||
groups = [ | ||
"system:bootstrappers", | ||
"system:nodes", | ||
"system:node-proxier", | ||
] | ||
}] | ||
|
||
masters_map = [for arn in var.masters_role_arns : { | ||
rolearn = arn | ||
username = "master:{{AccountID}}:{{SessionName}}" | ||
groups = [ | ||
"system:masters", | ||
] | ||
}] | ||
} | ||
|
||
resource "kubernetes_config_map" "aws_auth" { | ||
metadata { | ||
name = "aws-auth" | ||
namespace = "kube-system" | ||
} | ||
data = { | ||
mapAccounts = yamlencode([ | ||
for account_id in var.auth_map_accounts : lookup(local.account_map, account_id, account_id) | ||
]) | ||
mapRoles = yamlencode(flatten([ | ||
local.nodes_map, | ||
local.fargate_map, | ||
local.masters_map, [ | ||
for role in var.auth_map_roles : { | ||
rolearn = role.arn | ||
username = role.username | ||
groups = role.groups | ||
} | ||
] | ||
])) | ||
mapUsers = yamlencode([ | ||
for user in var.auth_map_users : { | ||
userarn = user.arn | ||
username = user.username | ||
groups = user.groups | ||
} | ||
]) | ||
} | ||
|
||
depends_on = [ | ||
aws_eks_cluster.main, | ||
] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
locals { | ||
fixed_names = true | ||
} | ||
|
||
resource "aws_eks_node_group" "main" { | ||
for_each = var.node_groups | ||
|
||
node_group_name = local.fixed_names ? each.key : null | ||
node_group_name_prefix = local.fixed_names ? null : each.key | ||
cluster_name = aws_eks_cluster.main.name | ||
node_role_arn = aws_iam_role.node.arn | ||
subnet_ids = coalesce(each.value.subnet_ids, var.node_group_subnet_ids) | ||
tags = merge(var.common_tags, { | ||
Name = each.key | ||
}) | ||
|
||
scaling_config { | ||
min_size = each.value.min_size | ||
max_size = each.value.max_size | ||
desired_size = each.value.desired_size | ||
} | ||
|
||
update_config { | ||
max_unavailable = null | ||
max_unavailable_percentage = 10 | ||
} | ||
|
||
ami_type = each.value.ami_type | ||
capacity_type = each.value.capacity_type | ||
disk_size = each.value.disk_size | ||
instance_types = [each.value.instance_type] | ||
labels = each.value.labels | ||
|
||
# launch_template { | ||
# id = each.value.launch_template_id | ||
# name = each.value.launch_template_name | ||
# version = each.value.launch_template_version | ||
# } | ||
|
||
dynamic "remote_access" { | ||
for_each = var.ec2_ssh_key != null ? [1] : [] | ||
content { | ||
ec2_ssh_key = var.ec2_ssh_key | ||
source_security_group_ids = var.source_security_group_ids | ||
} | ||
} | ||
|
||
dynamic "taint" { | ||
for_each = each.value.taints | ||
content { | ||
key = taint.value.key | ||
value = taint.value.value | ||
effect = taint.value.effect | ||
} | ||
} | ||
|
||
lifecycle { | ||
# only if node_group_name_prefix is in use, but (local.fixed_names == false) does not work.. | ||
create_before_destroy = true | ||
ignore_changes = [ | ||
scaling_config[0].desired_size, # needed for autoscaling to work | ||
] | ||
} | ||
|
||
depends_on = [ | ||
aws_eks_cluster.main, | ||
aws_iam_role.node, | ||
aws_iam_role_policy_attachment.eks_worker_node_policy, | ||
kubernetes_config_map.aws_auth, | ||
] | ||
|
||
} | ||
|
||
|
||
resource "aws_eks_fargate_profile" "main" { | ||
for_each = var.fargate_profiles | ||
|
||
fargate_profile_name = each.key | ||
cluster_name = aws_eks_cluster.main.name | ||
pod_execution_role_arn = aws_iam_role.fargate.arn | ||
subnet_ids = coalesce(each.value.subnet_ids, var.fargate_subnet_ids) | ||
tags = merge(var.common_tags, { | ||
Name = each.key | ||
}) | ||
|
||
selector { | ||
namespace = each.value.namespace | ||
labels = each.value.labels | ||
} | ||
|
||
depends_on = [ | ||
aws_eks_cluster.main, | ||
aws_iam_role.fargate, | ||
aws_iam_role_policy_attachment.eks_fargate_pod_execution_role_policy, | ||
kubernetes_config_map.aws_auth, | ||
] | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module "network" { | ||
source = "github.com/opsd-io/terraform-module-aws-network" | ||
|
||
vpc_name = "k8s-test-vpc" | ||
cidr_block = "10.100.0.0/16" | ||
|
||
public_subnet_groups = { | ||
"public1" = { | ||
availability_zones = { | ||
"a" = { cidr_block = "10.100.1.0/24", nat_gateway = true } | ||
"b" = { cidr_block = "10.100.2.0/24" } | ||
"c" = { cidr_block = "10.100.3.0/24" } | ||
} | ||
} | ||
} | ||
private_subnet_groups = { | ||
"nodes1" = { | ||
nat_group_name = "public1" | ||
availability_zones = { | ||
"a" = { cidr_block = "10.100.101.0/24" } | ||
"b" = { cidr_block = "10.100.102.0/24" } | ||
"c" = { cidr_block = "10.100.103.0/24" } | ||
} | ||
} | ||
"fargate1" = { | ||
nat_group_name = "public1" | ||
availability_zones = { | ||
"a" = { cidr_block = "10.100.201.0/24" } | ||
"b" = { cidr_block = "10.100.202.0/24" } | ||
"c" = { cidr_block = "10.100.203.0/24" } | ||
} | ||
} | ||
} | ||
} | ||
|
||
module "kubernetes" { | ||
source = "github.com/opsd-io/terraform-module-aws-kubernetes" | ||
name = "basic-k8s-example" | ||
|
||
subnet_ids = [ | ||
for subnet in module.network.public_subnet_groups["public1"] : subnet.id | ||
] | ||
|
||
node_group_subnet_ids = [ | ||
for subnet in module.network.private_subnet_groups["nodes1"] : subnet.id | ||
] | ||
node_groups = { | ||
main = { | ||
max_size = 9 | ||
desired_size = 1 | ||
disk_size = 8 | ||
} | ||
} | ||
|
||
fargate_subnet_ids = [ | ||
for subnet in module.network.private_subnet_groups["fargate1"] : subnet.id | ||
] | ||
fargate_profiles = { | ||
"default-namespace" = { # default is bad, better put it "far" | ||
namespace = "default" | ||
} | ||
"amazonaws-components" = { # this should put CoreDNS on fargate | ||
namespace = "*" | ||
labels = { "eks.amazonaws.com/component" = "*" } | ||
} | ||
} | ||
} | ||
|
||
module "autoscaler" { | ||
source = "github.com/opsd-io/terraform-module-eks-autoscaler" | ||
cluster_region = module.kubernetes.region | ||
cluster_name = module.kubernetes.name | ||
openid_arn = module.kubernetes.openid_arn | ||
openid_sub = module.kubernetes.openid_sub | ||
} |
Oops, something went wrong.