Skip to content

Commit

Permalink
Refactor to use new batch L2 constructs (#6)
Browse files Browse the repository at this point in the history
@aws-cdk/aws-batch-alpha now has L2 constructs for the Batch Service
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-batch-alpha-readme.html

update deps and refactor to use them
  • Loading branch information
gdamjan-loka authored Aug 30, 2023
1 parent 3a39bc5 commit e337c63
Show file tree
Hide file tree
Showing 6 changed files with 1,253 additions and 1,365 deletions.
33 changes: 16 additions & 17 deletions infrastructure/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,26 @@
"test:fix": "jest --updateSnapshot"
},
"dependencies": {
"@aws-cdk/assert": "^2.50.0",
"@aws-cdk/aws-batch-alpha": "^2.50.0-alpha.0",
"aws-cdk-lib": "^2.50.0",
"constructs": "^10.1.161",
"source-map-support": "^0.5.16"
"@aws-cdk/aws-batch-alpha": "^2.93.0-alpha.0",
"aws-cdk": "^2.93.0",
"aws-cdk-lib": "^2.93.0",
"constructs": "^10.2.70",
"source-map-support": "^0.5.21",
"ts-node": "^10.9.1"
},
"devDependencies": {
"@types/jest": "^29.2.3",
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"aws-cdk": "^2.50.0",
"eslint": "^8.27.0",
"jest": "^29.3.1",
"prettier": "^2.6.2",
"ts-jest": "^29.0.3",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
"@types/jest": "^29.5.4",
"@types/node": "^20.5.7",
"@typescript-eslint/eslint-plugin": "^6.5.0",
"@typescript-eslint/parser": "^6.5.0",
"eslint": "^8.48.0",
"jest": "^29.6.4",
"prettier": "^3.0.3",
"ts-jest": "^29.1.1",
"typescript": "^5.2.2"
},
"engines": {
"node": ">=16",
"node": ">=18",
"yarn": "~1.22.0 <2"
}
}
31 changes: 0 additions & 31 deletions infrastructure/src/batch.ts

This file was deleted.

53 changes: 42 additions & 11 deletions infrastructure/src/stack.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import path from "path";
import { Construct } from "constructs";
import { CfnOutput, Stack, StackProps } from "aws-cdk-lib";
import { CfnOutput, Size, Stack, StackProps } from "aws-cdk-lib";
import { DockerImageAsset } from "aws-cdk-lib/aws-ecr-assets";
import { ContainerImage } from "aws-cdk-lib/aws-ecs";
import { createBatch } from "./batch";
import {
Action,
EcsFargateContainerDefinition,
EcsJobDefinition,
FargateComputeEnvironment,
JobQueue,
Reason,
RetryStrategy,
} from "@aws-cdk/aws-batch-alpha";

import { createVPC } from "./vpc";
import { createWebApp } from "./webapp";
import path from "path";

export class AwsBatchDemoStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
Expand All @@ -19,16 +28,38 @@ export class AwsBatchDemoStack extends Stack {
const containerImage = ContainerImage.fromDockerImageAsset(asset);
/**
* Alternatively, we can use an image already in ECR
const containerImage = ContainerImage.fromEcrRepository(…);
*/
* const containerImage = ContainerImage.fromEcrRepository(…);
*/

const batch = createBatch(this, "batch", {
/**
* Batch on Fargate
*/
const batchComputeEnv = new FargateComputeEnvironment(this, "myFargateComputeEnv", {
vpc,
image: containerImage,
imageCommand: ["/app/bin/demoapp-compute", "--name", "Ref::inputdata"],
computeEnvironmentName: "batch-demo",
});

const app = createWebApp(this, vpc, containerImage, batch);
const batchQueue = new JobQueue(this, "JobQueue", { jobQueueName: "batch-demo" });
batchQueue.addComputeEnvironment(batchComputeEnv, 100);

const batchDefn = new EcsJobDefinition(this, "JobDefn", {
jobDefinitionName: "batch-demo",
container: new EcsFargateContainerDefinition(this, "containerDefn", {
image: containerImage,
command: ["/app/bin/demoapp-compute", "--name", "Ref::inputdata"],
memory: Size.mebibytes(512),
cpu: 256,
}),
retryAttempts: 5,
retryStrategies: [RetryStrategy.of(Action.EXIT, Reason.CANNOT_PULL_CONTAINER)],
});

/**
* Create the Webapp on Fargate
*/
const app = createWebApp(this, vpc, containerImage, { jobQueue: batchQueue, jobDefinition: batchDefn });

batchDefn.grantSubmitJob(app.taskDefinition.taskRole, batchQueue);

/**
* Cloudformation Outputs
Expand All @@ -37,10 +68,10 @@ export class AwsBatchDemoStack extends Stack {
value: `http://${app.loadBalancer.loadBalancerDnsName}/`,
});
new CfnOutput(this, "JobDefinitonArn", {
value: batch.jobDefinition.jobDefinitionArn,
value: batchDefn.jobDefinitionArn,
});
new CfnOutput(this, "JobQueueArn", {
value: batch.jobQueue.jobQueueArn,
value: batchQueue.jobQueueArn,
});
}
}
22 changes: 12 additions & 10 deletions infrastructure/src/webapp.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { JobDefinition, JobQueue } from "@aws-cdk/aws-batch-alpha";
import { JobDefinitionBase, JobQueue } from "@aws-cdk/aws-batch-alpha";
import { IVpc } from "aws-cdk-lib/aws-ec2";
import { ContainerImage } from "aws-cdk-lib/aws-ecs";
import { ApplicationLoadBalancedFargateService } from "aws-cdk-lib/aws-ecs-patterns";
import { ApplicationProtocol } from "aws-cdk-lib/aws-elasticloadbalancingv2";
import { PolicyStatement } from "aws-cdk-lib/aws-iam";
import { Construct } from "constructs";

export function createWebApp(
scope: Construct,
vpc: IVpc,
containerImage: ContainerImage,
batch: { jobQueue: JobQueue; jobDefinition: JobDefinition }
batch: { jobQueue: JobQueue; jobDefinition: JobDefinitionBase },
) {
const loadBalancedFargate = new ApplicationLoadBalancedFargateService(scope, "Service", {
vpc,
Expand All @@ -26,7 +27,14 @@ export function createWebApp(
},
},
desiredCount: 2,
// redirectHTTP: true,
/* A real production deployment would have https,
* and a DNS Zone and Certificates and all,
* but this is a demo.
*/
redirectHTTP: false,
domainName: undefined,
domainZone: undefined,
protocol: ApplicationProtocol.HTTP,
});

loadBalancedFargate.targetGroup.configureHealthCheck({
Expand All @@ -37,13 +45,7 @@ export function createWebApp(
new PolicyStatement({
actions: ["batch:ListJobs"],
resources: ["*"],
})
);
loadBalancedFargate.taskDefinition.addToTaskRolePolicy(
new PolicyStatement({
actions: ["batch:SubmitJob"],
resources: [batch.jobQueue.jobQueueArn, batch.jobDefinition.jobDefinitionArn],
})
}),
);

return loadBalancedFargate;
Expand Down
Loading

0 comments on commit e337c63

Please sign in to comment.