Skip to content

Instantly share code, notes, and snippets.

@pahud
Created May 20, 2020
Embed
What would you like to do?
create or import existing EKS cluster in AWS CDK
import cdk = require('@aws-cdk/core');
import eks = require('@aws-cdk/aws-eks');
import ec2 = require('@aws-cdk/aws-ec2');
import iam = require('@aws-cdk/aws-iam');
import { Stack } from '@aws-cdk/core';
const DEFAULT_CLUSTER_VERSION = '1.16'
export class EksStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// use an existing vpc or create a new one
const vpc = this.node.tryGetContext('use_default_vpc') === '1' ?
ec2.Vpc.fromLookup(this, 'Vpc', { isDefault: true }) :
this.node.tryGetContext('use_vpc_id') ?
ec2.Vpc.fromLookup(this, 'Vpc', { vpcId: this.node.tryGetContext('use_vpc_id') }) :
new ec2.Vpc(this, 'Vpc', { maxAzs: 3, natGateways: 1 });
const clusterVersion = this.node.tryGetContext('cluster_version') ?? DEFAULT_CLUSTER_VERSION
const mastersRole = new iam.Role(this, 'AdminRole', {
assumedBy: new iam.AccountRootPrincipal()
});
// this creates a new cluster
const cluster = new eks.Cluster(this, 'EKSCluster', {
vpc,
mastersRole,
version: clusterVersion,
});
// to import existing cluster, we need prepare the following info
const clusterName = this.node.tryGetContext('clusterName') ?? process.env.clusterName ?? undefined;
const clusterArn = this.node.tryGetContext('clusterArn') ?? process.env.clusterArn ?? undefined;
const clusterCertificateAuthorityData = this.node.tryGetContext('clusterCertificateAuthorityData') ?? process.env.clusterArn ?? undefined;
const clusterEndpoint = this.node.tryGetContext('clusterEndpoint') ?? process.env.clusterEndpoint ?? undefined;
// given your sg is common seperated string like this 'sg-xxxx,sg-xxx,sg-xxxx'
const securityGroupsString: string = this.node.tryGetContext('securityGroups') ?? process.env.securityGroups ?? undefined;
const securityGroups = securityGroupsString.split(',').map(sg => ec2.SecurityGroup.fromSecurityGroupId(this, `clusterSg${sg}`, sg) )
// this import an existing cluster
const existingCluster = eks.Cluster.fromClusterAttributes(this, 'ExistingCluster', {
clusterName,
clusterArn,
clusterCertificateAuthorityData,
clusterEndpoint,
securityGroups,
vpc,
})
new cdk.CfnOutput(this, 'Region', { value: Stack.of(this).region })
new cdk.CfnOutput(this, 'ClusterVersion', { value: clusterVersion })
}
}
@lazarofrancoe

This comment has been minimized.

Copy link

@lazarofrancoe lazarofrancoe commented Jun 1, 2021

So, there is no way to get an existing cluster just from the name?

@pradoz

This comment has been minimized.

Copy link

@pradoz pradoz commented Aug 8, 2021

So, there is no way to get an existing cluster just from the name?

You can, but you won't be able to access many properties. For example, if you don't give an iam.openIdConnectProvider then you will get an error for Cluster.addServiceAccount.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment