Skip to content

Instantly share code, notes, and snippets.

@Hailong
Created June 2, 2021 13:37
Show Gist options
  • Save Hailong/03ec8583ba02ffda7b8b35c356ff2d89 to your computer and use it in GitHub Desktop.
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.
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)
})
}))
}
@Hailong
Copy link
Author

Hailong commented Jun 2, 2021

As of the time being, this script has the same effect of running the PIP command localstack start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment