Skip to content

Commit

Permalink
add initial support for docker lambda execution
Browse files Browse the repository at this point in the history
  • Loading branch information
whummer committed Apr 27, 2017
1 parent 22cd2f9 commit ce025ed
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 78 deletions.
13 changes: 6 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LABEL authors="Waldemar Hummer (whummer@atlassian.com), Gianluca Bortoli (giallo

# install general packages
RUN apk update && \
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 && \
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 && \
update-ca-certificates

# set workdir
Expand Down Expand Up @@ -51,14 +51,16 @@ RUN make init
# add rest of the code
ADD localstack/ localstack/

# fix some permissions
# fix some permissions and create local user
RUN mkdir -p /.npm && \
mkdir -p localstack/infra/elasticsearch/data && \
chmod 777 . && \
chmod 755 /root && \
chmod -R 777 /.npm && \
chmod -R 777 localstack/infra/elasticsearch/config && \
chmod -R 777 localstack/infra/elasticsearch/data && \
chmod -R 777 localstack/infra/elasticsearch/logs
chmod -R 777 localstack/infra/elasticsearch/logs && \
adduser -D localstack

# install supervisor daemon & copy config file
ADD supervisord.conf /etc/supervisord.conf
Expand All @@ -71,10 +73,7 @@ ENV AWS_ACCESS_KEY_ID=foobar \
AWS_SECRET_ACCESS_KEY=foobar \
AWS_DEFAULT_REGION=us-east-1 \
MAVEN_CONFIG=/opt/code/localstack \
USER=docker

# assign random user id
USER 24624336
USER=localstack

# run tests (to verify the build before pushing the image)
ADD tests/ tests/
Expand Down
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ VENV_DIR = .venv
VENV_RUN = . $(VENV_DIR)/bin/activate
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
AWS_STS_TMPFILE = /tmp/aws-java-sdk-sts.jar
TMP_DIR = /tmp/localstack
DOCKER_SOCK ?= /var/run/docker.sock

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

docker-run: ## Run Docker image locally
port_mappings="$(shell echo $(SERVICES) | sed 's/[^0-9]/ /g' | sed 's/\([0-9][0-9]*\)/-p \1:\1/g' | sed 's/ */ /g')"; \
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)
mkdir -p $(TMP_DIR); \
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)

web: ## Start web application (dashboard)
($(VENV_RUN); bin/localstack web --port=8080)
Expand All @@ -66,6 +69,9 @@ test: ## Run automated tests
make lint && \
$(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).*' .

test-docker: ## Run automated tests in Docker
ENTRYPOINT="--entrypoint= -v `pwd`/localstack:/opt/code/localstack/localstack" CMD="make test" make docker-run

lint: ## Run code linter to check code style
($(VENV_RUN); pep8 --max-line-length=120 --ignore=E128 --exclude=node_modules,legacy,$(VENV_DIR),dist .)

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

## Developing

Expand Down Expand Up @@ -236,6 +241,7 @@ make web

## Change Log

* v0.4.0: Execute Lambda functions in Docker containers; CORS headers for S3
* v0.3.11: Add Route53, SES, CloudFormation; DynamoDB fault injection; UI tweaks; refactor config
* v0.3.10: Add initial support for S3 bucket notifications; fix subprocess32 installation
* v0.3.9: Make services/ports configurable via $SERVICES; add tests for Firehose+S3
Expand Down
21 changes: 12 additions & 9 deletions localstack/config.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import re
import os
from localstack.constants import *

# Randomly inject faults to Kinesis
KINESIS_ERROR_PROBABILITY = 0.0
if os.environ.get('KINESIS_ERROR_PROBABILITY'):
KINESIS_ERROR_PROBABILITY = float(os.environ['KINESIS_ERROR_PROBABILITY'])
KINESIS_ERROR_PROBABILITY = float(os.environ.get('KINESIS_ERROR_PROBABILITY') or 0.0)

# Randomly inject faults to DynamoDB
DYNAMODB_ERROR_PROBABILITY = 0.0
if os.environ.get('DYNAMODB_ERROR_PROBABILITY'):
DYNAMODB_ERROR_PROBABILITY = float(os.environ['DYNAMODB_ERROR_PROBABILITY'])
DYNAMODB_ERROR_PROBABILITY = float(os.environ.get('DYNAMODB_ERROR_PROBABILITY') or 0.0)

# Allow custom hostname for services
HOSTNAME = LOCALHOST
if os.environ.get('HOSTNAME'):
HOSTNAME = os.environ['HOSTNAME']
HOSTNAME = os.environ.get('HOSTNAME') or LOCALHOST

# whether to use Lambda functions in a Docker container
LAMDA_EXECUTOR = os.environ.get('LAMDA_EXECUTOR') or 'docker'

# temporary folder
TMP_FOLDER = '/tmp/localstack'
if not os.path.exists(TMP_FOLDER):
os.makedirs(TMP_FOLDER)


def parse_service_ports():
Expand Down
1 change: 0 additions & 1 deletion localstack/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@

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

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

0 comments on commit ce025ed

Please sign in to comment.