2
votes

I am working on a CDK app in which I have to create a VPC and EKS cluster. but I am not directly using the CDK to run the cloudformation. I want to create a cloudformation template separately and run it using AWS CLI. But whenever I am creating the cloudformation template EKS has asset parameters, which cause error for me while I run the template. How to avoid those parameters.

These are my files. bin/eks.ts

#!/usr/bin/env node
import cdk = require('@aws-cdk/core');
import { VPCStack, EKSStack } from '../lib/eks-stack';
import { Construct, TagManager, Tag } from '@aws-cdk/core';
import { Scope } from 'babel__traverse';

const app = new cdk.App();

const environment_variables = { region: "us-east-1", account: "348394859384543" }

const appVPCStack = new VPCStack(app, "appDemoVPCStack", { env: environment_variables })
Tag.add(appVPCStack, "owner", "tamizh");
Tag.add(appVPCStack, "purpose", "testing");

const appEKSStack = new EKSStack(app, "appDemoEKSStack", { env: environment_variables, vpcStack: appVPCStack })
Tag.add(appEKSStack, "owner", "tamizh");
Tag.add(appEKSStack, "purpose", "testing");

app.synth();

lib/eks.ts

import cdk = require('@aws-cdk/core');
import ec2 = require('@aws-cdk/aws-ec2');
import { DefaultInstanceTenancy, GatewayVpcEndpointAwsService, GatewayVpcEndpoint } from '@aws-cdk/aws-ec2';
import { ManagedPolicy } from '@aws-cdk/aws-iam';
import eks = require('@aws-cdk/aws-eks');
import  iam = require('@aws-cdk/aws-iam');
import asg = require("@aws-cdk/aws-autoscaling");
import { TagManager, TagType } from '@aws-cdk/core';

export class VPCStack extends cdk.Stack {
  public readonly vpc: ec2.Vpc;
  private endpoint: GatewayVpcEndpoint;
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    const eksClusterName = this.node.tryGetContext("eks.clusterName");
    this.vpc = new ec2.Vpc(this, eksClusterName+'VPC', {
      // VPC configurations
    })
  }
}

export interface EKSProps extends cdk.StackProps {
  vpcStack: VPCStack;
}

export class EKSStack extends cdk.Stack {

  constructor(scope: cdk.Construct, id: string, props: EKSProps) {
    super(scope, id, props);

    const vpc = props.vpcStack.vpc;

    // Context variables for dynamic configuration
    const current_env = this.node.tryGetContext("env.type");
    const eksClusterName = this.node.tryGetContext("eks.clusterName");
    const nodeGroupKeyName = this.node.tryGetContext("eks.nodeGroupKeyName");
    const nodeGroupMaxCapacity = this.node.tryGetContext("eks.nodeGroupMaxCapacity");
    const nodeGroupMinCapacity = this.node.tryGetContext("eks.nodeGroupMinCapacity");
    const nodeGroupDesiredCapacity = this.node.tryGetContext("eks.nodeGroupDesiredCapacity");
    const nodeGroupInstanceType = this.node.tryGetContext("eks.nodeGroupInstanceType");

    // Role to access the cluster from using kubeconfig
    // aws eks update-kubeconfig --name eks --region <region> --role-arn <role-arn>
    const clusterAdmin = new iam.Role(this, eksClusterName+'AdminRole', {
      assumedBy: new iam.AccountRootPrincipal()
    });

    // Cluster properties
    const clusterProps = {
      clusterName: current_env+eksClusterName,
      // Default capacity as 0 denotes infinite number of worker nodes
      // To avoid allocate the max number worker node while creating control plane
      defaultCapacity: 0,
      vpc: vpc,
      mastersRole: clusterAdmin
    }

    // Create a new EKS cluster control plane
    const cluster = new eks.Cluster(this, eksClusterName, clusterProps);

    const eksOptimizedImage = {
      //standard or GPU-optimized
      nodeType: eks.NodeType.STANDARD
    };
    const nodeGroupMachineImage = new eks.EksOptimizedImage(eksOptimizedImage);


    // defining autoscaling group for worker nodes which can be scalled up or down at any time
    const rcAsg = new asg.AutoScalingGroup(this, current_env+'ASG', {
      vpc: vpc,
      instanceType: nodeGroupInstanceType,
      machineImage: nodeGroupMachineImage,
      // Create a keypair to ssh into the worker nodes and give the keypair here 
      // It should be same account and region
      // keyName: nodeGroupKeyName,
      minCapacity: nodeGroupMinCapacity,
      maxCapacity: nodeGroupMaxCapacity,
      desiredCapacity: nodeGroupDesiredCapacity,
      updateType: asg.UpdateType.ROLLING_UPDATE,
      vpcSubnets: {subnetType: ec2.SubnetType.PRIVATE}
    });

    cluster.addAutoScalingGroup(rcAsg, {
      mapRole: true
    })
  }
}

Once I ran this, got output template as bellow.

{
  "Transform": "AWS::Serverless-2016-10-31",
  "Resources": {
    // all resources
  },
  "Parameters": {
    "AssetParametersea4957b1606259534983fnjdfs934r4b6ad19a204S3Bucket371D99F8": {
      "Type": "String",
      "Description": "S3 bucket for asset \"ea4957b1606m93439fmrefew99cc02944b6ad19a204\""
    },
    // more parameters.
  }
}

How to avoid these asset parameters?

1

1 Answers

0
votes

The answer is you can't avoid the asset parameters in CDK apps. Not every app has assets, whenever there is a functionality that cannot be done by plain cloudformation then they introduce these asset parameters which will be used during the CDK deploy time.

reference - https://github.com/aws/aws-cdk/issues/5403