Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(aws-batch): Support omitting ComputeEnvironment security groups so that they can be specified in Launch Template #21579

Merged
merged 23 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/@aws-cdk/aws-batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,42 @@ const myComputeEnv = new batch.ComputeEnvironment(this, 'ComputeEnv', {
});
```

Note that if your launch template explicitly specifies network interfaces,
for example to use an Elastic Fabric Adapter, you must explicitly tell CDK not to
auto-create security groups in the `ComputeEnvironment` construct. Instead, you must
define them in the Launch Template. For example:

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

const efaSecurityGroup = new ec2.SecurityGroup(this, 'EFASecurityGroup', {
vpc,
});

const launchTemplateEFA = new ec2.CfnLaunchTemplate(this, 'LaunchTemplate', {
launchTemplateName: 'LaunchTemplateName',
launchTemplateData: {
networkInterfaces: [{
deviceIndex: 0,
subnetId: vpc.privateSubnets[0].subnetId,
interfaceType: 'efa',
groups: [efaSecurityGroup.securityGroupId],
}],
},
});

const computeEnvironmentEFA = new batch.ComputeEnvironment(this, 'EFAComputeEnv', {
managed: true,
computeResources: {
securityGroups: batch.ComputeEnvironmentSecurityGroups.NONE,
vpc,
launchTemplate: {
launchTemplateName: launchTemplateEFA.launchTemplateName as string,
},
},
});
```

### Importing an existing Compute Environment

To import an existing batch compute environment, call `ComputeEnvironment.fromComputeEnvironmentArn()`.
Expand Down
33 changes: 29 additions & 4 deletions packages/@aws-cdk/aws-batch/lib/compute-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@ export enum ComputeResourceType {
*/
FARGATE_SPOT = 'FARGATE_SPOT',
}
/**
* Flag to determine whether or not the ComputeEnvironment should
* autocreate Security Groups.
*/
export enum ComputeEnvironmentSecurityGroups {
/**
* The Compute Environment will not create any security groups
*
* This is needed if you are instead assigning security groups
* to network interfaces defined in a launch template
*/
NONE = 'NONE',
/**
* The Compute Environment will create security groups if none
* are explicity specified
* to network interfaces defined in a launch template
*/
AUTOMATIC = 'AUTOMATIC',
}

/**
* Properties for how to prepare compute resources
Expand Down Expand Up @@ -142,7 +161,7 @@ export interface ComputeResources {
*
* @default - AWS default security group.
*/
readonly securityGroups?: ec2.ISecurityGroup[];
readonly securityGroups?: ec2.ISecurityGroup[] | ComputeEnvironmentSecurityGroups;
tcutts marked this conversation as resolved.
Show resolved Hide resolved

/**
* The VPC network that all compute resources will be connected to.
Expand Down Expand Up @@ -584,25 +603,31 @@ export class ComputeEnvironment extends Resource implements IComputeEnvironment,
return instanceTypes.map((type: ec2.InstanceType) => type.toString());
}

private buildConnections(vpc?: ec2.IVpc, securityGroups?:ec2.ISecurityGroup[]): ec2.Connections {
private buildConnections(vpc?: ec2.IVpc, securityGroups?:ec2.ISecurityGroup[] | ComputeEnvironmentSecurityGroups ): ec2.Connections {

if (vpc === undefined) {
return new ec2.Connections({});
}

if (securityGroups === undefined) {
if (securityGroups === undefined ||
securityGroups === ComputeEnvironmentSecurityGroups.AUTOMATIC) {
return new ec2.Connections({
securityGroups: [
new ec2.SecurityGroup(this, 'Resource-Security-Group', { vpc }),
],
});
}

if (securityGroups === ComputeEnvironmentSecurityGroups.NONE) {
return new ec2.Connections({});
}

return new ec2.Connections({ securityGroups });
};

private getSecurityGroupIds(): string[] | undefined {
if (this.connections === undefined) {
if (this.connections === undefined ||
this.connections.securityGroups.length < 1) {
return undefined;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading