From dee01c5424bc9bc5b9975b3d445e41936d12ac64 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Sun, 16 Apr 2023 19:16:14 +0100 Subject: [PATCH] more notes on AWS SAM --- DevOps/AWS/{AWS_SAM => }/AWS_SAM.md | 47 +++++++++++++++++++ DevOps/AWS/Developing_AWS_projects_locally.md | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) rename DevOps/AWS/{AWS_SAM => }/AWS_SAM.md (76%) diff --git a/DevOps/AWS/AWS_SAM/AWS_SAM.md b/DevOps/AWS/AWS_SAM.md similarity index 76% rename from DevOps/AWS/AWS_SAM/AWS_SAM.md rename to DevOps/AWS/AWS_SAM.md index 68b9a06..7122418 100644 --- a/DevOps/AWS/AWS_SAM/AWS_SAM.md +++ b/DevOps/AWS/AWS_SAM.md @@ -113,3 +113,50 @@ It also lists the `get` event that we can use to call API Gateway and trigger th The full template is below: ![](/_img/sam-template-yaml.png) + +## Adding our own code + +We will create our own function and API Gateway trigger. + +We will place our function after the existing `HelloWorldFunction` + +```yaml +ClockFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: clock/ + Handler: handler.clock + Runtime: nodejs16.x + Events: + ClockApi: + Type: Api + Properties: + Path: /clock + Method: get +``` + +Just like with `HelloWorld`, we will create a directory for this function: `clock` and we will initialise it as an `npm` project. + +```sh +mkdir clock +cd clock +npm init +``` + +We will use `handler.js` as our root, handler function. + +We have said in the template file that our `Handler: handler.clock`, therefore the main function in the `handler` module should be `clock`: + +```js +const moment = require("moment"); + +exports.clock = async (event) => { + console.log("Clock function run"); + const message = moment().format(); + const response = { + statusCode: 200, + body: JSON.stringify(message), + }; + return response; +}; +``` diff --git a/DevOps/AWS/Developing_AWS_projects_locally.md b/DevOps/AWS/Developing_AWS_projects_locally.md index 8e29361..49eeadb 100644 --- a/DevOps/AWS/Developing_AWS_projects_locally.md +++ b/DevOps/AWS/Developing_AWS_projects_locally.md @@ -12,4 +12,4 @@ To develop AWS projects locally the following core tools can be used to set up a - Interact with AWS services from your local machine - AWS SDK - Integrate AWS services into your given programming language and application code -- [AWS SAM](/DevOps/AWS/AWS_SAM/AWS_SAM.md) +- [AWS SAM](/DevOps/AWS/AWS_SAM.md)