|
| 1 | +# WireMock on LocalStack |
| 2 | + |
| 3 | +This repo contains a [LocalStack Extension](https://github.com/localstack/localstack-extensions) that facilitates developing [WireMock](https://wiremock.org)-based applications locally. |
| 4 | + |
| 5 | +The extension supports two modes: |
| 6 | + |
| 7 | +- **OSS WireMock**: Uses the open-source `wiremock/wiremock` image (default) |
| 8 | +- **WireMock Runner**: Uses `wiremock/wiremock-runner` with WireMock Cloud integration (requires API token) |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- Docker |
| 13 | +- LocalStack Pro (free trial available) |
| 14 | +- `localstack` CLI |
| 15 | +- `make` |
| 16 | +- [WireMock CLI](https://docs.wiremock.io/cli/overview) (for WireMock Runner mode) |
| 17 | + |
| 18 | +## Install from GitHub repository |
| 19 | + |
| 20 | +This extension can be installed directly from this Github repo via: |
| 21 | + |
| 22 | +```bash |
| 23 | +localstack extensions install "git+https://github.com/localstack/localstack-extensions.git#egg=localstack-wiremock&subdirectory=wiremock" |
| 24 | +``` |
| 25 | + |
| 26 | +## Install local development version |
| 27 | + |
| 28 | +To install the extension into LocalStack in developer mode, you will need Python 3.11, and create a virtual environment in the extensions project. |
| 29 | + |
| 30 | +In the newly generated project, simply run |
| 31 | + |
| 32 | +```bash |
| 33 | +make install |
| 34 | +``` |
| 35 | + |
| 36 | +Then, to enable the extension for LocalStack, run |
| 37 | + |
| 38 | +```bash |
| 39 | +localstack extensions dev enable . |
| 40 | +``` |
| 41 | + |
| 42 | +You can then start LocalStack with `EXTENSION_DEV_MODE=1` to load all enabled extensions: |
| 43 | + |
| 44 | +```bash |
| 45 | +EXTENSION_DEV_MODE=1 localstack start |
| 46 | +``` |
| 47 | + |
| 48 | +## Usage |
| 49 | + |
| 50 | +### OSS WireMock Mode (Default) |
| 51 | + |
| 52 | +Start LocalStack without any special configuration: |
| 53 | + |
| 54 | +```bash |
| 55 | +localstack start |
| 56 | +``` |
| 57 | + |
| 58 | +The WireMock server will be available at `http://wiremock.localhost.localstack.cloud:4566`. |
| 59 | + |
| 60 | +You can import stubs using the WireMock Admin API (assuming a `stubs.json` exists in the local working directory): |
| 61 | + |
| 62 | +```bash |
| 63 | +curl -X POST -H "Content-Type: application/json" \ |
| 64 | + --data-binary "@stubs.json" \ |
| 65 | + "http://wiremock.localhost.localstack.cloud:4566/__admin/mappings/import" |
| 66 | +``` |
| 67 | + |
| 68 | +### WireMock Runner Mode (Cloud Integration) |
| 69 | + |
| 70 | +To use WireMock Runner with WireMock Cloud, you need: |
| 71 | + |
| 72 | +1. A WireMock Cloud API token |
| 73 | +2. A `.wiremock` directory with your mock API configuration |
| 74 | + |
| 75 | +#### Step 1: Get your WireMock Cloud API Token |
| 76 | + |
| 77 | +1. Sign up at [WireMock Cloud](https://app.wiremock.cloud) |
| 78 | +2. Go to Settings → API Tokens |
| 79 | +3. Create a new token |
| 80 | + |
| 81 | +#### Step 2: Create your Mock API configuration |
| 82 | + |
| 83 | +First, create a Mock API in WireMock Cloud, then pull the configuration locally: |
| 84 | + |
| 85 | +```bash |
| 86 | +# Install WireMock CLI if not already installed |
| 87 | +npm install -g @wiremock/cli |
| 88 | + |
| 89 | +# Login with your API token |
| 90 | +wiremock login |
| 91 | + |
| 92 | +# Pull your Mock API configuration |
| 93 | +# Find your Mock API ID from the WireMock Cloud URL (e.g., https://app.wiremock.cloud/mock-apis/zwg1l/...) |
| 94 | +wiremock pull mock-api <mock-api-id> |
| 95 | +``` |
| 96 | + |
| 97 | +This creates a `.wiremock` directory with your `wiremock.yaml` configuration. |
| 98 | + |
| 99 | +#### Step 3: Start LocalStack with WireMock Runner |
| 100 | + |
| 101 | +```bash |
| 102 | +LOCALSTACK_WIREMOCK_API_TOKEN="your-api-token" \ |
| 103 | +LOCALSTACK_WIREMOCK_CONFIG_DIR="/path/to/your/project" \ |
| 104 | +localstack start |
| 105 | +``` |
| 106 | + |
| 107 | +**Environment Variables:** |
| 108 | + |
| 109 | +- `WIREMOCK_API_TOKEN`: Your WireMock Cloud API token (required for runner mode) |
| 110 | +- `WIREMOCK_CONFIG_DIR`: Path to the directory containing your `.wiremock` folder (required for runner mode) |
| 111 | + |
| 112 | +Note: When using the LocalStack CLI, prefix environment variables with `LOCALSTACK_` to forward them to the container. |
| 113 | + |
| 114 | +## Sample Application |
| 115 | + |
| 116 | +See the `sample-app-runner/` directory for a complete example using Terraform that demonstrates: |
| 117 | + |
| 118 | +- Creating an API Gateway |
| 119 | +- Lambda function that calls WireMock stubs |
| 120 | +- Integration testing with mocked external APIs |
0 commit comments