|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -from datetime import timezone, datetime |
4 | 3 | from enum import Enum |
5 | 4 | import logging |
6 | 5 | import os |
|
27 | 26 | from e3.aws.troposphere.iam.policy_document import PolicyDocument |
28 | 27 | from e3.aws.troposphere.iam.policy_statement import PolicyStatement |
29 | 28 | from e3.aws.troposphere.iam.role import Role |
30 | | -from e3.aws.util.ecr import build_and_push_image |
31 | 29 | from e3.aws.util import color_diff, modified_diff_lines |
32 | 30 |
|
33 | 31 | if TYPE_CHECKING: |
34 | 32 | from typing import Any, Callable |
35 | 33 | from troposphere import AWSObject |
36 | 34 | import botocore.client |
37 | 35 | from e3.aws.troposphere import Stack |
38 | | - from python_on_whales import DockerClient |
39 | 36 |
|
40 | 37 | logger = logging.getLogger("e3.aws.troposphere.awslambda") |
41 | 38 |
|
@@ -600,102 +597,6 @@ def invoke_permission( |
600 | 597 | return awslambda.Permission(name_to_id(target.name + name_suffix), **params) |
601 | 598 |
|
602 | 599 |
|
603 | | -class DockerFunction(Function): |
604 | | - """Lambda using a Docker image.""" |
605 | | - |
606 | | - def __init__( |
607 | | - self, |
608 | | - name: str, |
609 | | - description: str, |
610 | | - role: str | GetAtt | Role, |
611 | | - source_dir: str, |
612 | | - repository_name: str, |
613 | | - image_tag: str, |
614 | | - timeout: int = 3, |
615 | | - architecture: Architecture | None = None, |
616 | | - memory_size: int | None = None, |
617 | | - logs_retention_in_days: int | None = 731, |
618 | | - environment: dict[str, str] | None = None, |
619 | | - logging_config: awslambda.LoggingConfig | None = None, |
620 | | - dl_config: awslambda.DeadLetterConfig | None = None, |
621 | | - docker_client: DockerClient | None = None, |
622 | | - **build_args: Any, |
623 | | - ): |
624 | | - """Initialize an AWS lambda function using a Docker image. |
625 | | -
|
626 | | - :param name: function name |
627 | | - :param description: a description of the function |
628 | | - :param role: role to be asssumed during lambda execution |
629 | | - :param source_dir: directory containing Dockerfile and dependencies |
630 | | - :param repository_name: ECR repository name |
631 | | - :param image_tag: docker image version |
632 | | - :param timeout: maximum execution time (default: 3s) |
633 | | - :param architecture: x86_64 or arm64. (default: x86_64) |
634 | | - :param memory_size: the amount of memory available to the function at |
635 | | - runtime. The value can be any multiple of 1 MB. |
636 | | - :param logs_retention_in_days: The number of days to retain the log |
637 | | - events in the lambda log group |
638 | | - :param environment: Environment variables that are accessible from |
639 | | - function code during execution |
640 | | - :param logging_config: The function's Amazon CloudWatch Logs settings |
641 | | - :param dl_config: The dead letter config that specifies the topic or |
642 | | - queue where lambda sends asynchronous events when they fail processing |
643 | | - :param docker_client: Docker client to use for building and pushing. |
644 | | - This is here in case the user wants to customize the Docker client, |
645 | | - for example to use podman. |
646 | | - :param build_args: args to pass to docker build |
647 | | - """ |
648 | | - super().__init__( |
649 | | - name=name, |
650 | | - description=description, |
651 | | - role=role, |
652 | | - timeout=timeout, |
653 | | - architecture=architecture, |
654 | | - memory_size=memory_size, |
655 | | - logs_retention_in_days=logs_retention_in_days, |
656 | | - environment=environment, |
657 | | - logging_config=logging_config, |
658 | | - dl_config=dl_config, |
659 | | - ) |
660 | | - self.source_dir: str = source_dir |
661 | | - self.repository_name: str = repository_name |
662 | | - timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d-%H-%M-%S-%f") |
663 | | - self.image_tag: str = f"{image_tag}-{timestamp}" |
664 | | - self.image_uri: str | None = None |
665 | | - self.docker_client = docker_client |
666 | | - self.build_args = build_args |
667 | | - if "platforms" not in self.build_args: |
668 | | - match self.architecture: |
669 | | - case Architecture.ARM64: |
670 | | - self.build_args["platforms"] = ["linux/arm64"] |
671 | | - case Architecture.X86_64 | None: |
672 | | - self.build_args["platforms"] = ["linux/amd64"] |
673 | | - case _: |
674 | | - raise UnknownPlatform(self.architecture) |
675 | | - |
676 | | - def resources(self, stack: Stack) -> list[AWSObject]: |
677 | | - """Compute AWS resources for the construct. |
678 | | -
|
679 | | - Build and push the Docker image to ECR repository. |
680 | | - Only push ECR image if stack is to be deployed. |
681 | | - """ |
682 | | - if stack.dry_run: |
683 | | - self.image_uri = "<dry_run_image_uri>" |
684 | | - else: |
685 | | - assert stack.deploy_session is not None |
686 | | - self.image_uri = build_and_push_image( |
687 | | - self.source_dir, |
688 | | - self.repository_name, |
689 | | - self.image_tag, |
690 | | - stack.deploy_session, |
691 | | - push=True, |
692 | | - docker_client=self.docker_client, |
693 | | - **self.build_args, |
694 | | - ) |
695 | | - |
696 | | - return self.lambda_resources(image_uri=self.image_uri) |
697 | | - |
698 | | - |
699 | 600 | class PyFunction(Function): |
700 | 601 | """Lambda with a Python runtime.""" |
701 | 602 |
|
|
0 commit comments