From 89b7680badc51cd77b14e90fd0b2374fb29fe2ff Mon Sep 17 00:00:00 2001 From: Daghan Acay Date: Sun, 18 Nov 2018 21:06:04 +1100 Subject: [PATCH 1/7] initial commit --- .gitignore | 3 --- 1 file changed, 3 deletions(-) diff --git a/.gitignore b/.gitignore index 67f5f65..2b48c8b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -.env -.envrc - # package directories node_modules jspm_packages From 3b6697ce9b8fc3fa1226cc48109e8d8fb0a7122f Mon Sep 17 00:00:00 2001 From: Daghan Acay Date: Sun, 18 Nov 2018 21:54:39 +1100 Subject: [PATCH 2/7] finished first lab --- Lab_1_-_First_Function/README.MD | 67 +++++++++++++++---- .../solution/Task/serverless.yml | 3 + 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/Lab_1_-_First_Function/README.MD b/Lab_1_-_First_Function/README.MD index 7458666..a9b95e5 100644 --- a/Lab_1_-_First_Function/README.MD +++ b/Lab_1_-_First_Function/README.MD @@ -5,20 +5,26 @@ In this lab, we'll be creating our first lambda function using the Serverless fr The Serverless framework abstracts all the hassle of setting up, deploying, and maintaining out functions and their corresponding configuration and events. -### Requirements: +## Requirements: - AWS CLI - AWS account set up on CLI - Postman - Node.js v8.10+ installed - Serverless framework installed globally -### Outcomes: +## Outcomes: + +We will achieve the following outcomes by the end of this lab + - Create a Lambda function using the Serverless Framework - Locally invoke a function - Deploy a function to AWS - Test a remote function -### Overview: +## Overview: + +Overview of the steps are as follows + - Create a lambda function - Locally invoking a function - Deploying a function @@ -26,21 +32,33 @@ The Serverless framework abstracts all the hassle of setting up, deploying, and - Adding an API to the function - Testing the API -### Steps: -1. Create a new project using the Serverless framework create command +### Steps: Create a lambda function +1. Go to the root folder, e.g. Serverless-AWS-CRUD-Workshop/ and create a new project using the Serverless framework create command where [your service name] is something you call to your service e.g. myTestService. Make sure you do not use speciala characters like underscore "_". ``` sls create --template aws-nodejs --name [your service name] ``` -2. Open the `serverless.yml` file and add the following to the bottom of the configuration file to create a new hello Function: +2. Open the `Serverless-AWS-CRUD-Workshop/serverless.yml` set up the AWS environment by replacing + +```yaml +provider: + name: aws + runtime: nodejs6.10 + stage: dev + region: ap-southeast-2 + timeout: 30 + versionFunctions: false +``` +This will tell the serverless that you want your service to be deployed to Syndey region (you can choose others off course). Now find and replace the following ```yaml functions: hello: handler: src/hello-handler.handler ``` +This creates a new hello `Function` with handler method defined in `hello-handler.js` which we will do next -3. Create a new directory called `src` -4. Create a new file called `hello-handler.js` +3. Create a new directory called `Serverless-AWS-CRUD-Workshop/src` +4. Create a new file called `Serverless-AWS-CRUD-Workshop/src/hello-handler.js` 5. Place the following code in that function ```javascript @@ -54,11 +72,21 @@ module.exports.handler = async (event, context) => { }; ``` +Alternatively you can copy the file from Lab_1_-_First_Function/solution/Task/src/hello-handler.js + 6. Test this function to confirm it prints "Hello world" with the serverless invoke local command ```bash sls invoke local --function hello ``` +You shoud revieve +```json +{ + "statusCode": 200, + "body": "{\"message\":\"Hello world\"}" +} +``` +At this point you can delete `Serverless-AWS-CRUD-Workshop/handler.js` file since we are no longer using it. 7. Once you\'ve confirmed the function works as expected, we can deploy our function to our cloud account with ```bash @@ -70,9 +98,9 @@ sls deploy -v sls invoke --function hello ``` -9. If you're able to get a response back, congratulations! You've deployed your first lambda function. +9. If you're able to get a response back, congratulations! You've deployed your first lambda function. You can see where your lmbad function bgy loging into AWS console and your lambda functions under Sydney region. We now need to put an API in front of it if we want to call it from a website frontend. -Add an API and API endpoint to your project by adding the following code to your `serverless.yml` file. Under the hello handler. +Add an API and API endpoint to your project by adding the code under `### Add everything below` to your `serverless.yml` file, under the existing hello handler. ```yaml functions: hello: @@ -90,10 +118,10 @@ functions: sls deploy -v ``` -11. After the deployment completes. You should see an api endpoint under `endpoints`. Click on this link to open it in the browser. +11. After the deployment completes. You should see an api endpoint under `endpoints`. Click on this link to open it in the browser. You can see your deployed endpoint at AWS console under `API gateway` under dev-myTestService and he you can find the API url under Stages->dev called Invoke URL ### Your task -1. Create a new directory in the src directory called `book` +1. Create a new directory under the src directory called `book` 2. Create four new functions in the serverless yaml file: - createBook - getBook @@ -107,7 +135,20 @@ Each function should have the same API event path of `book` but the methods shou - DELETE 3. Create a new function handler file for each of these functions and copy the same code from the initial hello handler. -4. Change each function's return message and confirm they're all working with Postman. +4. Change each function's return message and confirm they're all working with Postman. + +### Solution +You can copy Lab_1_-_First_Function/solution/Task folder contents to root folder and run +``` +sls deploy +``` + +## Test your work + +After you have deployed the book endpoints you can test your work using Postman. Import the `AWS Serverless CRUD.postman_collection.json` into postman. +- create a new environment called "meetup-book" +- in the invironment define variable "API_ENDPOINT" and asign it to your API URL eg https://XXXXXX.execute-api.ap-southeast-2.amazonaws.com/dev +- Now you can chose any of the endpoint under "AWS Serverless CRUD" collection and see if it works! ### References 1. [Postman](https://www.getpostman.com/apps) diff --git a/Lab_1_-_First_Function/solution/Task/serverless.yml b/Lab_1_-_First_Function/solution/Task/serverless.yml index e979547..f115fe6 100644 --- a/Lab_1_-_First_Function/solution/Task/serverless.yml +++ b/Lab_1_-_First_Function/solution/Task/serverless.yml @@ -3,6 +3,9 @@ service: serverless-crud provider: name: aws runtime: nodejs8.10 + stage: dev + region: ap-southeast-2 + timeout: 30 versionFunctions: false functions: From 8b0732353143939cb4e9de213bce4aaf3b57f8d2 Mon Sep 17 00:00:00 2001 From: Daghan Acay Date: Sun, 18 Nov 2018 22:04:32 +1100 Subject: [PATCH 3/7] final signed Signed-off-by: Daghan Acay --- serverless.yml | 42 ++++++++++++++++++++++++++++++++++ src/book/createBook-handler.js | 8 +++++++ src/book/deleteBook-handler.js | 8 +++++++ src/book/getBook-handler.js | 8 +++++++ src/book/updateBook-handler.js | 8 +++++++ src/hello-handler.js | 8 +++++++ 6 files changed, 82 insertions(+) create mode 100644 serverless.yml create mode 100644 src/book/createBook-handler.js create mode 100644 src/book/deleteBook-handler.js create mode 100644 src/book/getBook-handler.js create mode 100644 src/book/updateBook-handler.js create mode 100644 src/hello-handler.js diff --git a/serverless.yml b/serverless.yml new file mode 100644 index 0000000..f115fe6 --- /dev/null +++ b/serverless.yml @@ -0,0 +1,42 @@ +service: serverless-crud + +provider: + name: aws + runtime: nodejs8.10 + stage: dev + region: ap-southeast-2 + timeout: 30 + versionFunctions: false + +functions: + hello: + handler: src/hello-handler.handler + events: + - http: + path: hello + method: get + createBook: + handler: src/book/createBook-handler.handler + events: + - http: + path: book + method: post + getBook: + handler: src/book/getBook-handler.handler + events: + - http: + path: book + method: get + updateBook: + handler: src/book/updateBook-handler.handler + events: + - http: + path: book + method: put + deleteBook: + handler: src/book/deleteBook-handler.handler + events: + - http: + path: book + method: delete + diff --git a/src/book/createBook-handler.js b/src/book/createBook-handler.js new file mode 100644 index 0000000..3421b99 --- /dev/null +++ b/src/book/createBook-handler.js @@ -0,0 +1,8 @@ +module.exports.handler = async (event, context) => { + return { + statusCode: 201, + body: JSON.stringify({ + message: 'Create' + }) + }; +}; \ No newline at end of file diff --git a/src/book/deleteBook-handler.js b/src/book/deleteBook-handler.js new file mode 100644 index 0000000..a712e40 --- /dev/null +++ b/src/book/deleteBook-handler.js @@ -0,0 +1,8 @@ +module.exports.handler = async (event, context) => { + return { + statusCode: 200, + body: JSON.stringify({ + message: 'Delete' + }) + }; +}; \ No newline at end of file diff --git a/src/book/getBook-handler.js b/src/book/getBook-handler.js new file mode 100644 index 0000000..8d48179 --- /dev/null +++ b/src/book/getBook-handler.js @@ -0,0 +1,8 @@ +module.exports.handler = async (event, context) => { + return { + statusCode: 200, + body: JSON.stringify({ + message: 'Get' + }) + }; +}; \ No newline at end of file diff --git a/src/book/updateBook-handler.js b/src/book/updateBook-handler.js new file mode 100644 index 0000000..33115e6 --- /dev/null +++ b/src/book/updateBook-handler.js @@ -0,0 +1,8 @@ +module.exports.handler = async (event, context) => { + return { + statusCode: 200, + body: JSON.stringify({ + message: 'Update' + }) + }; +}; \ No newline at end of file diff --git a/src/hello-handler.js b/src/hello-handler.js new file mode 100644 index 0000000..1b435f3 --- /dev/null +++ b/src/hello-handler.js @@ -0,0 +1,8 @@ +module.exports.handler = async (event, context) => { + return { + statusCode: 200, + body: JSON.stringify({ + message: 'Hello world' + }) + }; +}; \ No newline at end of file From 1adcb6e3aefe85f5addec435d371203dca381c29 Mon Sep 17 00:00:00 2001 From: Daghan Acay Date: Sun, 18 Nov 2018 22:06:01 +1100 Subject: [PATCH 4/7] final signed Signed-off-by: Daghan Acay --- serverless.yml | 42 ---------------------------------- src/book/createBook-handler.js | 8 ------- src/book/deleteBook-handler.js | 8 ------- src/book/getBook-handler.js | 8 ------- src/book/updateBook-handler.js | 8 ------- src/hello-handler.js | 8 ------- 6 files changed, 82 deletions(-) delete mode 100644 serverless.yml delete mode 100644 src/book/createBook-handler.js delete mode 100644 src/book/deleteBook-handler.js delete mode 100644 src/book/getBook-handler.js delete mode 100644 src/book/updateBook-handler.js delete mode 100644 src/hello-handler.js diff --git a/serverless.yml b/serverless.yml deleted file mode 100644 index f115fe6..0000000 --- a/serverless.yml +++ /dev/null @@ -1,42 +0,0 @@ -service: serverless-crud - -provider: - name: aws - runtime: nodejs8.10 - stage: dev - region: ap-southeast-2 - timeout: 30 - versionFunctions: false - -functions: - hello: - handler: src/hello-handler.handler - events: - - http: - path: hello - method: get - createBook: - handler: src/book/createBook-handler.handler - events: - - http: - path: book - method: post - getBook: - handler: src/book/getBook-handler.handler - events: - - http: - path: book - method: get - updateBook: - handler: src/book/updateBook-handler.handler - events: - - http: - path: book - method: put - deleteBook: - handler: src/book/deleteBook-handler.handler - events: - - http: - path: book - method: delete - diff --git a/src/book/createBook-handler.js b/src/book/createBook-handler.js deleted file mode 100644 index 3421b99..0000000 --- a/src/book/createBook-handler.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports.handler = async (event, context) => { - return { - statusCode: 201, - body: JSON.stringify({ - message: 'Create' - }) - }; -}; \ No newline at end of file diff --git a/src/book/deleteBook-handler.js b/src/book/deleteBook-handler.js deleted file mode 100644 index a712e40..0000000 --- a/src/book/deleteBook-handler.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports.handler = async (event, context) => { - return { - statusCode: 200, - body: JSON.stringify({ - message: 'Delete' - }) - }; -}; \ No newline at end of file diff --git a/src/book/getBook-handler.js b/src/book/getBook-handler.js deleted file mode 100644 index 8d48179..0000000 --- a/src/book/getBook-handler.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports.handler = async (event, context) => { - return { - statusCode: 200, - body: JSON.stringify({ - message: 'Get' - }) - }; -}; \ No newline at end of file diff --git a/src/book/updateBook-handler.js b/src/book/updateBook-handler.js deleted file mode 100644 index 33115e6..0000000 --- a/src/book/updateBook-handler.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports.handler = async (event, context) => { - return { - statusCode: 200, - body: JSON.stringify({ - message: 'Update' - }) - }; -}; \ No newline at end of file diff --git a/src/hello-handler.js b/src/hello-handler.js deleted file mode 100644 index 1b435f3..0000000 --- a/src/hello-handler.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports.handler = async (event, context) => { - return { - statusCode: 200, - body: JSON.stringify({ - message: 'Hello world' - }) - }; -}; \ No newline at end of file From bc59a8c5e022d4c2329c0110318e3f48f8e7c450 Mon Sep 17 00:00:00 2001 From: Daghan Acay Date: Sun, 18 Nov 2018 22:10:40 +1100 Subject: [PATCH 5/7] added the headers Signed-off-by: Daghan Acay --- Lab_1_-_First_Function/README.MD | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Lab_1_-_First_Function/README.MD b/Lab_1_-_First_Function/README.MD index a9b95e5..3b62c68 100644 --- a/Lab_1_-_First_Function/README.MD +++ b/Lab_1_-_First_Function/README.MD @@ -33,6 +33,7 @@ Overview of the steps are as follows - Testing the API ### Steps: Create a lambda function + 1. Go to the root folder, e.g. Serverless-AWS-CRUD-Workshop/ and create a new project using the Serverless framework create command where [your service name] is something you call to your service e.g. myTestService. Make sure you do not use speciala characters like underscore "_". ``` sls create --template aws-nodejs --name [your service name] @@ -57,6 +58,8 @@ functions: ``` This creates a new hello `Function` with handler method defined in `hello-handler.js` which we will do next +### Locally invoking a function + 3. Create a new directory called `Serverless-AWS-CRUD-Workshop/src` 4. Create a new file called `Serverless-AWS-CRUD-Workshop/src/hello-handler.js` 5. Place the following code in that function @@ -88,16 +91,21 @@ You shoud revieve ``` At this point you can delete `Serverless-AWS-CRUD-Workshop/handler.js` file since we are no longer using it. +### Deploying a function + 7. Once you\'ve confirmed the function works as expected, we can deploy our function to our cloud account with ```bash sls deploy -v ``` +### Cloud invoking a function 8. Test that the function works in the cloud by running the invoke command again, but this time, without the local command ```bash sls invoke --function hello ``` +### Adding an API to the function + 9. If you're able to get a response back, congratulations! You've deployed your first lambda function. You can see where your lmbad function bgy loging into AWS console and your lambda functions under Sydney region. We now need to put an API in front of it if we want to call it from a website frontend. Add an API and API endpoint to your project by adding the code under `### Add everything below` to your `serverless.yml` file, under the existing hello handler. @@ -120,7 +128,8 @@ sls deploy -v 11. After the deployment completes. You should see an api endpoint under `endpoints`. Click on this link to open it in the browser. You can see your deployed endpoint at AWS console under `API gateway` under dev-myTestService and he you can find the API url under Stages->dev called Invoke URL -### Your task +## Your task + 1. Create a new directory under the src directory called `book` 2. Create four new functions in the serverless yaml file: - createBook @@ -143,7 +152,7 @@ You can copy Lab_1_-_First_Function/solution/Task folder contents to root folder sls deploy ``` -## Test your work +### Testing the API After you have deployed the book endpoints you can test your work using Postman. Import the `AWS Serverless CRUD.postman_collection.json` into postman. - create a new environment called "meetup-book" From 5fa37e3cbe30fdbcc440b8db7e9df4a642e30303 Mon Sep 17 00:00:00 2001 From: Daghan Acay Date: Sun, 27 Jan 2019 18:09:07 +1100 Subject: [PATCH 6/7] requested changes --- .gitignore | 3 +++ Lab_1_-_First_Function/README.MD | 16 +++++++++------- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 2b48c8b..67f5f65 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.env +.envrc + # package directories node_modules jspm_packages diff --git a/Lab_1_-_First_Function/README.MD b/Lab_1_-_First_Function/README.MD index 3b62c68..03e6dba 100644 --- a/Lab_1_-_First_Function/README.MD +++ b/Lab_1_-_First_Function/README.MD @@ -34,23 +34,25 @@ Overview of the steps are as follows ### Steps: Create a lambda function -1. Go to the root folder, e.g. Serverless-AWS-CRUD-Workshop/ and create a new project using the Serverless framework create command where [your service name] is something you call to your service e.g. myTestService. Make sure you do not use speciala characters like underscore "_". +1. Go to the root folder, e.g. Serverless-AWS-CRUD-Workshop/ and create a new project using the Serverless framework create command where [your service name] is something you call to your service e.g. myTestService. Make sure you do not use special characters like underscore "_". ``` sls create --template aws-nodejs --name [your service name] ``` -2. Open the `Serverless-AWS-CRUD-Workshop/serverless.yml` set up the AWS environment by replacing +2. Open the Serverless-AWS-CRUD-Workshop/serverless.yml file. This file contains all the configuration and resources for your serverless project. To tell the serverless framework to use AWS, replace everything in the document with the configuration as follows ```yaml provider: name: aws - runtime: nodejs6.10 + runtime: nodejs8.10 stage: dev region: ap-southeast-2 timeout: 30 versionFunctions: false ``` -This will tell the serverless that you want your service to be deployed to Syndey region (you can choose others off course). Now find and replace the following +The configuration above will tell the serverless framework that you want to deploy your service in the Sydney region. +Next, we're going to specify our functions to deploy. +Paste the configuration below under the previous configuration. ```yaml functions: hello: @@ -106,7 +108,7 @@ sls invoke --function hello ### Adding an API to the function -9. If you're able to get a response back, congratulations! You've deployed your first lambda function. You can see where your lmbad function bgy loging into AWS console and your lambda functions under Sydney region. +9. If you're able to get a response back, congratulations! You've deployed your first lambda function. You can see where your lambda function by logging into AWS console and your lambda functions in the Sydney region. We now need to put an API in front of it if we want to call it from a website frontend. Add an API and API endpoint to your project by adding the code under `### Add everything below` to your `serverless.yml` file, under the existing hello handler. ```yaml @@ -156,8 +158,8 @@ sls deploy After you have deployed the book endpoints you can test your work using Postman. Import the `AWS Serverless CRUD.postman_collection.json` into postman. - create a new environment called "meetup-book" -- in the invironment define variable "API_ENDPOINT" and asign it to your API URL eg https://XXXXXX.execute-api.ap-southeast-2.amazonaws.com/dev -- Now you can chose any of the endpoint under "AWS Serverless CRUD" collection and see if it works! +- in the environment, define a new variable called "API_ENDPOINT" and assign it to your API URL eg https://XXXXXX.execute-api.ap-southeast-2.amazonaws.com/dev +- Now you can choose any of the function endpoints under "AWS Serverless CRUD" collection and see if it works! ### References 1. [Postman](https://www.getpostman.com/apps) From d163efc4c2ecca12a63163d42959e31892d544f1 Mon Sep 17 00:00:00 2001 From: Daghan Acay Date: Sun, 27 Jan 2019 18:12:50 +1100 Subject: [PATCH 7/7] final changes --- Lab_1_-_First_Function/README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lab_1_-_First_Function/README.MD b/Lab_1_-_First_Function/README.MD index 03e6dba..65c9bbb 100644 --- a/Lab_1_-_First_Function/README.MD +++ b/Lab_1_-_First_Function/README.MD @@ -128,7 +128,7 @@ functions: sls deploy -v ``` -11. After the deployment completes. You should see an api endpoint under `endpoints`. Click on this link to open it in the browser. You can see your deployed endpoint at AWS console under `API gateway` under dev-myTestService and he you can find the API url under Stages->dev called Invoke URL +11. After the deployment completes you should see an api endpoint under `endpoints`. Click on this link to open it in the browser. You can see your deployed endpoint at AWS console under `API gateway` under dev-myTestService. You can find the API url under Stages->dev called Invoke URL ## Your task