|
| 1 | +## Developer Guide |
| 2 | + |
| 3 | +install `awscliv2`, `aws-sam-cli`, `nodejs 18` (and `make` only for macOS and Linux) on your machine and ensure that your aws iam account is configured. the configuration profile should be located at `~/.aws/config`, access credentials should be located at `~/.aws/credentials`. |
| 4 | + |
| 5 | +To build and deploy your application for the first time, run the following commands in your shell using `makefile` (only for macOS and Linux): |
| 6 | + |
| 7 | +```bash |
| 8 | +make i |
| 9 | +make build |
| 10 | +make deploy |
| 11 | +``` |
| 12 | + |
| 13 | +The second command will build the source of your application. The 3rd command will package and deploy your application to AWS, with a series of prompts: |
| 14 | + |
| 15 | +* **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. |
| 16 | +* **AWS Region**: The AWS region you want to deploy your app to. |
| 17 | +* **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. |
| 18 | +* **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. |
| 19 | +* **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. |
| 20 | + |
| 21 | +You can find your API Gateway Endpoint URL in the output values displayed after deployment. |
| 22 | + |
| 23 | +## Use the SAM CLI to build and test locally |
| 24 | + |
| 25 | +Build your application with the `make build` command. |
| 26 | + |
| 27 | +```bash |
| 28 | +ts-lambda-kit$ make build |
| 29 | +``` |
| 30 | + |
| 31 | +The SAM CLI installs dependencies defined in `package.json`, compiles TypeScript with esbuild, creates a deployment package, and saves it in the `.aws-sam/build` folder. |
| 32 | + |
| 33 | +Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. |
| 34 | + |
| 35 | +Run functions locally and invoke them with the `sam local invoke` command. |
| 36 | + |
| 37 | +```bash |
| 38 | +ts-lambda-kit$ sam local invoke {lambda-function-name} --event events/event.json |
| 39 | +``` |
| 40 | + |
| 41 | +The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. |
| 42 | + |
| 43 | +```bash |
| 44 | +ts-lambda-kit$ sam local start-api |
| 45 | +ts-lambda-kit$ curl http://localhost:3000/ |
| 46 | +``` |
| 47 | + |
| 48 | +The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. |
| 49 | + |
| 50 | +```yaml |
| 51 | + Events: |
| 52 | + ApiGatewayRouteGetUsers: |
| 53 | + Type: Api |
| 54 | + Properties: |
| 55 | + Path: /users |
| 56 | + Method: GET |
| 57 | + RestApiId: |
| 58 | + Ref: RestApiGateway |
| 59 | +``` |
| 60 | +
|
| 61 | +## Add a resource to your application |
| 62 | +The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. |
| 63 | +
|
| 64 | +## Fetch, tail, and filter Lambda function logs |
| 65 | +
|
| 66 | +To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. |
| 67 | + |
| 68 | +`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. |
| 69 | + |
| 70 | +```bash |
| 71 | +ts-lambda-kit$ sam logs -n {lambda-function-name} --stack-name typescript-aws-lambda-serverless-restapi-kit --tail |
| 72 | +``` |
| 73 | + |
| 74 | +You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). |
| 75 | + |
| 76 | +## Unit tests |
| 77 | + |
| 78 | +Tests are defined in the `__tests__` folder in this project. Use NPM to install the [Jest test framework](https://jestjs.io/) and run unit tests. |
| 79 | + |
| 80 | +```bash |
| 81 | +ts-lambda-kit$ npm install |
| 82 | +ts-lambda-kit$ npm run test |
| 83 | +``` |
| 84 | + |
| 85 | +## Cleanup |
| 86 | + |
| 87 | +To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: |
| 88 | + |
| 89 | +```bash |
| 90 | +make destroy |
| 91 | +``` |
| 92 | + |
| 93 | +## Resources |
| 94 | + |
| 95 | +See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. |
| 96 | + |
| 97 | +Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) |
| 98 | + |
| 99 | + |
0 commit comments