Coder Social home page Coder Social logo

aws-lambda-docker-local-example's Introduction

AWS Lambda Docker Local Development

About

Docker-only local lambda development environment with docker lambda and docker aws-lambda-api-gateway-local

Prerequisites

  • Docker && Docker Compose
  • Environment Variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY (Only need when deploy)

You should set up the aws credentials environment variables.

To set these variables on Linux or macOS, use the export command:

export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key

To set these variables on Windows, use the set command:

set AWS_ACCESS_KEY_ID=your_access_key_id
set AWS_SECRET_ACCESS_KEY=your_secret_access_key

Getting Started

1. Init A New Lambda Nodejs Function

docker run -it -v ${PWD}:/var/task lambci/lambda:build-nodejs12.x sh -c "sam init"

2. Create docker-compose.yaml

Create a docker-compose.yaml in your inited folder:

Instead your runtime and build image, for me, it's lambci/lambda:nodejs12.x and lambci/lambda:build-nodejs12.x, and define the runtime function entry for yours: hello-world/app.lambdaHandler

version: '3'
services:
  runtime:
    image: lambci/lambda:nodejs12.x
    ports:
      - "9001:9001"
    volumes: 
      - ".:/var/task:ro,delegated"
    environment:
        DOCKER_LAMBDA_WATCH: 1
        DOCKER_LAMBDA_STAY_OPEN: 1
    command: 
      - hello-world/app.lambdaHandler
  api-gateway:
    image: owenyoung/aws-lambda-api-gateway-local
    ports:
      - "3000:3000"
    volumes:
      - ".:/var/task"
    environment:
      LAMBDA_ENDPOINT: "http://runtime:9001"
  build:
    image: lambci/lambda:build-nodejs12.x
    volumes:
      - ".:/var/task:delegated"
    environment:
      - AWS_ACCESS_KEY_ID
      - AWS_SECRET_ACCESS_KEY
    command: 
      - sh
      - -c
      - sam build

3. Start Developing

docker-compose up runtime api-gateway

Now, you can visit your api at: http://127.0.0.1:3000/hello (For official sam-app example, you can instead your real path), anything what you changed will trigger the auto reloading.

4. Build

docker-compose run build

5. Build And deploy

First time for deploy:

docker-compose run build sh -c "sam build && sam deploy --guided" 
docker-compose run build && docker-compose run build sh -c "sam build && sam deploy"

6. Create A Makefile To Make Things Easy

For example:

.PHONY: start build deploy init-deploy api-sh runtime-sh

start:
	docker-compose up api-gateway runtime
build:
	docker-compose run build
init-deploy:
	docker-compose run build sh -c "sam build && sam deploy --guided" 
deploy:
	docker-compose run build sh -c "sam build && sam deploy"
api-sh:
	docker-compose exec api-gateway /bin/sh
runtime-sh:
	docker-compose exec runtime /bin/sh

aws-lambda-docker-local-example's People

Contributors

theowenyoung avatar

Watchers

 avatar

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.