|
1 |
| -# serverless-AWS |
| 1 | +# serverless-AWS |
| 2 | +Example REST API using the serverless stack from AWS: |
| 3 | + - Lambda |
| 4 | + - DynamoDB |
| 5 | + - SAM |
| 6 | + - API Gateway |
| 7 | + |
| 8 | +- [serverless-AWS](#serverless-aws) |
| 9 | + - [Available endpoints:](#available-endpoints) |
| 10 | + - [Prerequisites](#prerequisites) |
| 11 | + - [How to deploy the API](#how-to-deploy-the-api) |
| 12 | + - [SAM configuration](#sam-configuration) |
| 13 | + - [SAM deploy](#sam-deploy) |
| 14 | + - [Entities](#entities) |
| 15 | + - [Post](#post) |
| 16 | + - [Comment](#comment) |
| 17 | + - [Integration Tests](#integration-tests) |
| 18 | + |
| 19 | +## Available endpoints: |
| 20 | +| Method | Resource | |
| 21 | +| ------ |:------------------------------------:| |
| 22 | +| GET | /posts | |
| 23 | +| GET | /posts/{postId} | |
| 24 | +| POST | /posts | |
| 25 | +| POST | /posts/{postId}/comments | |
| 26 | +| PUT | /posts/{postId} | |
| 27 | +| DELETE | /posts/{postId} | |
| 28 | +| DELETE | /posts/{postId}/comments/{commentId} | |
| 29 | + |
| 30 | +## Prerequisites |
| 31 | +In order to deploy this API into you AWS environment, you will first need: |
| 32 | + - AWS account (educate, free tier or normal) |
| 33 | + - [SAM cli installed](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) |
| 34 | + - [AWS credentials configured](https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/setup-credentials.html) |
| 35 | + |
| 36 | +## How to deploy the API |
| 37 | +### SAM configuration |
| 38 | +You can manually create this file or use the command `sam deploy --guided` to interactively create it. |
| 39 | + |
| 40 | +``` |
| 41 | +version = 0.1 |
| 42 | +[default] |
| 43 | +[default.deploy] |
| 44 | +[default.deploy.parameters] |
| 45 | +stack_name = "serverless-aws" |
| 46 | +s3_bucket = "s3.cloudapps.codeurjc" |
| 47 | +s3_prefix = "serverless-aws" |
| 48 | +region = "us-east-1" |
| 49 | +capabilities = "CAPABILITY_IAM" |
| 50 | +``` |
| 51 | + |
| 52 | +Important information here is the **S3 Bucket** where your code will get packaged to and it's prefix. Also the **region** in where your API will get deployed, and where your DynamoDB is, please notice lines 4 to 6 in `src/dbManager.js`: |
| 53 | + |
| 54 | +``` |
| 55 | +AWS.config.update({ |
| 56 | + endpoint: "https://dynamodb.us-east-1.amazonaws.com" |
| 57 | +}); |
| 58 | +``` |
| 59 | +### SAM deploy |
| 60 | +Once `samconfig.toml` is in place, you can already perform `sam deploy` and your lambda application sould get deployed in your AWS account. Thanks to the last line of the file `template.yaml`, the logs from deploy should at the very end print the prod url where the API is now available: |
| 61 | + |
| 62 | +``` |
| 63 | +Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/posts/" |
| 64 | +``` |
| 65 | + |
| 66 | +Once deployed, feel free to use the provided **Postman** collection against the API. |
| 67 | + |
| 68 | +## Entities |
| 69 | +### Post |
| 70 | +``` |
| 71 | +{ |
| 72 | + "name": "Jack Nicholson", |
| 73 | + "nickname": "Nicky", |
| 74 | + "title": "My Joker was good too", |
| 75 | + "content": "Respect for Heath Ledger and Joaquin Phoenix", |
| 76 | + "comments": {} |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +### Comment |
| 81 | +``` |
| 82 | +{ |
| 83 | + "nickname": "Random Internet Guy", |
| 84 | + "comment": "Here is Johnny!", |
| 85 | + "creationDate": "2019-11-26 10:15:55" |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## Integration Tests |
| 90 | +Find inside directory `integrationtests` a straightforward postman collection, including test assertions, testing every endpoint and a main application flow: |
| 91 | + 1. Create a Post |
| 92 | + 2. Get all available Posts |
| 93 | + 3. Get a specific Post checking structure |
| 94 | + 4. Modify the Post's nickname |
| 95 | + 5. Get it again to check whether it was modified correctly |
| 96 | + 6. Add a Comment to the Post |
| 97 | + 7. Delete the Comment |
| 98 | + 8. Delete the Post |
0 commit comments