Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hungds99/aws-serverless-crud-api
⛳️ User management API deploy in AWS using Serverless architecture
https://github.com/hungds99/aws-serverless-crud-api
aws cdk lambda nodejs serverless
Last synced: 3 days ago
JSON representation
⛳️ User management API deploy in AWS using Serverless architecture
- Host: GitHub
- URL: https://github.com/hungds99/aws-serverless-crud-api
- Owner: hungds99
- Created: 2023-10-31T06:55:42.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-02T01:16:14.000Z (about 1 year ago)
- Last Synced: 2023-11-02T02:26:56.344Z (about 1 year ago)
- Topics: aws, cdk, lambda, nodejs, serverless
- Language: TypeScript
- Homepage:
- Size: 55.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# A Simple CRUD API using CDK TypeScript
![Screenshot 2023-10-30 at 15 39 51](https://github.com/hungds99/aws-serverless-crud-api/assets/34293141/7a09e4b3-f9f6-4c29-81ef-861d680dd9a2)
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 gatewayThe `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
```typescript
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`
```typescript
export const createUser = async (event: any): Promise => {
const user = await userService.save(JSON.parse(event.body));
return { statusCode: 201, body: JSON.stringify(user) };
};
```
- Define a lambda function
```typescript
const createUserFn = new NodejsFunction(this, 'createUserFn', {
functionName: 'createUserFn',
description: 'Create user',
entry: 'src/handlers/user.ts',
handler: 'createUser',
});
```
- Grant permission to access DynamoDB
```typescript
usersTable.grantReadWriteData(createUserFn);
```### Step 3: Create an API Gateway
```typescript
const restApi = new RestApi(this, 'userManagementRestApi', {
restApiName: 'User Management API',
description: 'User management REST API',
});
```### Step 4: Create resources and methods with Lambda integration
```typescript
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