Run LocalStack as a Background Step
Background steps are useful for running services that need to run for the entire lifetime of a Build stage. This tutorial shows how to run LocalStack as a Background step in a Harness CI pipeline. LocalStack is software that emulates cloud services (such as AWS) when developing locally, or for testing in continuous integration pipelines.
If you don't have a Harness account yet, you can create one for free at app.harness.io
Create Your Pipeline
- Create a new pipeline in Harness CI.
- Click Add Stage and select Build. Give your stage a name, optionally configure the repository to be cloned, then click Set Up Stage.
- Select "Cloud" in the Infrastructure tab.
Add Background Step
This will run the LocalStack Docker image as a Background step in your pipeline.
- In the Execution tab of your pipeline stage, click Add Step. The Step Library dialogue window should appear, select the Background step.
- Enter "localstack" in the Name field.
- Expand the Additional Configuration section, click the Container Registry field and either select an existing Docker Hub connector, or create one.
- Enter the desired LocalStack Docker image in the Image field (for example,
- If you have a LocalStack API key, add a
LOCALSTACK_API_KEYenvironment variable in the Environment Variables section.
- Click Apply Changes.
This will add a step to ensure the LocalStack service is healthy. The step will run the
curl command to poll the LocalStack service's
/health endpoint until it returns successfully. This ensures that LocalStack is ready to receive traffic before the pipeline continues.
- In the Execution tab of your pipeline stage, click Add Step then select Run.
- Enter "localstack health" in the Name field.
- Enter the following command in the Command field.
until curl --fail --silent --max-time 1 http://localstack:4566/health; do sleep 2; done
- Expand the Optional Configuration section, click the Container Registry field and select your Docker Hub connector.
curlimages/curl:7.83.1in the Images field.
- Click Apply Changes.
Notice that the
curl command is able to reach the LocalStack service at
localstack:4566. This is because both the service and step share the same Docker network.
Optional YAML Configuration
If you switch from Visual to YAML in the Pipeline Studio, your pipeline should look similar to this:
name: Stage 1
name: localstack health
command: until curl --fail --silent --max-time 1 http://localstack:4566/health; do sleep 2; done
Run Your Pipeline
- Click the Save button, then click Run.
- Click Run Pipeline in the Run Pipeline dialogue window.
- Observe your pipeline execution.
localstackstep logs should show a line similar to this:
Running on https://0.0.0.0:4566 (CTRL + C to quit)
localstack healthstep should complete successfully when the LocalStack service is healthy.