AWS Batch

A minimal AWS Batch example.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from aws_cdk import aws_batch as batch
from aws_cdk import aws_ec2 as ec2
from aws_cdk import aws_ecs as ecs
from aws_cdk import aws_stepfunctions as sfn
from aws_cdk import aws_stepfunctions_tasks as sfn_tasks
from aws_cdk import core as cdk

from examples.batch_example import banana


class BatchExample(cdk.Stack):
    def __init__(self, scope, id, **kwargs):
        super().__init__(scope, id, **kwargs)

        batch_job = BatchConstruct(self, "batch_construct").batch_job

        self.definition = (
            banana.task(self) >> batch_job >> sfn.Succeed(self, "Success!")
        )

        self.state_machine = sfn.StateMachine(
            self,
            "example_batch_sfn",
            definition=self.definition,
            state_machine_name="example_batch_state_machine",
        )


class BatchConstruct(cdk.Construct):
    def __init__(self, scope, id, **kwargs):
        super().__init__(scope, id, **kwargs)

        default_vpc = ec2.Vpc.from_lookup(
            self,
            "default_vpc",
            is_default=True,
        )

        compute_resources = batch.ComputeResources(
            vpc=default_vpc,
        )

        compute_environment = batch.ComputeEnvironment(
            self,
            "example_compute_environment",
            compute_resources=compute_resources,
        )

        job_queue = batch.JobQueue(
            self,
            "batch_job_queue",
            compute_environments=[
                batch.JobQueueComputeEnvironment(
                    compute_environment=compute_environment,
                    order=1,
                )
            ],
        )

        job_definition = batch.JobDefinition(
            self,
            "example_job_definition",
            container=batch.JobDefinitionContainer(
                image=ecs.ContainerImage.from_asset("./examples/batch/"),
                vcpus=2,
                memory_limit_mib=8,
            ),
        )

        self.batch_job = sfn_tasks.BatchSubmitJob(
            self,
            "example_batch_job",
            job_definition_arn=job_definition.job_definition_arn,
            job_name="example_batch_job",
            job_queue_arn=job_queue.job_queue_arn,
        )
1
2
3
4
5
6
from orkestra import compose


@compose
def banana(event, context):
    return "banana"
1
2
3
4
5
FROM python:3.8-slim

ENTRYPOINT ["echo"]

CMD ["hello, world"]

batch example