Skip to content

Commit

Permalink
feat(eks): retrieve ami with ssm (#4156)
Browse files Browse the repository at this point in the history
* feat(eks): retrieve ami with ssm

* package-lock changes

* chore: remove deprecated, delete ami.ts, move into cluster

* chore: refactor EksOptimizedAmiProps -> EksOptimizedImageProps

* chore: add missing - to JSDoc

* chore: rename NodeType.NORMAL -> STANDARD

* remove note about environment-agnostic stacks in README

* chore: revert package-lock
  • Loading branch information
Jimmy Gaussen authored and mergify[bot] committed Sep 23, 2019
1 parent b3973d0 commit 622a4e1
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 197 deletions.
5 changes: 0 additions & 5 deletions packages/@aws-cdk/aws-eks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ cluster.addResource('mypod', {
});
```

**NOTE**: in order to determine the default AMI for for Amazon EKS instances the
`eks.Cluster` resource must be defined within a stack that is configured with an
explicit `env.region`. See [Environments](https://docs.aws.amazon.com/cdk/latest/guide/environments.html)
in the AWS CDK Developer Guide for more details.

Here is a [complete sample](https://github.com/aws/aws-cdk/blob/master/packages/%40aws-cdk/aws-eks/test/integ.eks-kubectl.lit.ts).

### Capacity
Expand Down
183 changes: 0 additions & 183 deletions packages/@aws-cdk/aws-eks/lib/ami.ts

This file was deleted.

79 changes: 77 additions & 2 deletions packages/@aws-cdk/aws-eks/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import ec2 = require('@aws-cdk/aws-ec2');
import { Subnet } from '@aws-cdk/aws-ec2';
import iam = require('@aws-cdk/aws-iam');
import lambda = require('@aws-cdk/aws-lambda');
import ssm = require('@aws-cdk/aws-ssm');
import { CfnOutput, Construct, Duration, IResource, Resource, Stack, Tag } from '@aws-cdk/core';
import path = require('path');
import { EksOptimizedAmi, nodeTypeForInstanceType } from './ami';
import { AwsAuth } from './aws-auth';
import { ClusterResource } from './cluster-resource';
import { CfnCluster, CfnClusterProps } from './eks.generated';
Expand Down Expand Up @@ -449,7 +449,7 @@ export class Cluster extends Resource implements ICluster {
const asg = new autoscaling.AutoScalingGroup(this, id, {
...options,
vpc: this.vpc,
machineImage: new EksOptimizedAmi({
machineImage: new EksOptimizedImage({
nodeType: nodeTypeForInstanceType(options.instanceType),
kubernetesVersion: this.version,
}),
Expand Down Expand Up @@ -769,3 +769,78 @@ class ImportedCluster extends Resource implements ICluster {
}
}
}

/**
* Properties for EksOptimizedImage
*/
export interface EksOptimizedImageProps {
/**
* What instance type to retrieve the image for (standard or GPU-optimized)
*
* @default NodeType.STANDARD
*/
readonly nodeType?: NodeType;

/**
* The Kubernetes version to use
*
* @default - The latest version
*/
readonly kubernetesVersion?: string;
}

/**
* Construct an Amazon Linux 2 image from the latest EKS Optimized AMI published in SSM
*/
export class EksOptimizedImage implements ec2.IMachineImage {
private readonly nodeType?: NodeType;
private readonly kubernetesVersion?: string;

private readonly amiParameterName: string;

/**
* Constructs a new instance of the EcsOptimizedAmi class.
*/
public constructor(props: EksOptimizedImageProps) {
this.nodeType = props && props.nodeType;
this.kubernetesVersion = props && props.kubernetesVersion || LATEST_KUBERNETES_VERSION;

// set the SSM parameter name
this.amiParameterName = `/aws/service/eks/optimized-ami/${this.kubernetesVersion}/`
+ ( this.nodeType === NodeType.STANDARD ? "amazon-linux-2/" : "" )
+ ( this.nodeType === NodeType.GPU ? " amazon-linux2-gpu/" : "" )
+ "recommended/image_id";
}

/**
* Return the correct image
*/
public getImage(scope: Construct): ec2.MachineImageConfig {
const ami = ssm.StringParameter.valueForStringParameter(scope, this.amiParameterName);
return {
imageId: ami,
osType: ec2.OperatingSystemType.LINUX
};
}
}

const LATEST_KUBERNETES_VERSION = '1.13';

/**
* Whether the worker nodes should support GPU or just standard instances
*/
export enum NodeType {
/**
* Standard instances
*/
STANDARD = 'Standard',

/**
* GPU instances
*/
GPU = 'GPU',
}

export function nodeTypeForInstanceType(instanceType: ec2.InstanceType) {
return instanceType.toString().startsWith('p2') || instanceType.toString().startsWith('p3') ? NodeType.GPU : NodeType.STANDARD;
}
1 change: 0 additions & 1 deletion packages/@aws-cdk/aws-eks/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './cluster';
export * from './ami';
export * from './aws-auth-mapping';
export * from './k8s-resource';
export * from './aws-auth';
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-eks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"@aws-cdk/aws-ec2": "^1.9.0",
"@aws-cdk/aws-iam": "^1.9.0",
"@aws-cdk/aws-lambda": "^1.9.0",
"@aws-cdk/aws-ssm": "^1.9.0",
"@aws-cdk/core": "^1.9.0"
},
"homepage": "https://github.com/aws/aws-cdk",
Expand All @@ -84,6 +85,7 @@
"@aws-cdk/aws-ec2": "^1.9.0",
"@aws-cdk/aws-iam": "^1.9.0",
"@aws-cdk/aws-lambda": "^1.9.0",
"@aws-cdk/aws-ssm": "^1.8.0",
"@aws-cdk/core": "^1.9.0"
},
"engines": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,9 @@
"ClusterDefaultCapacityLaunchConfig72790CF7": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-12345",
"ImageId": {
"Ref": "SsmParameterValueawsserviceeksoptimizedami113amazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "m5.large",
"IamInstanceProfile": {
"Ref": "ClusterDefaultCapacityInstanceProfile70387741"
Expand Down Expand Up @@ -1207,6 +1209,10 @@
"ClusterKubernetesResourceHandlerCodeArtifactHash27D4393F": {
"Type": "String",
"Description": "Artifact hash for asset \"eks-integ-defaults/Cluster/KubernetesResourceHandler/Code\""
},
"SsmParameterValueawsserviceeksoptimizedami113amazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/eks/optimized-ami/1.13/amazon-linux-2/recommended/image_id"
}
},
"Outputs": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,9 @@
"EKSClusterNodesLaunchConfig921F1106": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-12345",
"ImageId": {
"Ref": "SsmParameterValueawsserviceeksoptimizedami113amazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t2.medium",
"IamInstanceProfile": {
"Ref": "EKSClusterNodesInstanceProfile0F2DB3B9"
Expand Down Expand Up @@ -929,6 +931,12 @@
}
}
},
"Parameters": {
"SsmParameterValueawsserviceeksoptimizedami113amazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/eks/optimized-ami/1.13/amazon-linux-2/recommended/image_id"
}
},
"Outputs": {
"EKSClusterConfigCommand3809C9C9": {
"Value": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,9 @@
"EKSClusterNodesLaunchConfig921F1106": {
"Type": "AWS::AutoScaling::LaunchConfiguration",
"Properties": {
"ImageId": "ami-12345",
"ImageId": {
"Ref": "SsmParameterValueawsserviceeksoptimizedami113amazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter"
},
"InstanceType": "t2.medium",
"IamInstanceProfile": {
"Ref": "EKSClusterNodesInstanceProfile0F2DB3B9"
Expand Down Expand Up @@ -1207,6 +1209,10 @@
"EKSClusterKubernetesResourceHandlerCodeArtifactHashA13FF2C6": {
"Type": "String",
"Description": "Artifact hash for asset \"eks-integ-test-basic/EKSCluster/KubernetesResourceHandler/Code\""
},
"SsmParameterValueawsserviceeksoptimizedami113amazonlinux2recommendedimageidC96584B6F00A464EAD1953AFF4B05118Parameter": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/aws/service/eks/optimized-ami/1.13/amazon-linux-2/recommended/image_id"
}
},
"Outputs": {
Expand Down
Loading

0 comments on commit 622a4e1

Please sign in to comment.