Spaces:
Configuration error
Configuration error
Upload 15 files
Browse files- .gitignore +1 -1
- Ekran g/303/266r/303/274nt/303/274s/303/274 2024-09-18 174122.png +0 -0
- README.md +160 -11
- buildspec.yml +22 -0
- env.json +11 -0
- event-get-all-items.json +3 -0
- event-get-by-id.json +6 -0
- event-post-item.json +4 -0
- get-all-items.mjs +44 -0
- get-by-id.mjs +47 -0
- package.json +30 -0
- put-item.mjs +49 -0
- put-item2.mjs +0 -0
- samconfig.toml +30 -0
- template.yaml +132 -0
.gitignore
CHANGED
@@ -1 +1 @@
|
|
1 |
-
|
|
|
1 |
+
node_modules
|
Ekran g/303/266r/303/274nt/303/274s/303/274 2024-09-18 174122.png
ADDED
README.md
CHANGED
@@ -1,11 +1,160 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# sam-node
|
2 |
+
|
3 |
+
This project contains source code and supporting files for a serverless application that you can deploy with the AWS Serverless Application Model (AWS SAM) command line interface (CLI). It includes the following files and folders:
|
4 |
+
|
5 |
+
- `src` - Code for the application's Lambda function.
|
6 |
+
- `events` - Invocation events that you can use to invoke the function.
|
7 |
+
- `__tests__` - Unit tests for the application code.
|
8 |
+
- `template.yaml` - A template that defines the application's AWS resources.
|
9 |
+
|
10 |
+
The application uses several AWS resources, including Lambda functions, an API Gateway API, and Amazon DynamoDB tables. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code.
|
11 |
+
|
12 |
+
If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit.
|
13 |
+
The AWS Toolkit is an open-source plugin for popular IDEs that uses the AWS SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds step-through debugging for Lambda function code.
|
14 |
+
|
15 |
+
To get started, see the following:
|
16 |
+
|
17 |
+
* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
18 |
+
* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
19 |
+
* [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
20 |
+
* [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
21 |
+
* [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
22 |
+
* [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
23 |
+
* [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
24 |
+
* [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
25 |
+
* [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
26 |
+
* [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html)
|
27 |
+
* [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html)
|
28 |
+
|
29 |
+
## Deploy the sample application
|
30 |
+
|
31 |
+
The AWS SAM CLI is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
|
32 |
+
|
33 |
+
To use the AWS SAM CLI, you need the following tools:
|
34 |
+
|
35 |
+
* AWS SAM CLI - [Install the AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html).
|
36 |
+
* Node.js - [Install Node.js 18](https://nodejs.org/en/), including the npm package management tool.
|
37 |
+
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community).
|
38 |
+
|
39 |
+
To build and deploy your application for the first time, run the following in your shell:
|
40 |
+
|
41 |
+
```bash
|
42 |
+
sam build
|
43 |
+
sam deploy --guided
|
44 |
+
```
|
45 |
+
|
46 |
+
The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts:
|
47 |
+
|
48 |
+
* **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.
|
49 |
+
* **AWS Region**: The AWS region you want to deploy your app to.
|
50 |
+
* **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.
|
51 |
+
* **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.
|
52 |
+
* **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.
|
53 |
+
|
54 |
+
The API Gateway endpoint API will be displayed in the outputs when the deployment is complete.
|
55 |
+
|
56 |
+
## Use the AWS SAM CLI to build and test locally
|
57 |
+
|
58 |
+
Build your application by using the `sam build` command.
|
59 |
+
|
60 |
+
```bash
|
61 |
+
my-application$ sam build
|
62 |
+
```
|
63 |
+
|
64 |
+
The AWS SAM CLI installs dependencies that are defined in `package.json`, creates a deployment package, and saves it in the `.aws-sam/build` folder.
|
65 |
+
|
66 |
+
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.
|
67 |
+
|
68 |
+
Run functions locally and invoke them with the `sam local invoke` command.
|
69 |
+
|
70 |
+
```bash
|
71 |
+
my-application$ sam local invoke putItemFunction --event events/event-post-item.json
|
72 |
+
my-application$ sam local invoke getAllItemsFunction --event events/event-get-all-items.json
|
73 |
+
```
|
74 |
+
|
75 |
+
The AWS SAM CLI can also emulate your application's API. Use the `sam local start-api` command to run the API locally on port 3000.
|
76 |
+
|
77 |
+
```bash
|
78 |
+
my-application$ sam local start-api
|
79 |
+
my-application$ curl http://localhost:3000/
|
80 |
+
```
|
81 |
+
|
82 |
+
The AWS 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.
|
83 |
+
|
84 |
+
```yaml
|
85 |
+
Events:
|
86 |
+
Api:
|
87 |
+
Type: Api
|
88 |
+
Properties:
|
89 |
+
Path: /
|
90 |
+
Method: GET
|
91 |
+
```
|
92 |
+
|
93 |
+
## Add a resource to your application
|
94 |
+
The application template uses 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 that aren't included in the [AWS SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use the standard [AWS CloudFormation resource types](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html).
|
95 |
+
|
96 |
+
Update `template.yaml` to add a dead-letter queue to your application. In the **Resources** section, add a resource named **MyQueue** with the type **AWS::SQS::Queue**. Then add a property to the **AWS::Serverless::Function** resource named **DeadLetterQueue** that targets the queue's Amazon Resource Name (ARN), and a policy that grants the function permission to access the queue.
|
97 |
+
|
98 |
+
```
|
99 |
+
Resources:
|
100 |
+
MyQueue:
|
101 |
+
Type: AWS::SQS::Queue
|
102 |
+
getAllItemsFunction:
|
103 |
+
Type: AWS::Serverless::Function
|
104 |
+
Properties:
|
105 |
+
Handler: src/handlers/get-all-items.getAllItemsHandler
|
106 |
+
Runtime: nodejs18.x
|
107 |
+
DeadLetterQueue:
|
108 |
+
Type: SQS
|
109 |
+
TargetArn: !GetAtt MyQueue.Arn
|
110 |
+
Policies:
|
111 |
+
- SQSSendMessagePolicy:
|
112 |
+
QueueName: !GetAtt MyQueue.QueueName
|
113 |
+
```
|
114 |
+
|
115 |
+
The dead-letter queue is a location for Lambda to send events that could not be processed. It's only used if you invoke your function asynchronously, but it's useful here to show how you can modify your application's resources and function configuration.
|
116 |
+
|
117 |
+
Deploy the updated application.
|
118 |
+
|
119 |
+
```bash
|
120 |
+
my-application$ sam deploy
|
121 |
+
```
|
122 |
+
|
123 |
+
Open the [**Applications**](https://console.aws.amazon.com/lambda/home#/applications) page of the Lambda console, and choose your application. When the deployment completes, view the application resources on the **Overview** tab to see the new resource. Then, choose the function to see the updated configuration that specifies the dead-letter queue.
|
124 |
+
|
125 |
+
## Fetch, tail, and filter Lambda function logs
|
126 |
+
|
127 |
+
To simplify troubleshooting, the AWS SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs that are generated by your 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.
|
128 |
+
|
129 |
+
**NOTE:** This command works for all Lambda functions, not just the ones you deploy using AWS SAM.
|
130 |
+
|
131 |
+
```bash
|
132 |
+
my-application$ sam logs -n putItemFunction --stack-name sam-app --tail
|
133 |
+
```
|
134 |
+
|
135 |
+
**NOTE:** This uses the logical name of the function within the stack. This is the correct name to use when searching logs inside an AWS Lambda function within a CloudFormation stack, even if the deployed function name varies due to CloudFormation's unique resource name generation.
|
136 |
+
|
137 |
+
You can find more information and examples about filtering Lambda function logs in the [AWS SAM CLI documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html).
|
138 |
+
|
139 |
+
## Unit tests
|
140 |
+
|
141 |
+
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.
|
142 |
+
|
143 |
+
```bash
|
144 |
+
my-application$ npm install
|
145 |
+
my-application$ npm run test
|
146 |
+
```
|
147 |
+
|
148 |
+
## Cleanup
|
149 |
+
|
150 |
+
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:
|
151 |
+
|
152 |
+
```bash
|
153 |
+
sam delete --stack-name sam-node
|
154 |
+
```
|
155 |
+
|
156 |
+
## Resources
|
157 |
+
|
158 |
+
For an introduction to the AWS SAM specification, the AWS SAM CLI, and serverless application concepts, see the [AWS SAM Developer Guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html).
|
159 |
+
|
160 |
+
Next, you can use the AWS Serverless Application Repository to deploy ready-to-use apps that go beyond Hello World samples and learn how authors developed their applications. For more information, see the [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) and the [AWS Serverless Application Repository Developer Guide](https://docs.aws.amazon.com/serverlessrepo/latest/devguide/what-is-serverlessrepo.html).
|
buildspec.yml
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: 0.2
|
2 |
+
phases:
|
3 |
+
install:
|
4 |
+
commands:
|
5 |
+
# Install all dependencies (including dependencies for running tests)
|
6 |
+
- npm install
|
7 |
+
pre_build:
|
8 |
+
commands:
|
9 |
+
# Discover and run unit tests in the '__tests__' directory
|
10 |
+
- npm run test
|
11 |
+
# Remove all unit tests to reduce the size of the package that will be ultimately uploaded to Lambda
|
12 |
+
- rm -rf ./__tests__
|
13 |
+
# Remove all dependencies not needed for the Lambda deployment package (the packages from devDependencies in package.json)
|
14 |
+
- npm prune --production
|
15 |
+
build:
|
16 |
+
commands:
|
17 |
+
# Use AWS SAM to package the application by using AWS CloudFormation
|
18 |
+
- aws cloudformation package --template template.yaml --s3-bucket $S3_BUCKET --output-template template-export.yml
|
19 |
+
artifacts:
|
20 |
+
type: zip
|
21 |
+
files:
|
22 |
+
- template-export.yml
|
env.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"getAllItemsFunction": {
|
3 |
+
"SAMPLE_TABLE": "<TABLE-NAME>"
|
4 |
+
},
|
5 |
+
"getByIdFunction": {
|
6 |
+
"SAMPLE_TABLE": "<TABLE-NAME>"
|
7 |
+
},
|
8 |
+
"putItemFunction": {
|
9 |
+
"SAMPLE_TABLE": "<TABLE-NAME>"
|
10 |
+
}
|
11 |
+
}
|
event-get-all-items.json
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"httpMethod": "GET"
|
3 |
+
}
|
event-get-by-id.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"httpMethod": "GET",
|
3 |
+
"pathParameters": {
|
4 |
+
"id": "id1"
|
5 |
+
}
|
6 |
+
}
|
event-post-item.json
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"httpMethod": "POST",
|
3 |
+
"body": "{\"id\": \"id1\",\"name\": \"name1\"}"
|
4 |
+
}
|
get-all-items.mjs
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Create clients and set shared const values outside of the handler.
|
2 |
+
|
3 |
+
// Create a DocumentClient that represents the query to add an item
|
4 |
+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
5 |
+
import { DynamoDBDocumentClient, ScanCommand } from '@aws-sdk/lib-dynamodb';
|
6 |
+
const client = new DynamoDBClient({});
|
7 |
+
const ddbDocClient = DynamoDBDocumentClient.from(client);
|
8 |
+
|
9 |
+
// Get the DynamoDB table name from environment variables
|
10 |
+
const tableName = process.env.SAMPLE_TABLE;
|
11 |
+
|
12 |
+
/**
|
13 |
+
* A simple example includes a HTTP get method to get all items from a DynamoDB table.
|
14 |
+
*/
|
15 |
+
export const getAllItemsHandler = async (event) => {
|
16 |
+
if (event.httpMethod !== 'GET') {
|
17 |
+
throw new Error(`getAllItems only accept GET method, you tried: ${event.httpMethod}`);
|
18 |
+
}
|
19 |
+
// All log statements are written to CloudWatch
|
20 |
+
console.info('received:', event);
|
21 |
+
|
22 |
+
// get all items from the table (only first 1MB data, you can use `LastEvaluatedKey` to get the rest of data)
|
23 |
+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#scan-property
|
24 |
+
// https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html
|
25 |
+
var params = {
|
26 |
+
TableName : tableName
|
27 |
+
};
|
28 |
+
|
29 |
+
try {
|
30 |
+
const data = await ddbDocClient.send(new ScanCommand(params));
|
31 |
+
var items = data.Items;
|
32 |
+
} catch (err) {
|
33 |
+
console.log("Error", err);
|
34 |
+
}
|
35 |
+
|
36 |
+
const response = {
|
37 |
+
statusCode: 200,
|
38 |
+
body: JSON.stringify(items)
|
39 |
+
};
|
40 |
+
|
41 |
+
// All log statements are written to CloudWatch
|
42 |
+
console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);
|
43 |
+
return response;
|
44 |
+
}
|
get-by-id.mjs
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Create clients and set shared const values outside of the handler.
|
2 |
+
|
3 |
+
// Create a DocumentClient that represents the query to add an item
|
4 |
+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
5 |
+
import { DynamoDBDocumentClient, GetCommand } from '@aws-sdk/lib-dynamodb';
|
6 |
+
const client = new DynamoDBClient({});
|
7 |
+
const ddbDocClient = DynamoDBDocumentClient.from(client);
|
8 |
+
|
9 |
+
// Get the DynamoDB table name from environment variables
|
10 |
+
const tableName = process.env.SAMPLE_TABLE;
|
11 |
+
|
12 |
+
/**
|
13 |
+
* A simple example includes a HTTP get method to get one item by id from a DynamoDB table.
|
14 |
+
*/
|
15 |
+
export const getByIdHandler = async (event) => {
|
16 |
+
if (event.httpMethod !== 'GET') {
|
17 |
+
throw new Error(`getMethod only accept GET method, you tried: ${event.httpMethod}`);
|
18 |
+
}
|
19 |
+
// All log statements are written to CloudWatch
|
20 |
+
console.info('received:', event);
|
21 |
+
|
22 |
+
// Get id from pathParameters from APIGateway because of `/{id}` at template.yaml
|
23 |
+
const id = event.pathParameters.id;
|
24 |
+
|
25 |
+
// Get the item from the table
|
26 |
+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#get-property
|
27 |
+
var params = {
|
28 |
+
TableName : tableName,
|
29 |
+
Key: { id: id },
|
30 |
+
};
|
31 |
+
|
32 |
+
try {
|
33 |
+
const data = await ddbDocClient.send(new GetCommand(params));
|
34 |
+
var item = data.Item;
|
35 |
+
} catch (err) {
|
36 |
+
console.log("Error", err);
|
37 |
+
}
|
38 |
+
|
39 |
+
const response = {
|
40 |
+
statusCode: 200,
|
41 |
+
body: JSON.stringify(item)
|
42 |
+
};
|
43 |
+
|
44 |
+
// All log statements are written to CloudWatch
|
45 |
+
console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);
|
46 |
+
return response;
|
47 |
+
}
|
package.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"name": "delete-test-01",
|
3 |
+
"description": "delete-test-01-description",
|
4 |
+
"version": "0.0.1",
|
5 |
+
"private": true,
|
6 |
+
"dependencies": {
|
7 |
+
"@aws-sdk/client-dynamodb": "^3.188.0",
|
8 |
+
"@aws-sdk/lib-dynamodb": "^3.188.0"
|
9 |
+
},
|
10 |
+
"devDependencies": {
|
11 |
+
"aws-sdk-client-mock": "^2.0.0",
|
12 |
+
"jest": "^29.2.1"
|
13 |
+
},
|
14 |
+
"scripts": {
|
15 |
+
"test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js"
|
16 |
+
},
|
17 |
+
"jest": {
|
18 |
+
"testMatch": [
|
19 |
+
"**/__tests__/**/*.[jt]s?(x)",
|
20 |
+
"**/?(*.)+(spec|test).[jt]s?(x)",
|
21 |
+
"**/__tests__/**/*.mjs?(x)",
|
22 |
+
"**/?(*.)+(spec|test).mjs?(x)"
|
23 |
+
],
|
24 |
+
"moduleFileExtensions": [
|
25 |
+
"mjs",
|
26 |
+
"js"
|
27 |
+
]
|
28 |
+
}
|
29 |
+
}
|
30 |
+
|
put-item.mjs
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Create clients and set shared const values outside of the handler.
|
2 |
+
|
3 |
+
// Create a DocumentClient that represents the query to add an item
|
4 |
+
import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
|
5 |
+
import { DynamoDBDocumentClient, PutCommand } from '@aws-sdk/lib-dynamodb';
|
6 |
+
const client = new DynamoDBClient({});
|
7 |
+
const ddbDocClient = DynamoDBDocumentClient.from(client);
|
8 |
+
|
9 |
+
// Get the DynamoDB table name from environment variables
|
10 |
+
const tableName = process.env.SAMPLE_TABLE;
|
11 |
+
|
12 |
+
/**
|
13 |
+
* A simple example includes a HTTP post method to add one item to a DynamoDB table.
|
14 |
+
*/
|
15 |
+
export const putItemHandler = async (event) => {
|
16 |
+
if (event.httpMethod !== 'POST') {
|
17 |
+
throw new Error(`postMethod only accepts POST method, you tried: ${event.httpMethod} method.`);
|
18 |
+
}
|
19 |
+
// All log statements are written to CloudWatch
|
20 |
+
console.info('received:', event);
|
21 |
+
|
22 |
+
// Get id and name from the body of the request
|
23 |
+
const body = JSON.parse(event.body);
|
24 |
+
const id = body.id;
|
25 |
+
const name = body.name;
|
26 |
+
|
27 |
+
// Creates a new item, or replaces an old item with a new item
|
28 |
+
// https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html#put-property
|
29 |
+
var params = {
|
30 |
+
TableName : tableName,
|
31 |
+
Item: { id : id, name: name }
|
32 |
+
};
|
33 |
+
|
34 |
+
try {
|
35 |
+
const data = await ddbDocClient.send(new PutCommand(params));
|
36 |
+
console.log("Success - item added or updated", data);
|
37 |
+
} catch (err) {
|
38 |
+
console.log("Error", err.stack);
|
39 |
+
}
|
40 |
+
|
41 |
+
const response = {
|
42 |
+
statusCode: 200,
|
43 |
+
body: JSON.stringify(body)
|
44 |
+
};
|
45 |
+
|
46 |
+
// All log statements are written to CloudWatch
|
47 |
+
console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);
|
48 |
+
return response;
|
49 |
+
};
|
put-item2.mjs
ADDED
File without changes
|
samconfig.toml
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# More information about the configuration file can be found here:
|
2 |
+
# https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
|
3 |
+
version = 0.1
|
4 |
+
|
5 |
+
[default.global.parameters]
|
6 |
+
stack_name = "sam-node"
|
7 |
+
|
8 |
+
[default.build.parameters]
|
9 |
+
cached = true
|
10 |
+
parallel = true
|
11 |
+
|
12 |
+
[default.validate.parameters]
|
13 |
+
lint = true
|
14 |
+
|
15 |
+
[default.deploy.parameters]
|
16 |
+
capabilities = "CAPABILITY_IAM"
|
17 |
+
confirm_changeset = true
|
18 |
+
resolve_s3 = true
|
19 |
+
|
20 |
+
[default.package.parameters]
|
21 |
+
resolve_s3 = true
|
22 |
+
|
23 |
+
[default.sync.parameters]
|
24 |
+
watch = true
|
25 |
+
|
26 |
+
[default.local_start_api.parameters]
|
27 |
+
warm_containers = "EAGER"
|
28 |
+
|
29 |
+
[default.local_start_lambda.parameters]
|
30 |
+
warm_containers = "EAGER"
|
template.yaml
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
AWSTemplateFormatVersion: 2010-09-09
|
2 |
+
Description: >-
|
3 |
+
sam-node
|
4 |
+
Transform:
|
5 |
+
- AWS::Serverless-2016-10-31
|
6 |
+
|
7 |
+
# Resources declares the AWS resources that you want to include in the stack
|
8 |
+
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/resources-section-structure.html
|
9 |
+
Resources:
|
10 |
+
# Each Lambda function is defined by properties:
|
11 |
+
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
|
12 |
+
|
13 |
+
# This is a Lambda function config associated with the source code: get-all-items.js
|
14 |
+
getAllItemsFunction:
|
15 |
+
Type: AWS::Serverless::Function
|
16 |
+
Properties:
|
17 |
+
Handler: src/handlers/get-all-items.getAllItemsHandler
|
18 |
+
Runtime: nodejs18.x
|
19 |
+
Architectures:
|
20 |
+
- x86_64
|
21 |
+
MemorySize: 128
|
22 |
+
Timeout: 100
|
23 |
+
Description: A simple example includes a HTTP get method to get all items from
|
24 |
+
a DynamoDB table.
|
25 |
+
Policies:
|
26 |
+
# Give Create/Read/Update/Delete Permissions to the SampleTable
|
27 |
+
- DynamoDBCrudPolicy:
|
28 |
+
TableName: !Ref SampleTable
|
29 |
+
Environment:
|
30 |
+
Variables:
|
31 |
+
# Make table name accessible as environment variable from function code during execution
|
32 |
+
SAMPLE_TABLE: !Ref SampleTable
|
33 |
+
Events:
|
34 |
+
Api:
|
35 |
+
Type: Api
|
36 |
+
Properties:
|
37 |
+
Path: /
|
38 |
+
Method: GET
|
39 |
+
# Each Lambda function is defined by properties:
|
40 |
+
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
|
41 |
+
|
42 |
+
# This is a Lambda function config associated with the source code: get-by-id.js
|
43 |
+
getByIdFunction:
|
44 |
+
Type: AWS::Serverless::Function
|
45 |
+
Properties:
|
46 |
+
Handler: src/handlers/get-by-id.getByIdHandler
|
47 |
+
Runtime: nodejs18.x
|
48 |
+
Architectures:
|
49 |
+
- x86_64
|
50 |
+
MemorySize: 128
|
51 |
+
Timeout: 100
|
52 |
+
Description: A simple example includes a HTTP get method to get one item by
|
53 |
+
id from a DynamoDB table.
|
54 |
+
Policies:
|
55 |
+
# Give Create/Read/Update/Delete Permissions to the SampleTable
|
56 |
+
- DynamoDBCrudPolicy:
|
57 |
+
TableName: !Ref SampleTable
|
58 |
+
Environment:
|
59 |
+
Variables:
|
60 |
+
# Make table name accessible as environment variable from function code during execution
|
61 |
+
SAMPLE_TABLE: !Ref SampleTable
|
62 |
+
Events:
|
63 |
+
Api:
|
64 |
+
Type: Api
|
65 |
+
Properties:
|
66 |
+
Path: /{id}
|
67 |
+
Method: GET
|
68 |
+
# Each Lambda function is defined by properties:
|
69 |
+
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
|
70 |
+
|
71 |
+
# This is a Lambda function config associated with the source code: put-item.js
|
72 |
+
putItemFunction:
|
73 |
+
Type: AWS::Serverless::Function
|
74 |
+
Properties:
|
75 |
+
Handler: src/handlers/put-item.putItemHandler
|
76 |
+
Runtime: nodejs18.x
|
77 |
+
Architectures:
|
78 |
+
- x86_64
|
79 |
+
MemorySize: 128
|
80 |
+
Timeout: 100
|
81 |
+
Description: A simple example includes a HTTP post method to add one item to
|
82 |
+
a DynamoDB table.
|
83 |
+
Policies:
|
84 |
+
# Give Create/Read/Update/Delete Permissions to the SampleTable
|
85 |
+
- DynamoDBCrudPolicy:
|
86 |
+
TableName: !Ref SampleTable
|
87 |
+
Environment:
|
88 |
+
Variables:
|
89 |
+
# Make table name accessible as environment variable from function code during execution
|
90 |
+
SAMPLE_TABLE: !Ref SampleTable
|
91 |
+
Events:
|
92 |
+
Api:
|
93 |
+
Type: Api
|
94 |
+
Properties:
|
95 |
+
Path: /
|
96 |
+
Method: POST
|
97 |
+
# Simple syntax to create a DynamoDB table with a single attribute primary key, more in
|
98 |
+
# https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlesssimpletable
|
99 |
+
|
100 |
+
# DynamoDB table to store item: {id: <ID>, name: <NAME>}
|
101 |
+
SampleTable:
|
102 |
+
Type: AWS::Serverless::SimpleTable
|
103 |
+
Properties:
|
104 |
+
PrimaryKey:
|
105 |
+
Name: id
|
106 |
+
Type: String
|
107 |
+
ProvisionedThroughput:
|
108 |
+
ReadCapacityUnits: 2
|
109 |
+
WriteCapacityUnits: 2
|
110 |
+
|
111 |
+
ApplicationResourceGroup:
|
112 |
+
Type: AWS::ResourceGroups::Group
|
113 |
+
Properties:
|
114 |
+
Name:
|
115 |
+
Fn::Sub: ApplicationInsights-SAM-${AWS::StackName}
|
116 |
+
ResourceQuery:
|
117 |
+
Type: CLOUDFORMATION_STACK_1_0
|
118 |
+
ApplicationInsightsMonitoring:
|
119 |
+
Type: AWS::ApplicationInsights::Application
|
120 |
+
Properties:
|
121 |
+
ResourceGroupName:
|
122 |
+
Ref: ApplicationResourceGroup
|
123 |
+
AutoConfigurationEnabled: 'true'
|
124 |
+
Outputs:
|
125 |
+
WebEndpoint:
|
126 |
+
Description: API Gateway endpoint URL for Prod stage
|
127 |
+
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/"
|
128 |
+
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
|
129 |
+
Globals:
|
130 |
+
Function:
|
131 |
+
LoggingConfig:
|
132 |
+
LogFormat: JSON
|