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,
localstack/localstack:1.2.0
). - If you have a LocalStack API key, add a
LOCALSTACK_API_KEY
environment variable in the Environment Variables section. - Click Apply Changes.
Add Step
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.
- Enter
curlimages/curl:7.83.1
in 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:
pipeline:
name: my_pipeline
identifier: my_pipeline
projectIdentifier: My_Project
orgIdentifier: default
tags: {}
stages:
- stage:
name: Stage 1
identifier: Stage_1
description: ""
type: CI
spec:
cloneCodebase: false
platform:
os: Linux
arch: Amd64
runtime:
type: Cloud
spec: {}
execution:
steps:
- step:
type: Background
name: localstack
identifier: localstack
spec:
connectorRef: docker_hub
image: localstack/localstack:1.2.0
shell: Sh
envVariables:
LOCALSTACK_API_KEY: <+secrets.getValue("localstack-api-key")>
- step:
type: Run
name: localstack health
identifier: localstack_health
spec:
connectorRef: docker_hub
image: curlimages/curl:7.83.1
shell: Sh
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.
- The
localstack
step logs should show a line similar to this:Running on https://0.0.0.0:4566 (CTRL + C to quit)
Ready. - The
localstack health
step should complete successfully when the LocalStack service is healthy.