MCPcopy Create free account
hub / github.com/aws-samples/aws-cdk-examples / BatchFargateStack

Class BatchFargateStack

python/batch/batch-using-fargate/app.py:10–53  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8from constructs import Construct
9
10class BatchFargateStack(Stack):
11
12 def __init__(self, scope: Construct, id: str, **kwargs) -> None:
13 super().__init__(scope, id, **kwargs)
14
15 # This resource alone will create a private/public subnet in each AZ as well as nat/internet gateway(s)
16 vpc = ec2.Vpc(self, "VPC")
17
18 # To create number of Batch Compute Environment
19 count = 3
20
21 # Create AWS Batch Job Queue
22 self.batch_queue = batch.JobQueue(self, "JobQueue")
23
24 # For loop to create Batch Compute Environments
25 for i in range(count):
26 name = "MyFargateEnv" + str(i)
27 fargate_spot_environment = batch.FargateComputeEnvironment(self, name,
28 vpc_subnets=ec2.SubnetSelection(subnet_type=ec2.SubnetType.PRIVATE_WITH_NAT),
29 vpc=vpc
30 )
31
32 self.batch_queue.add_compute_environment(fargate_spot_environment, i)
33
34 # Task execution IAM role for Fargate
35 task_execution_role = iam.Role(self, "TaskExecutionRole",
36 assumed_by=iam.ServicePrincipal(
37 "ecs-tasks.amazonaws.com"),
38 managed_policies=[iam.ManagedPolicy.from_aws_managed_policy_name("service-role/AmazonECSTaskExecutionRolePolicy")])
39
40 # Create Job Definition to submit job in batch job queue.
41 batch_jobDef = batch.EcsJobDefinition(self, "MyJobDef",
42 container=batch.EcsFargateContainerDefinition(self, "FargateCDKJobDef",
43 image=ecs.ContainerImage.from_registry("public.ecr.aws/amazonlinux/amazonlinux:latest"),
44 command=["sleep", "60"],
45 memory=Size.mebibytes(512),
46 cpu=0.25,
47 execution_role=task_execution_role
48 )
49 )
50
51 # Output resources
52 CfnOutput(self, "BatchJobQueue",value=self.batch_queue.job_queue_name)
53 CfnOutput(self, "JobDefinition",value=batch_jobDef.job_definition_name)
54
55
56

Callers 1

app.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected