Skip to content

Commit 1031f42

Browse files
committed
Merge branch 'morosi-fix' into 'master'
Move DockerFunction to own module See merge request it/e3-aws!109
2 parents 587c52c + 716fca1 commit 1031f42

File tree

3 files changed

+114
-100
lines changed

3 files changed

+114
-100
lines changed

src/e3/aws/troposphere/awslambda/__init__.py

Lines changed: 0 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from datetime import timezone, datetime
43
from enum import Enum
54
import logging
65
import os
@@ -27,15 +26,13 @@
2726
from e3.aws.troposphere.iam.policy_document import PolicyDocument
2827
from e3.aws.troposphere.iam.policy_statement import PolicyStatement
2928
from e3.aws.troposphere.iam.role import Role
30-
from e3.aws.util.ecr import build_and_push_image
3129
from e3.aws.util import color_diff, modified_diff_lines
3230

3331
if TYPE_CHECKING:
3432
from typing import Any, Callable
3533
from troposphere import AWSObject
3634
import botocore.client
3735
from e3.aws.troposphere import Stack
38-
from python_on_whales import DockerClient
3936

4037
logger = logging.getLogger("e3.aws.troposphere.awslambda")
4138

@@ -600,102 +597,6 @@ def invoke_permission(
600597
return awslambda.Permission(name_to_id(target.name + name_suffix), **params)
601598

602599

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-
699600
class PyFunction(Function):
700601
"""Lambda with a Python runtime."""
701602

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from __future__ import annotations
2+
from datetime import timezone, datetime
3+
import logging
4+
from typing import TYPE_CHECKING
5+
from troposphere import awslambda, GetAtt
6+
7+
from e3.aws.troposphere.iam.role import Role
8+
from e3.aws.util.ecr import build_and_push_image
9+
from e3.aws.troposphere.awslambda import Function, Architecture, UnknownPlatform
10+
11+
if TYPE_CHECKING:
12+
from typing import Any
13+
from troposphere import AWSObject
14+
from e3.aws.troposphere import Stack
15+
from python_on_whales import DockerClient
16+
17+
logger = logging.getLogger(__name__)
18+
19+
20+
class DockerFunction(Function):
21+
"""Lambda using a Docker image."""
22+
23+
def __init__(
24+
self,
25+
name: str,
26+
description: str,
27+
role: str | GetAtt | Role,
28+
source_dir: str,
29+
repository_name: str,
30+
image_tag: str,
31+
timeout: int = 3,
32+
architecture: Architecture | None = None,
33+
memory_size: int | None = None,
34+
logs_retention_in_days: int | None = 731,
35+
environment: dict[str, str] | None = None,
36+
logging_config: awslambda.LoggingConfig | None = None,
37+
dl_config: awslambda.DeadLetterConfig | None = None,
38+
docker_client: DockerClient | None = None,
39+
**build_args: Any,
40+
):
41+
"""Initialize an AWS lambda function using a Docker image.
42+
43+
:param name: function name
44+
:param description: a description of the function
45+
:param role: role to be asssumed during lambda execution
46+
:param source_dir: directory containing Dockerfile and dependencies
47+
:param repository_name: ECR repository name
48+
:param image_tag: docker image version
49+
:param timeout: maximum execution time (default: 3s)
50+
:param architecture: x86_64 or arm64. (default: x86_64)
51+
:param memory_size: the amount of memory available to the function at
52+
runtime. The value can be any multiple of 1 MB.
53+
:param logs_retention_in_days: The number of days to retain the log
54+
events in the lambda log group
55+
:param environment: Environment variables that are accessible from
56+
function code during execution
57+
:param logging_config: The function's Amazon CloudWatch Logs settings
58+
:param dl_config: The dead letter config that specifies the topic or
59+
queue where lambda sends asynchronous events when they fail processing
60+
:param docker_client: Docker client to use for building and pushing.
61+
This is here in case the user wants to customize the Docker client,
62+
for example to use podman.
63+
:param build_args: args to pass to docker build
64+
"""
65+
super().__init__(
66+
name=name,
67+
description=description,
68+
role=role,
69+
timeout=timeout,
70+
architecture=architecture,
71+
memory_size=memory_size,
72+
logs_retention_in_days=logs_retention_in_days,
73+
environment=environment,
74+
logging_config=logging_config,
75+
dl_config=dl_config,
76+
)
77+
self.source_dir: str = source_dir
78+
self.repository_name: str = repository_name
79+
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%d-%H-%M-%S-%f")
80+
self.image_tag: str = f"{image_tag}-{timestamp}"
81+
self.image_uri: str | None = None
82+
self.docker_client = docker_client
83+
self.build_args = build_args
84+
if "platforms" not in self.build_args:
85+
match self.architecture:
86+
case Architecture.ARM64:
87+
self.build_args["platforms"] = ["linux/arm64"]
88+
case Architecture.X86_64 | None:
89+
self.build_args["platforms"] = ["linux/amd64"]
90+
case _:
91+
raise UnknownPlatform(self.architecture)
92+
93+
def resources(self, stack: Stack) -> list[AWSObject]:
94+
"""Compute AWS resources for the construct.
95+
96+
Build and push the Docker image to ECR repository.
97+
Only push ECR image if stack is to be deployed.
98+
"""
99+
if stack.dry_run:
100+
self.image_uri = "<dry_run_image_uri>"
101+
else:
102+
assert stack.deploy_session is not None
103+
self.image_uri = build_and_push_image(
104+
self.source_dir,
105+
self.repository_name,
106+
self.image_tag,
107+
stack.deploy_session,
108+
push=True,
109+
docker_client=self.docker_client,
110+
**self.build_args,
111+
)
112+
113+
return self.lambda_resources(image_uri=self.image_uri)

tests/tests_e3_aws/troposphere/awslambda/awslambda_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
Function,
2525
PyFunction,
2626
Py38Function,
27-
DockerFunction,
2827
Alias,
2928
Version,
3029
AutoVersion,
@@ -33,6 +32,7 @@
3332
BlueGreenAliasConfiguration,
3433
Architecture,
3534
)
35+
from e3.aws.troposphere.awslambda.docker import DockerFunction
3636
from e3.aws.troposphere.awslambda.flask_apigateway_wrapper import FlaskLambdaHandler
3737
from e3.aws.troposphere.sqs import Queue
3838
from e3.aws.util.ecr import get_ecr_credentials

0 commit comments

Comments
 (0)