Skip to content

prefect.infrastructure.provisioners.ecs

AuthenticationResource

Source code in src/prefect/infrastructure/provisioners/ecs.py
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
class AuthenticationResource:
    def __init__(
        self,
        work_pool_name: str,
        user_name: str = "prefect-ecs-user",
        policy_name: str = "prefect-ecs-policy",
        credentials_block_name: Optional[str] = None,
    ):
        self._user_name = user_name
        self._credentials_block_name = (
            credentials_block_name or f"{work_pool_name}-aws-credentials"
        )
        self._policy_name = policy_name
        self._policy_document = {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Sid": "PrefectEcsPolicy",
                    "Effect": "Allow",
                    "Action": [
                        "ec2:AuthorizeSecurityGroupIngress",
                        "ec2:CreateSecurityGroup",
                        "ec2:CreateTags",
                        "ec2:DescribeNetworkInterfaces",
                        "ec2:DescribeSecurityGroups",
                        "ec2:DescribeSubnets",
                        "ec2:DescribeVpcs",
                        "ecs:CreateCluster",
                        "ecs:DeregisterTaskDefinition",
                        "ecs:DescribeClusters",
                        "ecs:DescribeTaskDefinition",
                        "ecs:DescribeTasks",
                        "ecs:ListAccountSettings",
                        "ecs:ListClusters",
                        "ecs:ListTaskDefinitions",
                        "ecs:RegisterTaskDefinition",
                        "ecs:RunTask",
                        "ecs:StopTask",
                        "ecs:TagResource",
                        "logs:CreateLogStream",
                        "logs:PutLogEvents",
                        "logs:DescribeLogGroups",
                        "logs:GetLogEvents",
                    ],
                    "Resource": "*",
                }
            ],
        }
        self._iam_user_resource = IamUserResource(user_name=user_name)
        self._iam_policy_resource = IamPolicyResource(policy_name=policy_name)
        self._credentials_block_resource = CredentialsBlockResource(
            user_name=user_name, block_document_name=self._credentials_block_name
        )
        self._execution_role_resource = ExecutionRoleResource()

    @property
    def resources(self):
        return [
            self._execution_role_resource,
            self._iam_user_resource,
            self._iam_policy_resource,
            self._credentials_block_resource,
        ]

    async def get_task_count(self):
        """
        Returns the number of tasks that will be executed to provision this resource.

        Returns:
            int: The number of tasks to be provisioned.
        """
        return sum([await resource.get_task_count() for resource in self.resources])

    async def requires_provisioning(self) -> bool:
        """
        Check if this resource requires provisioning.

        Returns:
            bool: True if provisioning is required, False otherwise.
        """
        return any(
            [await resource.requires_provisioning() for resource in self.resources]
        )

    async def get_planned_actions(self) -> List[str]:
        """
        Returns a description of the planned actions for provisioning this resource.

        Returns:
            Optional[str]: A description of the planned actions for provisioning the resource,
                or None if provisioning is not required.
        """
        return [
            action
            for resource in self.resources
            for action in await resource.get_planned_actions()
        ]

    async def provision(
        self,
        base_job_template: Dict[str, Any],
        advance: Callable[[], None],
    ):
        """
        Provisions the authentication resources.

        Args:
            base_job_template: The base job template of the work pool to provision
                infrastructure for.
            advance: A callback function to indicate progress.
        """
        # Provision task execution role
        role_arn = await self._execution_role_resource.provision(
            base_job_template=base_job_template, advance=advance
        )
        # Update policy document with the role ARN
        self._policy_document["Statement"].append(
            {
                "Sid": "AllowPassRoleForEcs",
                "Effect": "Allow",
                "Action": "iam:PassRole",
                "Resource": role_arn,
            }
        )
        # Provision the IAM user
        await self._iam_user_resource.provision(advance=advance)
        # Provision the IAM policy
        policy_arn = await self._iam_policy_resource.provision(
            policy_document=self._policy_document, advance=advance
        )
        # Attach the policy to the user
        if policy_arn:
            iam_client = boto3.client("iam")
            await anyio.to_thread.run_sync(
                partial(
                    iam_client.attach_user_policy,
                    UserName=self._user_name,
                    PolicyArn=policy_arn,
                )
            )
        await self._credentials_block_resource.provision(
            base_job_template=base_job_template,
            advance=advance,
        )

    @property
    def next_steps(self):
        return [
            next_step
            for resource in self.resources
            for next_step in resource.next_steps
        ]

get_planned_actions() async

Returns a description of the planned actions for provisioning this resource.

Returns:

Type Description
List[str]

Optional[str]: A description of the planned actions for provisioning the resource, or None if provisioning is not required.

Source code in src/prefect/infrastructure/provisioners/ecs.py
448
449
450
451
452
453
454
455
456
457
458
459
460
async def get_planned_actions(self) -> List[str]:
    """
    Returns a description of the planned actions for provisioning this resource.

    Returns:
        Optional[str]: A description of the planned actions for provisioning the resource,
            or None if provisioning is not required.
    """
    return [
        action
        for resource in self.resources
        for action in await resource.get_planned_actions()
    ]

get_task_count() async

Returns the number of tasks that will be executed to provision this resource.

Returns:

Name Type Description
int

The number of tasks to be provisioned.

Source code in src/prefect/infrastructure/provisioners/ecs.py
428
429
430
431
432
433
434
435
async def get_task_count(self):
    """
    Returns the number of tasks that will be executed to provision this resource.

    Returns:
        int: The number of tasks to be provisioned.
    """
    return sum([await resource.get_task_count() for resource in self.resources])

provision(base_job_template, advance) async

Provisions the authentication resources.

Parameters:

Name Type Description Default
base_job_template Dict[str, Any]

The base job template of the work pool to provision infrastructure for.

required
advance Callable[[], None]

A callback function to indicate progress.

required
Source code in src/prefect/infrastructure/provisioners/ecs.py
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
async def provision(
    self,
    base_job_template: Dict[str, Any],
    advance: Callable[[], None],
):
    """
    Provisions the authentication resources.

    Args:
        base_job_template: The base job template of the work pool to provision
            infrastructure for.
        advance: A callback function to indicate progress.
    """
    # Provision task execution role
    role_arn = await self._execution_role_resource.provision(
        base_job_template=base_job_template, advance=advance
    )
    # Update policy document with the role ARN
    self._policy_document["Statement"].append(
        {
            "Sid": "AllowPassRoleForEcs",
            "Effect": "Allow",
            "Action": "iam:PassRole",
            "Resource": role_arn,
        }
    )
    # Provision the IAM user
    await self._iam_user_resource.provision(advance=advance)
    # Provision the IAM policy
    policy_arn = await self._iam_policy_resource.provision(
        policy_document=self._policy_document, advance=advance
    )
    # Attach the policy to the user
    if policy_arn:
        iam_client = boto3.client("iam")
        await anyio.to_thread.run_sync(
            partial(
                iam_client.attach_user_policy,
                UserName=self._user_name,
                PolicyArn=policy_arn,
            )
        )
    await self._credentials_block_resource.provision(
        base_job_template=base_job_template,
        advance=advance,
    )

requires_provisioning() async

Check if this resource requires provisioning.

Returns:

Name Type Description
bool bool

True if provisioning is required, False otherwise.

Source code in src/prefect/infrastructure/provisioners/ecs.py
437
438
439
440
441
442
443
444
445
446
async def requires_provisioning(self) -> bool:
    """
    Check if this resource requires provisioning.

    Returns:
        bool: True if provisioning is required, False otherwise.
    """
    return any(
        [await resource.requires_provisioning() for resource in self.resources]
    )

ClusterResource

Source code in src/prefect/infrastructure/provisioners/ecs.py
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
class ClusterResource:
    def __init__(self, cluster_name: str = "prefect-ecs-cluster"):
        self._ecs_client = boto3.client("ecs")
        self._cluster_name = cluster_name
        self._requires_provisioning = None

    async def get_task_count(self):
        """
        Returns the number of tasks that will be executed to provision this resource.

        Returns:
            int: The number of tasks to be provisioned.
        """
        return 1 if await self.requires_provisioning() else 0

    async def requires_provisioning(self) -> bool:
        """
        Check if this resource requires provisioning.

        Returns:
            bool: True if provisioning is required, False otherwise.
        """
        if self._requires_provisioning is None:
            response = await anyio.to_thread.run_sync(
                partial(
                    self._ecs_client.describe_clusters, clusters=[self._cluster_name]
                )
            )
            if response["clusters"] and response["clusters"][0]["status"] == "ACTIVE":
                self._requires_provisioning = False
            else:
                self._requires_provisioning = True
        return self._requires_provisioning

    async def get_planned_actions(self) -> List[str]:
        """
        Returns a description of the planned actions for provisioning this resource.

        Returns:
            Optional[str]: A description of the planned actions for provisioning the resource,
                or None if provisioning is not required.
        """
        if await self.requires_provisioning():
            return [
                "Creating an ECS cluster for running Prefect flows:"
                f" [blue]{self._cluster_name}[/]"
            ]
        return []

    async def provision(
        self,
        base_job_template: Dict[str, Any],
        advance: Callable[[], None],
    ):
        """
        Provisions an ECS cluster.

        Will update the `cluster` variable in the job template to reference the cluster.

        Args:
            base_job_template: The base job template of the work pool to provision
                infrastructure for.
            advance: A callback function to indicate progress.
        """
        if await self.requires_provisioning():
            console = current_console.get()
            console.print("Provisioning ECS cluster")
            await anyio.to_thread.run_sync(
                partial(self._ecs_client.create_cluster, clusterName=self._cluster_name)
            )
            advance()

        base_job_template["variables"]["properties"]["cluster"][
            "default"
        ] = self._cluster_name

    @property
    def next_steps(self):
        return []

