Skip to content

[Bug]: Localstack don't override AWS Credentials #9798

@oflorez-airgraft

Description

@oflorez-airgraft

Module

LocalStack

Testcontainers version

1.20.4

Using the latest Testcontainers version?

Yes

Host OS

Linux, Mac

Host Arch

x86

Docker version

Docker version 27.4.0, build bde2b89

What happened?

based in this example https://testcontainers.com/guides/testing-aws-service-integrations-using-localstack/#_further_reading, i created an integration test for SQS, and i notice that test is taking my local AWS configuration instead of TestContainers and is creating many queues in my real account

Relevant log output

Additional Information

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@testcontainers
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
class IntegrationTest {

private static final String QUEUE = UUID.randomUUID().toString();

@Container
static LocalStackContainer localStack =
        new LocalStackContainer(DockerImageName.parse("localstack/localstack:latest"))
                .withServices(SQS);

@Autowired
ObjectMapper objectMapper;
@Autowired
SqsTemplate sqsTemplate;


@DynamicPropertySource
static void overrideProperties(DynamicPropertyRegistry registry) {

    registry.add("spring.cloud.aws.credentials.access-key", () -> localStack.getAccessKey());
    registry.add("spring.cloud.aws.credentials.secret-key", () -> localStack.getSecretKey());
    registry.add("spring.cloud.aws.sqs.endpoint", () -> localStack.getEndpointOverride(SQS).toString());
    registry.add("spring.cloud.aws.region.static", () -> localStack.getRegion());
    registry.add("queue", () -> QUEUE);

}

@BeforeAll
static void beforeAll() throws IOException, InterruptedException {
    localStack.execInContainer("awslocal", "sqs", "create-queue", "--queue-name", QUEUE);
}

@Test
void test() {
    var request = generateRequest();
    var requestString = convertRequestToString(request);

    sqsTemplate.send(QUEUE, requestString);

}

private Request generateRequest() {
    return new Request(
            UUID.randomUUID().toString(),
    );
}

private String convertRequestToString(Request request) {
    try {
        return objectMapper.writeValueAsString(request);
    } catch (JsonProcessingException e) {
        throw new RuntimeException(e);
    }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions