Spring Cloud AWS
SQS Connection Issue
AWS Service Endpoint
Local Development
Cloud Integration

Spring Cloud AWS SQS fails to connect to service endpoint locally

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Local SQS connection errors in Spring Cloud AWS usually come from endpoint, region, credential, or queue bootstrap mismatches. These issues are especially common with LocalStack because SDK defaults are tuned for real AWS, not local emulation. A stable local setup requires explicit configuration and deterministic startup order.

Confirm LocalStack and Queue Availability First

Before debugging Spring beans, verify emulator health and queue existence.

bash
docker run -d --name localstack -p 4566:4566 localstack/localstack
curl -s http://localhost:4566/_localstack/health

Create and verify queue:

bash
aws --endpoint-url=http://localhost:4566 sqs create-queue --queue-name orders-queue
aws --endpoint-url=http://localhost:4566 sqs list-queues

If queue bootstrap fails at this stage, fix emulator setup first.

Set Endpoint, Region, and Credentials Explicitly

Do not rely on default provider chains during local troubleshooting.

yaml
1spring:
2  cloud:
3    aws:
4      region:
5        static: us-east-1
6      credentials:
7        access-key: test
8        secret-key: test
9      sqs:
10        endpoint: http://localhost:4566

Explicit values prevent accidental fallback to real AWS endpoints.

Define an Explicit SqsClient Bean for Local Profile

If auto-configuration is ambiguous, create a local-profile client bean.

java
1import org.springframework.context.annotation.Bean;
2import org.springframework.context.annotation.Configuration;
3import org.springframework.context.annotation.Profile;
4import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
5import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
6import software.amazon.awssdk.regions.Region;
7import software.amazon.awssdk.services.sqs.SqsClient;
8
9import java.net.URI;
10
11@Configuration
12@Profile("local")
13public class LocalSqsConfig {
14
15    @Bean
16    SqsClient sqsClient() {
17        return SqsClient.builder()
18            .endpointOverride(URI.create("http://localhost:4566"))
19            .region(Region.US_EAST_1)
20            .credentialsProvider(
21                StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test"))
22            )
23            .build();
24    }
25}

This removes ambiguity around endpoint resolution and signing configuration.

Listener Wiring and Smoke Test

Minimal listener:

java
1import io.awspring.cloud.sqs.annotation.SqsListener;
2import org.springframework.stereotype.Component;
3
4@Component
5public class OrderListener {
6
7    @SqsListener("orders-queue")
8    public void onMessage(String payload) {
9        System.out.println("Received: " + payload);
10    }
11}

Send test payload:

bash
aws --endpoint-url=http://localhost:4566 sqs send-message \
  --queue-url http://localhost:4566/000000000000/orders-queue \
  --message-body '{"orderId":101}'

If listener does not receive message, verify queue URL and region match exactly.

Avoid Startup Race Conditions

A frequent failure mode is app startup before queue creation. Use queue bootstrap script or readiness checks in local and CI environments.

Example shell guard:

bash
until aws --endpoint-url=http://localhost:4566 sqs get-queue-url --queue-name orders-queue >/dev/null 2>&1; do
  sleep 1
done

Deterministic startup order saves repeated debugging cycles.

Version Compatibility Notes

Spring Cloud AWS version must align with Spring Boot generation and AWS SDK expectations. If endpoint or credential behavior looks inconsistent, verify dependency versions before changing business code.

Prefer managed dependency BOMs and avoid ad hoc version overrides unless fully tested.

Local Profile Isolation

Keep local SQS config isolated under dedicated profile:

  • Prevent local endpoint leaking into non-local environments.
  • Keep production credentials and region settings separate.
  • Ensure CI integration tests use local profile explicitly.

Isolation reduces risk and makes configuration intent obvious. It also prevents accidental outbound calls to real AWS accounts during local debugging sessions.

Add an End-to-End Smoke Test

A short integration test catches endpoint and queue name regressions early. Run it in CI with the same local profile used by developers.

java
1import org.junit.jupiter.api.Test;
2import software.amazon.awssdk.services.sqs.SqsClient;
3import software.amazon.awssdk.services.sqs.model.SendMessageRequest;
4
5class SqsSmokeTest {
6    private final SqsClient sqsClient = /* inject from Spring context */;
7
8    @Test
9    void canSendMessageToLocalQueue() {
10        sqsClient.sendMessage(SendMessageRequest.builder()
11            .queueUrl("http://localhost:4566/000000000000/orders-queue")
12            .messageBody("health-check")
13            .build());
14    }
15}

Even this small test prevents long debugging sessions caused by subtle config drift.

Common Pitfalls

  • Queue not created before listener startup.
  • LocalStack endpoint configured, but region still mismatched.
  • Missing explicit credentials causing provider-chain confusion.
  • Falling back to real AWS due to wrong property keys.
  • Mixing local and production profile values in one config file.

Summary

  • Most local SQS endpoint failures are configuration and bootstrap-order issues.
  • Verify emulator health and queue existence before Spring debugging.
  • Configure endpoint, region, and credentials explicitly for local profile.
  • Use explicit client bean wiring when auto-configuration is unclear.
  • Keep local and production settings isolated to prevent accidental cross-environment behavior.

Course illustration
Course illustration

All Rights Reserved.