Skip to content

Commit d2b70fd

Browse files
authored
Merge pull request #9 from rgtoa/main
created a lambda function 8d
2 parents e6efe38 + c863ac1 commit d2b70fd

8 files changed

Lines changed: 6261 additions & 335 deletions

File tree

examples/with-lambda/README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# AWS Lambda Deployment Guide
2+
3+
---
4+
5+
## Prerequisites
6+
7+
Before deploying, ensure you have the following:
8+
- **AWS Account** with access to **AWS Lambda** and **API Gateway**, as well as with necessary IAM Role Permissions.
9+
- **AWS CLI** installed and configured (`aws configure`).
10+
- **Yarn** installed (`npm install -g yarn`).
11+
- **API Gateway** configured to trigger the Lambda function.
12+
13+
---
14+
15+
## Test Running Locally
16+
17+
You can test the Lambda function locally before deployment using `yarn lambda:dev`,
18+
Then install all necessary packages using `yarn install`.
19+
20+
21+
## Deploying to AWS (using AWS Website)
22+
23+
1. Go to the AWS Lambda Console: https://console.aws.amazon.com/lambda
24+
2. Log in your AWS account.
25+
3. Create a function by selecting 'Author from scratch', entering a function name, and selecting runtime.
26+
4. Upload your FunctionCode.zip.
27+
5. Click Deploy.
28+
6. Go to Configuration -> Triggers.
29+
7. Select 'API Gateway' and choose to create a new API.
30+
8. Set Deployment Stage then click 'Add'.
31+
9. Go to your API -> Stages -> YOUR_DEPLOYMENT_STAGE.
32+
10. Copy and paste the Invoke URL to your browser to test.
33+
34+
35+
## Deploying to AWS (using AWS CLI)
36+
37+
1. Zip your Lambda Function Code.
38+
2. Deploy the Lambda Function by running the following command to create the function:.
39+
`aws lambda create-function --function-name <YOUR_FUNCTION> \`
40+
41+
3. Create an API Gateway:
42+
`aws apigateway create-rest-api --name <YOUR_API_NAME>`
43+
44+
4. Get the Root Resource ID of the new API:
45+
`aws apigateway get-resources --rest-api-id <API_ID>`
46+
47+
5. Create a new resource in the API Gateway:
48+
`aws apigateway create-resource --rest-api-id <API_ID> \`
49+
50+
6. Create a GET method that triggers Lambda:
51+
`aws apigateway put-method --rest-api-id <API_ID> \`
52+
53+
7. Link the method to the Lambda Function:
54+
`aws apigateway put-integration --rest-api-id <API_ID> \`
55+
56+
8. Deploy the API Gateway and get the Invoke URL:
57+
`aws apigateway create-deployment --rest-api-id <API_ID> \`
58+
59+
9. Allow API Gateway to invoke your Lambda Function:
60+
`aws lambda add-permission --function-name <YOUR_FUNCTION> \`
61+
62+
10. Update the function code for later use by running this code:
63+
`aws lambda update-function-code --function-name <YOUR_FUNCTION> \`
64+
65+
11. Verify the deployment using this code:
66+
`aws lambda invoke --function-name <YOUR_FUNCTION> output.txt`
67+
OR:
68+
`curl -X POST "https://<API_ID>.execute-api.<REGION>.amazonaws.com/prod/lambda"`

examples/with-lambda/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { server } from '@stackpress/ingest/fetch';
2+
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
3+
4+
// server starter
5+
const app = server();
6+
7+
app.get("/", (req, res) => {
8+
res.setHTML('<h1>Hello, World from Lambda!</h1>');
9+
});
10+
11+
// AWS Lambda handler
12+
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
13+
return {
14+
statusCode: 200,
15+
headers: { "Content-Type": "text/html" },
16+
body: '<h1>Hello, World from Lambda!</h1>',
17+
};
18+
};
19+
20+
// local server starter for testing
21+
if (process.env.LAMBDA_LOCAL === 'true') {
22+
app.create().listen(3000, () => {
23+
console.log('Server is running on port 3000');
24+
console.log('------------------------------');
25+
});
26+
}

examples/with-lambda/package.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "ingest-with-lambda",
3+
"version": "1.0.0",
4+
"description": "A simple boilerplate for using Ingest with Lambda.",
5+
"private": true,
6+
"scripts": {
7+
"build": "tsc",
8+
"dev": "LAMBDA_LOCAL=true ts-node index.ts"
9+
},
10+
"dependencies": {
11+
"@stackpress/ingest": "^0.3.27",
12+
"@types/aws-lambda": "^8.10.147",
13+
"@whatwg-node/server": "^0.9.67",
14+
"tslib": "^2.8.1"
15+
},
16+
"devDependencies": {
17+
"@types/node": "^22.13.1",
18+
"serverless-offline": "^14.4.0",
19+
"ts-node": "10.9.2",
20+
"typescript": "^4.9.5"
21+
}
22+
}

examples/with-lambda/tsconfig.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"esModuleInterop": true,
5+
"lib": ["es2021", "es7", "es6", "dom"],
6+
"module": "commonjs",
7+
"noUnusedLocals": true,
8+
"outDir": "./dist/",
9+
"preserveConstEnums": true,
10+
"resolveJsonModule": true,
11+
"removeComments": true,
12+
"sourceMap": false,
13+
"strict": true,
14+
"target": "es2017",
15+
"skipLibCheck": true
16+
},
17+
"include": ["src/**/*.ts", "index.ts"],
18+
"exclude": ["dist", "node_modules"]
19+
}
20+

examples/with-netlify/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "ingest-with-fetch",
2+
"name": "ingest-with-netlify",
33
"version": "1.0.0",
4-
"description": "A simple boilerplate for using Ingest with fetch API.",
4+
"description": "A simple boilerplate for using Ingest with Netlify",
55
"private": true,
66
"scripts": {
77
"build": "tsc",

0 commit comments

Comments
 (0)