How to use localstack with Gitlab CI

If you’re using a standard style .gitlab-ci.yml format such as the below, it won’t work.

image: ubuntu

services:
  - localstack/localstack:latest

variables:
  SERVICES: s3
  DEFAULT_REGION: eu-west-1
  AWS_ACCESS_KEY_ID: localkey
  AWS_SECRET_ACCESS_KEY: localsecret
  HOSTNAME_EXTERNAL: localstack
  HOSTNAME: localstack
  S3_PORT_EXTERNAL: 4572
  LOCALSTACK_HOSTNAME: localstack

test:python36:
  script:
    - pip install awscli
    - aws s3api list-buckets --endpoint-url=http://localstack:4572
Could not connect to the endpoint URL: "http://localstack:4572/"
ERROR: Job failed: exit code 1

However if you use build stages instead as below, it will work.

stages:
  - test

test-application:
  stage: test
  image: ubuntu
  variables:
    SERVICES: s3:4572
    HOSTNAME_EXTERNAL: localstack 
    DEFAULT_REGION: eu-west-1
    AWS_ACCESS_KEY_ID: localkey
    AWS_SECRET_ACCESS_KEY: localsecret
  services:
    - name: localstack/localstack
      alias: localstack
  script:
    - pip install awscli
    - aws s3api list-buckets --endpoint-url=http://localstack:4572
{
    "Buckets": [],
    "Owner": {
        "DisplayName": "webfile",
        "ID": "bcaf1ffd86f41161ca5fb16fd081034f"
    }
}
Job succeeded

2 thoughts on “How to use localstack with Gitlab CI

  1. cherry

    Hello, I’m trying to pass SQS URL to my application using –env SQS_QUEUE_URL=http://${LOCALSTACK_PORT_4576_TCP_ADDR}:${LOCALSTACK_PORT_4576_TCP_PORT} But I’m getting failed: Connection refused (Connection refused) message.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *