You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This page contains easily customisable snippets to show you how to manage LocalStack in a GitLab CI pipeline.
9
+
This page contains easily customizable snippets to show you how to manage LocalStack in a GitLab CI pipeline with the [`lstk` CLI](/aws/developer-tools/running-localstack/lstk/).
10
10
11
-
## Snippets
12
-
13
-
### Start up Localstack
11
+
GitLab runs your job in one container and the Docker daemon in another, so every snippet below pairs the job with a Docker-in-Docker (`dind`) service.
14
12
15
13
:::tip
16
14
While working with a Docker-in-Docker (`dind`) setup, the Docker runner requires `privileged` mode.
17
15
You must always use `privileged = true` in your GitLab CI's `config.toml` file while setting up LocalStack in GitLab CI runners.
18
16
For more information, see [GitLab CI Docker-in-Docker](https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor) documentation.
19
17
:::
20
18
19
+
## Snippets
20
+
21
+
### Start up LocalStack
22
+
21
23
LocalStack requires a [CI Auth Token](https://app.localstack.cloud/workspace/auth-tokens), which you must add to the repository's environment variables as `LOCALSTACK_AUTH_TOKEN`.
22
24
Go to your project's **Settings > CI/CD** and expand the **Variables** section.
23
25
Select the **Add Variable** button and fill in the necessary details with `LOCALSTACK_AUTH_TOKEN` as the key and your CI Auth Token as the value.
@@ -26,141 +28,181 @@ After you create the variable, you can use it in the `.gitlab-ci.yml` file.
26
28
However, variables set in the GitLab UI are not automatically passed down to service containers.
27
29
You need to assign them as variables in the UI, and then re-assign them in your `.gitlab-ci.yml`.
28
30
29
-
#### Service
31
+
#### Container
32
+
33
+
In this setup, `lstk` owns the emulator's lifecycle: `DOCKER_HOST` points it at the `dind` daemon, and `lstk start` runs the emulator container there.
`lstk start` pulls the image, validates your license, and returns only once the emulator is ready, so no separate wait step is needed.
67
+
Because the emulator runs on the `dind` daemon, its ports are published on the `docker` service rather than on the job container.
68
+
The `/etc/hosts` entry and `LOCALSTACK_HOST` are what let `lstk` and your tests reach it at `localhost.localstack.cloud:4566`; without them `lstk` falls back to `127.0.0.1`, where nothing is listening.
69
+
70
+
:::note
71
+
`lstk`bind-mounts the Docker socket into the emulator, and sets the emulator's own `DOCKER_HOST`, only when it reaches the daemon over a Unix socket.
72
+
A TCP `dind` daemon has no socket to mount, so services that spawn their own containers (Lambda, ECS, EKS) need the daemon address passed in explicitly.
73
+
`lstk start`forwards `LOCALSTACK_`-prefixed variables to the emulator, which strips the prefix, so set `LOCALSTACK_DOCKER_HOST` to the `dind` daemon as seen from inside the `dind` network (its bridge gateway, usually `tcp://172.17.0.1:2375`).
74
+
:::
75
+
76
+
#### Service
77
+
78
+
Alternatively, run LocalStack as a GitLab service container and use `lstk` purely as a client, pointing it at the service with `LSTK_ENDPOINT_URL`.
79
+
GitLab passes the job's `variables` to service containers too, so the emulator picks up both the auth token and the Docker connection directly, with no prefixing required.
You can check the logs of the LocalStack container to see if the activation was successful.
83
-
If the CI Auth Token activation fails, LocalStack container will exit with an error code.
116
+
GitLab starts service containers before the job's first command, but does not wait for them to become ready, hence the health poll.
84
117
85
-
### Dump Localstack logs
118
+
### Dump LocalStack logs
86
119
87
120
```yaml showshowLineNumbers
88
121
...
89
122
job:
90
-
variables:
91
-
LOCALSTACK_HOST: <LS_HOST>:<LS_PORT>
92
123
script:
93
-
- localstack logs | tee localstack.log
94
-
...
124
+
- set +e
125
+
- <your test command>; status=$?
126
+
- lstk logs --verbose | tee localstack.log
127
+
- exit $status
128
+
artifacts:
129
+
when: always
130
+
paths:
131
+
- localstack.log
132
+
...
95
133
```
96
134
97
-
In case of the service setup `LOCALSTACK_HOST` will be `localstack:4566`.
135
+
Collect the logs as the last `script` step rather than in `after_script`, where the emulator container is no longer reachable.
136
+
Capturing the test command's exit code keeps the job's result intact while still writing the logs after a failing test, which is when they matter most.
137
+
138
+
In the [Service](#service) setup, `lstk logs` is not available, because `lstk` does not manage the service container.
139
+
Set `CI_DEBUG_SERVICES: "true"` to have GitLab stream the service container's logs into the job log instead.
98
140
99
-
### Store Localstack state
141
+
### Store LocalStack state
100
142
101
-
You can preserve your AWS infrastructure with Localstack in various ways.
143
+
You can preserve your AWS infrastructure with LocalStack in various ways.
Additional information about state export and import[here](/aws/developer-tools/snapshots/saving-snapshots-locally/).
185
+
Additional information about snapshots[here](/aws/developer-tools/snapshots/saving-snapshots-locally/).
144
186
145
187
#### Cloud Pod
146
188
147
189
```yaml showshowLineNumbers
148
190
...
149
191
job:
150
192
before_script:
151
-
- localstack pod load <POD_NAME> || true
193
+
- lstk load pod:<POD_NAME> || true
152
194
script:
153
195
...
154
-
- localstack pod save <POD_NAME>
196
+
- lstk save pod:<POD_NAME>
155
197
...
156
198
```
157
199
158
-
Find more information about cloud pods[here](/aws/developer-tools/snapshots/cloud-pods).
200
+
Find more information about Cloud Pods[here](/aws/developer-tools/snapshots/cloud-pods).
159
201
160
202
## Current Limitations
161
203
162
-
-Localstack must be able to reach a docker socket to provision containers for certain services, ie Lambda, EKS, ECS...etc
163
-
-the runner must be able to resolve the Localstack domain (by default _localhost.localstack.cloud_), see the sample pipelines for a possible solution
164
-
-to be able to separate steps into their own jobs one must preserve Localstack's state, since Gitlab is not preserving jobrelated containers/services during the pipelines
165
-
- to start up Localstack in Gitlab CI Docker tools are necessary
166
-
-when Localstack run as a container, it's not accessible during the `after_script` phase
204
+
-LocalStack must be able to reach a Docker socket to provision containers for certain services, such as Lambda, EKS, and ECS.
205
+
-The runner must be able to resolve the LocalStack domain (by default _localhost.localstack.cloud_); see the sample pipelines for a possible solution.
206
+
-To separate steps into their own jobs, you must preserve LocalStack's state, since GitLab does not preserve job-related containers or services across a pipeline.
207
+
-Docker tooling is necessary to start up LocalStack in GitLab CI.
208
+
-When LocalStack runs as a container, it is not accessible during the `after_script` phase.
0 commit comments