API Gateway and Lambda Integration

Introduction

One of the most common cases is the creation of Rest APIs using the GW, lambda, and dynamodb APIs. The most common use of these APIs are web applications. We program the infrastructure using AWS CDK TS framework and use Python code for Lambda. The following example demonstrates how to create a new CDK stack with a DynamoDB table, a Lambda function, and an API Gateway with Lambda integration. The Lambda function writes data to the DynamoDB table and returns a response to the API Gateway.
Typical customers who often use this case are those who want to create a Rest API using AWS API Gateway and AWS Lambda. This is a common use case for customers who want to create a Rest API using AWS API Gateway and AWS Lambda. For example, these are healthcare companies, financial services, and e-commerce companies.

Content Covered

  • Creating a DynamoDB table
  • Creating a Lambda function
  • Creating an API Gateway with Lambda integration
  • Example Python code for Lambda

Demo Architecture stack

This small stack have a DynamoDB table, a Lambda function, and an API Gateway with Lambda integration. The Lambda function writes data to the DynamoDB table and returns a response to the API Gateway.

Dynamodb Stream with Lambda, SQS and Lambda

AWS CDK Script (TypeScript)

First, we need to create a new CDK stack. The following example demonstrates how to create a new CDK stack with a DynamoDB table, a Lambda function, and an API Gateway with Lambda integration.

stack.ts file

import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as apigateway from '@aws-cdk/aws-apigateway';
import * as dynamodb from '@aws-cdk/aws-dynamodb';

export class ApiLambdaStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const table = new dynamodb.Table(this, 'MyTable', {
      partitionKey: { name: 'pk', type: dynamodb.AttributeType.STRING },
      billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
    });

    // Define the Lambda function
    const lambdaFunction = new lambda.Function(this, 'MyLambdaFunction', {
      runtime: lambda.Runtime.PYTHON_3_12,
      code: lambda.Code.fromAsset('lambda'),
      handler: 'app.handler',
      timeout: Duration.seconds(60),
      memorySize: 256,
      environment: {
        TABLE_NAME: table.tableName
      }
    });

    table.grantReadWriteData(lambdaFunction);

    // Define the API Gateway with Lambda integration
    const api = new apigateway.LambdaRestApi(this, 'MyApi', {
      handler: lambdaFunction,
      proxy: true // Enable proxy integration
    });
  }
}

Lambda Function (Python)

app.py file:

import json
import boto3
import os

def handler(event, context):
    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(os.environ['TABLE_NAME'])

    response = table.put_item(
       Item={
            'pk': '1',
            'data': 'Hello, world!'
        }
    )

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

Copyright © 2024. All rights reserved.