Skip to content

Commit ce025ed

Browse files
committed
add initial support for docker lambda execution
1 parent 22cd2f9 commit ce025ed

14 files changed

Lines changed: 182 additions & 78 deletions

File tree

Dockerfile

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ LABEL authors="Waldemar Hummer (whummer@atlassian.com), Gianluca Bortoli (giallo
44

55
# install general packages
66
RUN apk update && \
7-
apk add --update autoconf automake build-base ca-certificates git libffi-dev libtool linux-headers make nodejs openssl openssl-dev python python-dev py-pip supervisor zip && \
7+
apk add --update autoconf automake build-base ca-certificates docker git libffi-dev libtool linux-headers make nodejs openssl openssl-dev python python-dev py-pip supervisor zip && \
88
update-ca-certificates
99

1010
# set workdir
@@ -51,14 +51,16 @@ RUN make init
5151
# add rest of the code
5252
ADD localstack/ localstack/
5353

54-
# fix some permissions
54+
# fix some permissions and create local user
5555
RUN mkdir -p /.npm && \
5656
mkdir -p localstack/infra/elasticsearch/data && \
5757
chmod 777 . && \
5858
chmod 755 /root && \
5959
chmod -R 777 /.npm && \
60+
chmod -R 777 localstack/infra/elasticsearch/config && \
6061
chmod -R 777 localstack/infra/elasticsearch/data && \
61-
chmod -R 777 localstack/infra/elasticsearch/logs
62+
chmod -R 777 localstack/infra/elasticsearch/logs && \
63+
adduser -D localstack
6264

6365
# install supervisor daemon & copy config file
6466
ADD supervisord.conf /etc/supervisord.conf
@@ -71,10 +73,7 @@ ENV AWS_ACCESS_KEY_ID=foobar \
7173
AWS_SECRET_ACCESS_KEY=foobar \
7274
AWS_DEFAULT_REGION=us-east-1 \
7375
MAVEN_CONFIG=/opt/code/localstack \
74-
USER=docker
75-
76-
# assign random user id
77-
USER 24624336
76+
USER=localstack
7877

7978
# run tests (to verify the build before pushing the image)
8079
ADD tests/ tests/

Makefile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ VENV_DIR = .venv
33
VENV_RUN = . $(VENV_DIR)/bin/activate
44
AWS_STS_URL = http://central.maven.org/maven2/com/amazonaws/aws-java-sdk-sts/1.11.14/aws-java-sdk-sts-1.11.14.jar
55
AWS_STS_TMPFILE = /tmp/aws-java-sdk-sts.jar
6+
TMP_DIR = /tmp/localstack
7+
DOCKER_SOCK ?= /var/run/docker.sock
68

79
usage: ## Show this help
810
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
@@ -57,7 +59,8 @@ docker-push: ## Push Docker image to registry
5759

5860
docker-run: ## Run Docker image locally
5961
port_mappings="$(shell echo $(SERVICES) | sed 's/[^0-9]/ /g' | sed 's/\([0-9][0-9]*\)/-p \1:\1/g' | sed 's/ */ /g')"; \
60-
docker run -it -e DEBUG=$(DEBUG) -e SERVICES=$(SERVICES) -e KINESIS_ERROR_PROBABILITY=$(KINESIS_ERROR_PROBABILITY) -p 4567-4581:4567-4581 -p 8080:8080 $$port_mappings $(IMAGE_NAME)
62+
mkdir -p $(TMP_DIR); \
63+
docker run -it $(ENTRYPOINT) -e DEBUG=$(DEBUG) -e SERVICES=$(SERVICES) -e LAMDA_EXECUTOR=$(LAMDA_EXECUTOR) -e KINESIS_ERROR_PROBABILITY=$(KINESIS_ERROR_PROBABILITY) -p 4567-4581:4567-4581 -p 8080:8080 $$port_mappings -v $(TMP_DIR):$(TMP_DIR) -v $(DOCKER_SOCK):$(DOCKER_SOCK) -e DOCKER_HOST="unix://$(DOCKER_SOCK)" $(IMAGE_NAME) $(CMD)
6164

