|
| 1 | +--- |
| 2 | +title: "Initialization Hooks" |
| 3 | +linkTitle: "Initialization Hooks" |
| 4 | +weight: 5 |
| 5 | +description: > |
| 6 | + Writing shell or SQL scripts to customize or initialize your Snowflake emulator |
| 7 | +cascade: |
| 8 | + type: docs |
| 9 | +hide_readingtime: true |
| 10 | +--- |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +LocalStack for Snowflake supports automatically executing `*.sf.sql` with [Init Hooks](https://docs.localstack.cloud/references/init-hooks/) when mounted into the Docker container. A script can be added to one of these stages in the lifecycle: |
| 15 | + |
| 16 | +- `BOOT`: the container is running, but LocalStack hasn’t started |
| 17 | +- `START`: the Python process is running, and LocalStack is starting |
| 18 | +- `READY`: LocalStack is ready for requests |
| 19 | +- `SHUTDOWN`: LocalStack is shutting down |
| 20 | + |
| 21 | +A script can be in one of four states: `UNKNOWN`, `RUNNING`, `SUCCESSFUL`, or `ERROR`. By default, scripts are in the `UNKNOWN` state when first discovered. |
| 22 | + |
| 23 | +## Getting started |
| 24 | + |
| 25 | +To begin, create a script called `test.sf.sql` with the following SQL statements: |
| 26 | + |
| 27 | +```sql |
| 28 | +CREATE DATABASE foobar123; |
| 29 | +CREATE DATABASE test123; |
| 30 | +SHOW DATABASES; |
| 31 | +``` |
| 32 | + |
| 33 | +Mount the script into `/etc/localstack/init/ready.d/` using Docker Compose or the `localstack` CLI: |
| 34 | + |
| 35 | +{{< tabpane >}} |
| 36 | +{{< tab header="docker-compose.yml" lang="yml" >}} |
| 37 | +version: "3.8" |
| 38 | + |
| 39 | +services: |
| 40 | + localstack: |
| 41 | + container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}" |
| 42 | + image: localstack/snowflake |
| 43 | + ports: |
| 44 | + - "127.0.0.1:4566:4566" |
| 45 | + - "127.0.0.1:4510-4559:4510-4559" |
| 46 | + - "127.0.0.1:443:443" |
| 47 | + environment: |
| 48 | + - LOCALSTACK_AUTH_TOKEN=${LOCALSTACK_AUTH_TOKEN:?} |
| 49 | + - DEBUG=1 |
| 50 | + volumes: |
| 51 | + - "/path/to/test.sf.sql:/etc/localstack/init/ready.d/test.sf.sql" # ready hook |
| 52 | + - "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack" |
| 53 | + - "/var/run/docker.sock:/var/run/docker.sock" |
| 54 | +{{< /tab >}} |
| 55 | +{{< tab header="CLI" lang="bash" >}} |
| 56 | +# DOCKER_FLAGS are additional parameters to the `docker run` command of localstack start |
| 57 | + |
| 58 | +DOCKER_FLAGS='-v /path/to/test.sf.sql:/etc/localstack/init/ready.d/test.sf.sql' DEBUG=1 localstack start |
| 59 | +{{< /tab >}} |
| 60 | +{{< /tabpane >}} |
| 61 | + |
| 62 | +Start the Snowflake emulator, and the following logs will appear: |
| 63 | + |
| 64 | +```bash |
| 65 | +DEBUG --- [et.reactor-0] s.analytics.handler : REQ: POST /queries/v1/query-request {"sqlText": "CREATE DATABASE foobar123", ... |
| 66 | +DEBUG --- [et.reactor-0] s.analytics.handler : REQ: POST /queries/v1/query-request {"sqlText": "CREATE DATABASE test123", ... |
| 67 | +DEBUG --- [et.reactor-0] s.analytics.handler : REQ: POST /queries/v1/query-request {"sqlText": "SHOW DATABASES", ... |
| 68 | +``` |
0 commit comments