I can't run Lambda in LocalStack

Now, I installed LocalStack and try to run Lamda. But, it doesn’t work as I wanted. Below are the commands I excuted.
(The space between the"http" and “://” is due to a restriction regarding post)

  1. sudo apt install awscli
  2. sudo pip install aws-local
  3. aws configure --profile localstack
    AWS Access Key ID [None]: dummy
    AWS Secret Access Key [None]: dummy
    Default region name [None]: us-east-1
    Default output format[None]: json
  4. git clone https: //github.com/localstack/localstack.git
  5. cd localstack
  6. vi docker-compose.yml
version: "3.8"

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack:latest
    ports:
      - "4566:4566"            # LocalStack Gateway
    environment:
      - SERVICES=${SERVICES-}
      - DEBUG=${DEBUG-}
      - DATA_DIR=${DATA_DIR-}
      - PORT_WEB_UI=${PORT_WEB_UI-}
      - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY-}
      - DOCKER_HOST=unix:///var/run/docker.sock
      - LAMBDA_DOCKER_NETWORK=host
      - LAMBDA_EXECUTOR=docker-reuse
    volumes:
      - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
  1. docker-compose up -d
  2. aws --endpoint-url=http://localhost:4566 --profile localstack s3api create-bucket --bucket test-bucket
  3. touch lambda.py & vi lambda.py
    lambda.py---------------------------------------
import boto3
from boto3.session import Session
from datetime import datetime


session = Session(aws_access_key_id='dummy',
        aws_secret_access_key='dummy',
        region_name='us-east-1'
        )

s3 = session.resource(
        service_name='s3',
        endpoint_url='http ://localhost:4566'
        )

def lambda_handler(event, context):
    bucket = 'test-bucket'
    key = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') + '.txt'
    file_contents = 'Lambda Save File'

    s3.Bucket(bucket).put_object(Key=key,Body=file_contents)

    return 'create file' + key

  1. zip lambda.zip lambda.py
  2. aws --endpoint-url=http ://localhost:4566 lambda create-function --function-name test --runtime python3.8 --handler lambda.lambda_handler --role arn:aws:iam::000000000000:role/r1 --zip-file fileb://lambda.zip --region us-east-1 --profile localstack
  3. aws lambda --endpoint-url=http ://localhost:4566 invoke --function-name test --profile localstack result.log

After excuting the avobe commands, I got the error Read timeout on endpoint URL: “http ://localhost:4566/2015-03-31/functions/test/invocations”. I can not solve this error by adding --cli-read-timeout option. Please let me know how to solve the problem.

Hi @hirata1998,

I will share some points which you can follow:

1 Like