get_planned_actions() async

Returns a description of the planned actions for provisioning this resource.

Returns:

Type Description
List[str]

Optional[str]: A description of the planned actions for provisioning the resource, or None if provisioning is not required.

Source code in src/prefect/infrastructure/provisioners/ecs.py
552
553
554
555
556
557
558
559
560
561
562
563
564
565
async def get_planned_actions(self) -> List[str]:
    """
    Returns a description of the planned actions for provisioning this resource.

    Returns:
        Optional[str]: A description of the planned actions for provisioning the resource,
            or None if provisioning is not required.
    """
    if await self.requires_provisioning():
        return [
            "Creating an ECS cluster for running Prefect flows:"
            f" [blue]{self._cluster_name}[/]"
        ]
    return []

get_task_count() async

Returns the number of tasks that will be executed to provision this resource.

Returns:

Name Type Description
int

The number of tasks to be provisioned.

Source code in src/prefect/infrastructure/provisioners/ecs.py
524
525
526
527
528
529
530
531
async def get_task_count(self):
    """
    Returns the number of tasks that will be executed to provision this resource.

    Returns:
        int: The number of tasks to be provisioned.
    """
    return 1 if await self.requires_provisioning() else 0

provision(base_job_template, advance) async

Provisions an ECS cluster.

Will update the cluster variable in the job template to reference the cluster.

Parameters:

Name Type Description Default
base_job_template Dict[str, Any]

The base job template of the work pool to provision infrastructure for.

required
advance Callable[[], None]

A callback function to indicate progress.

required
Source code in src/prefect/infrastructure/provisioners/ecs.py
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
async def provision(
    self,
    base_job_template: Dict[str, Any],
    advance: Callable[[], None],
):
    """
    Provisions an ECS cluster.

    Will update the `cluster` variable in the job template to reference the cluster.

    Args:
        base_job_template: The base job template of the work pool to provision
            infrastructure for.
        advance: A callback function to indicate progress.
    """
    if await self.requires_provisioning():
        console = current_console.get()
        console.print("Provisioning ECS cluster")
        await anyio.to_thread.run_sync(
            partial(self._ecs_client.create_cluster, clusterName=self._cluster_name)
        )
        advance()

    base_job_template["variables"]["properties"]["cluster"][
        "default"
    ] = self._cluster_name

requires_provisioning() async

Check if this resource requires provisioning.

Returns:

Name Type Description
bool bool

True if provisioning is required, False otherwise.

Source code in src/prefect/infrastructure/provisioners/ecs.py
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
async def requires_provisioning(self) -> bool:
    """
    Check if this resource requires provisioning.

    Returns:
        bool: True if provisioning is required, False otherwise.
    """
    if self._requires_provisioning is None:
        response = await anyio.to_thread.run_sync(
            partial(
                self._ecs_client.describe_clusters, clusters=[self._cluster_name]
            )
        )
        if response["clusters"] and response["clusters"][0]["status"] == "ACTIVE":
            self._requires_provisioning = False
        else:
            self._requires_provisioning = True
    return self._requires_provisioning

ContainerRepositoryResource

Source code in src/prefect/infrastructure/provisioners/ecs.py
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
class ContainerRepositoryResource:
    def __init__(self, work_pool_name: str, repository_name: str = "prefect-flows"):
        self._ecr_client = boto3.client("ecr")
        self._repository_name = repository_name
        self._requires_provisioning = None
        self._work_pool_name = work_pool_name
        self._next_steps = []

    async def get_task_count(self):
        """
        Returns the number of tasks that will be executed to provision this resource.

        Returns:
            int: The number of tasks to be provisioned.
        """
        return 3 if await self.requires_provisioning() else 0

    async def _get_prefect_created_registry(self):
        try:
            registries = await anyio.to_thread.run_sync(
                partial(
                    self._ecr_client.describe_repositories,
                    repositoryNames=[self._repository_name],
                )
            )
            return next(iter(registries), None)
        except self._ecr_client.exceptions.RepositoryNotFoundException:
            return None

    async def requires_provisioning(self) -> bool:
        """
        Check if this resource requires provisioning.

        Returns:
            bool: True if provisioning is required, False otherwise.
        """
        if self._requires_provisioning is not None:
            return self._requires_provisioning

        if await self._get_prefect_created_registry() is not None:
            self._requires_provisioning = False
            return False

        self._requires_provisioning = True
        return True

    async def get_planned_actions(self) -> List[str]:
        """
        Returns a description of the planned actions for provisioning this resource.

        Returns:
            Optional[str]: A description of the planned actions for provisioning the resource,
                or None if provisioning is not required.
        """
        if await self.requires_provisioning():
            return [
                "Creating an ECR repository for storing Prefect images:"
                f" [blue]{self._repository_name}[/]"
            ]
        return []

    async def provision(
        self,
        base_job_template: Dict[str, Any],
        advance: Callable[[], None],
    ):
        """
        Provisions an ECR repository.

        Args:
            base_job_template: The base job template of the work pool to provision
                infrastructure for.
            advance: A callback function to indicate progress.
        """
        if await self.requires_provisioning():
            console = current_console.get()
            console.print("Provisioning ECR repository")
            response = await anyio.to_thread.run_sync(
                partial(
                    self._ecr_client.create_repository,
                    repositoryName=self._repository_name,
                )
            )
            advance()
            console.print("Authenticating with ECR")
            auth_token = self._ecr_client.get_authorization_token()
            user, passwd = (
                base64.b64decode(
                    auth_token["authorizationData"][0]["authorizationToken"]
                )
                .decode()
                .split(":")
            )
            proxy_endpoint = auth_token["authorizationData"][0]["proxyEndpoint"]
            await run_process(f"docker login -u {user} -p {passwd} {proxy_endpoint}")
            advance()
            console.print("Setting default Docker build namespace")
            namespace = response["repository"]["repositoryUri"].split("/")[0]
            update_current_profile({PREFECT_DEFAULT_DOCKER_BUILD_NAMESPACE: namespace})
            self._update_next_steps(namespace)
            advance()

    def _update_next_steps(self, repository_uri: str):
        self._next_steps.extend(
            [
                dedent(
                    f"""\

                    Your default Docker build namespace has been set to [blue]{repository_uri!r}[/].

                    To build and push a Docker image to your newly created repository, use [blue]{self._repository_name!r}[/] as your image name:
                    """
                ),
                Panel(
                    Syntax(
                        dedent(
                            f"""\
                                from prefect import flow
                                from prefect.docker import DockerImage


                                @flow(log_prints=True)
                                def my_flow(name: str = "world"):
                                    print(f"Hello {{name}}! I'm a flow running on ECS!")


                                if __name__ == "__main__":
                                    my_flow.deploy(
                                        name="my-deployment",
                                        work_pool_name="{self._work_pool_name}",
                                        image=DockerImage(
                                            name="{self._repository_name}:latest",
                                            platform="linux/amd64",
                                        )
                                    )"""
                        ),
                        "python",
                        background_color="default",
                    ),
                    title="example_deploy_script.py",
                    expand=False,
                ),
            ]
        )

    @property
    def next_steps(self):
        return self._next_steps

get_planned_actions() async

Returns a description of the planned actions for provisioning this resource.

Returns:

Type Description
List[str]

Optional[str]: A description of the planned actions for provisioning the resource, or None if provisioning is not required.

Source code in src/prefect/infrastructure/provisioners/ecs.py
881
882
883
884
885
886
887
888
889
890
891
892
893
894
async def get_planned_actions(self) -> List[str]:
    """
    Returns a description of the planned actions for provisioning this resource.

    Returns:
        Optional[str]: A description of the planned actions for provisioning the resource,
            or None if provisioning is not required.
    """
    if await self.requires_provisioning():
        return [
            "Creating an ECR repository for storing Prefect images:"
            f" [blue]{self._repository_name}[/]"
        ]
    return []

get_task_count() async

Returns the number of tasks that will be executed to provision this resource.

Returns:

