Giter Site home page Giter Site logo

lambda-python-dynamodb's Introduction

python-lambda-with-database

AWS Lambda functions are a great way to deploy your Python code or API (e.g. Flask, FastAPI) - at a cheap cost and not having to worry about servers. But since Lambda functions are ephemeral (they disappear after they finish running), how do we persist data? In this tutorial, we'll use AWS DynamoDB as a database for us to store information used by our Lambda functions.

Getting Started in AWS Lambda

Create a new function in the Lambda Function Console

Go to the Code tab, configure a new Test event and then hit Test

Go back into configure test event and change the JSON accordingly:

{
  "user": "{whateverusername_aws}"
}

Edit the Lambda function code to your desire, for example:

import json

def lambda_handler(event, context):
    user = event["user"]
    return {
        'message': f"Hello {user}"
    }

Press Deploy (to save) and then run Test

Move your code over to your IDE (i.e. VS Code) and start updating the rest there.

DynamoDB

Amazon has 9-10 database types, but only two of them are serverless; DynamoDB and Aurora. Aurora is a sequel DB which we do not need right now.

Go to the AWS console and search 'DynamoDB'

Click 'Create Table' to get started

Enter a table name and a partition key (i.e 'user')

Go to customize settings and change the read and write capacity from Provisioned to On-Demand. Skip everying else and hit 'Create Table'.

AWS CLI

Next we want to be able to interact with our DynamoDB table in our VS code. To do that we need to make sure AWS CLI is installed.

Download the latest AWS CLI here

Once downloaded (or if you already had it installed), run:

aws sts get-caller-identity

to make sure your AWS CLI is configured properly.

AWS SDK

The AWS SDK is called boto3. Run:

pip install boto3

and then in your code include:

import boto3

Once that is installed we can access the DynamoDB table.

lambda-python-dynamodb's People

Contributors

markbuckle avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.