more notes on AWS SAM

This commit is contained in:
thomasabishop 2023-04-16 19:16:14 +01:00
parent fe0528be0e
commit dee01c5424
2 changed files with 48 additions and 1 deletions

View file

@ -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;
};
```

View file

@ -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)