| |
| provider "aws_eksctl" { |
| credentials = file("~/.aws/credentials") |
| } |
|
|
| variable "vpcId" {} |
|
|
| module "eks-cluster" { |
| source = "./modules/eks.tfvars" |
| source = "./modules/eks" |
| vpcId = var.vpc_id |
| data "aws_eksctl" { |
| source = "./modules/eks-cluster" |
| vpc_id = var.vpc_id |
| data 'aws_iam_policy' "eks-cluster"{ |
| name = "${var.name}-${local.environment}" |
| policy = file("./modules/eks-cluster") // Path of your EKS Cluster module, which should have variables like this: provider { vpc_id = var.vpc_id; } resource "aws_iam_role" "eksctl-IAMRoleForEKSCluster"{ |
| name = "${var.name}-${local.environment}" |
| policy { |
| statement { |
| effect = "Allow", |
| principals = ["arn:aws:iam::aws:eks:eu-west-1:987654321054:role/${var.name}-clusterRole"] |
| actions { |
| type = "sts:GetCallerIdentity" |
| effect = "Allow", |
| resources = ["*"], |
| sideloaded_resources = true, |
| principals = [aws_iam_user.eks-cluster] |
| } |
| ] |
| policy { |
| effect = "Allow", |
| actions = ["sts:GetCallerIdentity"] |
| resources = [aws_iam_role.eks-cluster] |
| sideloaded_resources = true, |
| } |
| ]} resource "aws_kms_key" { |
| name = "${var.name}-ebs-encryption-key", |
| policy = jsonencode({ |
| key_id = aws_kms_alias.eksctl-clusterRole["arn"] |
| description = "Kubernetes Service Account Key" , |
| kms_key_id = var.ebs_encryption_key, |
| }), |
| provider = aws_kms { key_arn = data.aws_kms_alias.eksctl-clusterRoleKey["arn"] |
| policy = jsonencode({ |
| name = "${var.name}-ebs-encryption-key", |
| description = "Kubernetes Service Account Key" , |
| key_id = var.eksctl-clusterRoleKey, |
| }), |
| arn = aws_kms_alias.eksctl-clusterRoleKey["arn"], |
| tags = { |
| environment = var.environment, |
| environment = var.environment, |
| }}) resource "aws_eksctl" { |
| depends_on = [ aws_kms_alias.eksctl-clusterRoleKey ] |
| provider = aws_iam_instance_profile.eksctl-master ["arn:aws:iam::aws:policy/AmazonEKSManagedPolicy"] |
| name = "${var.name}-eksctl" |
| node_group = var.node_groups[0] // this variable should contain a list of nodes to be created in each availability zone, e.g., ["us-east-1a", "us-east-2b"] |
| subnet_ids = [var.subnets[0], var.subnets[1]] // this variable should contain a list of private IP addresses in each availability zone, e.g., ["us-east-2a", "us-east-2b"] |
| vpc_id = aws_eksctl_cluster.eksctl[0].vpc_id // this variable should contain a list of private IP addresses in each availability zone, e.g., ["us-east-1a", "us-east-2b"] |
| node_type = var.node_types[0]//this can be 't3.medium' or t3.large', // this variable should contain a list of private IP addresses in each availability zone, e.g., ["us-east-1a", "us-east-2b"] # This is the name for your EKS cluster |
| node_count = var.node_count[0]//this can be '3' or more depending on how many nodes you want to create in each availability zone, e.g., [1, 4], // this variable should contain a list of private IP addresses in the same subnets as your EKS cluster |
| node_size = var.node_type[0]//this can be 't3.medium' or t3.large', |
| } |
| resource "aws_eksctl" { |
| depends_on = [ aws_kms_key.eks-clusterRoleKey ] |
| name = "${var.name}-eksctl", // this variable should contain the EKS cluster's name, e.g., 'myEksCluster', # This is for setting up a Kubernetes service account key pair and IAM role with permissions to create/delete resources in your VPC |
| version = "1.24"//this can be any valid Terraform provider module that you have created, e.g., 'aws_eksctl' or aws_kms_key', |
| node_groups { |
| availability_zones = ["us-east-1a", "us-east-2b"] //this variable should contain a list of private IP addresses in each subnet, e.g., [aws_subnets[0], aws_subnets[1]] |
| instance_type = var.node_types[0]// this can be 't3.medium' or t3.large', //this variable should contain a list of private IP addresses in each subnet, e.g., ["us-east-1a", "us-east end2b"] # This is the name for your EKS cluster |
| node_count = var.node_count[0]// this can be '3' or more depending on how many nodes you want to create in each availability zone, e.g., [1,4], //this variable should contain a list of private IP addresses # This is the name for your EKS cluster |
| subnets = ["subnet-05a987236c", "subnet-bcd"]// this can be 't3.medium' or t3.large', //this variable should contain a list of private IP addresses in each availability zone, e.g., [aws_eksctl_cluster[0].vpc_id] |
| vpc_security_group = aws_kms_key.eks-clusterRoleKey["arn"]// this is the name for your EKS cluster's security group (if you have created one) or can be generated using terraform provider modules like: https://github.com/terraform-providers/terraform-provider-aws |
| } # This will create a Kubernetes service account key pair and IAM role with permissions to access the EKS cluster resources, e.g., 'eksctl' or aws_kms_key', // this variable should contain your VPC ID (if you have created one) var.vpcId |
| } |
| provider = "aws" |
| region = "us-east-1" |
| subnets = ["subnet-05a987236c", "subnet-bcd"] |
| node_groups { |
| availability_zones = ["us-east-1a", "us-east-2b"]//this can be any valid Terraform provider module, e.g., 'aws_eksctl' or aws_kms_key', // this variable should contain a list of private IP addresses in each subnet |
| instance_type = var.node_types[0]// this can be 't3.medium' or t3.large', |
| node_count = var.node_count[0]// this can be 3 or more depending on how many nodes you want to create for your EKS cluster, e.g., [1], |
| subnets = ["subnet-05a987236c", "subnet-bcd"]// this can be 't3.medium' or t3.large', # This is for setting up an IAM policy that allows Kubernetes service account to access the EKS cluster resources, e.g., 'eksctl' or aws_kms_key', //this variable should contain a list of private IP addresses in each subnet |
| vpc_id = var.vpcId// this is for setting up an IAM policy that allows Kubernetes service account to access the EKS cluster resources, e.g., 'eksctl' or aws_kms_key', //this variable should contain your VPC ID (if you have created one) |
| } # This will create a private subnet for each availability zone in which this node group is located and attach it to the EKS cluster, e.g., 'eksctl' or aws_kms_key', //this variable should contain your VPC ID (if you have created one) var.vpcId |
| } |
| provider = "aws" |
| tags = { |
| environment = "dev"// This is for setting up an IAM policy that allows Kubernetes service account to access the EKS cluster resources, e.g., 'eksctl' or aws_kms_key', // this variable should contain your VPC ID (if you have created one) var.vpcId |
| } # This will create a private subnet for each availability zone in which this node group is located and attach it to the EKS cluster, e.g., 'eksctl' or aws_kms_key', //this variable should contain your VPC ID (if you have created one) var.vpcId |
| } |
| provider = "aws" |
| tags = { |
| environment = "dev"// This is for setting up an IAM policy that allows Kubernetes service account to access the EKS cluster resources, e.g., 'eksctl' or aws_kms_key', // this variable should contain your VPC ID (if you have created one) var.vpcId |
| } # This will create a private subnet for each availability zone in which this node group is located and attach it to the EKS cluster, e.g., 'eksctl' or aws_kms_key', //this variable should contain your VPC ID (if you have created one) var.vpcId |
| } |
| provider = "aws" |
| tags = { |
| environment = "dev"// This is for setting up an IAM policy that allows Kubernetes service account to access the EKS cluster resources, e.g., 'eksctl' or aws_kms_key', // this variable should contain your VPC ID (if you have created one) var.vpcId |
| } # This will create a private subnet for each availability zone in which this node group is located and attach it to the EKS cluster, e.g., 'eksctl' or aws_kms_key', //this variable should contain your VPC ID (if you have created one) var.vpcId |
| } |
| } |
| } |
| |
|
|
| |
| Create a comprehensive IAM role for an EKS cluster using the provided document as inspiration but do not use any direct code examples or templates from it. The policy should grant permissions to manage Kubernetes clusters, including creating and updating them with specific tags (e.g., `eksctl` command-line tool), managing pods within a specified IAM role (`arn:aws:iam::123456789012:role/my_cluster`), handling secrets for Kubernetes, configuring the cluster to use specific AWS services (Amazon SageMaker and Amazon EKS), managing security groups with a focus on enabling HTTPS traffic within an IAM role. The policy must also ensure that it adheres strictly to least privilege principles by granting only necessary permissions for these tasks, avoids any unintended access outside of the `dev-infra` VPC and includes conditions wherein actions are restricted based on tags (e.g., tagging resources with 'env:prod' or 'test'). The policy should also be compliant with AWS best practices to minimize security risks, such as avoiding overly broad permissions like `*`, using specific ARNs for S3 buckets and Amazon EKS clusters where possible, ensuring that the IAM role is not too permissive. |
|
|
| |