diff --git a/README.md b/README.md index 16c0635..1dd5513 100644 --- a/README.md +++ b/README.md @@ -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: ... @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index b8486d7..cdc4442 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" diff --git a/example/service/serverless.yml b/example/service/serverless.yml index 0858413..ea87fe8 100644 --- a/example/service/serverless.yml +++ b/example/service/serverless.yml @@ -4,18 +4,17 @@ 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: @@ -23,5 +22,6 @@ functions: environment: SSM_VAR: ${ssm:abc} # CF_VAR: ${cf:def} - events: - - sns: ${env:EXISTING_TOPIC_ARN} + +plugins: + - serverless-localstack diff --git a/package-lock.json b/package-lock.json index 5014cab..e603623 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "serverless-localstack", - "version": "1.1.3", + "version": "1.2.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "serverless-localstack", - "version": "1.1.3", + "version": "1.2.0", "license": "MIT", "dependencies": { "adm-zip": "^0.5.10", diff --git a/package.json b/package.json index 82c6e49..405b804 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/index.js b/src/index.js index dd223a7..c543f5d 100644 --- a/src/index.js +++ b/src/index.js @@ -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]; } @@ -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) + } + 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(); } ); }