Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use customized docker container with serverless framework

I use serverless framework to deploy python function onto aws lambda

my configuration file serverless.yml is following

frameworkVersion: "=1.27.3"

service: recipes

provider:
  name: aws
  endpointType: REGIONAL
  runtime: python3.6
  stage: dev
  region: eu-central-1
  memorySize: 512
  deploymentBucket:
    name: dfki-meta
  versionFunctions: false
  stackTags:
    Project: DFKIAPP
  # Allows updates to all resources except deleting/replacing EC2 instances
  stackPolicy:
    - Effect: Allow
      Principal: "*"
      Action: "Update:*"
      Resource: "*"
    - Effect: Deny
      Principal: "*"
      Action:
        - Update: Replace
        - Update: Delete
      Resource: "*"
      Condition:
        StringEquals:
          ResourceType:
            - AWS::EC2::Instance
  # Access to RDS and S3 Bucket
  iamRoleStatements:
    -  Effect: "Allow"
       Action: "s3:ListBucket"
       Resource: "*"

package:
  individually: true



functions:
  get_recipes:
    handler: handler.get_recipes
    module: recipes_crud
    package:
      include:
        - db/*
    timeout: 10
    events:
      - http:
          path: recipes
          method: get
          request:
            parameters:
              querystring:
                persona: true



plugins:
  # deploy conda package on lambda
  - serverless-python-requirements

custom:
  pythonRequirements:
    dockerizePip: non-linux
    dockerFile: prod_env_dockerfile/Dockerfile

and my docker file

lambci/lambda:python3.6
FROM lambci/lambda-base:build

ENV PATH=/var/lang/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin \
    LD_LIBRARY_PATH=/var/lang/lib:/lib64:/usr/lib64:/var/runtime:/var/runtime/lib:/var/task:/var/task/lib \
    AWS_EXECUTION_ENV=AWS_Lambda_python3.6 \
    PYTHONPATH=/var/runtime \
    PKG_CONFIG_PATH=/var/lang/lib/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig

RUN rm -rf /var/runtime /var/lang && \
  curl https://lambci.s3.amazonaws.com/fs/python3.6.tgz | tar -xz -C / && \
  sed -i '/^prefix=/c\prefix=/var/lang' /var/lang/lib/pkgconfig/python-3.6.pc && \
  curl https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tar.xz | tar -xJ && \
  cd Python-3.6.1 && \
  LIBS="$LIBS -lutil -lrt" ./configure --prefix=/var/lang && \
  make -j$(getconf _NPROCESSORS_ONLN) libinstall inclinstall && \
  cd .. && \
  rm -rf Python-3.6.1 && \
  pip3 install -U pip awscli virtualenv --no-cache-dir

RUN yum install -y wget
RUN wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
RUN bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda
RUN export PATH="$HOME/miniconda/bin:$PATH" && conda install -c prometeia -y pymssql

but seemingly sls do not use my dockerfile, it still create a image called sls-py-reqs-custom

(node:43146) ExperimentalWarning: The fs.promises API is experimental
Serverless: Installing requirements of recipes_crud/requirements.txt in .serverless/recipes_crud...
Serverless: Building custom docker image from prod_env_dockerfile/Dockerfile...
Serverless: Docker Image: sls-py-reqs-custom
Serverless: Packaging function: get_recipes...
Serverless: Excluding development dependencies...
Serverless: Injecting required Python packages to package...
Serverless: Uploading function: get_recipes (29.08 MB)...
Serverless: Successfully deployed function: get_recipes
Serverless: Successfully updated function: get_recipes

How can I force serverless to use my customized docker ?

like image 638
Hello lad Avatar asked Mar 01 '26 23:03

Hello lad


1 Answers

AWS Lambda supports the use of Docker images as a deployment format for your functions. This allows you to create more complex runtime environments that better suit your needs.

Here's an example of how you can configure your serverless.yml file to work with Docker:


service: your-service-name

provider:
  name: aws
  runtime: provided
  lambdaHashingVersion: 20201221

functions:
  hello:
    image: 
      name: your-docker-image:tag
      command:
        - your.handler

In this example, your-docker-image:tag is the name of the Docker image you want to use for your Lambda function. You should replace this with the name of your own Docker image.

your.handler is the path to your function's handler. This should be the name of the file where your handler is located, and the name of the handler itself, separated by a dot.

It's important to note that the Docker image needs to be uploaded to AWS's Elastic Container Registry (ECR). The Serverless Framework handles this process automatically, but make sure you have the necessary permissions to work with ECR.

Also note that the runtime is set to provided. This instructs AWS Lambda to use a custom runtime that you provide with your Docker image.

Another important detail is lambdaHashingVersion: 20201221. This option is required for using container images in AWS Lambda and signifies that the new hashing scheme is being used.

like image 164
Ilya Strelov Avatar answered Mar 03 '26 13:03

Ilya Strelov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!