From 2b02693d1432ce7bce5bf0a78fe4a9175dd6ad91 Mon Sep 17 00:00:00 2001 From: andrestone Date: Sat, 7 Mar 2020 11:59:02 +0100 Subject: [PATCH] feat(batch): ec2 launch template support (#6602) Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> --- packages/@aws-cdk/aws-batch/README.md | 35 +++++++++++++++++++ .../aws-batch/lib/compute-environment.ts | 26 ++++++++++++++ .../aws-batch/test/integ.batch.expected.json | 21 +++++++++++ .../@aws-cdk/aws-batch/test/integ.batch.ts | 19 ++++++++++ 4 files changed, 101 insertions(+) diff --git a/packages/@aws-cdk/aws-batch/README.md b/packages/@aws-cdk/aws-batch/README.md index aab1e7afc99c5..0945b8bea050c 100644 --- a/packages/@aws-cdk/aws-batch/README.md +++ b/packages/@aws-cdk/aws-batch/README.md @@ -18,3 +18,38 @@ This module is part of the [AWS Cloud Development Kit](https://github.com/aws/aws-cdk) project. + +## Launch template support + +### Usage +Simply define your Launch Template: +```typescript + const myLaunchTemplate = new ec2.CfnLaunchTemplate(this, 'LaunchTemplate', { + launchTemplateName: 'extra-storage-template', + launchTemplateData: { + blockDeviceMappings: [ + { + deviceName: '/dev/xvdcz', + ebs: { + encrypted: true, + volumeSize: 100, + volumeType: 'gp2' + } + } + ] + } + }); +``` +and use it: + +```typescript + const myComputeEnv = new batch.ComputeEnvironment(this, 'ComputeEnv', { + computeResources: { + launchTemplate: { + launchTemplateName: myLaunchTemplate.launchTemplateName as string, //or simply use an existing template name + }, + vpc, + }, + computeEnvironmentName: 'MyStorageCapableComputeEnvironment', + }); +``` diff --git a/packages/@aws-cdk/aws-batch/lib/compute-environment.ts b/packages/@aws-cdk/aws-batch/lib/compute-environment.ts index 39054245acb5c..49d4810d64f2e 100644 --- a/packages/@aws-cdk/aws-batch/lib/compute-environment.ts +++ b/packages/@aws-cdk/aws-batch/lib/compute-environment.ts @@ -46,6 +46,22 @@ export enum AllocationStrategy { SPOT_CAPACITY_OPTIMIZED = 'SPOT_CAPACITY_OPTIMIZED', } +/** + * Launch template property specification + */ +export interface LaunchTemplateSpecification { + /** + * The Launch template name + */ + readonly launchTemplateName: string; + /** + * The launch template version to be used (optional). + * + * @default - the default version of the launch template + */ + readonly version?: string; +} + /** * Properties for defining the structure of the batch compute cluster. */ @@ -79,6 +95,15 @@ export interface ComputeResources { */ readonly instanceRole?: string; + /** + * An optional launch template to associate with your compute resources. + * For more information, see README file. + * + * @default - no custom launch template will be used + * @link https://docs.aws.amazon.com/batch/latest/userguide/launch-templates.html + */ + readonly launchTemplate?: LaunchTemplateSpecification; + /** * The types of EC2 instances that may be launched in the compute environment. You can specify instance * families to launch any instance type within those families (for example, c4 or p3), or you can specify @@ -333,6 +358,7 @@ export class ComputeEnvironment extends Resource implements IComputeEnvironment }).roleName] }).attrArn, instanceTypes: this.buildInstanceTypes(props.computeResources.instanceTypes), + launchTemplate: props.computeResources.launchTemplate, maxvCpus: props.computeResources.maxvCpus || 256, minvCpus: props.computeResources.minvCpus || 0, securityGroupIds: this.buildSecurityGroupIds(props.computeResources.vpc, props.computeResources.securityGroups), diff --git a/packages/@aws-cdk/aws-batch/test/integ.batch.expected.json b/packages/@aws-cdk/aws-batch/test/integ.batch.expected.json index 936888ce29321..6519a5c34bb36 100644 --- a/packages/@aws-cdk/aws-batch/test/integ.batch.expected.json +++ b/packages/@aws-cdk/aws-batch/test/integ.batch.expected.json @@ -514,6 +514,24 @@ } } }, + "ec2launchtemplate": { + "Type": "AWS::EC2::LaunchTemplate", + "Properties": { + "LaunchTemplateData": { + "BlockDeviceMappings": [ + { + "DeviceName": "/dev/xvdcz", + "Ebs": { + "Encrypted": true, + "VolumeSize": 100, + "VolumeType": "gp2" + } + } + ] + }, + "LaunchTemplateName": "EC2LaunchTemplate" + } + }, "batchunmanagedcomputeenvResourceServiceInstanceRoleCA40AF77": { "Type": "AWS::IAM::Role", "Properties": { @@ -817,6 +835,9 @@ "InstanceTypes": [ "optimal" ], + "LaunchTemplate": { + "LaunchTemplateName": "EC2LaunchTemplate" + }, "MaxvCpus": 256, "MinvCpus": 0, "SecurityGroupIds": [ diff --git a/packages/@aws-cdk/aws-batch/test/integ.batch.ts b/packages/@aws-cdk/aws-batch/test/integ.batch.ts index 174e0bd5e5ba2..37c95d925cbec 100644 --- a/packages/@aws-cdk/aws-batch/test/integ.batch.ts +++ b/packages/@aws-cdk/aws-batch/test/integ.batch.ts @@ -10,6 +10,22 @@ const stack = new cdk.Stack(app, 'batch-stack'); const vpc = new ec2.Vpc(stack, 'vpc'); +const launchTemplate = new ec2.CfnLaunchTemplate(stack, 'ec2-launch-template', { + launchTemplateName: 'EC2LaunchTemplate', + launchTemplateData: { + blockDeviceMappings: [ + { + deviceName: '/dev/xvdcz', + ebs: { + encrypted: true, + volumeSize: 100, + volumeType: 'gp2' + } + } + ] + } +}); + new batch.JobQueue(stack, 'batch-job-queue', { computeEnvironments: [ { @@ -24,6 +40,9 @@ new batch.JobQueue(stack, 'batch-job-queue', { computeResources: { type: batch.ComputeResourceType.ON_DEMAND, vpc, + launchTemplate: { + launchTemplateName: launchTemplate.launchTemplateName as string, + }, }, }), order: 2,