Coder Social home page Coder Social logo

turiphro / aws-serverless-functions-and-serverless-containers Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 172 KB

A template and demo app for deploying to both AWS Lambda (Serverless Functions) and AWS Fargate (Serverless Containers)

Home Page: https://turiphro.nl/writings/template-serverless-functions-and-serverless-conta/

Dockerfile 2.37% Shell 11.39% HTML 45.84% JavaScript 1.63% Python 38.77%
aws lambda aws-lambda fargate aws-fargate serverless serverless-functions serverless-containers serverless-applications serverless-examples cloudformation docker containers functions

aws-serverless-functions-and-serverless-containers's Introduction

This repository contains the same example application twice:

  1. Serverless functions: AWS Lambda functions with an API Gateway
  2. Serverless containers: AWS Fargate (ECS) containers with a load balancer

The examples are meant to make it easy to compare both alternatives.

A common demo UI is provided (html/index.html) that allows switching between the two backends. Both versions use the same storage (a DynamoDB table) and share the same code (src/common.py). Wrappers are provided to handle the API Gateway events (Functions approach) or serve a simple Flask-based REST endpoint (Container approach). Deploying the stack will update the URLs in the demo UI (src/urls.js), which can be opend locally, or potentially be deployed to S3/CloudFront or elsewhere.

demo-ui-screenshot

The full stack is defined in an AWS CloudFormation template (template.yaml), backed by AWS SAM. Deployment includes a few additional steps, so a deployment script has been provided (deploy.sh).

aws-stack-infrastructure

Note that this repo is meant as a demo, and the stack only includes the absolute minimum necessary to create a working app. This demo is not meant to be used as template for production-ready applications. The CloudFormation template includes some hints about missing resources and security good practices.

It's interesting to look at the differences between both stacks:

  • containers need plenty of components: even if both approaches are "serverless" in the sense that we don't have to manage EC2 instances, the Container approach still needs an awful large amount of components to get started. The Lambda/API Gateway approach, in contrast, already comes with load balancing, concurrency, auto-scaling, and a VPC built-in. The AWS SAM framework comes with some handy 'meta' serverless resources; it might be time to create similar shortcuts for containers (AWS::Serverless::Container)?
  • Endpoint configuration: the Lambda variant configures the REST endpoints inside the CloudFormation template (and hence the endpoints scale independently), while the container variant has them all served together configured in code (although multiple containers & scaling policies could be made if necessary). The payloads are also slightly different due to the API Gateway proxy event vs the Flask (or another) framework.

Prerequisites

  • Install the AWS CLI (if you didn't already)
  • Install the AWS SAM CLI (if you didn't already)
  • Install Docker (if you didn't already)

Deploy

export DOCKER_IMAGE=${your-dockerhub-username}/serverless-blog

./deploy.sh

# now open or refresh html/index.html

Test locally

Note that actual HTTP calls require an existing DynamoDB table.

sam build

# 1) test locally, behind full API
# note: can keep the API running, but run 'sam build' after changes
sam local start-api
http GET http://127.0.0.1:3000/blog/
# or perform a raw function call (without API)
sam local invoke GetBlog --event <(echo '{"id": "foo"}') | jq .

# 2) test locally, Docker container
export TABLE_NAME=$SOME_DYNAMODB_TABLE
docker build -t blog .
docker run -it -p 5000:80 -v $PWD/src:/app -v ~/.aws:/root/.aws -e TABLE_NAME blog
# note: can keep the container running; the code will auto-reload
http GET http://127.0.0.1:5000/blog/

aws-serverless-functions-and-serverless-containers's People

Contributors

turiphro avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

jay0512

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.