aws eks create using cloud formation

To create a EKS using cloud formation, we can use the following cloud formation code. This is not an EKS Auto

AWSTemplateFormatVersion: '2010-09-09'
Description: 'EKS Cluster and Managed Node Group'

Parameters:
  ClusterName:
    Type: String
    Default: 'my-eks-cluster'
  VpcId:
    Type: AWS::EC2::VPC::Id
  SubnetIds:
    Type: List<AWS::EC2::Subnet::Id>
    Description: 'Select at least two subnets in different AZs'

Resources:
  # 1. IAM Role for the EKS Cluster (Control Plane)
  EKSClusterRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: [eks.amazonaws.com]
            Action: ['sts:AssumeRole']
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonEKSClusterPolicy

  # 2. The EKS Cluster
  MyEKSCluster:
    Type: AWS::EKS::Cluster
    Properties:
      Name: !Ref ClusterName
      RoleArn: !GetAtt EKSClusterRole.Arn
      ResourcesVpcConfig:
        SubnetIds: !Ref SubnetIds

  # 3. IAM Role for Managed Node Group (Worker Nodes)
  NodeGroupRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
          - Effect: Allow
            Principal:
              Service: [ec2.amazonaws.com]
            Action: ['sts:AssumeRole']
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AmazonEKSWorkerNodePolicy
        - arn:aws:iam::aws:policy/AmazonEC2ContainerRegistryReadOnly
        - arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy

  # 4. Managed Node Group
  MyNodeGroup:
    Type: AWS::EKS::Nodegroup
    Properties:
      ClusterName: !Ref MyEKSCluster
      NodeRole: !GetAtt NodeGroupRole.Arn
      Subnets: !Ref SubnetIds
      ScalingConfig:
        MinSize: 1
        DesiredSize: 1
        MaxSize: 3
      InstanceTypes:
        - t3.medium

Outputs:
  ClusterName:
    Value: !Ref MyEKSCluster

The run the following command to create cloudformation stack:

aws cloudformation create-stack \
  --stack-name my-eks-stack \
  --template-body file://eks.yaml \
  --parameters '[
    {"ParameterKey":"VpcId","ParameterValue":"vpc-0be7302ef8e66a9d8"},
    {"ParameterKey":"SubnetIds","ParameterValue":"subnet-07da2bbe645a5ffe2,subnet-07ccef3e6bc1afc5f"}
  ]' \
  --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
  --region ap-southeast-2


Comments

Popular posts from this blog

vllm : Failed to infer device type

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

android studio kotlin source is null error