Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _instructor/core-concepts/1-http-hello-world/handler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

module.exports.hello = (event, context, callback) => {
export const hello = (event, context, callback) => {
// WORKSHOP_START
/* Step 1. In this_file, Create a `200` response code and return the `event` data in the response body.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ service: my-first-service

provider:
name: aws
runtime: nodejs12.x
runtime: nodejs22.x

functions:
hello:
Expand Down
4 changes: 2 additions & 2 deletions _instructor/core-concepts/2-http-dynamic-content/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Finally remember to set the headers of the response as `'Content-Type': 'text/html'` to return HTML instead of the default `json`
*/
module.exports.queryParamsExample = (event, context, callback) => {
export const queryParamsExample = (event, context, callback) => {
// WORKSHOP_START
const response = {
statusCode: 200,
Expand Down Expand Up @@ -42,7 +42,7 @@ module.exports.queryParamsExample = (event, context, callback) => {

Finally, remember to set the headers of the response as `'Content-Type': 'text/html'` to return HTML instead of the default `json`
*/
module.exports.pathParamsExample = (event, context, callback) => {
export const pathParamsExample = (event, context, callback) => {
// WORKSHOP_START
const response = {
statusCode: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ service: using-dynamic-value

provider:
name: aws
runtime: nodejs12.x
runtime: nodejs22.x

functions:
queryParamsExample:
Expand Down
2 changes: 1 addition & 1 deletion _instructor/core-concepts/3-http-post-with-cors/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
For additional information, see the cors docs http://bit.ly/2FlFSWB
*/
// WORKSHOP_END
module.exports.functionWithCors = (event, context, callback) => {
export const functionWithCors = (event, context, callback) => {
const response = {
statusCode: 200,
// FINAL_START
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ service: using-http-cors-example

provider:
name: aws
runtime: nodejs12.x
runtime: nodejs22.x

functions:
hello:
Expand Down
4 changes: 2 additions & 2 deletions _instructor/core-concepts/4-using-env-vars/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
Return the environment variable in the `foo` function response
*/
// WORKSHOP_END
module.exports.foo = (event, context, callback) => {
export const foo = (event, context, callback) => {
// FINAL_START
console.log('process.env.MY_ENV_VAR', process.env.MY_ENV_VAR)
/* MY_ENV_VAR_FOR_BAR will be undefined */
Expand Down Expand Up @@ -42,7 +42,7 @@ module.exports.foo = (event, context, callback) => {
Return the environment variable in the `bar` function response
*/
// WORKSHOP_END
module.exports.bar = (event, context, callback) => {
export const bar = (event, context, callback) => {
// FINAL_START
/* both env variables will be accessible in bar */
console.log('process.env.MY_ENV_VAR', process.env.MY_ENV_VAR)
Expand Down
2 changes: 1 addition & 1 deletion _instructor/core-concepts/4-using-env-vars/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ service: using-env-variables-example
# WORKSHOP_END
provider:
name: aws
runtime: nodejs12.x
runtime: nodejs22.x
# FINAL_START
environment:
MY_ENV_VAR: 'hello there'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

module.exports.foo = (event, context, callback) => {
export const foo = (event, context, callback) => {
// FINAL_START
console.log('process.env.MY_SECRET', process.env.MY_SECRET)
// FINAL_END
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ service: serverless-variables-syntax-example
# WORKSHOP_END
provider:
name: aws
runtime: nodejs12.x
runtime: nodejs22.x
# WORKSHOP_START
environment:
MY_SECRET: xyz123
Expand Down
56 changes: 28 additions & 28 deletions _instructor/core-concepts/6-using-addition-resources/handler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* Include the AWS sdk.
* No need to add to package.json. It's included in lambda env
*/
const AWS = require('aws-sdk')
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, PutCommand, ScanCommand } from '@aws-sdk/lib-dynamodb';
// Connect to DynamoDB
const dynamoDb = new AWS.DynamoDB.DocumentClient()
const client = new DynamoDBClient({});
const dynamoDb = DynamoDBDocumentClient.from(client);

// Save item in DynamoDB table
module.exports.create = (event, context, callback) => {
export const create = async (event, context, callback) => {
const timestamp = new Date().getTime()
const body = JSON.parse(event.body)

Expand All @@ -30,48 +32,46 @@ module.exports.create = (event, context, callback) => {
}

// write the todo to the database
dynamoDb.put(params, (error) => {
// handle potential errors
if (error) {
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the dynamo item.',
})
}

try {
await dynamoDb.send(new PutCommand(params));
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(params.Item),
}
return callback(null, response)
})
} catch (error) {
// handle potential errors
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the dynamo item.',
})
}
}

/* Scan a dynamoDB table and return items */
module.exports.scan = (event, context, callback) => {
export const scan = async (event, context, callback) => {
const params = {
TableName: process.env.MY_TABLE,
}
// fetch all todos from the database
dynamoDb.scan(params, (error, result) => {
// handle potential errors
if (error) {
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todos.',
})
}

try {
const result = await dynamoDb.send(new ScanCommand(params));
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(result.Items),
}
return callback(null, response)
})
} catch (error) {
// handle potential errors
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todos.',
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ service: event-driven-app

provider:
name: aws
runtime: nodejs12.x
runtime: nodejs22.x
# WORKSHOP_START
# Step 3. In this_file, add the database table name to the service's `environment` variables. The `aws-sdk` will need to know the table name in order to access the table
# WORKSHOP_END
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

module.exports.hello = (event, context, callback) => {
export const hello = (event, context, callback) => {
const response = {
/* Status code required for default lambda integration */
statusCode: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "using-serverless-plugins",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ custom:

provider:
name: aws
runtime: nodejs12.x
runtime: nodejs22.x

functions:
hello:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

module.exports.hello = (event, context, callback) => {
export const hello = (event, context, callback) => {
const response = {
/* Status code required for default lambda integration */
statusCode: 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ custom:

provider:
name: aws
runtime: nodejs12.x
runtime: nodejs22.x
# WORKSHOP_START
# Step 4. In this_file, set the stage key to the custom.stage value set in previous step
# WORKSHOP_END
Expand Down
83 changes: 41 additions & 42 deletions _instructor/events/dynamodb-streams/handler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* Include the AWS sdk.
* No need to add to package.json. It's included in lambda env
*/
const AWS = require('aws-sdk')
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, PutCommand, ScanCommand, DeleteCommand } from '@aws-sdk/lib-dynamodb';
// Connect to DynamoDB
const dynamoDb = new AWS.DynamoDB.DocumentClient()
const client = new DynamoDBClient({});
const dynamoDb = DynamoDBDocumentClient.from(client);

// Save item in DynamoDB table
module.exports.create = (event, context, callback) => {
export const create = async (event, context, callback) => {
const timestamp = new Date().getTime()
const body = JSON.parse(event.body)

Expand All @@ -30,54 +32,52 @@ module.exports.create = (event, context, callback) => {
}

// write the todo to the database
dynamoDb.put(params, (error) => {
// handle potential errors
if (error) {
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the dynamo item.',
})
}

try {
await dynamoDb.send(new PutCommand(params));
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(params.Item),
}
return callback(null, response)
})
} catch (error) {
// handle potential errors
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t create the dynamo item.',
})
}
}

/* Scan a dynamoDB table and return items */
module.exports.scan = (event, context, callback) => {
export const scan = async (event, context, callback) => {
const params = {
TableName: process.env.MY_TABLE,
}
// fetch all todos from the database
dynamoDb.scan(params, (error, result) => {
// handle potential errors
if (error) {
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todos.',
})
}

try {
const result = await dynamoDb.send(new ScanCommand(params));
// create a response
const response = {
statusCode: 200,
body: JSON.stringify(result.Items),
}
return callback(null, response)
})
} catch (error) {
// handle potential errors
console.error(error)
return callback(null, {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the callback syntax from all functions as well. We can just return the response

do this for all instances of this inn the _instructor directory

statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t fetch the todos.',
})
}
}


module.exports.delete = (event, context, callback) => {
export const delete = async (event, context, callback) => {
const body = JSON.parse(event.body)

if (!body || !body.id) {
Expand Down Expand Up @@ -105,17 +105,8 @@ module.exports.delete = (event, context, callback) => {
}

// delete the todo from the database
dynamoDb.delete(params, (error) => {
// handle potential errors
if (error) {
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t remove the todo item.',
})
}

try {
await dynamoDb.send(new DeleteCommand(params));
// create a response
const response = {
statusCode: 200,
Expand All @@ -124,7 +115,15 @@ module.exports.delete = (event, context, callback) => {
}),
}
return callback(null, response)
})
} catch (error) {
// handle potential errors
console.error(error)
return callback(null, {
statusCode: error.statusCode || 501,
headers: { 'Content-Type': 'text/plain' },
body: 'Couldn\'t remove the todo item.',
})
}
// FINAL_END
}

Expand All @@ -137,7 +136,7 @@ module.exports.delete = (event, context, callback) => {
*/
// WORKSHOP_END
/* Function to handle items on the dynamoDB stream */
module.exports.dynamoStreamHandler = (event, context, callback) => {
export const dynamoStreamHandler = (event, context, callback) => {
// FINAL_START
event.Records.forEach((record) => {
console.log(record.eventID)
Expand Down
Loading
Loading