Name Type Description
int

The number of tasks to be provisioned.

Source code in src/prefect/infrastructure/provisioners/ecs.py
843
844
845
846
847
848
849
850
async def get_task_count(self):
    """
    Returns the number of tasks that will be executed to provision this resource.

    Returns:
        int: The number of tasks to be provisioned.
    """
    return 3 if await self.requires_provisioning() else 0

provision(base_job_template, advance) async

Provisions an ECR repository.

Parameters:

Name Type Description Default
base_job_template Dict[str, Any]

The base job template of the work pool to provision infrastructure for.

required
advance Callable[[], None]

A callback function to indicate progress.

required
Source code in src/prefect/infrastructure/provisioners/ecs.py
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
async def provision(
    self,
    base_job_template: Dict[str, Any],
    advance: Callable[[], None],
):
    """
    Provisions an ECR repository.

    Args:
        base_job_template: The base job template of the work pool to provision
            infrastructure for.
        advance: A callback function to indicate progress.
    """
    if await self.requires_provisioning():
        console = current_console.get()
        console.print("Provisioning ECR repository")
        response = await anyio.to_thread.run_sync(
            partial(
                self._ecr_client.create_repository,
                repositoryName=self._repository_name,
            )
        )
        advance()
        console.print("Authenticating with ECR")
        auth_token = self._ecr_client.get_authorization_token()
        user, passwd = (
            base64.b64decode(
                auth_token["authorizationData"][0]["authorizationToken"]
            )
            .decode()
            .split(":")
        )
        proxy_endpoint = auth_token["authorizationData"][0]["proxyEndpoint"]
        await run_process(f"docker login -u {user} -p {passwd} {proxy_endpoint}")
        advance()
        console.print("Setting default Docker build namespace")
        namespace = response["repository"]["repositoryUri"].split("/")[0]
        update_current_profile({PREFECT_DEFAULT_DOCKER_BUILD_NAMESPACE: namespace})
        self._update_next_steps(namespace)
        advance()

requires_provisioning() async

Check if this resource requires provisioning.

Returns:

Name Type Description
bool bool

True if provisioning is required, False otherwise.

Source code in src/prefect/infrastructure/provisioners/ecs.py
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
async def requires_provisioning(self) -> bool:
    """
    Check if this resource requires provisioning.

    Returns:
        bool: True if provisioning is required, False otherwise.
    """
    if self._requires_provisioning is not None:
        return self._requires_provisioning

    if await self._get_prefect_created_registry() is not None:
        self._requires_provisioning = False
        return False

    self._requires_provisioning = True
    return True

CredentialsBlockResource

Source code in src/prefect/infrastructure/provisioners/ecs.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
class CredentialsBlockResource:
    def __init__(self, user_name: str, block_document_name: str):
        self._block_document_name = block_document_name
        self._user_name = user_name
        self._requires_provisioning = None

    async def get_task_count(self):
        """
        Returns the number of tasks that will be executed to provision this resource.

        Returns:
            int: The number of tasks to be provisioned.
        """
        return 2 if await self.requires_provisioning() else 0

    @inject_client
    async def requires_provisioning(
        self, client: Optional["PrefectClient"] = None
    ) -> bool:
        if self._requires_provisioning is None:
            try:
                assert client is not None
                await client.read_block_document_by_name(
                    self._block_document_name, "aws-credentials"
                )
                self._requires_provisioning = False
            except ObjectNotFound:
                self._requires_provisioning = True
        return self._requires_provisioning

    async def get_planned_actions(self) -> List[str]:
        """
        Returns a description of the planned actions for provisioning this resource.

        Returns:
            Optional[str]: A description of the planned actions for provisioning the resource,
                or None if provisioning is not required.
        """
        if await self.requires_provisioning():
            return ["Storing generated AWS credentials in a block"]
        return []

    @inject_client
    async def provision(
        self,
        base_job_template: Dict[str, Any],
        advance: Callable[[], None],
        client: Optional["PrefectClient"] = None,
    ):
        """
        Provisions an AWS credentials block.

        Will generate new credentials if the block does not already exist. Updates
        the `aws_credentials` variable in the job template to reference the block.

        Args:
            base_job_template: The base job template.
            advance: A callback function to indicate progress.
            client: A Prefect client to use for interacting with the Prefect API.
        """
        assert client is not None, "Client injection failed"
        if not await self.requires_provisioning():
            block_doc = await client.read_block_document_by_name(
                self._block_document_name, "aws-credentials"
            )
        else:
            console = current_console.get()
            console.print("Generating AWS credentials")
            iam_client = boto3.client("iam")
            access_key_data = await anyio.to_thread.run_sync(
                partial(iam_client.create_access_key, UserName=self._user_name)
            )
            access_key = access_key_data["AccessKey"]
            advance()
            console.print("Creating AWS credentials block")
            assert client is not None

            try:
                credentials_block_type = await client.read_block_type_by_slug(
                    "aws-credentials"
                )
            except ObjectNotFound as exc:
                raise RuntimeError(
                    dedent(
                        """\
                    Unable to find block type "aws-credentials".
                    To register the `aws-credentials` block type, run:

                            pip install prefect-aws
                            prefect blocks register -m prefect_aws

                    """
                    )
                ) from exc

            credentials_block_schema = (
                await client.get_most_recent_block_schema_for_block_type(
                    block_type_id=credentials_block_type.id
                )
            )
            assert (
                credentials_block_schema is not None
            ), f"Unable to find schema for block type {credentials_block_type.slug}"

            block_doc = await client.create_block_document(
                block_document=BlockDocumentCreate(
                    name=self._block_document_name,
                    data={
                        "aws_access_key_id": access_key["AccessKeyId"],
                        "aws_secret_access_key": access_key["SecretAccessKey"],
                        "region_name": boto3.session.Session().region_name,
                    },
                    block_type_id=credentials_block_type.id,
                    block_schema_id=credentials_block_schema.id,
                )
            )
            advance()
        base_job_template["variables"]["properties"]["aws_credentials"]["default"] = {
            "$ref": {"block_document_id": str(block_doc.id)}
        }

    @property
    def next_steps(self):
        return []

get_planned_actions() async

Returns a description of the planned actions for provisioning this resource.

Returns:

Type Description
List[str]

Optional[str]: A description of the planned actions for provisioning the resource, or None if provisioning is not required.

Source code in src/prefect/infrastructure/provisioners/ecs.py
268
269
270
271
272
273
274
275
276
277
278
async def get_planned_actions(self) -> List[str]:
    """
    Returns a description of the planned actions for provisioning this resource.

    Returns:
        Optional[str]: A description of the planned actions for provisioning the resource,
            or None if provisioning is not required.
    """
    if await self.requires_provisioning():
        return ["Storing generated AWS credentials in a block"]
    return []

get_task_count() async

Returns the number of tasks that will be executed to provision this resource.

Returns:

Name Type Description
int

The number of tasks to be provisioned.

Source code in src/prefect/infrastructure/provisioners/ecs.py
244
245
246
247
248
249
250
251
async def get_task_count(self):
    """
    Returns the number of tasks that will be executed to provision this resource.

    Returns:
        int: The number of tasks to be provisioned.
    """
    return 2 if await self.requires_provisioning() else 0

provision(base_job_template, advance, client=None) async

Provisions an AWS credentials block.

Will generate new credentials if the block does not already exist. Updates the aws_credentials variable in the job template to reference the block.

Parameters:

Name Type Description Default
base_job_template Dict[str, Any]

The base job template.

required
advance Callable[[], None]

A callback function to indicate progress.

required
client Optional[PrefectClient]

A Prefect client to use for interacting with the Prefect API.

None
Source code in src/prefect/infrastructure/provisioners/ecs.py
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
@inject_client
async def provision(
    self,
    base_job_template: Dict[str, Any],
    advance: Callable[[], None],
    client: Optional["PrefectClient"] = None,
):
    """
    Provisions an AWS credentials block.

    Will generate new credentials if the block does not already exist. Updates
    the `aws_credentials` variable in the job template to reference the block.

    Args:
        base_job_template: The base job template.
        advance: A callback function to indicate progress.
        client: A Prefect client to use for interacting with the Prefect API.
    """
    assert client is not None, "Client injection failed"
    if not await self.requires_provisioning():
        block_doc = await client.read_block_document_by_name(
            self._block_document_name, "aws-credentials"
        )
    else:
        console = current_console.get()
        console.print("Generating AWS credentials")
        iam_client = boto3.client("iam")
        access_key_data = await anyio.to_thread.run_sync(
            partial(iam_client.create_access_key, UserName=self._user_name)
        )
        access_key = access_key_data["AccessKey"]
        advance()
        console.print("Creating AWS credentials block")
        assert client is not None

        try:
            credentials_block_type = await client.read_block_type_by_slug(
                "aws-credentials"
            )
        except ObjectNotFound as exc:
            raise RuntimeError(
                dedent(
                    """\
                Unable to find block type "aws-credentials".
                To register the `aws-credentials` block type, run:

                        pip install prefect-aws
                        prefect blocks register -m prefect_aws

                """
                )
            ) from exc

        credentials_block_schema = (
            await client.get_most_recent_block_schema_for_block_type(
                block_type_id=credentials_block_type.id
            )
        )
        assert (
            credentials_block_schema is not None
        ), f"Unable to find schema for block type {credentials_block_type.slug}"

        block_doc = await client.create_block_document(
            block_document=BlockDocumentCreate(
                name=self._block_document_name,
                data={
                    "aws_access_key_id": access_key["AccessKeyId"],
                    "aws_secret_access_key": access_key["SecretAccessKey"],
                    "region_name": boto3.session.Session().region_name,
                },
                block_type_id=credentials_block_type.id,
                block_schema_id=credentials_block_schema.id,
            )
        )
        advance()
    base_job_template["variables"]["properties"]["aws_credentials"]["default"] = {
        "$ref": {"block_document_id": str(block_doc.id)}
    }

