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

feat(batch): ComputeEnvironment implements IConnectable #21458

Merged
merged 10 commits into from
Aug 5, 2022
16 changes: 16 additions & 0 deletions packages/@aws-cdk/aws-batch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ const spotEnvironment = new batch.ComputeEnvironment(this, 'MySpotEnvironment',
});
```

### Compute Environments and Security Groups

Compute Environments now implement the IConnectable interface, which means you can use
tcutts marked this conversation as resolved.
Show resolved Hide resolved
connections on other CDK resources to manipulate the security groups and allow access.

For example, allowing Compute Environments to access an EFS filesystem:
tcutts marked this conversation as resolved.
Show resolved Hide resolved

```ts
import * as efs from '@aws-cdk/aws-efs';

declare const fs: efs.FileSystem;
declare const ce: batch.ComputeEnvironment;

fs.connections.allowDefaultPortFrom(ce);
tcutts marked this conversation as resolved.
Show resolved Hide resolved
```

### Fargate Compute Environment

It is possible to have AWS Batch submit jobs to be run on Fargate compute resources. Below is an example of how this can be done:
Expand Down
39 changes: 32 additions & 7 deletions packages/@aws-cdk/aws-batch/lib/compute-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export interface IComputeEnvironment extends IResource {
*
* Defines a batch compute environment to run batch jobs on.
*/
export class ComputeEnvironment extends Resource implements IComputeEnvironment {
export class ComputeEnvironment extends Resource implements IComputeEnvironment, ec2.IConnectable {
/**
* Fetches an existing batch compute environment by its amazon resource name.
*
Expand Down Expand Up @@ -357,6 +357,13 @@ export class ComputeEnvironment extends Resource implements IComputeEnvironment
*/
public readonly computeEnvironmentName: string;

/**
* Connections for this compute environment.
*
* @attribute
tcutts marked this conversation as resolved.
Show resolved Hide resolved
*/
public readonly connections: ec2.Connections;

constructor(scope: Construct, id: string, props: ComputeEnvironmentProps = { enabled: true, managed: true }) {
super(scope, id, {
physicalName: props.computeEnvironmentName,
Expand All @@ -370,8 +377,11 @@ export class ComputeEnvironment extends Resource implements IComputeEnvironment
const spotFleetRole = this.getSpotFleetRole(props);
let computeResources: CfnComputeEnvironment.ComputeResourcesProperty | undefined;

this.connections = this.buildConnections(props.computeResources?.vpc, props.computeResources?.securityGroups);

// Only allow compute resources to be set when using MANAGED type
if (props.computeResources && this.isManaged(props)) {

computeResources = {
bidPercentage: props.computeResources.bidPercentage,
desiredvCpus: props.computeResources.desiredvCpus,
Expand All @@ -380,7 +390,7 @@ export class ComputeEnvironment extends Resource implements IComputeEnvironment
launchTemplate: props.computeResources.launchTemplate,
maxvCpus: props.computeResources.maxvCpus || 256,
placementGroup: props.computeResources.placementGroup,
securityGroupIds: this.buildSecurityGroupIds(props.computeResources.vpc, props.computeResources.securityGroups),
securityGroupIds: this.getSecurityGroupIds(),
spotIamFleetRole: spotFleetRole?.roleArn,
subnets: props.computeResources.vpc.selectSubnets(props.computeResources.vpcSubnets).subnetIds,
tags: props.computeResources.computeResourcesTags,
Expand Down Expand Up @@ -576,14 +586,29 @@ export class ComputeEnvironment extends Resource implements IComputeEnvironment
return instanceTypes.map((type: ec2.InstanceType) => type.toString());
}

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

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

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

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

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

return securityGroups.map((group: ec2.ISecurityGroup) => group.securityGroupId);
return this.connections.securityGroups.map((group: ec2.ISecurityGroup) => group.securityGroupId);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-batch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ecr": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-efs": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-secretsmanager": "0.0.0",
"@aws-cdk/aws-ssm": "0.0.0",
Expand All @@ -104,6 +105,7 @@
"@aws-cdk/aws-ec2": "0.0.0",
"@aws-cdk/aws-ecr": "0.0.0",
"@aws-cdk/aws-ecs": "0.0.0",
"@aws-cdk/aws-efs": "0.0.0",
"@aws-cdk/aws-iam": "0.0.0",
"@aws-cdk/aws-secretsmanager": "0.0.0",
"@aws-cdk/aws-ssm": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Loading