0

I am running spring boot application and localstack with docker compose, it seems that I configured correctly but still throws the exception. my compose file:

version: '3.9'

services:
  localstack:
    container_name: "localstack"
    image: localstack/localstack
    ports:
      - "8080:8080"
      - "4566:4566"
      - "4510-4559:4510-4559"
    environment:
    DEBUG: 1
    volumes:
      - "./localstack/vol:/var/lib/localstack"
      - "./localstack/init:/etc/localstack/init"
    healthcheck:
    test: \[ "CMD", "curl", "-f", "http://localhost:4566/_localstack/health" \]
    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 30s
    networks:
      - my_network

resource-service:
  build: ./resource-service/.
  container_name: resource-service
  depends_on:
   - localstack
  ports:
   - "8083:8083"
  environment:
    SERVER_PORT: ${RESOURCE_SERVER_PORT}
    AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}
    AWS_SECRET_KEY: ${AWS_SECRET_KEY}
    AWS_ENDPOINT_URL: ${AWS_ENDPOINT_URL}
  env_file:
   - .env
  healthcheck:
    test: \[ "CMD", "curl", "-f", "http://localhost:8083/actuator/health" \]
    interval: 30s
    timeout: 10s
    retries: 3
    start_period: 30s
  networks:
   - my_network

networks:
  my_network:
    driver: bridge

I test connectivity with command from resource-service

docker exec -it resource-service /bin/sh 

/app # curl http://localstack:4566/_localstack/health 

and got the message:

{"services": {"acm": "available", "apigateway": "available", "cloudformation": "available", "cloudwatch": "available", "config": "available", "dynamodb": "available", "dynamodbstreams": "available", "ec2": "available", "es": "available", "events": "available", "firehose": "available", "iam": "available", "kinesis": "available", "kms": "available", "lambda": "available", "logs": "available", "opensearch": "available", "redshift": "available", "resource-groups": "available", "resourcegroupstaggingapi": "available", "route53": "available", "route53resolver": "available", "s3": "running"...}

but when I am trying to upload a file to the bucket I get error: java.net.UnknownHostException: module-1.localstack here, bucket module-01 is created.

in .env file AWS_ENDPOINT_URL is http://localstack:4566 and s3 client is configured in java:

@Bean
public S3Client s3Client() {
    log.info("accessKeyId: {}, endpoint: {}", accessKeyId, endpointUrl);
    return S3Client.builder()
            .region(Region.of(region))
            .endpointOverride(URI.create(endpointUrl))
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsBasicCredentials.create(accessKeyId, secretAccessKey)))
            .build();
}
2

1 Answer 1

0

I have added serviceConfiguration(...)

@Bean
public S3Client s3Client() {
    return S3Client.builder()
            .region(Region.of(region))
            .endpointOverride(URI.create(endpointUrl))
            .serviceConfiguration(
                    S3Configuration.builder()
                            .pathStyleAccessEnabled(true)// Critical for LocalStack
                            .build()
            )
            .credentialsProvider(StaticCredentialsProvider.create(
                    AwsBasicCredentials.create(accessKeyId, secretAccessKey)))
            .build();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.