ElasticContainerServicePushProvisioner

An infrastructure provisioner for ECS push work pools.

Source code in src/prefect/infrastructure/provisioners/ecs.py
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
class ElasticContainerServicePushProvisioner:
    """
    An infrastructure provisioner for ECS push work pools.
    """

    def __init__(self):
        self._console = Console()

    @property
    def console(self):
        return self._console

    @console.setter
    def console(self, value):
        self._console = value

    async def _prompt_boto3_installation(self):
        global boto3
        await run_process(
            [shlex.quote(sys.executable), "-m", "pip", "install", "boto3"]
        )
        boto3 = importlib.import_module("boto3")

    @staticmethod
    def is_boto3_installed():
        """
        Check if boto3 is installed.
        """
        try:
            importlib.import_module("boto3")
            return True
        except ModuleNotFoundError:
            return False

    def _generate_resources(
        self,
        work_pool_name: str,
        user_name: str = "prefect-ecs-user",
        policy_name: str = "prefect-ecs-policy",
        credentials_block_name: Optional[str] = None,
        cluster_name: str = "prefect-ecs-cluster",
        vpc_name: str = "prefect-ecs-vpc",
        ecs_security_group_name: str = "prefect-ecs-security-group",
        repository_name: str = "prefect-flows",
    ):
        return [
            AuthenticationResource(
                work_pool_name=work_pool_name,
                user_name=user_name,
                policy_name=policy_name,
                credentials_block_name=credentials_block_name,
            ),
            ClusterResource(cluster_name=cluster_name),
            VpcResource(
                vpc_name=vpc_name,
                ecs_security_group_name=ecs_security_group_name,
            ),
            ContainerRepositoryResource(
                work_pool_name=work_pool_name,
                repository_name=repository_name,
            ),
        ]

    async def provision(
        self,
        work_pool_name: str,
        base_job_template: dict,
    ) -> Dict[str, Any]:
        """
        Provisions the infrastructure for an ECS push work pool.

        Args:
            work_pool_name: The name of the work pool to provision infrastructure for.
            base_job_template: The base job template of the work pool to provision
                infrastructure for.

        Returns:
            dict: An updated copy base job template.
        """
        if not self.is_boto3_installed():
            if self.console.is_interactive and Confirm.ask(
                "boto3 is required to configure your AWS account. Would you like to"
                " install it?"
            ):
                await self._prompt_boto3_installation()
            else:
                raise RuntimeError(
                    "boto3 is required to configure your AWS account. Please install it"
                    " and try again."
                )

        try:
            if self.console.is_interactive and Confirm.ask(
                "Would you like to customize the resource names for your"
                " infrastructure? This includes an IAM user, IAM policy, ECS cluster,"
                " VPC, ECS security group, and ECR repository."
            ):
                user_name = prompt(
                    "Enter a name for the IAM user (manages ECS tasks)",
                    default="prefect-ecs-user",
                )
                policy_name = prompt(
                    (
                        "Enter a name for the IAM policy (defines ECS task execution"
                        " and image management permissions)"
                    ),
                    default="prefect-ecs-policy",
                )
                cluster_name = prompt(
                    "Enter a name for the ECS cluster (hosts ECS tasks)",
                    default="prefect-ecs-cluster",
                )
                credentials_name = prompt(
                    (
                        "Enter a name for the AWS credentials block (stores AWS"
                        " credentials for managing ECS tasks)"
                    ),
                    default=f"{work_pool_name}-aws-credentials",
                )
                vpc_name = prompt(
                    (
                        "Enter a name for the VPC (provides network isolation for ECS"
                        " tasks)"
                    ),
                    default="prefect-ecs-vpc",
                )
                ecs_security_group_name = prompt(
                    (
                        "Enter a name for the ECS security group (controls task network"
                        " traffic)"
                    ),
                    default="prefect-ecs-security-group",
                )
                repository_name = prompt(
                    (
                        "Enter a name for the ECR repository (stores Docker images for"
                        " ECS tasks)"
                    ),
                    default="prefect-flows",
                )

                provision_preview = Panel(
                    dedent(
                        f"""\
                            Custom names for infrastructure resources for
                            [blue]{work_pool_name}[/]:

                            - IAM user: [blue]{user_name}[/]
                            - IAM policy: [blue]{policy_name}[/]
                            - ECS cluster: [blue]{cluster_name}[/]
                            - AWS credentials block: [blue]{credentials_name}[/]
                            - VPC: [blue]{vpc_name}[/]
                            - ECS security group: [blue]{ecs_security_group_name}[/]
                            - ECR repository: [blue]{repository_name}[/]
                            """
                    ),
                    expand=False,
                )

                self.console.print(provision_preview)

                resources = self._generate_resources(
                    work_pool_name=work_pool_name,
                    user_name=user_name,
                    policy_name=policy_name,
                    credentials_block_name=credentials_name,
                    cluster_name=cluster_name,
                    vpc_name=vpc_name,
                    ecs_security_group_name=ecs_security_group_name,
                    repository_name=repository_name,
                )

            else:
                resources = self._generate_resources(work_pool_name=work_pool_name)

            with Progress(
                SpinnerColumn(),
                TextColumn(
                    "Checking your AWS account for infrastructure that needs to be"
                    " provisioned..."
                ),
                transient=True,
                console=self.console,
            ) as progress:
                inspect_aws_account_task = progress.add_task(
                    "inspect_aws_account", total=1
                )
                num_tasks = sum(
                    [await resource.get_task_count() for resource in resources]
                )
                progress.update(inspect_aws_account_task, completed=1)

            if num_tasks > 0:
                message = (
                    "Provisioning infrastructure for your work pool"
                    f" [blue]{work_pool_name}[/] will require: \n"
                )
                for resource in resources:
                    planned_actions = await resource.get_planned_actions()
                    for action in planned_actions:
                        message += f"\n\t - {action}"

                self.console.print(Panel(message))

                if self._console.is_interactive:
                    if not Confirm.ask(
                        "Proceed with infrastructure provisioning?",
                        console=self._console,
                    ):
                        return base_job_template
            else:
                self.console.print(
                    "No additional infrastructure required for work pool"
                    f" [blue]{work_pool_name}[/]"
                )
                # don't return early, we still need to update the base job template
                # provision calls will be no-ops, but update the base job template

            base_job_template_copy = deepcopy(base_job_template)
            next_steps = []
            with Progress(console=self._console, disable=num_tasks == 0) as progress:
                task = progress.add_task(
                    "Provisioning Infrastructure",
                    total=num_tasks,
                )
                for resource in resources:
                    with console_context(progress.console):
                        await resource.provision(
                            advance=partial(progress.advance, task),
                            base_job_template=base_job_template_copy,
                        )
                    next_steps.append(resource.next_steps)

            if next_steps:
                for step in next_steps:
                    for item in step:
                        self._console.print(item)

            if num_tasks > 0:
                self._console.print(
                    "Infrastructure successfully provisioned!", style="green"
                )

            return base_job_template_copy
        except Exception as exc:
            if hasattr(exc, "response"):
                # Catching boto3 ClientError
                response = getattr(exc, "response", {})
                error_message = get_from_dict(response, "Error.Message") or str(exc)
                raise RuntimeError(error_message) from exc
            # Catching any other exception
            raise RuntimeError(str(exc)) from exc

is_boto3_installed() staticmethod

Check if boto3 is installed.

Source code in src/prefect/infrastructure/provisioners/ecs.py
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
@staticmethod
def is_boto3_installed():
    """
    Check if boto3 is installed.
    """
    try:
        importlib.import_module("boto3")
        return True
    except ModuleNotFoundError:
        return False

provision(work_pool_name, base_job_template) async

Provisions the infrastructure for an ECS push work pool.

Parameters:

Name Type Description Default
work_pool_name str

