|
| 1 | +# LocalStack Configuration Guide |
| 2 | + |
| 3 | +This guide covers configuration options for customizing LocalStack container behavior in .NET Aspire applications. |
| 4 | + |
| 5 | +## Quick Reference |
| 6 | + |
| 7 | +| Option | Type | Default | Description | |
| 8 | +|--------|------|---------|-------------| |
| 9 | +| `EagerLoadedServices` | `IReadOnlyCollection<AwsService>` | `[]` (empty) | AWS services to pre-load at container startup | |
| 10 | +| `Lifetime` | `ContainerLifetime` | `Persistent` | Container lifecycle behavior | |
| 11 | +| `DebugLevel` | `int` | `0` | LocalStack DEBUG flag (0 or 1) | |
| 12 | +| `LogLevel` | `LocalStackLogLevel` | `Error` | LocalStack LS_LOG level | |
| 13 | +| `AdditionalEnvironmentVariables` | `IDictionary<string, string>` | `{}` (empty) | Custom environment variables | |
| 14 | + |
| 15 | +## Service Loading Strategy |
| 16 | + |
| 17 | +LocalStack supports two service loading strategies via the [`EAGER_SERVICE_LOADING`](https://docs.localstack.cloud/aws/capabilities/config/configuration/#core) configuration. |
| 18 | + |
| 19 | +### Lazy Loading (Default) |
| 20 | + |
| 21 | +Services start only when first accessed. This provides faster initial container startup but adds latency to the first request that uses each service. |
| 22 | + |
| 23 | +```csharp |
| 24 | +var localstack = builder.AddLocalStack(); // No eager loading - uses lazy loading |
| 25 | +``` |
| 26 | + |
| 27 | +**When to use:** |
| 28 | + |
| 29 | +- Local development (faster container startup) |
| 30 | +- Exploratory development where service usage isn't predictable |
| 31 | +- When working with many services but only using a few |
| 32 | + |
| 33 | +### Eager Loading |
| 34 | + |
| 35 | +Pre-loads specific AWS services during container startup. The container takes longer to start but subsequent requests have no cold-start latency. |
| 36 | + |
| 37 | +```csharp |
| 38 | +builder.AddLocalStack(configureContainer: container => |
| 39 | +{ |
| 40 | + container.EagerLoadedServices = [AwsService.Sqs, AwsService.DynamoDB, AwsService.S3]; |
| 41 | +}); |
| 42 | +``` |
| 43 | + |
| 44 | +This sets the [`EAGER_SERVICE_LOADING=1`](https://docs.localstack.cloud/aws/capabilities/config/configuration/#core) and [`SERVICES`](https://docs.localstack.cloud/aws/capabilities/config/configuration/#core) environment variables automatically. |
| 45 | + |
| 46 | +**When to use:** |
| 47 | + |
| 48 | +- CI/CD pipelines (consistent startup, no cold-start flakiness) |
| 49 | +- Integration tests (eliminate service initialization variance) |
| 50 | +- When you know exactly which services you'll use |
| 51 | + |
| 52 | +**Available Services:** |
| 53 | + |
| 54 | +You can eagerly load any AWS service supported by LocalStack. Common examples: |
| 55 | + |
| 56 | +```csharp |
| 57 | +container.EagerLoadedServices = |
| 58 | +[ |
| 59 | + AwsService.Sqs, // Simple Queue Service |
| 60 | + AwsService.DynamoDB, // DynamoDB |
| 61 | + AwsService.S3, // S3 Storage |
| 62 | + AwsService.Sns, // Simple Notification Service |
| 63 | + AwsService.Lambda, // Lambda Functions |
| 64 | + AwsService.CloudFormation,// CloudFormation |
| 65 | + AwsService.SecretsManager,// Secrets Manager |
| 66 | + AwsService.Ssm, // Systems Manager (Parameter Store) |
| 67 | + // ... see LocalStack docs for full list |
| 68 | +]; |
| 69 | +``` |
| 70 | + |
| 71 | +For a complete list of supported services, check the [LocalStack health endpoint](https://docs.localstack.cloud/aws/capabilities/networking/internal-endpoints/#localstack-endpoints) at `/_localstack/health`. |
| 72 | + |
| 73 | +## Container Lifetime |
| 74 | + |
| 75 | +Controls when the LocalStack container is created and destroyed. |
| 76 | + |
| 77 | +### Persistent (Default) |
| 78 | + |
| 79 | +Container survives application restarts. Data may persist between debugging sessions depending on your configuration. |
| 80 | + |
| 81 | +```csharp |
| 82 | +container.Lifetime = ContainerLifetime.Persistent; |
| 83 | +``` |
| 84 | + |
| 85 | +**When to use:** |
| 86 | + |
| 87 | +- Local development (container reuse between runs) |
| 88 | +- When combined with [LocalStack persistence](https://docs.localstack.cloud/aws/capabilities/state-management/persistence/) |
| 89 | + |
| 90 | +### Session |
| 91 | + |
| 92 | +Container is created when the application starts and destroyed when it stops. |
| 93 | + |
| 94 | +```csharp |
| 95 | +container.Lifetime = ContainerLifetime.Session; |
| 96 | +``` |
| 97 | + |
| 98 | +**When to use:** |
| 99 | + |
| 100 | +- CI/CD pipelines (clean slate for each run) |
| 101 | +- Integration tests (isolated test runs) |
| 102 | +- When you want guaranteed clean state |
| 103 | + |
| 104 | +**Recommendation:** Use `Session` for CI/CD and integration tests, `Persistent` for local development. |
| 105 | + |
| 106 | +## Logging and Debugging |
| 107 | + |
| 108 | +### Debug Level |
| 109 | + |
| 110 | +Controls LocalStack's [`DEBUG`](https://docs.localstack.cloud/aws/capabilities/config/configuration/#core) flag for increased log verbosity. |
| 111 | + |
| 112 | +```csharp |
| 113 | +container.DebugLevel = 1; // Enable verbose logging |
| 114 | +``` |
| 115 | + |
| 116 | +**Values:** |
| 117 | + |
| 118 | +- `0` (default): Standard log level |
| 119 | +- `1`: Increased log level with more verbose logs (useful for troubleshooting) |
| 120 | + |
| 121 | +### Log Level |
| 122 | + |
| 123 | +Controls LocalStack's [`LS_LOG`](https://docs.localstack.cloud/aws/capabilities/config/configuration/#core) environment variable. |
| 124 | + |
| 125 | +```csharp |
| 126 | +container.LogLevel = LocalStackLogLevel.Debug; |
| 127 | +``` |
| 128 | + |
| 129 | +**Available Levels:** |
| 130 | + |
| 131 | +- `Trace`: Detailed request/response logging |
| 132 | +- `TraceInternal`: Internal calls logging |
| 133 | +- `Debug`: Debug level logging |
| 134 | +- `Info`: Info level logging |
| 135 | +- `Warn`: Warning level logging |
| 136 | +- `Error` (default): Error level logging |
| 137 | +- `Warning`: Alias for `Warn` |
| 138 | + |
| 139 | +**Note:** `LS_LOG` currently overrides the `DEBUG` configuration in LocalStack. |
| 140 | + |
| 141 | +For more details on LocalStack logging, see the [official logging documentation](https://docs.localstack.cloud/aws/capabilities/config/logging/). |
| 142 | + |
| 143 | +## Advanced Environment Variables |
| 144 | + |
| 145 | +For advanced scenarios, you can pass custom environment variables to the LocalStack container: |
| 146 | + |
| 147 | +```csharp |
| 148 | +container.AdditionalEnvironmentVariables["LOCALSTACK_API_KEY"] = "your-pro-key"; |
| 149 | +container.AdditionalEnvironmentVariables["PERSISTENCE"] = "1"; |
| 150 | +``` |
| 151 | + |
| 152 | +**Important:** Do not manually set `SERVICES` or `EAGER_SERVICE_LOADING` - these are managed automatically when using `EagerLoadedServices`. An exception will be thrown if conflicts are detected. |
| 153 | + |
| 154 | +**Common Use Cases:** |
| 155 | + |
| 156 | +- LocalStack Pro features ([`LOCALSTACK_API_KEY`](https://docs.localstack.cloud/aws/capabilities/config/configuration/#localstack-pro)) |
| 157 | +- [Persistence configuration](https://docs.localstack.cloud/aws/capabilities/state-management/persistence/) (`PERSISTENCE`) |
| 158 | +- Custom [configuration options](https://docs.localstack.cloud/aws/capabilities/config/configuration/) |
| 159 | + |
| 160 | +## Configuration Patterns |
| 161 | + |
| 162 | +### Development |
| 163 | + |
| 164 | +```csharp |
| 165 | +builder.AddLocalStack(configureContainer: container => |
| 166 | +{ |
| 167 | + container.Lifetime = ContainerLifetime.Persistent; |
| 168 | + container.LogLevel = LocalStackLogLevel.Warn; |
| 169 | + // Use lazy loading for faster startup |
| 170 | +}); |
| 171 | +``` |
| 172 | + |
| 173 | +### CI/CD |
| 174 | + |
| 175 | +```csharp |
| 176 | +builder.AddLocalStack(configureContainer: container => |
| 177 | +{ |
| 178 | + container.Lifetime = ContainerLifetime.Session; |
| 179 | + container.LogLevel = LocalStackLogLevel.Error; |
| 180 | + |
| 181 | + // Eagerly load all services used in tests |
| 182 | + container.EagerLoadedServices = [AwsService.Sqs, AwsService.DynamoDB, AwsService.S3]; |
| 183 | +}); |
| 184 | +``` |
| 185 | + |
| 186 | +### Debugging |
| 187 | + |
| 188 | +```csharp |
| 189 | +builder.AddLocalStack(configureContainer: container => |
| 190 | +{ |
| 191 | + container.Lifetime = ContainerLifetime.Session; |
| 192 | + container.LogLevel = LocalStackLogLevel.Debug; |
| 193 | + container.DebugLevel = 1; |
| 194 | + |
| 195 | + // Eagerly load to see startup issues |
| 196 | + container.EagerLoadedServices = [AwsService.Sqs]; |
| 197 | +}); |
| 198 | +``` |
| 199 | + |
| 200 | +### Integration Testing |
| 201 | + |
| 202 | +```csharp |
| 203 | +builder.AddLocalStack(configureContainer: container => |
| 204 | +{ |
| 205 | + container.Lifetime = ContainerLifetime.Session; |
| 206 | + container.LogLevel = LocalStackLogLevel.Error; |
| 207 | + |
| 208 | + // Eagerly load services to avoid cold-start variance |
| 209 | + container.EagerLoadedServices = [AwsService.Sqs, AwsService.DynamoDB]; |
| 210 | +}); |
| 211 | +``` |
| 212 | + |
| 213 | +## Troubleshooting |
| 214 | + |
| 215 | +### Environment Variable Conflicts |
| 216 | + |
| 217 | +If you get an error about environment variable conflicts: |
| 218 | + |
| 219 | +```text |
| 220 | +InvalidOperationException: Cannot set 'SERVICES' or 'EAGER_SERVICE_LOADING' |
| 221 | +in AdditionalEnvironmentVariables when using EagerLoadedServices. |
| 222 | +``` |
| 223 | + |
| 224 | +**Solution:** Remove manual `SERVICES` or `EAGER_SERVICE_LOADING` from `AdditionalEnvironmentVariables`: |
| 225 | + |
| 226 | +```csharp |
| 227 | +// ❌ DON'T DO THIS |
| 228 | +container.AdditionalEnvironmentVariables["SERVICES"] = "sqs,dynamodb"; |
| 229 | +container.EagerLoadedServices = [AwsService.S3]; // Conflict! |
| 230 | +
|
| 231 | +// ✅ DO THIS |
| 232 | +container.EagerLoadedServices = [AwsService.Sqs, AwsService.DynamoDB, AwsService.S3]; |
| 233 | +``` |
| 234 | + |
| 235 | +### Container Health Checks Fail |
| 236 | + |
| 237 | +If health checks fail after container starts: |
| 238 | + |
| 239 | +1. **Enable verbose logging:** |
| 240 | + |
| 241 | + ```csharp |
| 242 | + container.DebugLevel = 1; |
| 243 | + container.LogLevel = LocalStackLogLevel.Debug; |
| 244 | + ``` |
| 245 | + |
| 246 | +2. **Check Aspire Dashboard:** Look at health check details for specific failing services |
| 247 | + |
| 248 | +3. **Try eager loading:** Some services may benefit from eager loading: |
| 249 | + |
| 250 | + ```csharp |
| 251 | + container.EagerLoadedServices = [AwsService.Sqs]; |
| 252 | + ``` |
| 253 | + |
| 254 | +### Data Not Persisting |
| 255 | + |
| 256 | +If you expect data to persist between runs: |
| 257 | + |
| 258 | +1. **Check container lifetime:** Ensure `ContainerLifetime.Persistent` is set |
| 259 | +2. **Enable persistence:** See [LocalStack Persistence documentation](https://docs.localstack.cloud/aws/capabilities/state-management/persistence/) |
| 260 | + |
| 261 | + ```csharp |
| 262 | + container.AdditionalEnvironmentVariables["PERSISTENCE"] = "1"; |
| 263 | + ``` |
| 264 | + |
| 265 | +## Best Practices |
| 266 | + |
| 267 | +1. **Use Session lifetime in CI/CD** - Ensures clean state for each test run |
| 268 | +2. **Eagerly load services in CI** - Eliminates cold-start variability in automated tests |
| 269 | +3. **Keep logging minimal in CI** - Use `LogLevel.Error` for cleaner output |
| 270 | +4. **Use Persistent lifetime for development** - Faster iteration with container reuse |
| 271 | +5. **Start with lazy loading** - Only use eager loading when you have a specific need |
| 272 | +6. **Limit eager loaded services** - Only pre-load services you actually use |
| 273 | +7. **Document your configuration** - Add comments explaining configuration choices |
| 274 | + |
| 275 | +## Related Documentation |
| 276 | + |
| 277 | +- [README](https://github.com/localstack-dotnet/dotnet-aspire-for-localstack/blob/master/README.md) - Getting started guide |
| 278 | +- [LocalStack Configuration](https://docs.localstack.cloud/aws/capabilities/config/configuration/) - Official LocalStack configuration reference |
| 279 | +- [LocalStack Persistence](https://docs.localstack.cloud/aws/capabilities/state-management/persistence/) - Data persistence options |
| 280 | +- [LocalStack.NET Client](https://github.com/localstack-dotnet/localstack-dotnet-client) - Client-side configuration |
0 commit comments