AWS Lambda with LocalStack S3 with Java SDK

349 views Asked by At

I have installed S3 on my LocalStack instance. I am attempting to access data I have inserted into some buckets on my S3 instance using a Lambda Function. The code that I use to do this is the following:

AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials()))
        .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("s3.localhost.localstack.cloud:4566",
                Regions.US_EAST_1.getName()))
        .build();

S3Object s3Object = s3Client.getObject("bucketName", "Key.sql");

The code I use to set the credentials is the following:

public AWSCredentials credentials() {
    AWSCredentials credentials = new BasicAWSCredentials(
            "test",
            "test"
    );
    return credentials;
}

From my Docker instance in the Docker LocalStack container, I go to the command line for the terminal and I run the following command:

awslocal lambda invoke --function-name optimization-data-loader /dev/stdout

When the execution runs, I receive the following result:

Unable to execute HTTP request: Connect to bucketName.localhost.localstack.cloud:4566 [bucketName.s3.localhost.localstack.cloud/127.0.0.1] failed: Connection refused (Connection refused)

I know for a fact that the S3 bucket is active and has data within it because I can access it directly via my browser.

1

There are 1 answers

0
SemperFenix On

I had a similar issue and resolved it by creating the S3Client this way :

`AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(credentials())).endpointOverride(URI.create("https://localhost.localstack.cloud:4566"))
            .forcePathStyle(true)
            .region(Region.US_EAST_1).build();`

The forcePathStyle was the key.