Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix autostart and add support for docker compose #242

Merged
merged 12 commits into from
Jan 26, 2024
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ custom:
docker:
# Enable this flag to run "docker ..." commands as sudo
sudo: False
compose_file: /home/localstack_compose.yml # optional to use docker compose instead of docker or localstack cli
stages:
local:
...
Expand Down Expand Up @@ -207,7 +208,7 @@ custom:
```

## Change Log

* v1.2.0: Add docker-compose config and fix autostart when plugin is not active
* v1.1.3: Fix replacing host from environment variable `AWS_ENDPOINT_URL`
* v1.1.2: Unify construction of target endpoint URL, add support for configuring `AWS_ENDPOINT_URL`
* v1.1.1: Fix layer deployment if `mountCode` is enabled by always packaging and deploying
Expand Down
41 changes: 10 additions & 31 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,36 +1,15 @@
version: '2'
version: "3.8"

services:
dev-env:
build:
context: .
working_dir: /app
volumes:
- .:/app
- ~/.aws/:/root/.aws
- ~/.gitconfig:/root/.gitconfig
- ~/.gitconfig.local:/root/.gitconfig.local
environment:
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- AWS_PROFILE
- AWS_SESSION_TOKEN
- AWS_SECURITY_TOKEN
- SNYK_TOKEN
entrypoint:
- /bin/bash
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
image: localstack/localstack
ports:
- "4567-4582:4567-4582"
- "8080:8080"
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
# Only start a subset of services required for testing.
- SERVICES=s3,sns,sqs,apigateway,lambda,dynamodb,dynamodbstreams,cloudformation
# - DEBUG=${DEBUG- }
# - DATA_DIR=${DATA_DIR- }
# - LAMBDA_EXECUTOR=${LAMBDA_EXECUTOR- }
# - KINESIS_ERROR_PROBABILITY=${KINESIS_ERROR_PROBABILITY- }
# - DOCKER_HOST=unix:///var/run/docker.sock
# volumes:
# - "${TMP_DIR:-/tmp/localstack}:${TMP_DIR:-/tmp/localstack}"
# - "/var/run/docker.sock:/var/run/docker.sock"
# LocalStack configuration: https://docs.localstack.cloud/references/configuration/
- DEBUG=${DEBUG:-0}
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"
12 changes: 6 additions & 6 deletions example/service/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ provider:
name: aws
profile: ${opt:profile, self:custom.profile}
stage: ${opt:stage, self:custom.defaultStage}
runtime: nodejs14.x
runtime: nodejs16.x
lambdaHashingVersion: '20201221'

plugins:
- serverless-localstack

custom:
defaultStage: local
profile: default
localstack:
debug: true
stages: [local]
autostart: true
compose_file: /home/localstack/Projects/serverless-localstack/docker-compose.yml

functions:
hello:
handler: handler.hello
environment:
SSM_VAR: ${ssm:abc}
# CF_VAR: ${cf:def}
events:
- sns: ${env:EXISTING_TOPIC_ARN}

plugins:
- serverless-localstack
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-localstack",
"version": "1.1.3",
"version": "1.2.0",
"description": "Connect Serverless to LocalStack!",
"main": "src/index.js",
"scripts": {
Expand Down
54 changes: 37 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,20 @@ class LocalstackPlugin {
* Start the LocalStack container in Docker, if it is not running yet.
*/
startLocalStack() {
if (!this.config.autostart) {
if (!(this.config.autostart && this.isActive())) {
return Promise.resolve();
}

const getContainer = () => {
return exec('docker ps').then(
(stdout) => {
const exists = stdout.split('\n').filter((line) => line.indexOf('localstack/localstack') >= 0 || line.indexOf('localstack_localstack') >= 0);
const exists = stdout.split('\n').filter(
(line) => (
line.indexOf('localstack/localstack') >= 0 ||
line.indexOf('localstack/localstack-pro') >= 0 ||
line.indexOf('localstack_localstack') >= 0
)
);
if (exists.length) {
return exists[0].replace('\t', ' ').split(' ')[0];
}
Expand Down Expand Up @@ -452,27 +458,41 @@ class LocalstackPlugin {
return containerID;
}

const startContainer = () => {
this.log('Starting LocalStack in Docker. This can take a while.');
const cwd = process.cwd();
const env = this.clone(process.env);
env.DEBUG = '1';
env.LAMBDA_EXECUTOR = env.LAMBDA_EXECUTOR || 'docker';
env.LAMBDA_REMOTE_DOCKER = env.LAMBDA_REMOTE_DOCKER || '0';
env.DOCKER_FLAGS = (env.DOCKER_FLAGS || '') + ` -v ${cwd}:${cwd}`;
env.START_WEB = env.START_WEB || '0';
const maxBuffer = (+env.EXEC_MAXBUFFER)||50*1000*1000; // 50mb buffer to handle output
if (this.shouldRunDockerSudo()) {
env.DOCKER_CMD = 'sudo docker';
}
const options = {env: env, maxBuffer};
return exec('localstack start -d', options).then(getContainer)
.then((containerID) => addNetworks(containerID))
.then((containerID) => checkStatus(containerID));
}

const startCompose = () => {
this.log('Starting LocalStack using the provided docker-compose file. This can take a while.');
return exec(`docker-compose -f ${this.config.docker.compose_file} up -d`).then(getContainer)
pinzon marked this conversation as resolved.
Show resolved Hide resolved
}

return getContainer().then(
(containerID) => {
if(containerID) {
return;
}
this.log('Starting LocalStack in Docker. This can take a while.');
const cwd = process.cwd();
const env = this.clone(process.env);
env.DEBUG = '1';
env.LAMBDA_EXECUTOR = env.LAMBDA_EXECUTOR || 'docker';
env.LAMBDA_REMOTE_DOCKER = env.LAMBDA_REMOTE_DOCKER || '0';
env.DOCKER_FLAGS = (env.DOCKER_FLAGS || '') + ` -d -v ${cwd}:${cwd}`;
env.START_WEB = env.START_WEB || '0';
const maxBuffer = (+env.EXEC_MAXBUFFER)||50*1000*1000; // 50mb buffer to handle output
if (this.shouldRunDockerSudo()) {
env.DOCKER_CMD = 'sudo docker';

if(this.config.docker && this.config.docker.compose_file){
return startCompose();
}
const options = {env: env, maxBuffer};
return exec('localstack start', options).then(getContainer)
.then((containerID) => addNetworks(containerID))
.then((containerID) => checkStatus(containerID));

return startContainer();
}
);
}
Expand Down
Loading