Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions _instructor/core-concepts/1-http-hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This lesson will walk through creating a basic function triggered by a http GET
<!-- AUTO-GENERATED-CONTENT:START (TOC) -->
- [Lesson Steps](#lesson-steps)
- [Complete code](#complete-code)
- [Additional Resources](#additional-resources)
<!-- AUTO-GENERATED-CONTENT:END -->

## Lesson Steps
Expand Down
7 changes: 3 additions & 4 deletions _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) => {
// WORKSHOP_START
/* Step 1. In this_file, Create a `200` response code and return the `event` data in the response body.

Expand All @@ -8,7 +8,7 @@ module.exports.hello = (event, context, callback) => {
For more details, see the http event docs link http://bit.ly/2mkgV4P
*/
const response = {}
return callback(null, response);
return response;
// WORKSHOP_END
// FINAL_START
const response = {
Expand All @@ -19,7 +19,6 @@ module.exports.hello = (event, context, callback) => {
event: event,
}),
}
/** callback(error, response) */
return callback(null, response)
return response
// FINAL_END
}
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
18 changes: 9 additions & 9 deletions _instructor/core-concepts/2-http-dynamic-content/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

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) => {
// WORKSHOP_START
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Hi ⊂◉‿◉つ',
}),
}
return callback(null, response)
return response
// WORKSHOP_END
// FINAL_START
let name
Expand All @@ -24,13 +24,13 @@ module.exports.queryParamsExample = (event, context, callback) => {
/* generate the hello paragraph */
const helloParagraph = greetPerson(name)

return callback(null, {
return {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
},
body: generateHtmlPage(helloParagraph),
})
}
// FINAL_END
}

Expand All @@ -42,15 +42,15 @@ 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) => {
// WORKSHOP_START
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Hi ⊂◉‿◉つ',
}),
}
return callback(null, response)
return response
// WORKSHOP_END
// FINAL_START
let name
Expand All @@ -61,14 +61,14 @@ module.exports.pathParamsExample = (event, context, callback) => {
/* generate the hello paragraph */
const helloParagraph = greetPerson(name)

// callback is sending HTML back
return callback(null, {
// return is sending HTML back
return {
statusCode: 200,
headers: {
'Content-Type': 'text/html',
},
body: generateHtmlPage(helloParagraph),
})
}
// FINAL_END
}

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
1 change: 1 addition & 0 deletions _instructor/core-concepts/3-http-post-with-cors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This lesson will walk through creating an http function triggered by a `POST` re
<!-- AUTO-GENERATED-CONTENT:START (TOC) -->
- [Lesson Steps](#lesson-steps)
- [Complete code](#complete-code)
- [Locking down cors to a specific domain](#locking-down-cors-to-a-specific-domain)
<!-- AUTO-GENERATED-CONTENT:END -->

## Lesson Steps
Expand Down
4 changes: 2 additions & 2 deletions _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) => {
const response = {
statusCode: 200,
// FINAL_START
Expand All @@ -22,5 +22,5 @@ module.exports.functionWithCors = (event, context, callback) => {
event: event,
}),
}
return callback(null, response)
return response
}
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
8 changes: 4 additions & 4 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) => {
// FINAL_START
console.log('process.env.MY_ENV_VAR', process.env.MY_ENV_VAR)
/* MY_ENV_VAR_FOR_BAR will be undefined */
Expand All @@ -31,7 +31,7 @@ module.exports.foo = (event, context, callback) => {
// FINAL_END
}),
}
return callback(null, response)
return response
}

// WORKSHOP_START
Expand All @@ -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) => {
// FINAL_START
/* both env variables will be accessible in bar */
console.log('process.env.MY_ENV_VAR', process.env.MY_ENV_VAR)
Expand All @@ -66,5 +66,5 @@ module.exports.bar = (event, context, callback) => {
// FINAL_END
}),
}
return callback(null, response)
return response
}
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) => {
// FINAL_START
console.log('process.env.MY_SECRET', process.env.MY_SECRET)
// FINAL_END
Expand All @@ -15,5 +15,5 @@ module.exports.foo = (event, context, callback) => {
message: `my super secret thing ${process.env.MY_SECRET}`
}),
}
return callback(null, response)
return response
}
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
64 changes: 32 additions & 32 deletions _instructor/core-concepts/6-using-addition-resources/handler.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
/* 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) => {
const timestamp = new Date().getTime()
const body = JSON.parse(event.body)

if (!body || !body.email) {
return callback(null, {
return {
statusCode: 401,
headers: { 'Content-Type': 'text/plain' },
body: JSON.stringify({
error: 'no body found or email found'
})
})
}
}

const params = {
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)
})
return response
} catch (error) {
// handle potential errors
console.error(error)
return {
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) => {
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)
})
return response
} catch (error) {
// handle potential errors
console.error(error)
return {
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) => {
const response = {
/* Status code required for default lambda integration */
statusCode: 200,
Expand All @@ -8,5 +8,5 @@ module.exports.hello = (event, context, callback) => {
}),
}
/** callback(error, response) */
return callback(null, response)
return response
}
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
4 changes: 2 additions & 2 deletions _instructor/core-concepts/8-using-multiple-stages/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) => {
const response = {
/* Status code required for default lambda integration */
statusCode: 200,
Expand All @@ -8,5 +8,5 @@ module.exports.hello = (event, context, callback) => {
}),
}
/** callback(error, response) */
return callback(null, response)
return response
}
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
Loading