Giter VIP home page Giter VIP logo

aws-lambda-api's Introduction

Product API Using AWS Lambda and API Gateway

This is a sample project to demonstrate how to build a REST API using AWS Lambda and API Gateway.

URL: Not deployed at the moment

Structure

Alt text

Prerequisites

Setup

Note: Assuming you have already created an AWS account and have the AWS CLI installed and configured.

Create IAM Policy and Role

Create an IAM policy with the following permissions:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "Stmt1428341300017",
      "Action": [
        "dynamodb:GetItem",
        "dynamodb:PutItem",
        "dynamodb:Scan"
      ],
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Sid": "",
      "Resource": "*",
      "Action": [
        "logs:CreateLogGroup",
        "logs:CreateLogStream",
        "logs:PutLogEvents"
      ],
      "Effect": "Allow"
    }
  ]
}

Create an IAM role for AWS Service - Lambda with the above policy attached.

Create DynamoDB Table

Create a DynamoDB table with the following properties:

  • Table name: products
  • Primary partition key: id (String)
  • Fields: id, name, description

Create Lambda Function

Create a Lambda function as described in the file LambdaFunctionOverHttps.py in this repository. The handler works as the entry point for the Lambda function and takes the following parameters:

  • event: The event data passed to the Lambda function which should contain the operation to be performed and the payload.
  • context: The runtime information of the Lambda function.

The following operations are supported:

  • get: Get a product by ID
  • create: Create a new product
  • getAll: Get all products
  • echo: Return the event data [for testing purposes]

Here are the inputs for each operation:

  • get: {"operation": "get", "payload": {"id": "1"}}
  • create: {"operation": "create", "payload": "Item": {{"id": "1", "name": "Product 1", "description": "Description of Product 1"}}}
  • getAll: {"operation": "getAll", "payload": {}}

To publish the Lambda function, run the following command:

zip function.zip LambdaFunctionOverHttps.py
aws lambda create-function --function-name LambdaFunctionOverHttps --zip-file fileb://function.zip --handler LambdaFunctionOverHttps.handler --runtime python3.9 --role <role-arn>

Create API Gateway

Create an API Gateway with the following properties:

  • Resource name: DynamoDBManager
  • Resource path: /DynamoDBManager
  • Method: POST
  • API Key required: true

Note: For API Key creation, need to attach a usage plan to the API Gateway.

Add CloudWatch Logs

Add CloudWatch logs to the Lambda function to log the events. The logs can be viewed in the CloudWatch console.

Test the API

Test the API using POSTMAN at the following URL with POST method:

https://v8u02y61t5.execute-api.us-east-2.amazonaws.com/test/DynamoDBManager

Headers must contain:

{
    'x-api-key': '<api-key>'
}

And the body must contain the operation and payload as described above.

Create Operation

Get Operation

References

aws-lambda-api's People

Contributors

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