The name of the work pool to provision infrastructure for.

required
base_job_template dict

The base job template of the work pool to provision infrastructure for.

required

Returns:

Name Type Description
dict Dict[str, Any]

An updated copy base job template.

Source code in src/prefect/infrastructure/provisioners/ecs.py
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
async def provision(
    self,
    work_pool_name: str,
    base_job_template: dict,
) -> Dict[str, Any]:
    """
    Provisions the infrastructure for an ECS push work pool.

    Args:
        work_pool_name: The name of the work pool to provision infrastructure for.
        base_job_template: The base job template of the work pool to provision
            infrastructure for.

    Returns:
        dict: An updated copy base job template.
    """
    if not self.is_boto3_installed():
        if self.console.is_interactive and Confirm.ask(
            "boto3 is required to configure your AWS account. Would you like to"
            " install it?"
        ):
            await self._prompt_boto3_installation()
        else:
            raise RuntimeError(
                "boto3 is required to configure your AWS account. Please install it"
                " and try again."
            )

    try:
        if self.console.is_interactive and Confirm.ask(
            "Would you like to customize the resource names for your"
            " infrastructure? This includes an IAM user, IAM policy, ECS cluster,"
            " VPC, ECS security group, and ECR repository."
        ):
            user_name = prompt(
                "Enter a name for the IAM user (manages ECS tasks)",
                default="prefect-ecs-user",
            )
            policy_name = prompt(
                (
                    "Enter a name for the IAM policy (defines ECS task execution"
                    " and image management permissions)"
                ),
                default="prefect-ecs-policy",
            )
            cluster_name = prompt(
                "Enter a name for the ECS cluster (hosts ECS tasks)",
                default="prefect-ecs-cluster",
            )
            credentials_name = prompt(
                (
                    "Enter a name for the AWS credentials block (stores AWS"
                    " credentials for managing ECS tasks)"
                ),
                default=f"{work_pool_name}-aws-credentials",
            )
            vpc_name = prompt(
                (
                    "Enter a name for the VPC (provides network isolation for ECS"
                    " tasks)"
                ),
                default="prefect-ecs-vpc",
            )
            ecs_security_group_name = prompt(
                (
                    "Enter a name for the ECS security group (controls task network"
                    " traffic)"
                ),
                default="prefect-ecs-security-group",
            )
            repository_name = prompt(
                (
                    "Enter a name for the ECR repository (stores Docker images for"
                    " ECS tasks)"
                ),
                default="prefect-flows",
            )

            provision_preview = Panel(
                dedent(
                    f"""\
                        Custom names for infrastructure resources for
                        [blue]{work_pool_name}[/]:

                        - IAM user: [blue]{user_name}[/]
                        - IAM policy: [blue]{policy_name}[/]
                        - ECS cluster: [blue]{cluster_name}[/]
                        - AWS credentials block: [blue]{credentials_name}[/]
                        - VPC: [blue]{vpc_name}[/]
                        - ECS security group: [blue]{ecs_security_group_name}[/]
                        - ECR repository: [blue]{repository_name}[/]
                        """
                ),
                expand=False,
            )

            self.console.print(provision_preview)

            resources = self._generate_resources(
                work_pool_name=work_pool_name,
                user_name=user_name,
                policy_name=policy_name,
                credentials_block_name=credentials_name,
                cluster_name=cluster_name,
                vpc_name=vpc_name,
                ecs_security_group_name=ecs_security_group_name,
                repository_name=repository_name,
            )

        else:
            resources = self._generate_resources(work_pool_name=work_pool_name)

        with Progress(
            SpinnerColumn(),
            TextColumn(
                "Checking your AWS account for infrastructure that needs to be"
                " provisioned..."
            ),
            transient=True,
            console=self.console,
        ) as progress:
            inspect_aws_account_task = progress.add_task(
                "inspect_aws_account", total=1
            )
            num_tasks = sum(
                [await resource.get_task_count() for resource in resources]
            )
            progress.update(inspect_aws_account_task, completed=1)

        if num_tasks > 0:
            message = (
                "Provisioning infrastructure for your work pool"
                f" [blue]{work_pool_name}[/] will require: \n"
            )
            for resource in resources:
                planned_actions = await resource.get_planned_actions()
                for action in planned_actions:
                    message += f"\n\t - {action}"

            self.console.print(Panel(message))

            if self._console.is_interactive:
                if not Confirm.ask(
                    "Proceed with infrastructure provisioning?",
                    console=self._console,
                ):
                    return base_job_template
        else:
            self.console.print(
                "No additional infrastructure required for work pool"
                f" [blue]{work_pool_name}[/]"
            )
            # don't return early, we still need to update the base job template
            # provision calls will be no-ops, but update the base job template

        base_job_template_copy = deepcopy(base_job_template)
        next_steps = []
        with Progress(console=self._console, disable=num_tasks == 0) as progress:
            task = progress.add_task(
                "Provisioning Infrastructure",
                total=num_tasks,
            )
            for resource in resources:
                with console_context(progress.console):
                    await resource.provision(
                        advance=partial(progress.advance, task),
                        base_job_template=base_job_template_copy,
                    )
                next_steps.append(resource.next_steps)

        if next_steps:
            for step in next_steps:
                for item in step:
                    self._console.print(item)

        if num_tasks > 0:
            self._console.print(
                "Infrastructure successfully provisioned!", style="green"
            )

        return base_job_template_copy
    except Exception as exc:
        if hasattr(exc, "response"):
            # Catching boto3 ClientError
            response = getattr(exc, "response", {})
            error_message = get_from_dict(response, "Error.Message") or str(exc)
            raise RuntimeError(error_message) from exc
        # Catching any other exception
        raise RuntimeError(str(exc)) from exc

ExecutionRoleResource

Source code in src/prefect/infrastructure/provisioners/ecs.py
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
class ExecutionRoleResource:
    def __init__(self, execution_role_name: str = "PrefectEcsTaskExecutionRole"):
        self._iam_client = boto3.client("iam")
        self._execution_role_name = execution_role_name
        self._trust_policy_document = json.dumps(
            {
                "Version": "2012-10-17",
                "Statement": [
                    {
                        "Effect": "Allow",
                        "Principal": {"Service": "ecs-tasks.amazonaws.com"},
                        "Action": "sts:AssumeRole",
                    }
                ],
            }
        )
        self._requires_provisioning = None

    async def get_task_count(self):
        """
        Returns the number of tasks that will be executed to provision this resource.

        Returns:
            int: The number of tasks to be provisioned.
        """
        return 1 if await self.requires_provisioning() else 0

    async def requires_provisioning(self) -> bool:
        """
        Check if this resource requires provisioning.

        Returns:
            bool: True if provisioning is required, False otherwise.
        """
        if self._requires_provisioning is None:
            try:
                await anyio.to_thread.run_sync(
                    partial(
                        self._iam_client.get_role, RoleName=self._execution_role_name
                    )
                )
                self._requires_provisioning = False
            except self._iam_client.exceptions.NoSuchEntityException:
                self._requires_provisioning = True

        return self._requires_provisioning

    async def get_planned_actions(self) -> List[str]:
        """
        Returns a description of the planned actions for provisioning this resource.

        Returns:
            Optional[str]: A description of the planned actions for provisioning the resource,
                or None if provisioning is not required.
        """
        if await self.requires_provisioning():
            return [
                "Creating an IAM role assigned to ECS tasks:"
                f" [blue]{self._execution_role_name}[/]"
            ]
        return []

    async def provision(
        self,
        base_job_template: Dict[str, Any],
        advance: Callable[[], None],
    ):
        """
        Provisions an IAM role.

        Args:
            base_job_template: The base job template of the work pool to provision
                infrastructure for.
            advance: A callback function to indicate progress.
        """
        if await self.requires_provisioning():
            console = current_console.get()
            console.print("Provisioning execution role")
            response = await anyio.to_thread.run_sync(
                partial(
                    self._iam_client.create_role,
                    RoleName=self._execution_role_name,
                    Description="Role for ECS tasks to access ECR and other resources.",
                    AssumeRolePolicyDocument=self._trust_policy_document,
                )
            )
            await anyio.to_thread.run_sync(
                partial(
                    self._iam_client.attach_role_policy,
                    RoleName=self._execution_role_name,
                    PolicyArn="arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
                )
            )
            advance()
        else:
            response = await anyio.to_thread.run_sync(
                partial(self._iam_client.get_role, RoleName=self._execution_role_name)
            )

        base_job_template["variables"]["properties"]["execution_role_arn"][
            "default"
        ] = response["Role"]["Arn"]
        return response["Role"]["Arn"]

    @property
    def next_steps(self):
        return []

get_planned_actions() async

Returns a description of the planned actions for provisioning this resource.

Returns:

Type Description
List[str]

Optional[str]: A description of the planned actions for provisioning the resource, or None if provisioning is not required.

