Skip to content

Commit

Permalink
feat(eks): default vpc (#3632)
Browse files Browse the repository at this point in the history
When defining an EKS cluster, the `vpc` property is now
optional. If not specified, a VPC will be defined with
the default configuration for this environment.

It can then be accessed through `cluster.vpc`.

Fixes #3541
  • Loading branch information
Elad Ben-Israel authored and mergify[bot] committed Aug 13, 2019
1 parent 6953e03 commit 3a96c27
Show file tree
Hide file tree
Showing 5 changed files with 948 additions and 9 deletions.
4 changes: 1 addition & 3 deletions packages/@aws-cdk/aws-eks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ manifests within EKS clusters.
This example defines an Amazon EKS cluster with a single pod:

```ts
const cluster = new eks.Cluster(this, 'hello-eks', { vpc });
const cluster = new eks.Cluster(this, 'hello-eks');

cluster.addCapacity('default', {
instanceType: new ec2.InstanceType('t2.medium'),
Expand Down Expand Up @@ -74,7 +74,6 @@ const clusterAdmin = new iam.Role(this, 'AdminRole', {

// now define the cluster and map role to "masters" RBAC group
new eks.Cluster(this, 'Cluster', {
vpc: vpc,
mastersRole: clusterAdmin
});
```
Expand Down Expand Up @@ -242,7 +241,6 @@ the cluster:

```ts
new eks.Cluster(this, 'cluster', {
vpc: vpc,
kubectlEnabled: false
});
```
Expand Down
12 changes: 7 additions & 5 deletions packages/@aws-cdk/aws-eks/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ export interface ClusterAttributes {
export interface ClusterProps {
/**
* The VPC in which to create the Cluster
*
* @default - a VPC with default configuration will be created and can be accessed through `cluster.vpc`.
*/
readonly vpc: ec2.IVpc;
readonly vpc?: ec2.IVpc;

/**
* Where to place EKS Control Plane ENIs
Expand Down Expand Up @@ -269,12 +271,12 @@ export class Cluster extends Resource implements ICluster {
* @param name the name of the Construct to create
* @param props properties in the IClusterProps interface
*/
constructor(scope: Construct, id: string, props: ClusterProps) {
constructor(scope: Construct, id: string, props: ClusterProps = { }) {
super(scope, id, {
physicalName: props.clusterName,
});

this.vpc = props.vpc;
this.vpc = props.vpc || new ec2.Vpc(this, 'DefaultVpc');
this.version = props.version;

this.tagSubnets();
Expand All @@ -288,7 +290,7 @@ export class Cluster extends Resource implements ICluster {
});

const securityGroup = props.securityGroup || new ec2.SecurityGroup(this, 'ControlPlaneSecurityGroup', {
vpc: props.vpc,
vpc: this.vpc,
description: 'EKS Control Plane Security Group',
});

Expand All @@ -299,7 +301,7 @@ export class Cluster extends Resource implements ICluster {

// Get subnetIds for all selected subnets
const placements = props.vpcSubnets || [{ subnetType: ec2.SubnetType.PUBLIC }, { subnetType: ec2.SubnetType.PRIVATE }];
const subnetIds = [...new Set(Array().concat(...placements.map(s => props.vpc.selectSubnets(s).subnetIds)))];
const subnetIds = [...new Set(Array().concat(...placements.map(s => this.vpc.selectSubnets(s).subnetIds)))];

const clusterProps: CfnClusterProps = {
name: this.physicalName,
Expand Down

0 comments on commit 3a96c27

Please sign in to comment.