Вот структура моего проекта, полученная по дереву -L 2:
Код: Выделить всё
.
├── Dockerfile
├── envs
│ ├── dev.json
│ └── prd.json
├── event.json
├── requirements.txt
├── src
│ ├── app.py
│ └── utils.py
└── template.yml
Код: Выделить всё
# Specify the base image
FROM public.ecr.aws/lambda/python:3.11 AS build-stage
# Install build dependencies for psutil (pyproject.toml)
RUN yum install -y gcc python3-devel gcc-c++
# Set the working directory inside the container
WORKDIR /app
# Copy requirements.txt from the root of your project
COPY requirements.txt .
# Install Python dependencies without plotly to a temporary directory
RUN pip3 install --no-cache-dir --upgrade pip && \
pip3 install --no-cache-dir --upgrade -r requirements.txt --target /tmp/python-deps
# -------------------- Stage 2 --------------------
# Build the final stage
FROM public.ecr.aws/lambda/python:3.11 AS final-stage
# Remove unnecessary dependencies (optional, but recommended)
RUN yum remove -y gcc python3-devel gcc-c++ && \
yum clean all
# Copy the Python dependencies from the build-stage
COPY --from=build-stage /tmp/python-deps /app
# Copy source code
COPY src /app/src
# Argument to specify environment
ARG ENVIRONMENT=dev
ENV ENVIRONMENT=${ENVIRONMENT}
# Copy the appropriate environment file based on the argument
COPY envs/${ENVIRONMENT}.json /app/.env
# Set the entry point for the Lambda function
CMD [ "app.lambda_handler" ]
Код: Выделить всё
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: AWS SAM template for testing Docker-based Lambda function
Parameters:
Environment:
Type: String
Description: The environment (dev or prd)
Default: dev
AllowedValues: [dev, prd]
Globals:
Function:
Timeout: 180
MemorySize: 256
Resources:
DockerDemo:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
PackageType: Image
ImageConfig:
Command: [app.lambda_handler]
Environment:
Variables:
ENVIRONMENT: !Ref Environment
ImageUri: !Sub docker-demo:${Environment}
Metadata:
DockerTag: !Ref Environment
DockerContext: .
Dockerfile: Dockerfile
DockerBuildArgs:
ENVIRONMENT: !Ref Environment
DockerEnvFiles:
- envs/${Environment}.json
Код: Выделить всё
# Importing our utils
import json
import utils
def lambda_handler(event : dict, context : dict) -> dict:
try:
env_vars = utils.load_env()
utils.load_mysql(env_vars = env_vars)
return {
'status' : 200,
'message' : 'Everything worked nice!'
}
except Exception as e:
return {
'status' : 400,
'message' : f'Something went wrong: \'{e}\''
}
Код: Выделить всё
sam build --use-container
Код: Выделить всё
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Validate SAM template: sam validate
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch
[*] Deploy: sam deploy --guided
- , а затем я получаю эту ошибку:
Код: Выделить всё
sam local invoke DockerDemo --event event.json
Код: Выделить всё
Invoking Container created from dockerdemo:dev
Local image was not found.
Removing rapid images for repo dockerdemo
Building image..................
Using local image: dockerdemo:rapid-x86_64.
START RequestId: b110d6b1-81bd-4c13-9cfe-27692c1a37b5 Version: $LATEST
Traceback (most recent call last): Unable to import module 'app': No module named 'app'
END RequestId: b110d6b1-81bd-4c13-9cfe-27692c1a37b5
REPORT RequestId: b110d6b1-81bd-4c13-9cfe-27692c1a37b5 Init Duration: 1.02 ms Duration: 435.02 ms Billed Duration: 436 ms Memory Size: 256 MB Max Memory Used: 256 MB
{"errorMessage": "Unable to import module 'app': No module named 'app'", "errorType": "Runtime.ImportModuleError", "requestId": "b110d6b1-81bd-4c13-9cfe-27692c1a37b5", "stackTrace": []}%
Подробнее здесь: https://stackoverflow.com/questions/770 ... module-app
Мобильная версия