Source code in src/prefect/infrastructure/provisioners/ecs.py
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
async def get_planned_actions(self) -> List[str]:
    """
    Returns a description of the planned actions for provisioning this resource.

    Returns:
        Optional[str]: A description of the planned actions for provisioning the resource,
            or None if provisioning is not required.
    """
    if await self.requires_provisioning():
        return [
            "Creating an IAM role assigned to ECS tasks:"
            f" [blue]{self._execution_role_name}[/]"
        ]
    return []

get_task_count() async

Returns the number of tasks that will be executed to provision this resource.

Returns:

Name Type Description
int

The number of tasks to be provisioned.

Source code in src/prefect/infrastructure/provisioners/ecs.py
1003
1004
1005
1006
1007
1008
1009
1010
async def get_task_count(self):
    """
    Returns the number of tasks that will be executed to provision this resource.

    Returns:
        int: The number of tasks to be provisioned.
    """
    return 1 if await self.requires_provisioning() else 0

provision(base_job_template, advance) async

Provisions an IAM role.

Parameters:

Name Type Description Default
base_job_template Dict[str, Any]

The base job template of the work pool to provision infrastructure for.

required
advance Callable[[], None]

A callback function to indicate progress.

required
Source code in src/prefect/infrastructure/provisioners/ecs.py
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
async def provision(
    self,
    base_job_template: Dict[str, Any],
    advance: Callable[[], None],
):
    """
    Provisions an IAM role.

    Args:
        base_job_template: The base job template of the work pool to provision
            infrastructure for.
        advance: A callback function to indicate progress.
    """
    if await self.requires_provisioning():
        console = current_console.get()
        console.print("Provisioning execution role")
        response = await anyio.to_thread.run_sync(
            partial(
                self._iam_client.create_role,
                RoleName=self._execution_role_name,
                Description="Role for ECS tasks to access ECR and other resources.",
                AssumeRolePolicyDocument=self._trust_policy_document,
            )
        )
        await anyio.to_thread.run_sync(
            partial(
                self._iam_client.attach_role_policy,
                RoleName=self._execution_role_name,
                PolicyArn="arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy",
            )
        )
        advance()
    else:
        response = await anyio.to_thread.run_sync(
            partial(self._iam_client.get_role, RoleName=self._execution_role_name)
        )

    base_job_template["variables"]["properties"]["execution_role_arn"][
        "default"
    ] = response["Role"]["Arn"]
    return response["Role"]["Arn"]

requires_provisioning() async

Check if this resource requires provisioning.

Returns:

Name Type Description
bool bool

True if provisioning is required, False otherwise.

Source code in src/prefect/infrastructure/provisioners/ecs.py
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
async def requires_provisioning(self) -> bool:
    """
    Check if this resource requires provisioning.

    Returns:
        bool: True if provisioning is required, False otherwise.
    """
    if self._requires_provisioning is None:
        try:
            await anyio.to_thread.run_sync(
                partial(
                    self._iam_client.get_role, RoleName=self._execution_role_name
                )
            )
            self._requires_provisioning = False
        except self._iam_client.exceptions.NoSuchEntityException:
            self._requires_provisioning = True

    return self._requires_provisioning

IamPolicyResource

Represents an IAM policy resource for managing ECS tasks.

Parameters:

Name Type Description Default
policy_name str

The name of the IAM policy. Defaults to "prefect-ecs-policy".

required
Source code in src/prefect/infrastructure/provisioners/ecs.py
 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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
class IamPolicyResource:
    """
    Represents an IAM policy resource for managing ECS tasks.

    Args:
        policy_name: The name of the IAM policy. Defaults to "prefect-ecs-policy".
    """

    def __init__(
        self,
        policy_name: str,
    ):
        self._iam_client = boto3.client("iam")
        self._policy_name = policy_name

        self._requires_provisioning = None

    async def get_task_count(self) -> int:
        """
        Returns the number of tasks that will be executed to provision this resource.

        Returns:
            int: The number of tasks to be provisioned.
        """
        return 1 if await self.requires_provisioning() else 0

    def _get_policy_by_name(self, name):
        paginator = self._iam_client.get_paginator("list_policies")
        page_iterator = paginator.paginate(Scope="Local")

        for page in page_iterator:
            for policy in page["Policies"]:
                if policy["PolicyName"] == name:
                    return policy
        return None

    async def requires_provisioning(self) -> bool:
        """
        Check if this resource requires provisioning.

        Returns:
            bool: True if provisioning is required, False otherwise.
        """
        if self._requires_provisioning is not None:
            return self._requires_provisioning
        policy = await anyio.to_thread.run_sync(
            partial(self._get_policy_by_name, self._policy_name)
        )
        if policy is not None:
            self._requires_provisioning = False
            return False

        self._requires_provisioning = True
        return True

    async def get_planned_actions(self) -> List[str]:
        """
        Returns a description of the planned actions for provisioning this resource.

        Returns:
            Optional[str]: A description of the planned actions for provisioning the resource,
                or None if provisioning is not required.
        """
        if await self.requires_provisioning():
            return [
                "Creating and attaching an IAM policy for managing ECS tasks:"
                f" [blue]{self._policy_name}[/]"
            ]
        return []

    async def provision(
        self,
        policy_document: Dict[str, Any],
        advance: Callable[[], None],
    ):
        """
        Provisions an IAM policy.

        Args:
            advance: A callback function to indicate progress.

        Returns:
            str: The ARN (Amazon Resource Name) of the created IAM policy.
        """
        if await self.requires_provisioning():
            console = current_console.get()
            console.print("Creating IAM policy")
            policy = await anyio.to_thread.run_sync(
                partial(
                    self._iam_client.create_policy,
                    PolicyName=self._policy_name,
                    PolicyDocument=json.dumps(policy_document),
                )
            )
            policy_arn = policy["Policy"]["Arn"]
            advance()
            return policy_arn
        else:
            policy = await anyio.to_thread.run_sync(
                partial(self._get_policy_by_name, self._policy_name)
            )
            # This should never happen, but just in case
            assert policy is not None, "Could not find expected policy"
            return policy["Arn"]

    @property
    def next_steps(self):
        return []

get_planned_actions() async

Returns a description of the planned actions for provisioning this resource.

Returns:

Type Description
List[str]

Optional[str]: A description of the planned actions for provisioning the resource, or None if provisioning is not required.

Source code in src/prefect/infrastructure/provisioners/ecs.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
async def get_planned_actions(self) -> List[str]:
    """
    Returns a description of the planned actions for provisioning this resource.

    Returns:
        Optional[str]: A description of the planned actions for provisioning the resource,
            or None if provisioning is not required.
    """
    if await self.requires_provisioning():
        return [
            "Creating and attaching an IAM policy for managing ECS tasks:"
            f" [blue]{self._policy_name}[/]"
        ]
    return []

get_task_count() async

Returns the number of tasks that will be executed to provision this resource.

Returns:

Name Type Description
int int

The number of tasks to be provisioned.

Source code in src/prefect/infrastructure/provisioners/ecs.py
67
68
69
70
71
72
73
74
async def get_task_count(self) -> int:
    """
    Returns the number of tasks that will be executed to provision this resource.

    Returns:
        int: The number of tasks to be provisioned.
    """
    return 1 if await self.requires_provisioning() else 0

provision(policy_document, advance) async

Provisions an IAM policy.

Parameters:

Name Type Description Default
advance Callable[[], None]

A callback function to indicate progress.

required

Returns:

Name Type Description
str

The ARN (Amazon Resource Name) of the created IAM policy.

Source code in src/prefect/infrastructure/provisioners/ecs.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
async def provision(
    self,
    policy_document: Dict[str, Any],
    advance: Callable[[], None],
):
    """
    Provisions an IAM policy.

    Args:
        advance: A callback function to indicate progress.

    Returns:
        str: The ARN (Amazon Resource Name) of the created IAM policy.
    """
    if await self.requires_provisioning():
        console = current_console.get()
        console.print("Creating IAM policy")
        policy = await anyio.to_thread.run_sync(
            partial(
                self._iam_client.create_policy,
                PolicyName=self._policy_name,
                PolicyDocument=json.dumps(policy_document),
            )
        )
        policy_arn = policy["Policy"]["Arn"]
        advance()
        return policy_arn
    else:
        policy = await anyio.to_thread.run_sync(
            partial(self._get_policy_by_name, self._policy_name)
        )
        # This should never happen, but just in case
        assert policy is not None, "Could not find expected policy"
        return policy["Arn"]

requires_provisioning() async

Check if this resource requires provisioning.

Returns:

Name Type Description
bool bool

True if provisioning is required, False otherwise.

