-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaws-lambda-example.ts
More file actions
76 lines (65 loc) · 1.68 KB
/
Copy pathaws-lambda-example.ts
File metadata and controls
76 lines (65 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// AWS Lambda Runtime Example
import { createAppLambda } from '@morojs/moro';
import type { LambdaEvent, LambdaContext } from '@morojs/moro';
const app = await createAppLambda();
// Define routes exactly the same way
app.get('/', (req: any, res: any) => {
return {
message: 'Hello from MoroJS on AWS Lambda!',
runtime: 'aws-lambda',
timestamp: new Date().toISOString(),
requestId: req.requestId,
};
});
app.get('/health', (req: any, res: any) => {
return {
status: 'healthy',
runtime: 'aws-lambda',
lambda: true,
region: process.env.AWS_REGION,
};
});
app.post('/api/data', (req: any, res: any) => {
return {
received: req.body,
runtime: 'aws-lambda',
method: req.method,
pathParameters: req.params,
};
});
app.get('/api/user/:id', (req: any, res: any) => {
return {
userId: req.params.id,
runtime: 'aws-lambda',
query: req.query,
sourceIp: req.ip,
};
});
// Export the handler for AWS Lambda
export const handler = app.getHandler();
// For typed Lambda handler (optional)
export const typedHandler = (event: LambdaEvent, context: LambdaContext) => {
return app.getHandler()(event, context);
};
/*
To deploy to AWS Lambda:
1. Package your code
2. Set the handler to: index.handler
3. Configure API Gateway to proxy all requests to Lambda
4. Or use AWS SAM template:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MoroJSFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: dist/
Handler: index.handler
Runtime: nodejs18.x
Events:
ApiEvent:
Type: Api
Properties:
Path: /{proxy+}
Method: ANY
*/