Language SDKs to Use with LocalStack

Language SDKs to Use with LocalStack

·

3 min read

Is your application interacting with Amazon Web Services? No worries! AWS provides client libraries and SDKs for a wide range of programming languages, here are just a few of them:

As promised, LocalStack can be a drop-in replacement for the most popular AWS services, which means there are many ways of easily configuring your clients to point to a different endpoint.

These client libraries allow developers to interact with various AWS/LocalStack services and APIs more easily and efficiently from within your preferred programming language. Each SDK typically provides a set of APIs, classes, and methods that abstract the low-level details of making HTTP requests and handling authentication, making it easier to integrate AWS services into your applications.

The TL;DR part: Here are some easy ways you can configure an S3 client in a few different other languages. Notice how similar they are and how you can always transition between AWS and LocalStack with just a few variables:

This will be a follow-along type of article :). Let’s have a look at an S3 Java client.

  1. Clone the repository.

     git clone <https://github.com/tinyg210/stack-bytes-apigw-lambda-s3.git>
    
  2. Export your LOCALSTACK_API_KEY as an environment variable.

     export LOCALSTACK_API_KEY=<YOUR_API_KEY>
    

    *Sidenote: make sure there’s apigw-lambda.jar in the /stack-bytes-lambda/target/ folder. If not, or if anything fails, please run mvn clean package shade:shade in the stack-bytes-lambda folder.

  3. Start LocalStack (we stay in the root folder).

     docker compose up
    

    Let’s have a look at our setup’s diagram:

    Notice how we have a small Java app, with an S3 client (in the S3Configs class) on the right-hand side.

  4. Switch to the stack-bytes-sdk folder where the new app resides.

     cd stack-bytes-sdk
    
     mvn clean package
    

    The S3 Java Client:

     private static final String ACCESS_KEY = "test";
     private static final String SECRET_KEY = "test";
    
     private static final String LOCALSTACK_ENDPOINT = "https://s3.localhost.localstack.cloud:4566";
    
     private static Region region = Region.US_EAST_1;
       protected static final String BUCKET_NAME = "quotes";
       // create an S3 client
       protected static S3Client s3Client = S3Client.builder()
           .endpointOverride(URI.create(LOCALSTACK_ENDPOINT))
           .credentialsProvider(StaticCredentialsProvider.create(
               AwsBasicCredentials.create(ACCESS_KEY, SECRET_KEY)))
           .region(region)
           .build();
    

    The Java SDK client has an easy and intuitive way of adding the necessary configurations so that everything can be replaced with real values and continue to work on the AWS platform.

  5. Let’s post a file to the S3 bucket using our new client in the S3PostRequest class:

     mvn exec:java -Dexec.mainClass="s3service.S3PostRequest"
    

    This will take an existing file in the src/main/resources folder and add it to the quotes bucket.

  6. Let’s read it now, using the same client:

     mvn exec:java -Dexec.mainClass="s3service.S3GetRequest"
    
     Object text: Author: Fiona
     Quote: I want what any princess wants - to live happily ever after... with the ogre I married.
    
  7. Now, let’s retrieve the same object using a different client and the dedicated Lambda function:

     curl --location 'http://id12345.execute-api.localhost.localstack.cloud:4566/dev/quoteApi?author=Fiona'
    
     {"text":"Quote: I want what any princess wants - to live happily ever after... with the ogre I married."}
    

The output formats differ, as this is the processed output of the GET Lambda, but essentially, they are the same.

For this case, we used the Java client, but don’t worry, the LocalStack Developer Hub is packed with examples using different other programming languages.

Nothing can stop you from using LocalStack as a drop-in replacement for AWS now. With minimal configurations, your applications won’t know the difference.