Source code in src/prefect/infrastructure/provisioners/ecs.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
async def requires_provisioning(self) -> bool:
    """
    Check if this resource requires provisioning.

    Returns:
        bool: True if provisioning is required, False otherwise.
    """
    if self._requires_provisioning is not None:
        return self._requires_provisioning
    policy = await anyio.to_thread.run_sync(
        partial(self._get_policy_by_name, self._policy_name)
    )
    if policy is not None:
        self._requires_provisioning = False
        return False

    self._requires_provisioning = True
    return True

IamUserResource

Represents an IAM user resource for managing ECS tasks.

Parameters:

Name Type Description Default
user_name str

The desired name of the IAM user.

required
Source code in src/prefect/infrastructure/provisioners/ecs.py
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
class IamUserResource:
    """
    Represents an IAM user resource for managing ECS tasks.

    Args:
        user_name: The desired name of the IAM user.
    """

    def __init__(self, user_name: str):
        self._iam_client = boto3.client("iam")
        self._user_name = user_name
        self._requires_provisioning = None

    async def get_task_count(self) -> int:
        """
        Returns the number of tasks that will be executed to provision this resource.

        Returns:
            int: The number of tasks to be provisioned.
        """
        return 1 if await self.requires_provisioning() else 0

    async def requires_provisioning(self) -> bool:
        """
        Check if this resource requires provisioning.

        Returns:
            bool: True if provisioning is required, False otherwise.
        """
        if self._requires_provisioning is None:
            try:
                await anyio.to_thread.run_sync(
                    partial(self._iam_client.get_user, UserName=self._user_name)
                )
                self._requires_provisioning = False
            except self._iam_client.exceptions.NoSuchEntityException:
                self._requires_provisioning = True

        return self._requires_provisioning

    async def get_planned_actions(self) -> List[str]:
        """
        Returns a description of the planned actions for provisioning this resource.

        Returns:
            Optional[str]: A description of the planned actions for provisioning the resource,
                or None if provisioning is not required.
        """
        if await self.requires_provisioning():
            return [
                "Creating an IAM user for managing ECS tasks:"
                f" [blue]{self._user_name}[/]"
            ]
        return []

    async def provision(
        self,
        advance: Callable[[], None],
    ):
        """
        Provisions an IAM user.

        Args:
            advance: A callback function to indicate progress.
        """
        console = current_console.get()
        if await self.requires_provisioning():
            console.print("Provisioning IAM user")
            await anyio.to_thread.run_sync(
                partial(self._iam_client.create_user, UserName=self._user_name)
            )
            advance()

    @property
    def next_steps(self):
        return []

get_planned_actions() async

Returns a description of the planned actions for provisioning this resource.

Returns:

Type Description
List[str]

Optional[str]: A description of the planned actions for provisioning the resource, or None if provisioning is not required.

Source code in src/prefect/infrastructure/provisioners/ecs.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
async def get_planned_actions(self) -> List[str]:
    """
    Returns a description of the planned actions for provisioning this resource.

    Returns:
        Optional[str]: A description of the planned actions for provisioning the resource,
            or None if provisioning is not required.
    """
    if await self.requires_provisioning():
        return [
            "Creating an IAM user for managing ECS tasks:"
            f" [blue]{self._user_name}[/]"
        ]
    return []

get_task_count() async

Returns the number of tasks that will be executed to provision this resource.

Returns:

Name Type Description
int int

The number of tasks to be provisioned.

Source code in src/prefect/infrastructure/provisioners/ecs.py
173
174
175
176
177
178
179
180
async def get_task_count(self) -> int:
    """
    Returns the number of tasks that will be executed to provision this resource.

    Returns:
        int: The number of tasks to be provisioned.
    """
    return 1 if await self.requires_provisioning() else 0

provision(advance) async

Provisions an IAM user.

Parameters:

Name Type Description Default
advance Callable[[], None]

A callback function to indicate progress.

required
Source code in src/prefect/infrastructure/provisioners/ecs.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
async def provision(
    self,
    advance: Callable[[], None],
):
    """
    Provisions an IAM user.

    Args:
        advance: A callback function to indicate progress.
    """
    console = current_console.get()
    if await self.requires_provisioning():
        console.print("Provisioning IAM user")
        await anyio.to_thread.run_sync(
            partial(self._iam_client.create_user, UserName=self._user_name)
        )
        advance()

requires_provisioning() async

Check if this resource requires provisioning.

Returns:

Name Type Description
bool bool

True if provisioning is required, False otherwise.

Source code in src/prefect/infrastructure/provisioners/ecs.py
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
async def requires_provisioning(self) -> bool:
    """
    Check if this resource requires provisioning.

    Returns:
        bool: True if provisioning is required, False otherwise.
    """
    if self._requires_provisioning is None:
        try:
            await anyio.to_thread.run_sync(
                partial(self._iam_client.get_user, UserName=self._user_name)
            )
            self._requires_provisioning = False
        except self._iam_client.exceptions.NoSuchEntityException:
            self._requires_provisioning = True

    return self._requires_provisioning

VpcResource

Source code in src/prefect/infrastructure/provisioners/ecs.py
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
class VpcResource:
    def __init__(
        self,
        vpc_name: str = "prefect-ecs-vpc",
        ecs_security_group_name: str = "prefect-ecs-security-group",
    ):
        self._ec2_client = boto3.client("ec2")
        self._ec2_resource = boto3.resource("ec2")
        self._vpc_name = vpc_name
        self._requires_provisioning = None
        self._ecs_security_group_name = ecs_security_group_name

    async def get_task_count(self):
        """
        Returns the number of tasks that will be executed to provision this resource.

        Returns:
            int: The number of tasks to be provisioned.
        """
        return 4 if await self.requires_provisioning() else 0

    async def _default_vpc_exists(self):
        response = await anyio.to_thread.run_sync(self._ec2_client.describe_vpcs)
        default_vpc = next(
            (
                vpc
                for vpc in response["Vpcs"]
                if vpc["IsDefault"] and vpc["State"] == "available"
            ),
            None,
        )
        return default_vpc is not None

    async def _get_prefect_created_vpc(self):
        vpcs = await anyio.to_thread.run_sync(
            partial(
                self._ec2_resource.vpcs.filter,
                Filters=[{"Name": "tag:Name", "Values": [self._vpc_name]}],
            )
        )
        return next(iter(vpcs), None)

    async def _get_existing_vpc_cidrs(self):
        response = await anyio.to_thread.run_sync(self._ec2_client.describe_vpcs)
        return [vpc["CidrBlock"] for vpc in response["Vpcs"]]

    async def _find_non_overlapping_cidr(self, default_cidr="172.31.0.0/16"):
        """Find a non-overlapping CIDR block"""
        response = await anyio.to_thread.run_sync(self._ec2_client.describe_vpcs)
        existing_cidrs = [vpc["CidrBlock"] for vpc in response["Vpcs"]]

        base_ip = ipaddress.ip_network(default_cidr)
        new_cidr = base_ip
        while True:
            if any(
                new_cidr.overlaps(ipaddress.ip_network(cidr)) for cidr in existing_cidrs
            ):
                # Increase the network address by the size of the network
                new_network_address = int(new_cidr.network_address) + 2 ** (
                    32 - new_cidr.prefixlen
                )
                try:
                    new_cidr = ipaddress.ip_network(
                        f"{ipaddress.IPv4Address(new_network_address)}/{new_cidr.prefixlen}"
                    )
                except ValueError:
                    raise Exception(
                        "Unable to find a non-overlapping CIDR block in the default"
                        " range"
                    )
            else:
                return str(new_cidr)

    async def requires_provisioning(self) -> bool:
        """
        Check if this resource requires provisioning.

        Returns:
            bool: True if provisioning is required, False otherwise.
        """
        if self._requires_provisioning is not None:
            return self._requires_provisioning

        if await self._default_vpc_exists():
            self._requires_provisioning = False
            return False

        if await self._get_prefect_created_vpc() is not None:
            self._requires_provisioning = False
            return False

        self._requires_provisioning = True
        return True

    async def get_planned_actions(self) -> List[str]:
        """
        Returns a description of the planned actions for provisioning this resource.

        Returns:
            Optional[str]: A description of the planned actions for provisioning the resource,
                or None if provisioning is not required.
        """
        if await self.requires_provisioning():
            new_vpc_cidr = await self._find_non_overlapping_cidr()
            return [
                f"Creating a VPC with CIDR [blue]{new_vpc_cidr}[/] for running"
                f" ECS tasks: [blue]{self._vpc_name}[/]"
            ]
        return []

    async def provision(
        self,
        base_job_template: Dict[str, Any],
        advance: Callable[[], None],
    ):
        """
        Provisions a VPC.

        Chooses a CIDR block to avoid conflicting with any existing VPCs. Will update
        the `vpc_id` variable in the job template to reference the VPC.

        Args:
            base_job_template: The base job template of the work pool to provision
                infrastructure for.
            advance: A callback function to indicate progress.
        """
        if await self.requires_provisioning():
            console = current_console.get()
            console.print("Provisioning VPC")
            new_vpc_cidr = await self._find_non_overlapping_cidr()
            vpc = await anyio.to_thread.run_sync(
                partial(self._ec2_resource.create_vpc, CidrBlock=new_vpc_cidr)
            )
            await anyio.to_thread.run_sync(vpc.wait_until_available)
            await anyio.to_thread.run_sync(
                partial(
                    vpc.create_tags,
                    Resources=[vpc.id],
                    Tags=[
                        {
                            "Key": "Name",
                            "Value": self._vpc_name,
                        },
                    ],
                )
            )
            advance()

            console.print("Creating internet gateway")
            internet_gateway = await anyio.to_thread.run_sync(
                self._ec2_resource.create_internet_gateway
            )
            await anyio.to_thread.run_sync(
                partial(
                    vpc.attach_internet_gateway, InternetGatewayId=internet_gateway.id
                )
            )
            advance()

            console.print("Setting up subnets")
            vpc_network = ipaddress.ip_network(new_vpc_cidr)
            subnet_cidrs = list(
                vpc_network.subnets(new_prefix=vpc_network.prefixlen + 2)
            )

            # Create subnets
            azs = (
                await anyio.to_thread.run_sync(
                    self._ec2_client.describe_availability_zones
                )
            )["AvailabilityZones"]
            zones = [az["ZoneName"] for az in azs]
            subnets = []
            for i, subnet_cidr in enumerate(subnet_cidrs[0:3]):
                subnets.append(
                    await anyio.to_thread.run_sync(
                        partial(
                            vpc.create_subnet,
                            CidrBlock=str(subnet_cidr),
                            AvailabilityZone=zones[i],
                        )
                    )
                )

            # Create a Route Table for the public subnet and add a route to the Internet Gateway
            public_route_table = await anyio.to_thread.run_sync(vpc.create_route_table)
            await anyio.to_thread.run_sync(
                partial(
                    public_route_table.create_route,
                    DestinationCidrBlock="0.0.0.0/0",
                    GatewayId=internet_gateway.id,
                )
            )
            await anyio.to_thread.run_sync(
                partial(
                    public_route_table.associate_with_subnet, SubnetId=subnets[0].id
                )
            )
            await anyio.to_thread.run_sync(
                partial(
                    public_route_table.associate_with_subnet, SubnetId=subnets[1].id
                )
            )
            await anyio.to_thread.run_sync(
                partial(
                    public_route_table.associate_with_subnet, SubnetId=subnets[2].id
                )
            )
            advance()

            console.print("Setting up security group")
            # Create a security group to block all inbound traffic
            await anyio.to_thread.run_sync(
                partial(
                    self._ec2_resource.create_security_group,
                    GroupName=self._ecs_security_group_name,
                    Description=(
                        "Block all inbound traffic and allow all outbound traffic"
                    ),
                    VpcId=vpc.id,
                )
            )
            advance()
        else:
            vpc = await self._get_prefect_created_vpc()

        if vpc is not None:
            base_job_template["variables"]["properties"]["vpc_id"]["default"] = str(
                vpc.id
            )

    @property
    def next_steps(self):
        return []