6265
web: ## Start web application (dashboard)
6366
($(VENV_RUN); bin/localstack web --port=8080)
@@ -66,6 +69,9 @@ test: ## Run automated tests
6669
make lint && \
6770
$(VENV_RUN); DEBUG=$(DEBUG) PYTHONPATH=`pwd` nosetests --with-coverage --logging-level=WARNING --nocapture --no-skip --exe --cover-erase --cover-tests --cover-inclusive --cover-package=localstack --with-xunit --exclude='$(VENV_DIR).*' .
6871

72+
test-docker: ## Run automated tests in Docker
73+
ENTRYPOINT="--entrypoint= -v `pwd`/localstack:/opt/code/localstack/localstack" CMD="make test" make docker-run
74+
6975
lint: ## Run code linter to check code style
7076
($(VENV_RUN); pep8 --max-line-length=120 --ignore=E128 --exclude=node_modules,legacy,$(VENV_DIR),dist .)
7177

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ You can pass the following environment variables to LocalStack:
113113
inject `ProvisionedThroughputExceededException` errors into Kinesis API responses.
114114
* `DYNAMODB_ERROR_PROBABILITY`: Decimal value between 0.0 (default) and 1.0 to randomly
115115
inject `ProvisionedThroughputExceededException` errors into DynamoDB API responses.
116+
* `LAMDA_EXECUTOR`: Method to use for executing Lambda functions. Valid values are `local` (run
117+
the code in a temporary directory on the local machine) or `docker` (run code in a separate
118+
Docker container). In the latter case, if *LocalStack* itself is started inside Docker, then
119+
the `docker` command needs to be available inside the container (usually requires to run the
120+
container in privileged mode). Default is `docker`, fallback to `local` if Docker is not available.
116121

117122
## Developing
118123

@@ -236,6 +241,7 @@ make web
236241

237242
## Change Log
238243

244+
* v0.4.0: Execute Lambda functions in Docker containers; CORS headers for S3
239245
* v0.3.11: Add Route53, SES, CloudFormation; DynamoDB fault injection; UI tweaks; refactor config
240246
* v0.3.10: Add initial support for S3 bucket notifications; fix subprocess32 installation
241247
* v0.3.9: Make services/ports configurable via $SERVICES; add tests for Firehose+S3

localstack/config.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import re
2+
import os
23
from localstack.constants import *
34

45
# Randomly inject faults to Kinesis
5-
KINESIS_ERROR_PROBABILITY = 0.0
6-
if os.environ.get('KINESIS_ERROR_PROBABILITY'):
7-
KINESIS_ERROR_PROBABILITY = float(os.environ['KINESIS_ERROR_PROBABILITY'])
6+
KINESIS_ERROR_PROBABILITY = float(os.environ.get('KINESIS_ERROR_PROBABILITY') or 0.0)
87

98
# Randomly inject faults to DynamoDB
10-
DYNAMODB_ERROR_PROBABILITY = 0.0
11-
if os.environ.get('DYNAMODB_ERROR_PROBABILITY'):
12-
DYNAMODB_ERROR_PROBABILITY = float(os.environ['DYNAMODB_ERROR_PROBABILITY'])
9+
DYNAMODB_ERROR_PROBABILITY = float(os.environ.get('DYNAMODB_ERROR_PROBABILITY') or 0.0)
1310

1411
# Allow custom hostname for services
15-
HOSTNAME = LOCALHOST
16-
if os.environ.get('HOSTNAME'):
17-
HOSTNAME = os.environ['HOSTNAME']
12+
HOSTNAME = os.environ.get('HOSTNAME') or LOCALHOST
13+
14+
# whether to use Lambda functions in a Docker container
15+
LAMDA_EXECUTOR = os.environ.get('LAMDA_EXECUTOR') or 'docker'
16+
17+
# temporary folder
18+
TMP_FOLDER = '/tmp/localstack'
19+
if not os.path.exists(TMP_FOLDER):
20+
os.makedirs(TMP_FOLDER)
1821

1922

2023
def parse_service_ports():

localstack/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989

9090
# Lambda defaults
9191
LAMBDA_TEST_ROLE = "arn:aws:iam::%s:role/lambda-test-role" % TEST_AWS_ACCOUNT_ID
92-
LAMBDA_MAIN_SCRIPT_NAME = 'handler.py'
9392

9493
# installation constants
9594
ELASTICSEARCH_JAR_URL = ('https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.0.zip')

0 commit comments

Comments
 (0)