| 7 | from constructs import Construct |
| 8 | |
| 9 | class BatchEC2Stack(Stack): |
| 10 | |
| 11 | def __init__(self, scope: Construct, id: str, **kwargs) -> None: |
| 12 | super().__init__(scope, id, **kwargs) |
| 13 | |
| 14 | # This resource alone will create a private/public subnet in each AZ as well as nat/internet gateway(s) |
| 15 | vpc = ec2.Vpc(self, "VPC") |
| 16 | |
| 17 | # To create number of Batch Compute Environment |
| 18 | count = 3 |
| 19 | |
| 20 | # Create AWS Batch Job Queue |
| 21 | self.batch_queue = batch.JobQueue(self, "JobQueue") |
| 22 | |
| 23 | # For loop to create Batch Compute Environments |
| 24 | for i in range(count): |
| 25 | name = "MyBatchEC2Env" + str(i) |
| 26 | batch_environment = batch.ManagedEc2EcsComputeEnvironment(self, name, |
| 27 | spot=True, |
| 28 | spot_bid_percentage=75, |
| 29 | vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PRIVATE_WITH_NAT), |
| 30 | vpc=vpc |
| 31 | ) |
| 32 | |
| 33 | self.batch_queue.add_compute_environment(batch_environment, i) |
| 34 | |
| 35 | # Create Job Definition to submit job in batch job queue. |
| 36 | batch_jobDef = batch.EcsJobDefinition(self, "MyJobDef", |
| 37 | container=batch.EcsEc2ContainerDefinition(self, "BatchCDKJobDef", |
| 38 | image=ecs.ContainerImage.from_registry("public.ecr.aws/amazonlinux/amazonlinux:latest"), |
| 39 | command=["sleep", "60"], |
| 40 | memory=Size.mebibytes(512), |
| 41 | cpu=1 |
| 42 | ) |
| 43 | ) |
| 44 | |
| 45 | # Output resources |
| 46 | CfnOutput(self, "BatchJobQueue",value=self.batch_queue.job_queue_name) |
| 47 | CfnOutput(self, "EcsJobDefinition",value=batch_jobDef.job_definition_name) |
| 48 | |
| 49 | |
| 50 | |