get_planned_actions() async

Returns a description of the planned actions for provisioning this resource.

Returns:

Type Description
List[str]

Optional[str]: A description of the planned actions for provisioning the resource, or None if provisioning is not required.

Source code in src/prefect/infrastructure/provisioners/ecs.py
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
async def get_planned_actions(self) -> List[str]:
    """
    Returns a description of the planned actions for provisioning this resource.

    Returns:
        Optional[str]: A description of the planned actions for provisioning the resource,
            or None if provisioning is not required.
    """
    if await self.requires_provisioning():
        new_vpc_cidr = await self._find_non_overlapping_cidr()
        return [
            f"Creating a VPC with CIDR [blue]{new_vpc_cidr}[/] for running"
            f" ECS tasks: [blue]{self._vpc_name}[/]"
        ]
    return []

get_task_count() async

Returns the number of tasks that will be executed to provision this resource.

Returns:

Name Type Description
int

The number of tasks to be provisioned.

Source code in src/prefect/infrastructure/provisioners/ecs.py
611
612
613
614
615
616
617
618
async def get_task_count(self):
    """
    Returns the number of tasks that will be executed to provision this resource.

    Returns:
        int: The number of tasks to be provisioned.
    """
    return 4 if await self.requires_provisioning() else 0

provision(base_job_template, advance) async

Provisions a VPC.

Chooses a CIDR block to avoid conflicting with any existing VPCs. Will update the vpc_id variable in the job template to reference the VPC.

Parameters:

Name Type Description Default
base_job_template Dict[str, Any]

The base job template of the work pool to provision infrastructure for.

required
advance Callable[[], None]

A callback function to indicate progress.

required
Source code in src/prefect/infrastructure/provisioners/ecs.py
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
async def provision(
    self,
    base_job_template: Dict[str, Any],
    advance: Callable[[], None],
):
    """
    Provisions a VPC.

    Chooses a CIDR block to avoid conflicting with any existing VPCs. Will update
    the `vpc_id` variable in the job template to reference the VPC.

    Args:
        base_job_template: The base job template of the work pool to provision
            infrastructure for.
        advance: A callback function to indicate progress.
    """
    if await self.requires_provisioning():
        console = current_console.get()
        console.print("Provisioning VPC")
        new_vpc_cidr = await self._find_non_overlapping_cidr()
        vpc = await anyio.to_thread.run_sync(
            partial(self._ec2_resource.create_vpc, CidrBlock=new_vpc_cidr)
        )
        await anyio.to_thread.run_sync(vpc.wait_until_available)
        await anyio.to_thread.run_sync(
            partial(
                vpc.create_tags,
                Resources=[vpc.id],
                Tags=[
                    {
                        "Key": "Name",
                        "Value": self._vpc_name,
                    },
                ],
            )
        )
        advance()

        console.print("Creating internet gateway")
        internet_gateway = await anyio.to_thread.run_sync(
            self._ec2_resource.create_internet_gateway
        )
        await anyio.to_thread.run_sync(
            partial(
                vpc.attach_internet_gateway, InternetGatewayId=internet_gateway.id
            )
        )
        advance()

        console.print("Setting up subnets")
        vpc_network = ipaddress.ip_network(new_vpc_cidr)
        subnet_cidrs = list(
            vpc_network.subnets(new_prefix=vpc_network.prefixlen + 2)
        )

        # Create subnets
        azs = (
            await anyio.to_thread.run_sync(
                self._ec2_client.describe_availability_zones
            )
        )["AvailabilityZones"]
        zones = [az["ZoneName"] for az in azs]
        subnets = []
        for i, subnet_cidr in enumerate(subnet_cidrs[0:3]):
            subnets.append(
                await anyio.to_thread.run_sync(
                    partial(
                        vpc.create_subnet,
                        CidrBlock=str(subnet_cidr),
                        AvailabilityZone=zones[i],
                    )
                )
            )

        # Create a Route Table for the public subnet and add a route to the Internet Gateway
        public_route_table = await anyio.to_thread.run_sync(vpc.create_route_table)
        await anyio.to_thread.run_sync(
            partial(
                public_route_table.create_route,
                DestinationCidrBlock="0.0.0.0/0",
                GatewayId=internet_gateway.id,
            )
        )
        await anyio.to_thread.run_sync(
            partial(
                public_route_table.associate_with_subnet, SubnetId=subnets[0].id
            )
        )
        await anyio.to_thread.run_sync(
            partial(
                public_route_table.associate_with_subnet, SubnetId=subnets[1].id
            )
        )
        await anyio.to_thread.run_sync(
            partial(
                public_route_table.associate_with_subnet, SubnetId=subnets[2].id
            )
        )
        advance()

        console.print("Setting up security group")
        # Create a security group to block all inbound traffic
        await anyio.to_thread.run_sync(
            partial(
                self._ec2_resource.create_security_group,
                GroupName=self._ecs_security_group_name,
                Description=(
                    "Block all inbound traffic and allow all outbound traffic"
                ),
                VpcId=vpc.id,
            )
        )
        advance()
    else:
        vpc = await self._get_prefect_created_vpc()

    if vpc is not None:
        base_job_template["variables"]["properties"]["vpc_id"]["default"] = str(
            vpc.id
        )

requires_provisioning() async

Check if this resource requires provisioning.

Returns:

Name Type Description
bool bool

True if provisioning is required, False otherwise.

Source code in src/prefect/infrastructure/provisioners/ecs.py
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
async def requires_provisioning(self) -> bool:
    """
    Check if this resource requires provisioning.

    Returns:
        bool: True if provisioning is required, False otherwise.
    """
    if self._requires_provisioning is not None:
        return self._requires_provisioning

    if await self._default_vpc_exists():
        self._requires_provisioning = False
        return False

    if await self._get_prefect_created_vpc() is not None:
        self._requires_provisioning = False
        return False

    self._requires_provisioning = True
    return True