Get started with the SDK for Kotlin
The AWS SDK for Kotlin provides Kotlin APIs for each AWS service. Using the SDK, you can build Kotlin applications that work with Amazon S3, Amazon EC2, Amazon DynamoDB, and more.
This tutorial shows you how to use Gradle to define dependencies for the AWS SDK for Kotlin. Then, you create code that writes data to a DynamoDB table. Although you might want to use the features of an IDE, all you need for this tutorial is a terminal window and a text editor.
Follow these steps to complete this tutorial:
Step 1: Set up for this tutorial
Before you begin this tutorial, you need an IAM Identity Center permission set that can access DynamoDB and a Kotlin development environment configured with IAM Identity Center single sign-on settings to access to AWS.
Follow the instructions in the Basic set up of this guide to get the basics setup for this tutorial.
After you have configured your development environment with single sign-on access for the Kotlin SDK and you have an active AWS access portal session, continue with Step 2.
Step 2: Create the project
To create the project for this tutorial, first use Gradle to create a Kotlin project.
Then, update the gradle.build.kts
file with the required settings and
dependencies for the AWS SDK for Kotlin.
To create a new project using Gradle
Note
Gradle version 8.1 offers the four prompts in step 3 below. If you use a different version of Gradle the prompts might differ.
-
Create a new directory called
getstarted
in a location of your choice, such as your desktop or home folder. -
Open a terminal or command prompt window and navigate to the
getstarted
directory you created. -
Use the following command to create a new Gradle project configuration file (
build.gradle.kts
) and a basic Kotlin class.gradle init --type kotlin-application --dsl kotlin
-
When prompted with
Project name
, pressEnter
. -
When prompted for
Source package
, enterexample.aws.getstarted
. -
When prompted with
Enter target version of Java (min. 7) (default: 11)
, pressEnter
. When prompted with
Generate build using new APIs and behavior
, press theEnter
key.
-
To configure your project with dependencies for the AWS SDK for Kotlin and Amazon S3
-
In the folder
getstarted
that you created in the previous procedure, navigiate to theapp
directory and open thebuild.gradle.kts
file. -
Replace its contents with the following Gradle code, and then save your changes.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { kotlin("jvm") version "1.9.10" application } group = "example.aws" version = "1.0-SNAPSHOT" repositories { mavenCentral() } dependencies { implementation("aws.sdk.kotlin:s3:1.0.0") testImplementation(kotlin("test")) } tasks.withType<Test> { useJUnitPlatform() } tasks.withType<KotlinCompile>() { kotlinOptions.jvmTarget = "17" } application.mainClass.set("example.aws.getstarted.AppKt")
The
dependencies
section contains an entry for the Amazon S3 module of the AWS SDK for Kotlin. The Gradle compiler is configured to use Java 17 in thetasks.withType<KotlinCompile>()
section.Note
For the latest version of the Amazon S3 module of the SDK for Kotlin, see the Maven central repository
and use that value in the following code.
Step 3: Write the code
After the project has been created and configured, edit the project’s default class
App
to use the following example code.
-
In your project folder
app
, navigate to the directorysrc/main/kotlin/example/aws/getstarted
. Open theApp.kt
file. -
Replace its contents with the following code and save the file.
package example.aws.getstarted import aws.sdk.kotlin.services.s3.* import aws.sdk.kotlin.services.s3.model.BucketLocationConstraint import aws.smithy.kotlin.runtime.content.ByteStream import kotlinx.coroutines.runBlocking import java.util.UUID val REGION = "us-west-2" val BUCKET = "bucket-${UUID.randomUUID()}" val KEY = "key" fun main(): Unit = runBlocking { S3Client .fromEnvironment { region = REGION } .use { s3 -> setupTutorial(s3) println("Creating object $BUCKET/$KEY...") s3.putObject { bucket = BUCKET key = KEY body = ByteStream.fromString("Testing with the Kotlin SDK") } println("Object $BUCKET/$KEY created successfully!") cleanUp(s3) } } suspend fun setupTutorial(s3: S3Client) { println("Creating bucket $BUCKET...") s3.createBucket { bucket = BUCKET if (REGION != "us-east-1") { // Do not set location constraint for us-east-1. createBucketConfiguration { locationConstraint = BucketLocationConstraint.fromValue(REGION) } } } println("Bucket $BUCKET created successfully!") } suspend fun cleanUp(s3: S3Client) { println("Deleting object $BUCKET/$KEY...") s3.deleteObject { bucket = BUCKET key = KEY } println("Object $BUCKET/$KEY deleted successfully!") println("Deleting bucket $BUCKET...") s3.deleteBucket { bucket = BUCKET } println("Bucket $BUCKET deleted successfully!") }
Step 4: Build and run the application
After the project is created and contains the example class, build and run the application.
-
Open a terminal or command prompt window and navigate to your project directory
getstarted
. -
Use the following command to build and run your application:
gradle run
The application calls the createBucket
In the cleanUp()
function at the end, the application deletes the object
and then deletes the S3 bucket.
To see the results in the Amazon S3 console
-
In
App.kt
, comment out the linecleanUp(s3)
in therunBlocking
section and save the file. -
Rebuild the project and put a new object into a new S3 bucket by running
gradle run
. -
Sign in to the Amazon S3 console
to view the new object in the new S3 bucket.
After you view the object, delete the S3 bucket.
Success
If your Gradle project built and ran without error, then congratulations. You have successfully built your first Kotlin application using the AWS SDK for Kotlin.
Cleanup
When you are done developing your new application, delete any AWS resources that
you created during this tutorial to avoid incurring any charges. You might also want to
delete or archive the project folder (get-started
) that you created in Step
2.
Follow these steps to clean up resources:
-
If you commented out the call to the
cleanUp()
function, delete the S3 bucket by using the Amazon S3 console.
Next steps
Now that you have the basics down, you can learn about the following: