Created
June 2, 2021 13:37
-
-
Save Hailong/03ec8583ba02ffda7b8b35c356ff2d89 to your computer and use it in GitHub Desktop.
Start up LocalStack platform with Node.js code. It creates the same environment variables and mount points as the PIP `localstack start` is doing.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Docker } from 'node-docker-api' | |
import { dirSync } from 'tmp' | |
export async function bootLocalStack () { | |
const docker = new Docker({ socketPath: '/var/run/docker.sock' }) | |
const tmpDir = dirSync() | |
await Promise.all((await docker.container.list({ all: 1 })) | |
.filter(container => container.data.Image === 'localstack/localstack') | |
.map(container => container.delete({ force: true }))) | |
await (await docker.container.create({ | |
name: 'localstack_main', | |
Image: 'localstack/localstack', | |
Env: [ | |
'LOCALSTACK_HOSTNAME=localhost', | |
'TEST_AWS_ACCOUNT_ID=000000000000', | |
'DEFAULT_REGION=us-east-1', | |
'LAMBDA_EXECUTOR=docker-reuse', | |
'DOCKER_HOST=unix:///var/run/docker.sock', | |
`HOST_TMP_FOLDER=${tmpDir.name}` | |
], | |
HostConfig: { | |
Privileged: true, | |
PortBindings: { | |
'4566/tcp': [{ HostPort: '4566' }], | |
'4571/tcp': [{ HostPort: '4571' }] | |
}, | |
Binds: [ | |
'/var/run/docker.sock:/var/run/docker.sock', | |
`${tmpDir.name}:/tmp/localstack` | |
] | |
} | |
})).start() | |
await Promise.all((await docker.container.list()) | |
.filter(container => container.data.Image === 'localstack/localstack') | |
.map(container => { | |
console.log(container.data) | |
return new Promise((resolve, reject) => { | |
const looping = setInterval(() => { | |
container.logs({ | |
stdout: true, | |
stderr: true | |
}).then(stream => { | |
stream.on('data', info => { | |
const str = info.toString() | |
console.log(str) | |
if (str.includes('Ready.')) { | |
clearInterval(looping) | |
resolve() | |
} | |
}) | |
stream.on('error', err => reject(err)) | |
}).catch(error => reject(error)) | |
}, 3000) | |
}) | |
})) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As of the time being, this script has the same effect of running the PIP command
localstack start