Skip to content

Commit

Permalink
feat(batch): add launchTemplateId in LaunchTemplateSpecification (#20184
Browse files Browse the repository at this point in the history
)

Closes #20163

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
robertd authored Jul 7, 2022
1 parent e6015a9 commit 269b8d0
Show file tree
Hide file tree
Showing 5 changed files with 368 additions and 6 deletions.
22 changes: 19 additions & 3 deletions packages/@aws-cdk/aws-batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ The alternative would be to use the `BEST_FIT_PROGRESSIVE` strategy in order for

Simply define your Launch Template:

```text
// This example is only available in TypeScript
```ts
const myLaunchTemplate = new ec2.CfnLaunchTemplate(this, 'LaunchTemplate', {
launchTemplateName: 'extra-storage-template',
launchTemplateData: {
Expand All @@ -138,7 +137,7 @@ const myLaunchTemplate = new ec2.CfnLaunchTemplate(this, 'LaunchTemplate', {
});
```

and use it:
And provide `launchTemplateName`:

```ts
declare const vpc: ec2.Vpc;
Expand All @@ -155,6 +154,23 @@ const myComputeEnv = new batch.ComputeEnvironment(this, 'ComputeEnv', {
});
```

Or provide `launchTemplateId` instead:

```ts
declare const vpc: ec2.Vpc;
declare const myLaunchTemplate: ec2.CfnLaunchTemplate;

const myComputeEnv = new batch.ComputeEnvironment(this, 'ComputeEnv', {
computeResources: {
launchTemplate: {
launchTemplateId: myLaunchTemplate.ref as string,
},
vpc,
},
computeEnvironmentName: 'MyStorageCapableComputeEnvironment',
});
```

### Importing an existing Compute Environment

To import an existing batch compute environment, call `ComputeEnvironment.fromComputeEnvironmentArn()`.
Expand Down
25 changes: 23 additions & 2 deletions packages/@aws-cdk/aws-batch/lib/compute-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,17 @@ export enum AllocationStrategy {
*/
export interface LaunchTemplateSpecification {
/**
* The Launch template name
* The Launch template ID. Mutually exclusive with `launchTemplateName`.
*
* @default - no launch template id provided
*/
readonly launchTemplateName: string;
readonly launchTemplateId?: string;
/**
* The Launch template name. Mutually exclusive with `launchTemplateId`
*
* @default - no launch template name provided
*/
readonly launchTemplateName?: string;
/**
* The launch template version to be used (optional).
*
Expand Down Expand Up @@ -518,6 +526,19 @@ export class ComputeEnvironment extends Resource implements IComputeEnvironment
throw new Error('Minimum vCpus cannot be greater than the maximum vCpus');
}
}

// Check if both launchTemplateId and launchTemplateName are provided
if (props.computeResources.launchTemplate &&
(props.computeResources.launchTemplate.launchTemplateId && props.computeResources.launchTemplate.launchTemplateName)) {
throw new Error('You must specify either the launch template ID or launch template name in the request, but not both.');
}

// Check if both launchTemplateId and launchTemplateName are missing
if (props.computeResources.launchTemplate &&
(!props.computeResources.launchTemplate.launchTemplateId && !props.computeResources.launchTemplate.launchTemplateName)) {
throw new Error('You must specify either the launch template ID or launch template name in the request.');
}

// Setting a bid percentage is only allowed on SPOT resources +
// Cannot use SPOT_CAPACITY_OPTIMIZED when using ON_DEMAND
if (props.computeResources.type === ComputeResourceType.ON_DEMAND) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,281 @@
"vpcVPCGW7984C166"
]
},
"batchdemandcomputeenvlaunchtemplate2ResourceSecurityGroupBEA8DDD5": {
"Type": "AWS::EC2::SecurityGroup",
"Properties": {
"GroupDescription": "batch-stack/batch-demand-compute-env-launch-template-2/Resource-Security-Group",
"SecurityGroupEgress": [
{
"CidrIp": "0.0.0.0/0",
"Description": "Allow all outbound traffic by default",
"IpProtocol": "-1"
}
],
"VpcId": {
"Ref": "vpcA2121C38"
}
},
"DependsOn": [
"vpcIGWE57CBDCA",
"vpcPrivateSubnet1DefaultRoute1AA8E2E5",
"vpcPrivateSubnet1RouteTableB41A48CC",
"vpcPrivateSubnet1RouteTableAssociation67945127",
"vpcPrivateSubnet1Subnet934893E8",
"vpcPrivateSubnet2DefaultRouteB0E07F99",
"vpcPrivateSubnet2RouteTable7280F23E",
"vpcPrivateSubnet2RouteTableAssociation007E94D3",
"vpcPrivateSubnet2Subnet7031C2BA",
"vpcPublicSubnet1DefaultRoute10708846",
"vpcPublicSubnet1EIPDA49DCBE",
"vpcPublicSubnet1NATGateway9C16659E",
"vpcPublicSubnet1RouteTable48A2DF9B",
"vpcPublicSubnet1RouteTableAssociation5D3F4579",
"vpcPublicSubnet1Subnet2E65531E",
"vpcPublicSubnet2DefaultRouteA1EC0F60",
"vpcPublicSubnet2EIP9B3743B1",
"vpcPublicSubnet2NATGateway9B8AE11A",
"vpcPublicSubnet2RouteTableEB40D4CB",
"vpcPublicSubnet2RouteTableAssociation21F81B59",
"vpcPublicSubnet2Subnet009B674F",
"vpcA2121C38",
"vpcVPCGW7984C166"
]
},
"batchdemandcomputeenvlaunchtemplate2EcsInstanceRoleEE146754": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": {
"Fn::Join": [
"",
[
"ec2.",
{
"Ref": "AWS::URLSuffix"
}
]
]
}
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AmazonEC2ContainerServiceforEC2Role"
]
]
}
]
},
"DependsOn": [
"vpcIGWE57CBDCA",
"vpcPrivateSubnet1DefaultRoute1AA8E2E5",
"vpcPrivateSubnet1RouteTableB41A48CC",
"vpcPrivateSubnet1RouteTableAssociation67945127",
"vpcPrivateSubnet1Subnet934893E8",
"vpcPrivateSubnet2DefaultRouteB0E07F99",
"vpcPrivateSubnet2RouteTable7280F23E",
"vpcPrivateSubnet2RouteTableAssociation007E94D3",
"vpcPrivateSubnet2Subnet7031C2BA",
"vpcPublicSubnet1DefaultRoute10708846",
"vpcPublicSubnet1EIPDA49DCBE",
"vpcPublicSubnet1NATGateway9C16659E",
"vpcPublicSubnet1RouteTable48A2DF9B",
"vpcPublicSubnet1RouteTableAssociation5D3F4579",
"vpcPublicSubnet1Subnet2E65531E",
"vpcPublicSubnet2DefaultRouteA1EC0F60",
"vpcPublicSubnet2EIP9B3743B1",
"vpcPublicSubnet2NATGateway9B8AE11A",
"vpcPublicSubnet2RouteTableEB40D4CB",
"vpcPublicSubnet2RouteTableAssociation21F81B59",
"vpcPublicSubnet2Subnet009B674F",
"vpcA2121C38",
"vpcVPCGW7984C166"
]
},
"batchdemandcomputeenvlaunchtemplate2InstanceProfileC5A36CBC": {
"Type": "AWS::IAM::InstanceProfile",
"Properties": {
"Roles": [
{
"Ref": "batchdemandcomputeenvlaunchtemplate2EcsInstanceRoleEE146754"
}
]
},
"DependsOn": [
"vpcIGWE57CBDCA",
"vpcPrivateSubnet1DefaultRoute1AA8E2E5",
"vpcPrivateSubnet1RouteTableB41A48CC",
"vpcPrivateSubnet1RouteTableAssociation67945127",
"vpcPrivateSubnet1Subnet934893E8",
"vpcPrivateSubnet2DefaultRouteB0E07F99",
"vpcPrivateSubnet2RouteTable7280F23E",
"vpcPrivateSubnet2RouteTableAssociation007E94D3",
"vpcPrivateSubnet2Subnet7031C2BA",
"vpcPublicSubnet1DefaultRoute10708846",
"vpcPublicSubnet1EIPDA49DCBE",
"vpcPublicSubnet1NATGateway9C16659E",
"vpcPublicSubnet1RouteTable48A2DF9B",
"vpcPublicSubnet1RouteTableAssociation5D3F4579",
"vpcPublicSubnet1Subnet2E65531E",
"vpcPublicSubnet2DefaultRouteA1EC0F60",
"vpcPublicSubnet2EIP9B3743B1",
"vpcPublicSubnet2NATGateway9B8AE11A",
"vpcPublicSubnet2RouteTableEB40D4CB",
"vpcPublicSubnet2RouteTableAssociation21F81B59",
"vpcPublicSubnet2Subnet009B674F",
"vpcA2121C38",
"vpcVPCGW7984C166"
]
},
"batchdemandcomputeenvlaunchtemplate2ResourceServiceInstanceRole41CADAC1": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Statement": [
{
"Action": "sts:AssumeRole",
"Effect": "Allow",
"Principal": {
"Service": "batch.amazonaws.com"
}
}
],
"Version": "2012-10-17"
},
"ManagedPolicyArns": [
{
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition"
},
":iam::aws:policy/service-role/AWSBatchServiceRole"
]
]
}
]
},
"DependsOn": [
"vpcIGWE57CBDCA",
"vpcPrivateSubnet1DefaultRoute1AA8E2E5",
"vpcPrivateSubnet1RouteTableB41A48CC",
"vpcPrivateSubnet1RouteTableAssociation67945127",
"vpcPrivateSubnet1Subnet934893E8",
"vpcPrivateSubnet2DefaultRouteB0E07F99",
"vpcPrivateSubnet2RouteTable7280F23E",
"vpcPrivateSubnet2RouteTableAssociation007E94D3",
"vpcPrivateSubnet2Subnet7031C2BA",
"vpcPublicSubnet1DefaultRoute10708846",
"vpcPublicSubnet1EIPDA49DCBE",
"vpcPublicSubnet1NATGateway9C16659E",
"vpcPublicSubnet1RouteTable48A2DF9B",
"vpcPublicSubnet1RouteTableAssociation5D3F4579",
"vpcPublicSubnet1Subnet2E65531E",
"vpcPublicSubnet2DefaultRouteA1EC0F60",
"vpcPublicSubnet2EIP9B3743B1",
"vpcPublicSubnet2NATGateway9B8AE11A",
"vpcPublicSubnet2RouteTableEB40D4CB",
"vpcPublicSubnet2RouteTableAssociation21F81B59",
"vpcPublicSubnet2Subnet009B674F",
"vpcA2121C38",
"vpcVPCGW7984C166"
]
},
"batchdemandcomputeenvlaunchtemplate2E12D5CBC": {
"Type": "AWS::Batch::ComputeEnvironment",
"Properties": {
"Type": "MANAGED",
"ComputeResources": {
"AllocationStrategy": "BEST_FIT",
"InstanceRole": {
"Fn::GetAtt": [
"batchdemandcomputeenvlaunchtemplate2InstanceProfileC5A36CBC",
"Arn"
]
},
"InstanceTypes": [
"optimal"
],
"LaunchTemplate": {
"LaunchTemplateId": {
"Ref": "ec2launchtemplate"
}
},
"MaxvCpus": 256,
"MinvCpus": 0,
"SecurityGroupIds": [
{
"Fn::GetAtt": [
"batchdemandcomputeenvlaunchtemplate2ResourceSecurityGroupBEA8DDD5",
"GroupId"
]
}
],
"Subnets": [
{
"Ref": "vpcPrivateSubnet1Subnet934893E8"
},
{
"Ref": "vpcPrivateSubnet2Subnet7031C2BA"
}
],
"Tags": {
"compute-env-tag": "123XYZ"
},
"Type": "EC2"
},
"ServiceRole": {
"Fn::GetAtt": [
"batchdemandcomputeenvlaunchtemplate2ResourceServiceInstanceRole41CADAC1",
"Arn"
]
},
"State": "ENABLED"
},
"DependsOn": [
"vpcIGWE57CBDCA",
"vpcPrivateSubnet1DefaultRoute1AA8E2E5",
"vpcPrivateSubnet1RouteTableB41A48CC",
"vpcPrivateSubnet1RouteTableAssociation67945127",
"vpcPrivateSubnet1Subnet934893E8",
"vpcPrivateSubnet2DefaultRouteB0E07F99",
"vpcPrivateSubnet2RouteTable7280F23E",
"vpcPrivateSubnet2RouteTableAssociation007E94D3",
"vpcPrivateSubnet2Subnet7031C2BA",
"vpcPublicSubnet1DefaultRoute10708846",
"vpcPublicSubnet1EIPDA49DCBE",
"vpcPublicSubnet1NATGateway9C16659E",
"vpcPublicSubnet1RouteTable48A2DF9B",
"vpcPublicSubnet1RouteTableAssociation5D3F4579",
"vpcPublicSubnet1Subnet2E65531E",
"vpcPublicSubnet2DefaultRouteA1EC0F60",
"vpcPublicSubnet2EIP9B3743B1",
"vpcPublicSubnet2NATGateway9B8AE11A",
"vpcPublicSubnet2RouteTableEB40D4CB",
"vpcPublicSubnet2RouteTableAssociation21F81B59",
"vpcPublicSubnet2Subnet009B674F",
"vpcA2121C38",
"vpcVPCGW7984C166"
]
},
"batchjobqueueE3C528F2": {
"Type": "AWS::Batch::JobQueue",
"Properties": {
Expand All @@ -1023,6 +1298,12 @@
"Ref": "batchspotcomputeenv2CE4DFD9"
},
"Order": 3
},
{
"ComputeEnvironment": {
"Ref": "batchdemandcomputeenvlaunchtemplate2E12D5CBC"
},
"Order": 4
}
],
"Priority": 1,
Expand Down
Loading

0 comments on commit 269b8d0

Please sign in to comment.