Skip to content

Commit c6122d4

Browse files
Remove callback syntax from all functions in _instructor directory
- Convert all AWS Lambda functions from callback syntax to modern return syntax - Remove callback parameter from function signatures (event, context, callback) -> (event, context) - Replace callback(null, response) with return response - Replace callback(error) with throw error - Add missing package.json files for directories using AWS SDK v3: - _instructor/events/dynamodb-streams/package.json - _instructor/events/sns/package.json Addresses review feedback to modernize Lambda functions and ensure proper v3 SDK dependencies. Co-authored-by: David Wells <DavidWells@users.noreply.github.com>
1 parent 7720444 commit c6122d4

File tree

18 files changed

+106
-88
lines changed

18 files changed

+106
-88
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export const hello = (event, context, callback) => {
2+
export const hello = (event, context) => {
33
// WORKSHOP_START
44
/* Step 1. In this_file, Create a `200` response code and return the `event` data in the response body.
55
@@ -8,7 +8,7 @@ export const hello = (event, context, callback) => {
88
For more details, see the http event docs link http://bit.ly/2mkgV4P
99
*/
1010
const response = {}
11-
return callback(null, response);
11+
return response;
1212
// WORKSHOP_END
1313
// FINAL_START
1414
const response = {
@@ -19,7 +19,6 @@ export const hello = (event, context, callback) => {
1919
event: event,
2020
}),
2121
}
22-
/** callback(error, response) */
23-
return callback(null, response)
22+
return response
2423
// FINAL_END
2524
}

