Skip to content

Commit

Permalink
make it pass
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Nov 9, 2024
1 parent f53494d commit bde1f21
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
55 changes: 27 additions & 28 deletions infrastructure/aws/cdk/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
import os
from typing import Any, Dict, List, Optional

from aws_cdk import App, CfnOutput, Duration, IgnoreMode, Stack, Tags
from aws_cdk import App, CfnOutput, Duration, Stack, Tags
from aws_cdk import aws_apigatewayv2 as apigw
from aws_cdk import aws_iam as iam
from aws_cdk import aws_lambda
from aws_cdk import aws_logs as logs
from aws_cdk.aws_apigatewayv2_integrations import HttpLambdaIntegration
from aws_cdk.aws_ecr_assets import Platform
from config import StackSettings
from constructs import Construct

Expand Down Expand Up @@ -51,42 +50,42 @@ def __init__(
environment = environment or {}

# LAMBDA + ECR
lambda_function = aws_lambda.DockerImageFunction(
self,
f"{id}-lambda",
code=aws_lambda.DockerImageCode.from_image_asset(
os.path.abspath(context_dir),
file="infrastructure/aws/lambda/ecr.Dockerfile",
platform=Platform.LINUX_AMD64,
ignore_mode=IgnoreMode.DOCKER,
),
architecture=aws_lambda.Architecture.X86_64,
memory_size=memory,
reserved_concurrent_executions=concurrent,
timeout=Duration.seconds(timeout),
environment={**DEFAULT_ENV, **environment},
log_retention=logs.RetentionDays.ONE_WEEK,
)

# PURE Lambda
# NOTE: Package size is too big for now so we can't use simple lambda package
# lambda_function = aws_lambda.Function(
# lambda_function = aws_lambda.DockerImageFunction(
# self,
# f"{id}-lambda",
# runtime=aws_lambda.Runtime.PYTHON_3_11,
# code=aws_lambda.Code.from_docker_build(
# path=os.path.abspath(context_dir),
# file="infrastructure/aws/lambda/lambda.Dockerfile",
# platform="linux/amd64",
# code=aws_lambda.DockerImageCode.from_image_asset(
# os.path.abspath(context_dir),
# file="infrastructure/aws/lambda/ecr.Dockerfile",
# platform=Platform.LINUX_AMD64,
# ignore_mode=IgnoreMode.DOCKER,
# ),
# handler="handler.handler",
# architecture=aws_lambda.Architecture.X86_64,
# memory_size=memory,
# reserved_concurrent_executions=concurrent,
# timeout=Duration.seconds(timeout),
# environment={**DEFAULT_ENV, **environment},
# log_retention=logs.RetentionDays.ONE_WEEK,
# )

# PURE Lambda
# NOTE: Package size is too big for now so we can't use simple lambda package
lambda_function = aws_lambda.Function(
self,
f"{id}-lambda",
runtime=aws_lambda.Runtime.PYTHON_3_12,
code=aws_lambda.Code.from_docker_build(
path=os.path.abspath(context_dir),
file="infrastructure/aws/lambda/lambda.Dockerfile",
platform="linux/amd64",
),
handler="handler.handler",
memory_size=memory,
reserved_concurrent_executions=concurrent,
timeout=Duration.seconds(timeout),
environment={**DEFAULT_ENV, **environment},
log_retention=logs.RetentionDays.ONE_WEEK,
)

for perm in permissions:
lambda_function.add_to_role_policy(perm)

Expand Down
8 changes: 4 additions & 4 deletions infrastructure/aws/lambda/ecr.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
ARG PYTHON_VERSION=3.11
ARG PYTHON_VERSION=3.12

FROM public.ecr.aws/lambda/python:${PYTHON_VERSION} AS builder

WORKDIR /tmp

# Install system dependencies to compile (numexpr)
RUN yum install -y gcc-c++
RUN dnf install -y gcc-c++

COPY runtimes/ /tmp/runtimes

Expand All @@ -18,9 +18,9 @@ ENV PYTHONUSERBASE=/assets
# we have to force using old package version that seems `almost` compatible with Lambda env botocore
# https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
RUN pip install --upgrade pip
RUN pip install /tmp/runtimes "mangum>=0.10.0" --user

This comment has been minimized.

Copy link
@vincentsarago

vincentsarago Nov 9, 2024

Author Member

#3

RUN pip install /tmp/runtimes "mangum>=0.10.0" "rasterio==1.3.9" "numpy~=1.0" --user

RUN mv ${PYTHONUSERBASE}/lib/python3.11/site-packages/* ${PYTHONUSERBASE}/
RUN mv ${PYTHONUSERBASE}/lib/python3.12/site-packages/* ${PYTHONUSERBASE}/
RUN rm -rf ${PYTHONUSERBASE}/lib

# Reduce package size and remove useless files
Expand Down
8 changes: 3 additions & 5 deletions infrastructure/aws/lambda/lambda.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
ARG PYTHON_VERSION=3.11
ARG PYTHON_VERSION=3.12

FROM public.ecr.aws/lambda/python:${PYTHON_VERSION}

WORKDIR /tmp

# Install system dependencies to compile (numexpr)
RUN yum install -y gcc gcc-c++ && \
yum clean all && \
rm -rf /var/cache/yum /var/lib/yum/history
RUN dnf install -y gcc gcc-c++

COPY runtimes/ /tmp/runtimes

Expand All @@ -18,7 +16,7 @@ COPY runtimes/ /tmp/runtimes
# we have to force using old package version that seems `almost` compatible with Lambda env botocore
# https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html
RUN pip install --upgrade pip
RUN pip install /tmp/runtimes "mangum>=0.10.0" "aiobotocore==2.13.3" -t /asset --no-binary pydantic
RUN pip install /tmp/runtimes "mangum>=0.10.0" "aiobotocore==2.13.3" "rasterio==1.3.9" "numpy~=1.0" -t /asset --no-binary pydantic,xarray,numpy,pandas

# Reduce package size and remove useless files
RUN cd /asset && find . -type f -name '*.pyc' | while read f; do n=$(echo $f | sed 's/__pycache__\///' | sed 's/.cpython-[0-9]*//'); cp $f $n; done;
Expand Down

0 comments on commit bde1f21

Please sign in to comment.