Skip to content

Commit

Permalink
feat: Updated node v3 templates to node v18 (serverless#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
Danwakeem authored Feb 2, 2023
1 parent b80e2c3 commit 9ea7045
Show file tree
Hide file tree
Showing 16 changed files with 285 additions and 294 deletions.
4 changes: 0 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@
Thanks for submitting a PR! We're excited to see what you've got for us!
Make sure to lint your code to match the rest of the repo.
Run `npm run lint` to lint
-->
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ app.get("/", (req, res, next) => {
});
});

app.get("/hello", (req, res, next) => {
app.get("/path", (req, res, next) => {
return res.status(200).json({
message: "Hello from path!",
});
Expand Down
4 changes: 2 additions & 2 deletions aws-node-express-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "",
"dependencies": {
"express": "^4.17.1",
"serverless-http": "^2.7.0"
"express": "^4.18.2",
"serverless-http": "^3.1.1"
}
}
4 changes: 2 additions & 2 deletions aws-node-express-api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ frameworkVersion: '3'

provider:
name: aws
runtime: nodejs14.x
runtime: nodejs18.x

functions:
api:
handler: handler.handler
handler: index.handler
events:
- httpApi: '*'
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const AWS = require("aws-sdk");
const { DynamoDBClient } = require("@aws-sdk/client-dynamodb");
const {
DynamoDBDocumentClient,
GetCommand,
PutCommand,
} = require("@aws-sdk/lib-dynamodb");
const express = require("express");
const serverless = require("serverless-http");


const app = express();

const USERS_TABLE = process.env.USERS_TABLE;
const dynamoDbClient = new AWS.DynamoDB.DocumentClient();
const client = new DynamoDBClient();
const dynamoDbClient = DynamoDBDocumentClient.from(client);

app.use(express.json());

Expand All @@ -18,7 +25,7 @@ app.get("/users/:userId", async function (req, res) {
};

try {
const { Item } = await dynamoDbClient.get(params).promise();
const { Item } = await dynamoDbClient.send(new GetCommand(params));
if (Item) {
const { userId, name } = Item;
res.json({ userId, name });
Expand Down Expand Up @@ -50,7 +57,7 @@ app.post("/users", async function (req, res) {
};

try {
await dynamoDbClient.put(params).promise();
await dynamoDbClient.send(new PutCommand(params));
res.json({ userId, name });
} catch (error) {
console.log(error);
Expand Down
4 changes: 2 additions & 2 deletions aws-node-express-dynamodb-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "",
"dependencies": {
"express": "^4.17.1",
"serverless-http": "^2.7.0"
"express": "^4.18.2",
"serverless-http": "^3.1.1"
}
}
4 changes: 2 additions & 2 deletions aws-node-express-dynamodb-api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ custom:

provider:
name: aws
runtime: nodejs14.x
runtime: nodejs18.x
iam:
role:
statements:
Expand All @@ -25,7 +25,7 @@ provider:

functions:
api:
handler: handler.handler
handler: index.handler
events:
- httpApi: '*'

Expand Down
4 changes: 1 addition & 3 deletions aws-node-http-api/handler.js → aws-node-http-api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
"use strict";

module.exports.hello = async (event) => {
module.exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
Expand Down
6 changes: 3 additions & 3 deletions aws-node-http-api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ frameworkVersion: '3'

provider:
name: aws
runtime: nodejs14.x
runtime: nodejs18.x

functions:
hello:
handler: handler.hello
api:
handler: index.handler
events:
- httpApi:
path: /
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

module.exports.run = async (event, context) => {
const time = new Date();
console.log(`Your cron function "${context.functionName}" ran at ${time}`);
Expand Down
8 changes: 3 additions & 5 deletions aws-node-scheduled-cron/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
service: aws-node-scheduled-cron

frameworkVersion: '3'


provider:
name: aws
runtime: nodejs14.x
runtime: nodejs18.x

functions:
rateHandler:
handler: handler.run
handler: index.run
events:
# Invoke Lambda function every minute
- schedule: rate(1 minute)
cronHandler:
handler: handler.run
handler: index.run
events:
# Invoke Lambda function every 2nd minute from Mon-Fri
- schedule: cron(0/2 * ? * MON-FRI *)
25 changes: 11 additions & 14 deletions aws-node-sqs-worker/handler.js → aws-node-sqs-worker/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const { SQS } = require("aws-sdk");

const sqs = new SQS();
const { SQSClient, SendMessageCommand } = require("@aws-sdk/client-sqs");
const sqs = new SQSClient();

const producer = async (event) => {
let statusCode = 200;
Expand All @@ -16,18 +15,16 @@ const producer = async (event) => {
}

try {
await sqs
.sendMessage({
QueueUrl: process.env.QUEUE_URL,
MessageBody: event.body,
MessageAttributes: {
AttributeName: {
StringValue: "Attribute Value",
DataType: "String",
},
await sqs.send(new SendMessageCommand({
QueueUrl: process.env.QUEUE_URL,
MessageBody: event.body,
MessageAttributes: {
AttributeName: {
StringValue: "Attribute Value",
DataType: "String",
},
})
.promise();
},
}));

message = "Message accepted!";
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions aws-node-sqs-worker/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ frameworkVersion: '3'

provider:
name: aws
runtime: nodejs14.x
runtime: nodejs18.x

constructs:
jobs:
type: queue
worker:
handler: handler.consumer
handler: index.consumer

functions:
producer:
handler: handler.producer
handler: index.producer
events:
- httpApi:
method: post
Expand Down
4 changes: 1 addition & 3 deletions aws-node/handler.js → aws-node/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

module.exports.hello = async (event) => {
module.exports.handler = async (event) => {
return {
statusCode: 200,
body: JSON.stringify(
Expand Down
7 changes: 3 additions & 4 deletions aws-node/serverless.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
service: aws-node # NOTE: update this with your service name

frameworkVersion: '3'

provider:
name: aws
runtime: nodejs14.x
runtime: nodejs18.x

functions:
hello:
handler: handler.hello
function1:
handler: index.handler
Loading

0 comments on commit 9ea7045

Please sign in to comment.