Using EKS with Lambda on AWS

Using EKS with Lambda on AWS

AWS EKS (Elastic Kubernetes Service) allows you to easily run Kubernetes clusters in the AWS cloud. Lambda is AWS' serverless compute service that allows you to run code without provisioning or managing servers. This article discusses how you can integrate EKS with Lambda to build serverless applications on Kubernetes.

Deploying Lambda functions to EKS

The main way to integrate EKS and Lambda is by deploying Lambda functions as Kubernetes deployments and services. This allows Kubernetes to manage and orchestrate the execution of Lambda code.

The steps to deploy a Lambda function to EKS are:

  1. Create a Lambda function using the AWS CLI or SDK. This deploys the code and configuration to Lambda.

  2. Create a Kubernetes Deployment and Service that points to the Lambda function ARN (Amazon Resource Name). The service exposes the function through a ClusterIP.

  3. Kubernetes will trigger invocations of the Lambda function through the service endpoint. It handles load balancing, auto-scaling and orchestration of the function.

Benefits of using EKS and Lambda together

  • Leverage Kubernetes APIs and tools to deploy, manage and scale Lambda functions

  • Build serverless applications as Kubernetes workloads for portability across environments

  • Take advantage of Kubernetes features like auto-scaling, rolling updates, blue-green deployments etc. for Lambda code

  • Integrate Lambda functions into existing container-based applications on EKS

This allows building fully serverless applications that leverage the power of Kubernetes for orchestration along with AWS Lambda's ease of use.

Here is a sample Python code for deploying a Lambda function as a Kubernetes workload:

# Create Lambda function
lambda_client.create_function(
   FunctionName='myfunction',
   Runtime='python3.8', 
   Handler='index.handler',
   Code={
      'ZipFile': bytecode
   }
)

# Create Kubernetes Deployment
api.create_namespaced_deployment(
   namespace='default',
   body={
      'apiVersion': 'apps/v1',
      'kind': 'Deployment',
      'metadata': {
         'name': 'lambda-deploy'
      },
      'spec': {
         'replicas': 1,
         'selector': {
            'matchLabels': {
               'app': 'lambda'
            }
         },
         'template': {
            'metadata': {
               'labels': {
                  'app': 'lambda'
               }
            },
            'spec': {
               'containers': [
                  {
                     'name': 'lambda-container',
                     'image': 'public.ecr.aws/lambda/python:3.8',
                     'env': [
                        {
                           'name': 'AWS_LAMBDA_FUNCTION_NAME', 
                           'value': 'myfunction'
                        },
                        {
                           'name': 'AWS_REGION',
                           'value': 'us-east-1' 
                        }
                     ]
                  }
               ]
            }
         }
      }
   }
)

# Expose Deployment as Kubernetes Service
api.create_namespaced_service(
   namespace='default',
   body={
      'apiVersion': 'v1', 
      'kind': 'Service',
      'metadata': {
         'name': 'lambda-service' 
      },
      'spec': {
         'ports': [
            {
               'port': 8080, 
               'targetPort': 8080
            }
         ],
         'selector': {
            'app': 'lambda'
         }
      }
   }
)

This demonstrates how to deploy a Lambda function as a Kubernetes workload and expose it through a service for invocation. EKS and Lambda provide a powerful way to build serverless applications on Kubernetes.

Did you find this article valuable?

Support Timur Galeev by becoming a sponsor. Any amount is appreciated!