Skip to content

Commit 24cd76c

Browse files
author
BARRY Thierno Ibrahima (Canal Plus Prestataire)
committed
add example
1 parent c312c9a commit 24cd76c

File tree

6 files changed

+202
-3
lines changed

6 files changed

+202
-3
lines changed

examples/fargate/main.tf

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
terraform {
2+
required_version = ">= 0.12.6"
3+
}
4+
5+
provider "aws" {
6+
version = ">= 2.28.1"
7+
region = var.region
8+
}
9+
10+
provider "random" {
11+
version = "~> 2.1"
12+
}
13+
14+
provider "local" {
15+
version = "~> 1.2"
16+
}
17+
18+
provider "null" {
19+
version = "~> 2.1"
20+
}
21+
22+
provider "template" {
23+
version = "~> 2.1"
24+
}
25+
26+
data "aws_eks_cluster" "cluster" {
27+
name = module.eks.cluster_id
28+
}
29+
30+
data "aws_eks_cluster_auth" "cluster" {
31+
name = module.eks.cluster_id
32+
}
33+
34+
provider "kubernetes" {
35+
host = data.aws_eks_cluster.cluster.endpoint
36+
cluster_ca_certificate = base64decode(data.aws_eks_cluster.cluster.certificate_authority.0.data)
37+
token = data.aws_eks_cluster_auth.cluster.token
38+
load_config_file = false
39+
version = "~> 1.11"
40+
}
41+
42+
data "aws_availability_zones" "available" {
43+
}
44+
45+
locals {
46+
cluster_name = "test-eks-${random_string.suffix.result}"
47+
}
48+
49+
resource "random_string" "suffix" {
50+
length = 8
51+
special = false
52+
}
53+
54+
module "vpc" {
55+
source = "terraform-aws-modules/vpc/aws"
56+
version = "2.47.0"
57+
58+
name = "test-vpc"
59+
cidr = "172.16.0.0/16"
60+
azs = data.aws_availability_zones.available.names
61+
private_subnets = ["172.16.1.0/24", "172.16.2.0/24", "172.16.3.0/24"]
62+
public_subnets = ["172.16.4.0/24", "172.16.5.0/24", "172.16.6.0/24"]
63+
enable_nat_gateway = true
64+
single_nat_gateway = true
65+
enable_dns_hostnames = true
66+
67+
public_subnet_tags = {
68+
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
69+
"kubernetes.io/role/elb" = "1"
70+
}
71+
72+
private_subnet_tags = {
73+
"kubernetes.io/cluster/${local.cluster_name}" = "shared"
74+
"kubernetes.io/role/internal-elb" = "1"
75+
}
76+
}
77+
78+
module "eks" {
79+
source = "../.."
80+
cluster_name = local.cluster_name
81+
cluster_version = "1.17"
82+
subnets = module.vpc.private_subnets
83+
84+
tags = {
85+
Environment = "test"
86+
GithubRepo = "terraform-aws-eks"
87+
GithubOrg = "terraform-aws-modules"
88+
}
89+
90+
vpc_id = module.vpc.vpc_id
91+
92+
fargate_profiles = {
93+
example = {
94+
namespace = "default"
95+
96+
# Kubernetes labels for selection
97+
# labels = {
98+
# Environment = "test"
99+
# GithubRepo = "terraform-aws-eks"
100+
# GithubOrg = "terraform-aws-modules"
101+
# }
102+
103+
tags = {
104+
Owner = "test"
105+
}
106+
}
107+
}
108+
109+
map_roles = var.map_roles
110+
map_users = var.map_users
111+
map_accounts = var.map_accounts
112+
}

examples/fargate/outputs.tf

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
output "cluster_endpoint" {
2+
description = "Endpoint for EKS control plane."
3+
value = module.eks.cluster_endpoint
4+
}
5+
6+
output "cluster_security_group_id" {
7+
description = "Security group ids attached to the cluster control plane."
8+
value = module.eks.cluster_security_group_id
9+
}
10+
11+
output "kubectl_config" {
12+
description = "kubectl config as generated by the module."
13+
value = module.eks.kubeconfig
14+
}
15+
16+
output "config_map_aws_auth" {
17+
description = "A kubernetes configuration to authenticate to this EKS cluster."
18+
value = module.eks.config_map_aws_auth
19+
}
20+
21+
output "region" {
22+
description = "AWS region."
23+
value = var.region
24+
}
25+
26+
output "fargate_profile_arns" {
27+
description = "Outputs from node groups"
28+
value = module.eks.fargate_profile_arns
29+
}