_instructor/core-concepts/2-http-dynamic-content/handler.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
77
Finally remember to set the headers of the response as `'Content-Type': 'text/html'` to return HTML instead of the default `json`
88
*/
9-
export const queryParamsExample = (event, context, callback) => {
9+
export const queryParamsExample = (event, context) => {
1010
// WORKSHOP_START
1111
const response = {
1212
statusCode: 200,
1313
body: JSON.stringify({
1414
message: 'Hi ⊂◉‿◉つ',
1515
}),
1616
}
17-
return callback(null, response)
17+
return response
1818
// WORKSHOP_END
1919
// FINAL_START
2020
let name
@@ -24,13 +24,13 @@ export const queryParamsExample = (event, context, callback) => {
2424
/* generate the hello paragraph */
2525
const helloParagraph = greetPerson(name)
2626

27-
return callback(null, {
27+
return {
2828
statusCode: 200,
2929
headers: {
3030
'Content-Type': 'text/html',
3131
},
3232
body: generateHtmlPage(helloParagraph),
33-
})
33+
}
3434
// FINAL_END
3535
}
3636

@@ -42,15 +42,15 @@ export const queryParamsExample = (event, context, callback) => {
4242
4343
Finally, remember to set the headers of the response as `'Content-Type': 'text/html'` to return HTML instead of the default `json`
4444
*/
45-
export const pathParamsExample = (event, context, callback) => {
45+
export const pathParamsExample = (event, context) => {
4646
// WORKSHOP_START
4747
const response = {
4848
statusCode: 200,
4949
body: JSON.stringify({
5050
message: 'Hi ⊂◉‿◉つ',
5151
}),
5252
}
53-
return callback(null, response)
53+
return response
5454
// WORKSHOP_END
5555
// FINAL_START
5656
let name
@@ -61,14 +61,14 @@ export const pathParamsExample = (event, context, callback) => {
6161
/* generate the hello paragraph */
6262
const helloParagraph = greetPerson(name)
6363

64-
// callback is sending HTML back
65-
return callback(null, {
64+
// return is sending HTML back
65+
return {
6666
statusCode: 200,
6767
headers: {
6868
'Content-Type': 'text/html',
6969
},
7070
body: generateHtmlPage(helloParagraph),
71-
})
71+
}
7272
// FINAL_END
7373
}
7474

_instructor/core-concepts/3-http-post-with-cors/handler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
For additional information, see the cors docs http://bit.ly/2FlFSWB
77
*/
88
// WORKSHOP_END
9-
export const functionWithCors = (event, context, callback) => {
9+
export const functionWithCors = (event, context) => {
1010
const response = {
1111
statusCode: 200,
1212
// FINAL_START
@@ -22,5 +22,5 @@ export const functionWithCors = (event, context, callback) => {
2222
event: event,
2323
}),
2424
}
25-
return callback(null, response)
25+
return response
2626
}

_instructor/core-concepts/4-using-env-vars/handler.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Return the environment variable in the `foo` function response
99
*/
1010
// WORKSHOP_END
11-
export const foo = (event, context, callback) => {
11+
export const foo = (event, context) => {
1212
// FINAL_START
1313
console.log('process.env.MY_ENV_VAR', process.env.MY_ENV_VAR)
1414
/* MY_ENV_VAR_FOR_BAR will be undefined */
@@ -31,7 +31,7 @@ export const foo = (event, context, callback) => {
3131
// FINAL_END
3232
}),
3333
}
34-
return callback(null, response)
34+
return response
3535
}
3636

3737
// WORKSHOP_START
@@ -42,7 +42,7 @@ export const foo = (event, context, callback) => {
4242
Return the environment variable in the `bar` function response
4343
*/
4444
// WORKSHOP_END
45-
export const bar = (event, context, callback) => {
45+
export const bar = (event, context) => {
4646
// FINAL_START
4747
/* both env variables will be accessible in bar */
4848
console.log('process.env.MY_ENV_VAR', process.env.MY_ENV_VAR)
@@ -66,5 +66,5 @@ export const bar = (event, context, callback) => {
6666
// FINAL_END
6767
}),
6868
}
69-
return callback(null, response)
69+
return response
7070
}

_instructor/core-concepts/5-using-serverless-variable-syntax/handler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export const foo = (event, context, callback) => {
2+
export const foo = (event, context) => {
33
// FINAL_START
44
console.log('process.env.MY_SECRET', process.env.MY_SECRET)
55
// FINAL_END
@@ -15,5 +15,5 @@ export const foo = (event, context, callback) => {
1515
message: `my super secret thing ${process.env.MY_SECRET}`
1616
}),
1717
}
18-
return callback(null, response)
18+
return response
1919
}

_instructor/core-concepts/6-using-addition-resources/handler.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ const client = new DynamoDBClient({});
88
const dynamoDb = DynamoDBDocumentClient.from(client);
99

1010
// Save item in DynamoDB table
11-
export const create = async (event, context, callback) => {
11+
export const create = async (event, context) => {
1212
const timestamp = new Date().getTime()
1313
const body = JSON.parse(event.body)
1414

1515
if (!body || !body.email) {
16-
return callback(null, {
16+
return {
1717
statusCode: 401,
1818
headers: { 'Content-Type': 'text/plain' },
1919
body: JSON.stringify({
2020
error: 'no body found or email found'
2121
})
22-
})
22+
}
2323
}
2424

2525
const params = {
@@ -39,20 +39,20 @@ export const create = async (event, context, callback) => {
3939
statusCode: 200,
4040
body: JSON.stringify(params.Item),
4141
}
42-
return callback(null, response)
42+
return response
4343
} catch (error) {
4444
// handle potential errors
4545
console.error(error)
46-
return callback(null, {
46+
return {
4747
statusCode: error.statusCode || 501,
4848
headers: { 'Content-Type': 'text/plain' },
4949
body: 'Couldn\'t create the dynamo item.',
50-
})
50+
}
5151
}
5252
}
5353

5454
/* Scan a dynamoDB table and return items */
55-
export const scan = async (event, context, callback) => {
55+
export const scan = async (event, context) => {
5656
const params = {
5757
TableName: process.env.MY_TABLE,
5858
}
@@ -64,14 +64,14 @@ export const scan = async (event, context, callback) => {
6464
statusCode: 200,
6565
body: JSON.stringify(result.Items),
6666
}
67-
return callback(null, response)
67+
return response
6868
} catch (error) {
6969
// handle potential errors
7070
console.error(error)
71-
return callback(null, {
71+
return {
7272
statusCode: error.statusCode || 501,
7373
headers: { 'Content-Type': 'text/plain' },
7474
body: 'Couldn\'t fetch the todos.',
75-
})
75+
}
7676
}
7777
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export const hello = (event, context, callback) => {
2+
export const hello = (event, context) => {
33
const response = {
44
/* Status code required for default lambda integration */
55
statusCode: 200,
@@ -8,5 +8,5 @@ export const hello = (event, context, callback) => {
88
}),
99
}
1010
/** callback(error, response) */
11-
return callback(null, response)
11+
return response
1212
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
export const hello = (event, context, callback) => {
2+
export const hello = (event, context) => {
33
const response = {
44
/* Status code required for default lambda integration */
55
statusCode: 200,
@@ -8,5 +8,5 @@ export const hello = (event, context, callback) => {
88
}),
99
}
1010
/** callback(error, response) */
11-
return callback(null, response)
11+
return response
1212
}

_instructor/events/dynamodb-streams/handler.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ const client = new DynamoDBClient({});
88
const dynamoDb = DynamoDBDocumentClient.from(client);
99

1010
// Save item in DynamoDB table
11-
export const create = async (event, context, callback) => {
11+
export const create = async (event, context) => {
1212
const timestamp = new Date().getTime()
1313
const body = JSON.parse(event.body)
1414

1515
if (!body || !body.email) {
16-
return callback(null, {
16+
return {
1717
statusCode: 401,
1818
headers: { 'Content-Type': 'text/plain' },
1919
body: JSON.stringify({
2020
error: 'no body found or email found'
2121
})
22-
})
22+
}
2323
}
2424

2525
const params = {
@@ -39,20 +39,20 @@ export const create = async (event, context, callback) => {
3939
statusCode: 200,
4040
body: JSON.stringify(params.Item),
4141
}
42-
return callback(null, response)
42+
return response
4343
} catch (error) {
4444
// handle potential errors
4545
console.error(error)
46-
return callback(null, {
46+
return {
4747
statusCode: error.statusCode || 501,
4848
headers: { 'Content-Type': 'text/plain' },
4949
body: 'Couldn\'t create the dynamo item.',
50-
})
50+
}
5151
}
5252
}
5353

5454
/* Scan a dynamoDB table and return items */
55-
export const scan = async (event, context, callback) => {
55+
export const scan = async (event, context) => {
5656
const params = {
5757
TableName: process.env.MY_TABLE,
5858
}
@@ -64,30 +64,30 @@ export const scan = async (event, context, callback) => {
6464
statusCode: 200,
6565
body: JSON.stringify(result.Items),
6666
}
67-
return callback(null, response)
67+
return response
6868
} catch (error) {
6969
// handle potential errors
7070
console.error(error)
71-
return callback(null, {
71+
return {
7272
statusCode: error.statusCode || 501,
7373
headers: { 'Content-Type': 'text/plain' },
7474
body: 'Couldn\'t fetch the todos.',
75-
})
75+
}
7676
}
7777
}
7878

7979

80-
export const delete = async (event, context, callback) => {
80+
export const delete = async (event, context) => {
8181
const body = JSON.parse(event.body)
8282

8383
if (!body || !body.id) {
84-
return callback(null, {
84+
return {
8585
statusCode: 401,
8686
headers: { 'Content-Type': 'text/plain' },
8787
body: JSON.stringify({
8888
error: 'no body found or id found'
8989
})
90-
})
90+
}
9191
}
9292
// WORKSHOP_START
9393
/* Step 1. In this_file, implement the delete item function here via `dynamoDb.delete` method.
@@ -114,15 +114,15 @@ export const delete = async (event, context, callback) => {
114114
message: `user ${body.id} deleted`
115115
}),
116116
}
117-
return callback(null, response)
117+
return response
118118
} catch (error) {
119119
// handle potential errors
120120
console.error(error)
121-
return callback(null, {
121+
return {
122122
statusCode: error.statusCode || 501,
123123
headers: { 'Content-Type': 'text/plain' },
124124
body: 'Couldn\'t remove the todo item.',
125-
})
125+
}
126126
}
127127
// FINAL_END
128128
}
@@ -136,7 +136,7 @@ export const delete = async (event, context, callback) => {
136136
*/
137137
// WORKSHOP_END
138138
/* Function to handle items on the dynamoDB stream */
139-
export const dynamoStreamHandler = (event, context, callback) => {
139+
export const dynamoStreamHandler = (event, context) => {
140140
// FINAL_START
141141
event.Records.forEach((record) => {
142142
console.log(record.eventID)
@@ -149,6 +149,6 @@ export const dynamoStreamHandler = (event, context, callback) => {
149149
console.log('REMOVAL EVENT. DO REMOVAL STUFF')
150150
}
151151
})
152-
return callback(null, `Successfully processed ${event.Records.length} records.`);
152+
return `Successfully processed ${event.Records.length} records.`;
153153
// FINAL_END
154154
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "dynamodb-streams-handler",
3+
"version": "1.0.0",
4+
"description": "DynamoDB streams handler using AWS SDK v3",
5+
"type": "module",
6+
"dependencies": {
7+
"@aws-sdk/client-dynamodb": "^3.668.0",
8+
"@aws-sdk/lib-dynamodb": "^3.668.0"
9+
}
10+
}

0 commit comments

Comments
 (0)