1. Answers
  2. Deploy Kotlin Docker Image on AWS Lambda with TypeScript

How do I deploy the kotlin:1.5 docker image on AWS Lambda using TypeScript?

In this guide, we will deploy a Docker image containing a Kotlin application to AWS Lambda using Pulumi and TypeScript. AWS Lambda supports container images, allowing you to package and deploy your applications using Docker. We will use the aws.lambda.Function resource to create and deploy our Lambda function.

Here’s the step-by-step process:

  1. Create an AWS Lambda function: We will define an AWS Lambda function that uses a Docker image containing the Kotlin application.
  2. Set up IAM Role: The Lambda function requires an IAM role with the necessary permissions.
  3. Deploy the Docker Image: We will use a pre-built Docker image kotlin:1.5 from Docker Hub.

Below is the complete Pulumi program to achieve this:

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an IAM role for the Lambda function
const lambdaRole = new aws.iam.Role("lambdaRole", {
    assumeRolePolicy: {
        Version: "2012-10-17",
        Statement: [{
            Action: "sts:AssumeRole",
            Principal: {
                Service: "lambda.amazonaws.com",
            },
            Effect: "Allow",
            Sid: "",
        }],
    },
});

// Attach the AWSLambdaBasicExecutionRole policy to the role
const lambdaRolePolicy = new aws.iam.RolePolicyAttachment("lambdaRolePolicy", {
    role: lambdaRole.name,
    policyArn: "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
});

// Create the Lambda function using the Docker image
const lambdaFunction = new aws.lambda.Function("kotlinLambda", {
    role: lambdaRole.arn,
    packageType: "Image",
    imageUri: "public.ecr.aws/lambda/kotlin:1.5", // Docker image URI
    timeout: 900, // Maximum allowable timeout for Lambda
});

// Export the URL of the Lambda function
export const lambdaFunctionName = lambdaFunction.name;

Key Points

  • IAM Role: The Lambda function requires an IAM role with the AWSLambdaBasicExecutionRole policy attached to allow it to write logs to CloudWatch.
  • Docker Image: The Lambda function uses the public.ecr.aws/lambda/kotlin:1.5 Docker image, which contains the Kotlin runtime.
  • Function Configuration: The Lambda function is configured with a timeout of 900 seconds, which is the maximum allowed.

Summary

We have successfully deployed a Kotlin application packaged as a Docker image to AWS Lambda using Pulumi and TypeScript. This setup includes creating an IAM role, attaching the necessary policies, and defining the Lambda function with the Docker image. This approach leverages the flexibility of containerized deployments on AWS Lambda.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up