examples/fargate/variables.tf

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
variable "region" {
2+
default = "us-west-2"
3+
}
4+
5+
variable "map_accounts" {
6+
description = "Additional AWS account numbers to add to the aws-auth configmap."
7+
type = list(string)
8+
9+
default = [
10+
"777777777777",
11+
"888888888888",
12+
]
13+
}
14+
15+
variable "map_roles" {
16+
description = "Additional IAM roles to add to the aws-auth configmap."
17+
type = list(object({
18+
rolearn = string
19+
username = string
20+
groups = list(string)
21+
}))
22+
23+
default = [
24+
{
25+
rolearn = "arn:aws:iam::66666666666:role/role1"
26+
username = "role1"
27+
groups = ["system:masters"]
28+
},
29+
]
30+
}
31+
32+
variable "map_users" {
33+
description = "Additional IAM users to add to the aws-auth configmap."
34+
type = list(object({
35+
userarn = string
36+
username = string
37+
groups = list(string)
38+
}))
39+
40+
default = [
41+
{
42+
userarn = "arn:aws:iam::66666666666:user/user1"
43+
username = "user1"
44+
groups = ["system:masters"]
45+
},
46+
{
47+
userarn = "arn:aws:iam::66666666666:user/user2"
48+
username = "user2"
49+
groups = ["system:masters"]
50+
},
51+
]
52+
}

modules/fargate/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Helper submodule to create and manage resources related to `aws_eks_fargate_prof
1313
| name | Fargate profile name | `string` | Auto generated in the following format `[cluster_name]-fargate-[fargate_profile_map_key]`| no |
1414
| namespace | Kubernetes namespace for selection | `string` | n/a | yes |
1515
| labels | Key-value map of Kubernetes labels for selection | `map(string)` | `{}` | no |
16+
| tags | Key-value map of resource tags. Will be merged with root module tags. | `map(string)` | `var.tags` | no |
1617

1718
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
1819
## Requirements

modules/fargate/fargate.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ resource "aws_iam_role_policy_attachment" "eks_fargate_pod" {
1313
}
1414

1515
resource "aws_eks_fargate_profile" "this" {
16-
for_each = local.create_eks ? var.fargate_profiles : {}
16+
for_each = local.create_eks ? local.fargate_profiles_expanded : {}
1717
cluster_name = var.cluster_name
1818
fargate_profile_name = lookup(each.value, "name", format("%s-fargate-%s", var.cluster_name, replace(each.key, "_", "-")))
1919
pod_execution_role_arn = local.pod_execution_role_arn
2020
subnet_ids = var.subnets
21-
tags = var.tags
21+
tags = each.value.tags
2222

2323
selector {
2424
namespace = each.value.namespace
25-
labels = each.value.labels
25+
labels = lookup(each.value, "labels", null)
2626
}
2727

2828
depends_on = [var.eks_depends_on]

modules/fargate/locals.tf

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ locals {
22
create_eks = var.create_eks && length(var.fargate_profiles) > 0
33
pod_execution_role_arn = var.create_fargate_pod_execution_role ? element(concat(aws_iam_role.eks_fargate_pod.*.arn, list("")), 0) : element(concat(data.aws_iam_role.custom_fargate_iam_role.*.arn, list("")), 0)
44
pod_execution_role_name = var.create_fargate_pod_execution_role ? element(concat(aws_iam_role.eks_fargate_pod.*.name, list("")), 0) : element(concat(data.aws_iam_role.custom_fargate_iam_role.*.name, list("")), 0)
5+
6+
fargate_profiles_expanded = { for k, v in var.fargate_profiles : k => merge(
7+
{ tags = var.tags },
8+
v,
9+
) if var.create_eks }
510
}

0 commit comments

Comments
 (0)