Giter VIP home page Giter VIP logo

aws-serverless-crud-api's Introduction

A Simple CRUD API using CDK TypeScript

Screenshot 2023-10-30 at 15 39 51

You should explore the contents of this project. It demonstrates a CDK app with stacks:

  • dynamodbCdkStack: Build dynamodb table
  • lambdaCdkStack: Build lambda function
  • apiCdkStack: Build api gateway

The cdk.json file tells the CDK Toolkit how to execute your app.

Prerequisites

  • Node.js
  • AWS CLI
  • AWS CDK
  • AWS Account

How to run

  • Clone this repository
  • Run the following commands:
    • cd aws-serverless-crud-api
    • npm install
    • Configure your AWS credentials
    • Copy .env.example to .env and update the values
    • cdk deploy --all -> Deploy all stacks
    • Play with the API
    • cdk destroy --all -> Destroy all stacks

APIs

  • GET /api/users: Get all users
  • GET /api/users/{id}: Get user by id
  • POST /api/users: Create new user
  • DELETE /api/users/{id}: Delete user by id
  • PUT /api/users/{id}: Update user by id

How to create an API

Step 1: Create a table in DynamoDB

const usersTable = new Table(this, 'usersTable', {
  tableName: TableNames.USERS,
  partitionKey: { name: 'id', type: AttributeType.STRING },
  billingMode: BillingMode.PAY_PER_REQUEST
});

Step 2: Create a lambda function

  • Add a lambda handler in src/handlers/user.ts
export const createUser = async (event: any): Promise<any> => {
  const user = await userService.save(JSON.parse(event.body));
  return { statusCode: 201, body: JSON.stringify(user) };
};
  • Define a lambda function
const createUserFn = new NodejsFunction(this, 'createUserFn', {
  functionName: 'createUserFn',
  description: 'Create user',
  entry: 'src/handlers/user.ts',
  handler: 'createUser',
});
  • Grant permission to access DynamoDB
usersTable.grantReadWriteData(createUserFn);

Step 3: Create an API Gateway

const restApi = new RestApi(this, 'userManagementRestApi', {
  restApiName: 'User Management API',
  description: 'User management REST API',
});

Step 4: Create resources and methods with Lambda integration

const users = apiRoot.addResource('users');
const createUserIntegration = new LambdaIntegration(createUserFn);
users.addMethod('POST', createUserIntegration);

Useful commands

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template

aws-serverless-crud-api's People

Contributors

hungds99 avatar deepsource-io[bot] avatar

Stargazers

Hung Dinh S. avatar  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.