DEV Community

Prakash Rao
Prakash Rao

Posted on

Building Serverless Applications with AWS Lambda and integrating with DynamoDB

Agenda

1.Introduction

  • What is serverless architecture?

2.Setting Up the Project

  • Creating a Lambda function with AWS CLI.
  • Creating an API Gateway to trigger Lambda.

3.Building the Application

  • Creating a simple RESTful API with AWS Lambda and API Gateway.
  • Integrating the API with DynamoDB to store data.

4.Testing the Application

  • Invoking the Lambda function using AWS CLI.
  • Testing the API endpoint using Postman or Curl.

5.Deploying the Application

  • Deploying the API to a production stage.

6.Conclusion

  • Summing up the steps and benefits of using AWS Lambda.
  • Further enhancements and scalability options.

What is serverless architecture?

Serverless architecture is not something new and has gained massive popularity for its ability to simplify development, reduce operational costs, and increase scalability. AWS Lambda, Amazon’s serverless compute service, plays a central role in this movement. With AWS Lambda, developers can run their code in response to events without having to provision or manage servers, making it easier to focus on the logic rather than infrastructure. This guide will show you how to build a complete serverless application using AWS Lambda and other AWS services like API Gateway and DynamoDB.

Setting Up the Project

Step 1: Create a Lambda Function with AWS CLI
Let's first create the Lambda function that will execute our serverless logic. Use the AWS CLI to create the Lambda function:

aws lambda create-function \
  --function-name MyLambdaFunction \
  --runtime python3.8 \
  --role arn:aws:iam::your-account-id:role/execution-role \
  --handler lambda_function.lambda_handler \
  --zip-file fileb://my-deployment-package.zip
Enter fullscreen mode Exit fullscreen mode

Here, the Lambda function is written in Python, and we’ll upload the code in a ZIP file. We need to ensure that the execution-role has the necessary permissions to execute the function.

Step 2: Create an API Gateway to Trigger Lambda
Next, let's set up an API Gateway to trigger our Lambda function when an HTTP request is made. We can create a REST API using the following AWS CLI command:

aws apigateway create-rest-api \
  --name "MyAPI" \
  --description "API for serverless Lambda" \
  --endpoint-configuration types=REGIONAL

Enter fullscreen mode Exit fullscreen mode

Once the API is created, let's make a note of the API ID provided in the response.

Building the Application

Step 3: Create a Resource for the API
Now, we need to create a resource, such as /items, that will handle the HTTP request.

API_ID=<api-id-which-we-got-earlier>
aws apigateway create-resource \
  --rest-api-id $API_ID \
  --parent-id $(aws apigateway get-resources --rest-api-id $API_ID --query 'items[0].id' --output text) \
  --path-part "items"
Enter fullscreen mode Exit fullscreen mode

Step 4: Define a POST Method for the Resource
After creating the resource, let's define a POST method that will trigger our Lambda function when a request is made:

aws apigateway put-method \
  --rest-api-id $API_ID \
  --resource-id <resource-id> \
  --http-method POST \
  --authorization-type NONE
Enter fullscreen mode Exit fullscreen mode

Next, let's integrate the POST method with the Lambda function:

aws apigateway put-integration \
  --rest-api-id $API_ID \
  --resource-id <resource-id> \
  --http-method POST \
  --integration-http-method POST \
  --type AWS_PROXY \
  --uri arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/arn:aws:lambda:<region>:<account-id>:function:MyLambdaFunction/invocations
Enter fullscreen mode Exit fullscreen mode

Step 5: Set Up DynamoDB Integration
Now to store incoming data in DynamoDB we need to create a DynamoDB table:

aws dynamodb create-table \
  --table-name MyTable \
  --attribute-definitions AttributeName=id,AttributeType=S AttributeName=name,AttributeType=S \
  --key-schema AttributeName=id,KeyType=HASH \
  --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
Enter fullscreen mode Exit fullscreen mode

Then, update the Lambda function code to interact with DynamoDB, adding items to the table. Afterward, update the Lambda function code using:

aws lambda update-function-code \
  --function-name MyLambdaFunction \
  --zip-file fileb://my-updated-deployment-package.zip
Enter fullscreen mode Exit fullscreen mode

Testing the Application

Step 6: Invoke the Lambda Function
Once everything is set up, we can test the Lambda function directly using AWS CLI. For example, to add an item to DynamoDB, we can run:

aws lambda invoke \
  --function-name MyLambdaFunction \
  --payload '{"id": "123", "name": "Sample"}' \
  output.txt
Enter fullscreen mode Exit fullscreen mode

Step 7: Test the API Endpoint
Use Curl to test the API. For instance, we can test the /items endpoint by sending a POST request with the relevant data:

curl -X POST -d '{"id": "123", "name": "Sample"}' https://<api-id>.execute-api.<region>.amazonaws.com/dev/items
Enter fullscreen mode Exit fullscreen mode

Deploying the Application

Step 8: Deploy the API
Once everything is locally tested, we can deploy the API to a production stage by following command:

aws apigateway create-deployment \
  --rest-api-id $API_ID \
  --stage-name prod
Enter fullscreen mode Exit fullscreen mode

This will deploy the API to the prod stage, and we'll receive a public URL that we can use to interact with the Lambda function via the API.

Conclusion

AWS Lambda allows us to build serverless applications that scale effortlessly, without the need to manage infrastructure. By using the AWS CLI, we streamlined the process of creating and managing Lambda functions, API Gateway resources, and integrating with DynamoDB. This approach makes it possible to quickly deploy a scalable and cost-effective application that responds to events in real time.

Top comments (0)