Giter Site home page Giter Site logo

example-serverless-python-api's Introduction

Deploy a Serverless API in 30 mins

Presented in Ep05 of the Coffee & Cloud Series

Contact: https://linktr.ee/awsukstartups

Objective

In this example, we will deploy a serverless Python function to API Gateway. The function will interact with a DynamoDB table for persistence.

The scenario is that we are building an dashboard app to show times when a train leaves a station heading to a destination. Each time a train departs we want to update our app to show the destination and the time that it departed.

Requirements

  • AWS Account
  • AWS CLI installed & configured
  • AWS CDK installed & configured
  • Git CLI installed
  • Docker installed
  • Curl or Httpie installed (for testing)

Expected Time

You should be able to deploy this in less than 30 mins.

Getting Started

1. Initiate a new CDK application:

$ cdk init app --language typescript sample-api
$ cd sample-api

2. Add the CDK resources for the application

In the generated lib/sample-api-stack.ts add the DynamoDB Table, Lambda function & the API Gateway definition and allow the function to do read writes on the DynamoDB table.

    const table = new Table(this, 'Table', {
      partitionKey: { type: AttributeType.STRING, name: 'id' },
    })

    const func = new PythonFunction(this, 'MyFunction', {
      entry: './lambda-api',
      runtime: Runtime.PYTHON_3_9,
      index: 'server.py',
      handler: 'handler',
      environment: {
        'TABLE_NAME': table.tableName
      }
    })

    new LambdaRestApi(this, 'RestAPI', {
      handler: func
    })

    table.grantReadWriteData(func)

Note: For the full code see lib/sample-api-stack.ts

Note here we are using the PythonFunction construct here. This is a special construct that can perform a number of Python-based operations on your behalf. In this case, we want to include some Python libraries into our application.

The PythonFunction construct requires that you have docker installed and accessible before you can build and deploy your project.

3. Create your lambda function

Before we can deploy the CDK stack, we need to have the code ready for the function. A sample Python function has been provided in lambda-api/.

Copy the folder lambda-api/ to your application. Make sure you have Pipenv, Pipenv.lock & server.py in the folder.

4. Deploy our Lambda API

Now that everything is prepared, we can deploy our application to AWS using the CDK CLI.

$ cdk deploy

When this operation is complete you'll see a API Gateway URL displayed. This is your new API endpoint!!

5. Test our API

Now we need to test the functionality of your new API.

In our hypothetical example, when a train departs a station we call a POST operation to the /update endpoint:

$ http POST https://9vszbm10h9.execute-api.eu-west-1.amazonaws.com/prod/update
{
  "new_departure": "Edinburgh",
  "time": "18:46"
}

We can then retrieve the last departed train and the time that it departed using the /last_departed endpoint:

$ http GET https://9vszbm10h9.execute-api.eu-west-1.amazonaws.com/prod/last_departed
{
  "departure_time": "18:46",
  "last_departed": "Edinburgh"
}

That's it! You've now deployed a serverless Python API to API Gateway!

Clean Up

If you want to remove the resources for this sample. Use the AWS CDK as follows:

$ cdk destroy

example-serverless-python-api's People

Contributors

fortejas avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.