connecting AWS SAM Local with dynamodb in docker

前端 未结 6 1896
既然无缘
既然无缘 2020-12-02 13:39

I\'ve set up an api gateway/aws lambda pair using AWS sam local and confirmed I can call it successfully after running

sam local start-api

I\'ve

6条回答
  •  攒了一身酷
    2020-12-02 14:04

    As @Paul mentioned, it is about configuring your network between the docker containers - lambda and database.

    Another approach that worked for me (using docker-compose).

    docker-compose:

    version: '2.1'
    
    services:
      db:
        image: ...
        ports:
          - "3306:3306"
        networks:
          - my_network
        environment:
          ...
        volumes:
          ...
    
    networks:
      my_network:
    

    Then, after docker-compose up, running docker network ls will show:

    NETWORK ID          NAME                        DRIVER              SCOPE
    7eb440d5c0e6        dev_my_network              bridge              local
    

    My docker container name is dev_db_1.

    My js code is:

    const connection = mysql.createConnection({
        host: "dev_db_1",
        port: 3306,
        ...
    });
    

    Then, running the sam command:

    sam local invoke --docker-network dev_my_network -e my.json
    

    Stack:

    • Docker: 18.03.1-ce
    • Docker-compose: 1.21.1
    • MacOS HighSierra 10.13.6

提交回复
热议问题