-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DRAFT: Split eks creation to own module (#9)
* Push EKS creation to it's own module
- Loading branch information
1 parent
215af13
commit 2da8e1c
Showing
9 changed files
with
317 additions
and
22 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
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
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,38 @@ | ||
data "aws_eks_cluster" "cluster" { | ||
depends_on = [module.eks.cluster_id] | ||
name = module.eks.cluster_name | ||
} | ||
|
||
data "aws_eks_cluster_auth" "cluster" { | ||
depends_on = [module.eks.cluster_id] | ||
name = module.eks.cluster_name | ||
} | ||
|
||
data "aws_vpc" "selected" { | ||
filter { | ||
name = "tag:Name" | ||
values = ["spacelift-created-vpc"] | ||
} | ||
} | ||
|
||
data "aws_subnets" "private" { | ||
filter { | ||
name = "vpc-id" | ||
values = [data.aws_vpc.selected.id] | ||
} | ||
|
||
filter { | ||
name = "tag:Name" | ||
values = ["private"] | ||
} | ||
} | ||
|
||
data "aws_security_group" "vpc" { | ||
vpc_id = data.aws_vpc.selected.id | ||
|
||
filter { | ||
name = "tag:Name" | ||
values = ["spacelift-created-vpc-default"] | ||
} | ||
|
||
} |
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,126 @@ | ||
resource "aws_iam_role" "admin_role" { | ||
name = "eks_admin_role_${var.cluster_name}" | ||
|
||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Effect = "Allow" | ||
Principal = { | ||
AWS = "arn:aws:iam::766808016710:root" # Replace YOUR_AWS_ACCOUNT_ID with your actual AWS account ID | ||
} | ||
Action = "sts:AssumeRole" | ||
}, | ||
] | ||
}) | ||
|
||
tags = var.tags | ||
} | ||
|
||
resource "aws_iam_role" "viewer_role" { | ||
name = "eks_viewer_role_${var.cluster_name}" | ||
|
||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Effect = "Allow" | ||
Principal = { | ||
AWS = "arn:aws:sts::766808016710:assumed-role/AWSReservedSSO_Developer_92af2c086e7e7f38/bryan.fauble@sagebase.org" | ||
} | ||
Action = "sts:AssumeRole" | ||
}, | ||
] | ||
}) | ||
|
||
tags = var.tags | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "admin_policy" { | ||
role = aws_iam_role.admin_role.name | ||
policy_arn = "arn:aws:iam::aws:policy/PowerUserAccess" | ||
} | ||
|
||
module "eks" { | ||
source = "terraform-aws-modules/eks/aws" | ||
version = "~> 20.12" | ||
|
||
cluster_name = var.cluster_name | ||
cluster_version = var.cluster_version | ||
|
||
cluster_endpoint_public_access = true | ||
|
||
cluster_addons = { | ||
# coredns = { | ||
# most_recent = true | ||
# } | ||
kube-proxy = { | ||
most_recent = true | ||
} | ||
vpc-cni = { | ||
most_recent = true | ||
} | ||
# TODO When the cluster is created we need to set the gp2 storageclass as default: | ||
# kubectl patch storageclass gp2 -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}' | ||
# This way any PVC that is created will use gp2 as the default storage class | ||
# aws-ebs-csi-driver = { | ||
# most_recent = true | ||
# } | ||
} | ||
# TODO: The AWS EBS CSI driver is not working right for some reason. PVC are made, but storage is not being allocated. Determine why | ||
vpc_id = data.aws_vpc.selected.id | ||
subnet_ids = data.aws_subnets.private.ids | ||
# TODO | ||
# control_plane_subnet_ids = data.vpc.intra_subnets module.vpc.intra_subnets | ||
cluster_security_group_id = data.aws_security_group.vpc.id | ||
|
||
iam_role_additional_policies = { | ||
AmazonEBSCSIDriverPolicy = "arn:aws:iam::aws:policy/service-role/AmazonEBSCSIDriverPolicy", | ||
SecretsManagerReadWrite = "arn:aws:iam::aws:policy/SecretsManagerReadWrite" | ||
WorkerNodePolicy = "arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy" | ||
} | ||
|
||
# Cluster access entry | ||
# To add the current caller identity as an administrator | ||
enable_cluster_creator_admin_permissions = true | ||
authentication_mode = "API" | ||
|
||
|
||
access_entries = { | ||
# One access entry with a policy associated | ||
eks_admin_role = { | ||
kubernetes_groups = [] | ||
principal_arn = aws_iam_role.admin_role.arn | ||
|
||
policy_associations = { | ||
eks_admin_role = { | ||
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSClusterAdminPolicy" | ||
access_scope = { | ||
type = "cluster" | ||
} | ||
} | ||
} | ||
} | ||
eks_viewer_role = { | ||
kubernetes_groups = [] | ||
principal_arn = aws_iam_role.viewer_role.arn | ||
|
||
policy_associations = { | ||
eks_admin_role = { | ||
policy_arn = "arn:aws:eks::aws:cluster-access-policy/AmazonEKSViewPolicy" | ||
access_scope = { | ||
type = "cluster" | ||
} | ||
} | ||
} | ||
} | ||
# https://docs.aws.amazon.com/eks/latest/userguide/access-policies.html#access-policy-permissions | ||
# TODO: Additional roles that need to be created: | ||
# AmazonEKSAdminViewPolicy? | ||
# AmazonEKSEditPolicy | ||
# AmazonEKSViewPolicy | ||
|
||
} | ||
tags = var.tags | ||
} | ||
|
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,3 @@ | ||
provider "aws" { | ||
region = var.region | ||
} |
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,25 @@ | ||
variable "cluster_name" { | ||
description = "Name of K8 cluster" | ||
type = string | ||
default = "dpe-k8-dev" | ||
} | ||
|
||
variable "cluster_version" { | ||
description = "Version of K8 cluster" | ||
type = string | ||
default = "1.30" | ||
} | ||
|
||
variable "region" { | ||
description = "AWS region" | ||
type = string | ||
default = "us-east-1" | ||
} | ||
|
||
variable "tags" { | ||
description = "AWS Resource Tags" | ||
type = map(string) | ||
default = { | ||
"CostCenter" = "No Program / 000000" | ||
} | ||
} |
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,3 @@ | ||
terraform { | ||
required_version = "<= 1.5.7" | ||
} |