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)
- sudo apt install awscli
- sudo pip install aws-local
- 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 - git clone https: //github.com/localstack/localstack.git
- cd localstack
- 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"
- docker-compose up -d
- aws --endpoint-url=http://localhost:4566 --profile localstack s3api create-bucket --bucket test-bucket
- 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
- zip lambda.zip lambda.py
- 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
- 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.