Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using container images with serverless functions... and containers!

Using container images with serverless functions... and containers!

ServerlessDays Warsaw, October 20th, 2021

Serverless functions have been designed from the start for simplicity, providing a managed runtime to execute your code. But code can include different kinds of dependencies, such as libraries, machine learning models, or graphical assets such as fonts. To help you with these use cases, AWS Lambda now supports container images as a packaging format. Let’s see how this works, when to use it, and how to build container images that work with both functions and traditional container environments.

Danilo Poccia

October 20, 2021
Tweet

More Decks by Danilo Poccia

Other Decks in Programming

Transcript

  1. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    © 2021, Amazon Web Services, Inc. or its affiliates.
    Using container images
    with serverless functions...
    and containers!
    Danilo Poccia
    Chief Evangelist (EMEA)
    Amazon Web Services
    @danilop

    View Slide

  2. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Agenda
    • Why & how using container images for serverless functions
    • A quick look at the tools
    • Using the same container images with serverless functions and
    traditional container environments
    2

    View Slide

  3. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    © 2021, Amazon Web Services, Inc. or its affiliates.
    Using container images
    to package serverless
    functions
    3

    View Slide

  4. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Why use container images with serverless functions?
    Simplify dependency management, especially
    for dependencies installed outside of the
    runtime platform (such as OS dependencies).
    AWS Lambda max deployment package size:
    • ZIP format: 250 MB
    • Container image: 10 GB
    Use existing container tooling.
    Centralize development workflow around
    tools processing container images.
    4

    View Slide

  5. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    FROM public.ecr.aws/lambda/nodejs:latest
    COPY app.js package*.json ./
    RUN npm install
    CMD [ "app.lambdaHandler" ]
    https://aws.amazon.com/blogs/aws/new-for-aws-lambda-container-image-support/
    Base container images prepared by AWS
    are shared via Docker Hub and Amazon
    Elastic Container Registry
    (ECR) Public
    Command can be overwritten in the
    function configuration
    Dockerfile

    View Slide

  6. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Container images for AWS Lambda
    6
    Runtime Interface Client (RIC)
    Runtime Interface Emulator (RIE)
    Your code
    AWS Lambda Local tests

    View Slide

  7. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Test container images locally (using RIC)
    7
    docker run \
    --env DYNAMODB_TABLE=Movies \
    --env AWS_ACCESS_KEY_ID="$(aws configure get default.aws_access_key_id)" \
    --env AWS_SECRET_ACCESS_KEY="$(aws configure get default.aws_secret_access_key)" \
    --env AWS_SESSION_TOKEN="$(aws configure get default.aws_session_token)" \
    --env AWS_REGION="$(aws configure get default.region)" \
    -p 9080:8080 \
    movie-service list.list
    curl -s "http://localhost:9080/2015-03-31/functions/function/invocations" -d '{}' | jq

    View Slide

  8. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Base container images – gallery.ecr.aws/lambda
    8

    View Slide

  9. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    AWS Lambda functions powered by Arm/Graviton2
    9
    https://aws.amazon.com/blogs/aws/aws-lambda-functions-powered-by-aws-graviton2-processor-run-your-functions-on-arm-and-get-up-to-34-better-price-performance/

    View Slide

  10. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    © 2021, Amazon Web Services, Inc. or its affiliates.
    Tools to help you use
    container images with
    serverless functions
    10

    View Slide

  11. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    AWS Serverless Application Model (SAM)
    11
    https://aws.amazon.com/blogs/compute/using-container-image-support-for-aws-lambda-with-aws-sam
    Resources:
    HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
    PackageType: Image
    ImageConfig:
    Command: ["app.lambda_handler"]
    Metadata:
    Dockerfile: Dockerfile
    DockerContext: ./hello_world
    DockerTag: v1

    View Slide

  12. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    AWS Cloud Development Kit (CDK)
    12
    https://github.com/aws-samples/aws-cdk-lambda-container

    View Slide

  13. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Serverless Framework
    13
    https://www.serverless.com/blog/container-support-for-lambda

    View Slide

  14. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    © 2021, Amazon Web Services, Inc. or its affiliates.
    Using the same
    container images with
    serverless functions and
    containers
    14

    View Slide

  15. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Same code
    15
    Container
    Image #1
    Your Code
    Lambda
    Function
    API Gateway
    Endpoint
    Container
    User
    User
    Build
    Container
    Image #2
    Application Load
    Balancer

    View Slide

  16. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Same container image
    16
    Container
    Image
    Your Code
    Lambda
    Function
    API Gateway
    Endpoint
    Container
    User
    User
    Build
    Application Load
    Balancer

    View Slide

  17. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Node.js – Serverless Express
    17
    https://github.com/vendia/serverless-express AWS Lambda
    +
    Amazon API Gateway
    Express
    Koa
    Hapi
    Sails

    View Slide

  18. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Python – Zappa
    18
    https://github.com/zappa/zappa AWS Lambda
    +
    Amazon API Gateway
    Django
    Flask
    WSGI

    View Slide

  19. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Java – Serverless Java container
    19
    https://github.com/awslabs/
    aws-serverless-java-container
    AWS Lambda
    +
    Amazon API Gateway
    Spring
    Spring Boot
    Apache Struts
    Jersey
    Spark

    View Slide

  20. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    © 2021, Amazon Web Services, Inc. or its affiliates.
    Demo “As you like it”
    20

    View Slide

  21. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Demo “As you like it” – Here’s the code
    21
    https://github.com/danilop/serverless-functions-and-containers

    View Slide

  22. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Takeaways
    • Using container images to package serverless functions can help
    if you are already suing containers (and container tooling) in your
    development workflow.
    • Container images can simplify managing large dependencies and
    dependencies managed outside of the runtime.
    • You can use the same container images to server web traffic with
    serverless functions and traditional container environments.
    22

    View Slide

  23. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Q&A
    Let me know what you think J
    @danilop
    23

    View Slide

  24. © 2021, Amazon Web Services, Inc. or its affiliates.
    USING CONTAINER IMAGES WITH SERVERLESS FUNCTIONS... AND CONTAINERS!
    Thank you!
    © 2021, Amazon Web Services, Inc. or its affiliates. 24
    @danilop

    View Slide