DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Build a Twitter Leaderboard App With Redis and AWS Lambda (Part 2)
  • Building Scalable Data Lake Using AWS
  • Building a Scalable ML Pipeline and API in AWS
  • Breaking AWS Lambda: Chaos Engineering for Serverless Devs

Trending

  • Scalable, Resilient Data Orchestration: The Power of Intelligent Systems
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • Apache Doris vs Elasticsearch: An In-Depth Comparative Analysis
  • Solid Testing Strategies for Salesforce Releases
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Use Lambda Function URL To Write a Serverless App Backed by DynamoDB

Use Lambda Function URL To Write a Serverless App Backed by DynamoDB

This is a Go Lambda function that's packaged and deployed using AWS SAM.

By 
Abhishek Gupta user avatar
Abhishek Gupta
DZone Core CORE ·
Updated May. 13, 22 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
39.7K Views

Join the DZone community and get the full member experience.

Join For Free

Lambda Function URL is a relatively new feature (at the time of writing this blog) that provides a dedicated HTTP(S) endpoint for your Lambda function. It is really useful when all you need is a single endpoint for your function (e.g. to serve as a webhook) and doesn't want to set up and configure an API Gateway. Looks like I can't seem to get enough of it! I have written a couple of blog posts on this topic that includes a practical example of using it to build a serverless backend and then deploying that solution using AWS CDK.

This is yet another blog post (it's a short one!) that demonstrates how you can use Lambda Function URL to write a simple application backed by DynamoDB. You will be able to invoke an API endpoint exposed by the Lambda Function URL, which in turn will execute operations (GetItem, PutItem, Scan) on DynamoDB. The function is written in Go using the DynamoDB package in AWS Go SDK and AWS Serverless Application Model (SAM) is used to quickly build and deploy the solution.

The code is available on GitHub for your reference

Let's get right to it! Before you move on, make sure you have the following ready:

Pre-requisites

  • Create an AWS account if you do not already have one and log in. Sufficient permissions are needed by the IAM user that you use to make necessary AWS service calls and manage the AWS resources.
  • AWS CLI installed and configured
  • Go (1.16 or above) installed
  • AWS Serverless Application Model (AWS SAM) installed
  • Git Installed

Deployment

Start by cloning the Github repo and change to the right directory:

Shell
 
git clone https://github.com/abhirockzz/lambda-functionurl-dynamodb-sam-go
cd lambda-functionurl-dynamodb-sam-go

Use AWS SAM to build the app:

Shell
 
sam build

Now you're ready to deploy the application:

Shell
 
sam deploy --guided

When prompted, enter the required information such as stack name, etc. See the example below:

Plain Text
 
Configuring SAM deploy
======================

        Looking for config file [samconfig.toml] :  Not found

        Setting default arguments for 'sam deploy'
        =========================================
        Stack Name [sam-app]: 
        AWS Region [us-east-1]: 
        #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
        Confirm changes before deploy [y/N]: y
        #SAM needs permission to be able to create roles to connect to the resources in your template
        Allow SAM CLI IAM role creation [Y/n]: y
        #Preserves the state of previously provisioned resources when an operation fails
        Disable rollback [y/N]: n
        DemoFunction Function Url may not have authorization defined, Is this okay? [y/N]: y
        Save arguments to configuration file [Y/n]: 
        SAM configuration file [samconfig.toml]: 
        SAM configuration environment [default]: 

Once you have run sam deploy --guided mode once and saved arguments to a configuration file (samconfig.toml), you can use sam deploy in future to use these defaults.

If successful, you should now have the following ready:

  • Lambda Function with an HTTP(S) URL
  • A DynamoDB table (called users)
  • IAM Role with the minimum required permissions for Lambda as well as DynamoDB (PutItem, GetItem, and Scan)

Note the output from the SAM deployment process. This contains the HTTP URL endpoint for your function required for testing

Test the Application

Testing the integration involves sending HTTP requests to the Lambda Function URL. This example used the curl CLI, but you can also use other options.

Export the Lambda Function URL endpoint as an environment variable. E.g. export LAMBDA_FUNCTION_URL_ENDPOINT=https://qfmpu2n74ik7m2m3ipv3m6yw5a0qiwmp.lambda-url.us-east-1.on.aws/

Shell
 
export LAMBDA_FUNCTION_URL_ENDPOINT=<SAM deployment output>

Invoke the endpoint to create new entries in the DynamoDB table:

Shell
 
curl -i -X POST -H "Content-Type: application/json" -d '{"email":"user1@foo.com", "username":"user-1"}' $LAMBDA_FUNCTION_URL_ENDPOINT
curl -i -X POST -H "Content-Type: application/json" -d '{"email":"user2@foo.com", "username":"user-2"}' $LAMBDA_FUNCTION_URL_ENDPOINT

You should get an HTTP 201 Created response in both cases. This indicates that the items have been added to the users table in DynamoDB.

Let's retrieve the info for the record we just created (we do that by adding a query parameter (email) to the URL):

Shell
 
curl -i $LAMBDA_FUNCTION_URL_ENDPOINT?email=user2@foo.com

To retrieve all the items that you just added:

Shell
 
curl -i $LAMBDA_FUNCTION_URL_ENDPOINT

Try to search for an item that you haven't yet added:

Shell
 
curl -i $LAMBDA_FUNCTION_URL_ENDPOINT?email=notthere@foo.com

You will get back a HTTP 404 Not Found response in this case.

Finally, try to insert a duplicate record:

Shell
 
curl -i -X POST -d '{"email":"user2@foo.com", "username":"user-2"}' $LAMBDA_FUNCTION_URL_ENDPOINT

The Lambda function returns an HTTP 409 Conflict in this case since a Condition Expression (attribute_not_exists(email)) prevents an existing item (with the same email) from being overwritten by the PutItem call.

That's it! You have tried all the operations exposed by the Lambda function.

Cleanup

Once you're done, please delete the stack:

Shell
 
sam delete --stack-name STACK_NAME

Confirm the stack has been deleted

Shell
 
aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus"

Wrap Up!

This was a short (but hopefully useful) tutorial! You can use this to quickly bootstrap a Serverless app with a DynamoDB backend with functionality exposed by a Lambda Function URL.

Happy coding!

AWS app AWS Lambda

Opinions expressed by DZone contributors are their own.

Related

  • Build a Twitter Leaderboard App With Redis and AWS Lambda (Part 2)
  • Building Scalable Data Lake Using AWS
  • Building a Scalable ML Pipeline and API in AWS
  • Breaking AWS Lambda: Chaos Engineering for Serverless Devs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!