[Spring Boot] Aws Sqs Localstack 사용한 테스트 코드 작성시 SdkClientException 해결

yesjm·2024년 4월 24일
0

SpringBoot/Kotlin

목록 보기
1/6

spring boot에서 aws sqs 설정을 하고 localstack을 활용해 테스트 코드를 작성하는 중 발생한 문제이다.

TestConfiguration 설정을 하고 build 했을때 계속해서 SdkClientException이 발생하고 connection timeout 이 발생해 빌드에 실패했다.

아래는 config 코드이다.

AwsTestConfig.kt

@TestConfiguration
class AwsTestConfig {
    private val imageName: DockerImageName =
        DockerImageName.parse("localstack/localstack")

    @Container
    private val container =
        LocalStackContainer(imageName)
            .withServices(
                LocalStackContainer.Service.S3,
                LocalStackContainer.Service.SQS,
            )

    private val credentialProvider =
        StaticCredentialsProvider.create(
            AwsBasicCredentials.create(
                container.accessKey,
                container.secretKey,
            ),
        )

    @Bean
    fun amazonS3Client(): S3Client {
        container.start()
        return S3Client.builder()
            .credentialsProvider(credentialProvider)
            .region(Region.of(container.region))
            .endpointOverride(container.endpoint)
            .build()
    }

    @Bean
    fun amazonSqsClient(): SqsAsyncClient {
        container.start()
        return SqsAsyncClient.builder()
            .credentialsProvider(credentialProvider)
            .region(Region.of(container.region))
            .endpointOverride(container.endpoint)
            .build()
    }
}

application.yml

spring:
  cloud:
    aws:
      s3:
        endpoint: "http://localhost:4566"
      sqs:
        endpoint: "http://localhost:4566"
      credentials:
        access-key: noop
        secret-key: noop
      region:
        static: us-east-1

config 자체에는 문제가 없었고, properties를 추가해주면 간단하게 해결되는 문제였다.
안되는걸 하루 종일 붙잡고 구글링을 하고있었는데 차분히 살펴보니 답이 있었다.

역시 공식 문서, github이 최고다.

testcontainers/tc-guide-testing-aws-service-integrations-using-localstack
https://github.com/testcontainers/tc-guide-testing-aws-service-integrations-using-localstack/blob/main/guide/testing-aws-service-integrations-using-localstack/index.adoc

profile
yesjm's second brain

0개의 댓글