Spaces:
Configuration error
Configuration error
Upload 15 files
Browse files- .gitignore +244 -1
- Dockerfile +17 -27
- README.md +43 -73
- app.py +28 -39
- app2.py +109 -0
- compose.yml +9 -0
- event.json +6 -58
- get.py +60 -0
- init.py +23 -0
- model.py +2 -59
- post.py +78 -0
- requirements.txt +4 -1
- samconfig.toml +1 -1
- template.yaml +12 -122
- test_app.py +18 -0
.gitignore
CHANGED
@@ -1 +1,244 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
|
3 |
+
|
4 |
+
### Linux ###
|
5 |
+
*~
|
6 |
+
|
7 |
+
# temporary files which can be created if a process still has a handle open of a deleted file
|
8 |
+
.fuse_hidden*
|
9 |
+
|
10 |
+
# KDE directory preferences
|
11 |
+
.directory
|
12 |
+
|
13 |
+
# Linux trash folder which might appear on any partition or disk
|
14 |
+
.Trash-*
|
15 |
+
|
16 |
+
# .nfs files are created when an open file is removed but is still being accessed
|
17 |
+
.nfs*
|
18 |
+
|
19 |
+
### OSX ###
|
20 |
+
*.DS_Store
|
21 |
+
.AppleDouble
|
22 |
+
.LSOverride
|
23 |
+
|
24 |
+
# Icon must end with two \r
|
25 |
+
Icon
|
26 |
+
|
27 |
+
# Thumbnails
|
28 |
+
._*
|
29 |
+
|
30 |
+
# Files that might appear in the root of a volume
|
31 |
+
.DocumentRevisions-V100
|
32 |
+
.fseventsd
|
33 |
+
.Spotlight-V100
|
34 |
+
.TemporaryItems
|
35 |
+
.Trashes
|
36 |
+
.VolumeIcon.icns
|
37 |
+
.com.apple.timemachine.donotpresent
|
38 |
+
|
39 |
+
# Directories potentially created on remote AFP share
|
40 |
+
.AppleDB
|
41 |
+
.AppleDesktop
|
42 |
+
Network Trash Folder
|
43 |
+
Temporary Items
|
44 |
+
.apdisk
|
45 |
+
|
46 |
+
### PyCharm ###
|
47 |
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
48 |
+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
49 |
+
|
50 |
+
# User-specific stuff:
|
51 |
+
.idea/**/workspace.xml
|
52 |
+
.idea/**/tasks.xml
|
53 |
+
.idea/dictionaries
|
54 |
+
|
55 |
+
# Sensitive or high-churn files:
|
56 |
+
.idea/**/dataSources/
|
57 |
+
.idea/**/dataSources.ids
|
58 |
+
.idea/**/dataSources.xml
|
59 |
+
.idea/**/dataSources.local.xml
|
60 |
+
.idea/**/sqlDataSources.xml
|
61 |
+
.idea/**/dynamic.xml
|
62 |
+
.idea/**/uiDesigner.xml
|
63 |
+
|
64 |
+
# Gradle:
|
65 |
+
.idea/**/gradle.xml
|
66 |
+
.idea/**/libraries
|
67 |
+
|
68 |
+
# CMake
|
69 |
+
cmake-build-debug/
|
70 |
+
|
71 |
+
# Mongo Explorer plugin:
|
72 |
+
.idea/**/mongoSettings.xml
|
73 |
+
|
74 |
+
## File-based project format:
|
75 |
+
*.iws
|
76 |
+
|
77 |
+
## Plugin-specific files:
|
78 |
+
|
79 |
+
# IntelliJ
|
80 |
+
/out/
|
81 |
+
|
82 |
+
# mpeltonen/sbt-idea plugin
|
83 |
+
.idea_modules/
|
84 |
+
|
85 |
+
# JIRA plugin
|
86 |
+
atlassian-ide-plugin.xml
|
87 |
+
|
88 |
+
# Cursive Clojure plugin
|
89 |
+
.idea/replstate.xml
|
90 |
+
|
91 |
+
# Ruby plugin and RubyMine
|
92 |
+
/.rakeTasks
|
93 |
+
|
94 |
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
95 |
+
com_crashlytics_export_strings.xml
|
96 |
+
crashlytics.properties
|
97 |
+
crashlytics-build.properties
|
98 |
+
fabric.properties
|
99 |
+
|
100 |
+
### PyCharm Patch ###
|
101 |
+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
|
102 |
+
|
103 |
+
# *.iml
|
104 |
+
# modules.xml
|
105 |
+
# .idea/misc.xml
|
106 |
+
# *.ipr
|
107 |
+
|
108 |
+
# Sonarlint plugin
|
109 |
+
.idea/sonarlint
|
110 |
+
|
111 |
+
### Python ###
|
112 |
+
# Byte-compiled / optimized / DLL files
|
113 |
+
__pycache__/
|
114 |
+
*.py[cod]
|
115 |
+
*$py.class
|
116 |
+
|
117 |
+
# C extensions
|
118 |
+
*.so
|
119 |
+
|
120 |
+
# Distribution / packaging
|
121 |
+
.Python
|
122 |
+
build/
|
123 |
+
develop-eggs/
|
124 |
+
dist/
|
125 |
+
downloads/
|
126 |
+
eggs/
|
127 |
+
.eggs/
|
128 |
+
lib/
|
129 |
+
lib64/
|
130 |
+
parts/
|
131 |
+
sdist/
|
132 |
+
var/
|
133 |
+
wheels/
|
134 |
+
*.egg-info/
|
135 |
+
.installed.cfg
|
136 |
+
*.egg
|
137 |
+
|
138 |
+
# PyInstaller
|
139 |
+
# Usually these files are written by a python script from a template
|
140 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
141 |
+
*.manifest
|
142 |
+
*.spec
|
143 |
+
|
144 |
+
# Installer logs
|
145 |
+
pip-log.txt
|
146 |
+
pip-delete-this-directory.txt
|
147 |
+
|
148 |
+
# Unit test / coverage reports
|
149 |
+
htmlcov/
|
150 |
+
.tox/
|
151 |
+
.coverage
|
152 |
+
.coverage.*
|
153 |
+
.cache
|
154 |
+
.pytest_cache/
|
155 |
+
nosetests.xml
|
156 |
+
coverage.xml
|
157 |
+
*.cover
|
158 |
+
.hypothesis/
|
159 |
+
|
160 |
+
# Translations
|
161 |
+
*.mo
|
162 |
+
*.pot
|
163 |
+
|
164 |
+
# Flask stuff:
|
165 |
+
instance/
|
166 |
+
.webassets-cache
|
167 |
+
|
168 |
+
# Scrapy stuff:
|
169 |
+
.scrapy
|
170 |
+
|
171 |
+
# Sphinx documentation
|
172 |
+
docs/_build/
|
173 |
+
|
174 |
+
# PyBuilder
|
175 |
+
target/
|
176 |
+
|
177 |
+
# Jupyter Notebook
|
178 |
+
.ipynb_checkpoints
|
179 |
+
|
180 |
+
# pyenv
|
181 |
+
.python-version
|
182 |
+
|
183 |
+
# celery beat schedule file
|
184 |
+
celerybeat-schedule.*
|
185 |
+
|
186 |
+
# SageMath parsed files
|
187 |
+
*.sage.py
|
188 |
+
|
189 |
+
# Environments
|
190 |
+
.env
|
191 |
+
.venv
|
192 |
+
env/
|
193 |
+
venv/
|
194 |
+
ENV/
|
195 |
+
env.bak/
|
196 |
+
venv.bak/
|
197 |
+
|
198 |
+
# Spyder project settings
|
199 |
+
.spyderproject
|
200 |
+
.spyproject
|
201 |
+
|
202 |
+
# Rope project settings
|
203 |
+
.ropeproject
|
204 |
+
|
205 |
+
# mkdocs documentation
|
206 |
+
/site
|
207 |
+
|
208 |
+
# mypy
|
209 |
+
.mypy_cache/
|
210 |
+
|
211 |
+
### VisualStudioCode ###
|
212 |
+
.vscode/*
|
213 |
+
!.vscode/settings.json
|
214 |
+
!.vscode/tasks.json
|
215 |
+
!.vscode/launch.json
|
216 |
+
!.vscode/extensions.json
|
217 |
+
.history
|
218 |
+
|
219 |
+
### Windows ###
|
220 |
+
# Windows thumbnail cache files
|
221 |
+
Thumbs.db
|
222 |
+
ehthumbs.db
|
223 |
+
ehthumbs_vista.db
|
224 |
+
|
225 |
+
# Folder config file
|
226 |
+
Desktop.ini
|
227 |
+
|
228 |
+
# Recycle Bin used on file shares
|
229 |
+
$RECYCLE.BIN/
|
230 |
+
|
231 |
+
# Windows Installer files
|
232 |
+
*.cab
|
233 |
+
*.msi
|
234 |
+
*.msm
|
235 |
+
*.msp
|
236 |
+
|
237 |
+
# Windows shortcuts
|
238 |
+
*.lnk
|
239 |
+
|
240 |
+
# Build folder
|
241 |
+
|
242 |
+
*/build/*
|
243 |
+
|
244 |
+
# End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode
|
Dockerfile
CHANGED
@@ -1,27 +1,17 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
CMD ["
|
18 |
-
|
19 |
-
# Sistem güncellemeleri ve temel bağımlılıkların kurulumu
|
20 |
-
RUN apt-get update && apt-get install -y \
|
21 |
-
build-essential \
|
22 |
-
&& rm -rf /var/lib/apt/lists/*
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
1 |
+
|
2 |
+
FROM python:3.10-bullseye
|
3 |
+
|
4 |
+
ADD app.py .
|
5 |
+
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
|
9 |
+
|
10 |
+
COPY requirements.txt .
|
11 |
+
|
12 |
+
RUN pip install -r requirements.txt
|
13 |
+
|
14 |
+
|
15 |
+
COPY app.py .
|
16 |
+
|
17 |
+
CMD ["python", "./app.py"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
README.md
CHANGED
@@ -1,18 +1,16 @@
|
|
1 |
-
# sam-
|
2 |
|
3 |
-
This project contains source code and supporting files for a serverless application that you can deploy with the
|
4 |
|
5 |
-
-
|
6 |
-
-
|
7 |
-
-
|
8 |
-
-
|
9 |
|
10 |
-
The application uses several AWS resources, including Lambda functions
|
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
|
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)
|
@@ -28,18 +26,18 @@ To get started, see the following:
|
|
28 |
|
29 |
## Deploy the sample application
|
30 |
|
31 |
-
The
|
32 |
|
33 |
-
To use the
|
34 |
|
35 |
-
*
|
36 |
-
*
|
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 |
|
@@ -51,98 +49,70 @@ The first command will build the source of your application. The second command
|
|
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 |
-
|
55 |
|
56 |
-
## Use the
|
57 |
|
58 |
-
Build your application
|
59 |
|
60 |
```bash
|
61 |
-
|
62 |
```
|
63 |
|
64 |
-
The
|
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 |
-
|
72 |
-
my-application$ sam local invoke getAllItemsFunction --event events/event-get-all-items.json
|
73 |
```
|
74 |
|
75 |
-
The
|
76 |
|
77 |
```bash
|
78 |
-
|
79 |
-
|
80 |
```
|
81 |
|
82 |
-
The
|
83 |
|
84 |
```yaml
|
85 |
Events:
|
86 |
-
|
87 |
Type: Api
|
88 |
Properties:
|
89 |
-
Path: /
|
90 |
-
Method:
|
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
|
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,
|
128 |
|
129 |
-
|
130 |
|
131 |
```bash
|
132 |
-
|
133 |
```
|
134 |
|
135 |
-
|
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 |
-
##
|
140 |
|
141 |
-
Tests are defined in the `
|
142 |
|
143 |
```bash
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
|
|
146 |
```
|
147 |
|
148 |
## Cleanup
|
@@ -150,11 +120,11 @@ my-application$ npm run test
|
|
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-
|
154 |
```
|
155 |
|
156 |
## Resources
|
157 |
|
158 |
-
|
159 |
|
160 |
-
Next, you can use
|
|
|
1 |
+
# sam-tutorial-demo
|
2 |
|
3 |
+
This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders.
|
4 |
|
5 |
+
- hello_world - 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 and an API Gateway API. 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 plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started.
|
|
|
|
|
14 |
|
15 |
* [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
16 |
* [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html)
|
|
|
26 |
|
27 |
## Deploy the sample application
|
28 |
|
29 |
+
The Serverless Application Model Command Line Interface (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.
|
30 |
|
31 |
+
To use the SAM CLI, you need the following tools.
|
32 |
|
33 |
+
* SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)
|
34 |
+
* [Python 3 installed](https://www.python.org/downloads/)
|
35 |
+
* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community)
|
36 |
|
37 |
To build and deploy your application for the first time, run the following in your shell:
|
38 |
|
39 |
```bash
|
40 |
+
sam build --use-container
|
41 |
sam deploy --guided
|
42 |
```
|
43 |
|
|
|
49 |
* **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.
|
50 |
* **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.
|
51 |
|
52 |
+
You can find your API Gateway Endpoint URL in the output values displayed after deployment.
|
53 |
|
54 |
+
## Use the SAM CLI to build and test locally
|
55 |
|
56 |
+
Build your application with the `sam build --use-container` command.
|
57 |
|
58 |
```bash
|
59 |
+
sam-tutorial-demo$ sam build --use-container
|
60 |
```
|
61 |
|
62 |
+
The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder.
|
63 |
|
64 |
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.
|
65 |
|
66 |
Run functions locally and invoke them with the `sam local invoke` command.
|
67 |
|
68 |
```bash
|
69 |
+
sam-tutorial-demo$ sam local invoke HelloWorldFunction --event events/event.json
|
|
|
70 |
```
|
71 |
|
72 |
+
The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000.
|
73 |
|
74 |
```bash
|
75 |
+
sam-tutorial-demo$ sam local start-api
|
76 |
+
sam-tutorial-demo$ curl http://localhost:3000/
|
77 |
```
|
78 |
|
79 |
+
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.
|
80 |
|
81 |
```yaml
|
82 |
Events:
|
83 |
+
HelloWorld:
|
84 |
Type: Api
|
85 |
Properties:
|
86 |
+
Path: /hello
|
87 |
+
Method: get
|
88 |
```
|
89 |
|
90 |
## Add a resource to your application
|
91 |
+
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
93 |
## Fetch, tail, and filter Lambda function logs
|
94 |
|
95 |
+
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.
|
96 |
|
97 |
+
`NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
|
98 |
|
99 |
```bash
|
100 |
+
sam-tutorial-demo$ sam logs -n HelloWorldFunction --stack-name "sam-tutorial-demo" --tail
|
101 |
```
|
102 |
|
103 |
+
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).
|
|
|
|
|
104 |
|
105 |
+
## Tests
|
106 |
|
107 |
+
Tests are defined in the `tests` folder in this project. Use PIP to install the test dependencies and run tests.
|
108 |
|
109 |
```bash
|
110 |
+
sam-tutorial-demo$ pip install -r tests/requirements.txt --user
|
111 |
+
# unit test
|
112 |
+
sam-tutorial-demo$ python -m pytest tests/unit -v
|
113 |
+
# integration test, requiring deploying the stack first.
|
114 |
+
# Create the env variable AWS_SAM_STACK_NAME with the name of the stack we are testing
|
115 |
+
sam-tutorial-demo$ AWS_SAM_STACK_NAME="sam-tutorial-demo" python -m pytest tests/integration -v
|
116 |
```
|
117 |
|
118 |
## Cleanup
|
|
|
120 |
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:
|
121 |
|
122 |
```bash
|
123 |
+
sam delete --stack-name "sam-tutorial-demo"
|
124 |
```
|
125 |
|
126 |
## Resources
|
127 |
|
128 |
+
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.
|
129 |
|
130 |
+
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/)
|
app.py
CHANGED
@@ -1,42 +1,31 @@
|
|
|
|
|
|
1 |
import json
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
"""Sample pure Lambda function
|
8 |
-
|
9 |
-
Parameters
|
10 |
-
----------
|
11 |
-
event: dict, required
|
12 |
-
API Gateway Lambda Proxy Input Format
|
13 |
-
|
14 |
-
Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format
|
15 |
-
|
16 |
-
context: object, required
|
17 |
-
Lambda Context runtime methods and attributes
|
18 |
-
|
19 |
-
Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
|
20 |
-
|
21 |
-
Returns
|
22 |
-
------
|
23 |
-
API Gateway Lambda Proxy Output Format: dict
|
24 |
-
|
25 |
-
Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
|
26 |
-
"""
|
27 |
-
|
28 |
-
# try:
|
29 |
-
# ip = requests.get("http://checkip.amazonaws.com/")
|
30 |
-
# except requests.RequestException as e:
|
31 |
-
# # Send some context about this error to Lambda Logs
|
32 |
-
# print(e)
|
33 |
-
|
34 |
-
# raise e
|
35 |
-
|
36 |
-
return {
|
37 |
-
"statusCode": 200,
|
38 |
-
"body": json.dumps({
|
39 |
-
"message": "hello world",
|
40 |
-
# "location": ip.text.replace("\n", "")
|
41 |
-
}),
|
42 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from post import send_data_to_dynamodb
|
2 |
+
from get import get_data_from_dynamodb
|
3 |
import json
|
4 |
+
import uuid
|
5 |
|
6 |
+
def get_user_input():
|
7 |
+
user_data = {
|
8 |
+
'title': input("Başlığı giriniz: "),
|
9 |
+
'keywords': input("Anahtar kelimeleri giriniz (virgül ile ayırarak): ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
}
|
11 |
+
return user_data
|
12 |
+
|
13 |
+
def main():
|
14 |
+
user_data = get_user_input()
|
15 |
+
|
16 |
+
|
17 |
+
item_id = str(uuid.uuid4())
|
18 |
+
|
19 |
+
|
20 |
+
user_data['id'] = item_id
|
21 |
+
|
22 |
+
|
23 |
+
response = send_data_to_dynamodb(user_data)
|
24 |
+
|
25 |
+
if response.status_code == 200:
|
26 |
+
print("Data successfully sent to DynamoDB!")
|
27 |
+
else:
|
28 |
+
print(f"Error: {response.status_code}, {response.text}")
|
29 |
+
|
30 |
+
if __name__ == '__main__':
|
31 |
+
main()
|
app2.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import json
|
3 |
+
import boto3
|
4 |
+
from botocore.exceptions import ClientError
|
5 |
+
from pydantic import BaseModel, ValidationError
|
6 |
+
|
7 |
+
class InputData(BaseModel):
|
8 |
+
title: str
|
9 |
+
text: str
|
10 |
+
keywords: list
|
11 |
+
|
12 |
+
def get_secret():
|
13 |
+
secret_name = "secret-manager"
|
14 |
+
region_name = "us-east-1"
|
15 |
+
|
16 |
+
# Create a Secrets Manager client
|
17 |
+
session = boto3.session.Session()
|
18 |
+
client = session.client(
|
19 |
+
service_name='secretsmanager',
|
20 |
+
region_name=region_name
|
21 |
+
)
|
22 |
+
|
23 |
+
try:
|
24 |
+
get_secret_value_response = client.get_secret_value(
|
25 |
+
SecretId=secret_name
|
26 |
+
)
|
27 |
+
except ClientError as e:
|
28 |
+
# Handle the exception
|
29 |
+
raise e
|
30 |
+
|
31 |
+
secret = get_secret_value_response['SecretString']
|
32 |
+
return json.loads(secret) # Return the secret as a dictionary
|
33 |
+
|
34 |
+
def lambda_handler(event, context):
|
35 |
+
# Retrieve secrets
|
36 |
+
secrets = get_secret()
|
37 |
+
aws_access_key = secrets['AWS_ACCESS_KEY_ID']
|
38 |
+
aws_secret_access_key = secrets['AWS_SECRET_ACCESS_KEY']
|
39 |
+
|
40 |
+
# DynamoDB tablosunun adını çevresel değişkenden al
|
41 |
+
table_name = os.environ['DYNAMODB_TABLE_NAME']
|
42 |
+
|
43 |
+
# Initialize DynamoDB resource
|
44 |
+
dynamodb = boto3.resource(
|
45 |
+
'dynamodb',
|
46 |
+
aws_access_key_id=aws_access_key,
|
47 |
+
aws_secret_access_key=aws_secret_access_key,
|
48 |
+
region_name='us-east-1'
|
49 |
+
)
|
50 |
+
|
51 |
+
# DynamoDB tablosuna erişim
|
52 |
+
table = dynamodb.Table(table_name)
|
53 |
+
|
54 |
+
lambda_name = 'LAMBDA_FUNCTION_NAME'
|
55 |
+
lambda_client = boto3.client('lambda')
|
56 |
+
|
57 |
+
try:
|
58 |
+
# Body'den JSON verilerini al.
|
59 |
+
body = json.loads(event.get('body', '{}'))
|
60 |
+
|
61 |
+
# Verileri Pydantic ile doğrula.
|
62 |
+
data = InputData(**body)
|
63 |
+
|
64 |
+
# DynamoDB'ye kaydetme işlemi.
|
65 |
+
table.put_item(
|
66 |
+
Item={
|
67 |
+
'title': data.title,
|
68 |
+
'text': data.text,
|
69 |
+
'keywords': ','.join(data.keywords) # Listeyi string olarak kaydet.
|
70 |
+
}
|
71 |
+
)
|
72 |
+
|
73 |
+
# Lambda fonksiyonunu çağırma işlemi.
|
74 |
+
lambda_payload = {
|
75 |
+
'text': data.text,
|
76 |
+
'title': data.title,
|
77 |
+
'keywords': data.keywords
|
78 |
+
}
|
79 |
+
response_invoke = lambda_client.invoke(
|
80 |
+
FunctionName=lambda_name,
|
81 |
+
InvocationType='RequestResponse',
|
82 |
+
Payload=json.dumps(lambda_payload)
|
83 |
+
)
|
84 |
+
|
85 |
+
return {
|
86 |
+
'statusCode': 200,
|
87 |
+
'body': json.dumps({
|
88 |
+
'message': 'Data successfully saved!',
|
89 |
+
'data': data.dict() # Doğrulanmış verileri döndür.
|
90 |
+
})
|
91 |
+
}
|
92 |
+
|
93 |
+
except ValidationError as e:
|
94 |
+
return {
|
95 |
+
'statusCode': 400,
|
96 |
+
'body': json.dumps({
|
97 |
+
'message': 'Invalid input data',
|
98 |
+
'errors': e.errors()
|
99 |
+
})
|
100 |
+
}
|
101 |
+
|
102 |
+
except Exception as e:
|
103 |
+
return {
|
104 |
+
'statusCode': 500,
|
105 |
+
'body': json.dumps({
|
106 |
+
'message': 'Internal server error',
|
107 |
+
'error': str(e)
|
108 |
+
})
|
109 |
+
}
|
compose.yml
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: "3"
|
2 |
+
|
3 |
+
services:
|
4 |
+
|
5 |
+
web:
|
6 |
+
build: .
|
7 |
+
ports:
|
8 |
+
-8000:8000
|
9 |
+
|
event.json
CHANGED
@@ -1,62 +1,10 @@
|
|
1 |
{
|
2 |
-
"body": "{\"
|
3 |
-
"
|
4 |
-
"path": "/hello",
|
5 |
-
"httpMethod": "GET",
|
6 |
-
"isBase64Encoded": false,
|
7 |
-
"queryStringParameters": {
|
8 |
-
"foo": "bar"
|
9 |
-
},
|
10 |
-
"pathParameters": {
|
11 |
-
"proxy": "/path/to/resource"
|
12 |
-
},
|
13 |
-
"stageVariables": {
|
14 |
-
"baz": "qux"
|
15 |
-
},
|
16 |
"headers": {
|
17 |
-
"
|
18 |
-
"Accept-Encoding": "gzip, deflate, sdch",
|
19 |
-
"Accept-Language": "en-US,en;q=0.8",
|
20 |
-
"Cache-Control": "max-age=0",
|
21 |
-
"CloudFront-Forwarded-Proto": "https",
|
22 |
-
"CloudFront-Is-Desktop-Viewer": "true",
|
23 |
-
"CloudFront-Is-Mobile-Viewer": "false",
|
24 |
-
"CloudFront-Is-SmartTV-Viewer": "false",
|
25 |
-
"CloudFront-Is-Tablet-Viewer": "false",
|
26 |
-
"CloudFront-Viewer-Country": "US",
|
27 |
-
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
|
28 |
-
"Upgrade-Insecure-Requests": "1",
|
29 |
-
"User-Agent": "Custom User Agent String",
|
30 |
-
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
|
31 |
-
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
|
32 |
-
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
|
33 |
-
"X-Forwarded-Port": "443",
|
34 |
-
"X-Forwarded-Proto": "https"
|
35 |
},
|
36 |
-
"
|
37 |
-
|
38 |
-
"resourceId": "123456",
|
39 |
-
"stage": "prod",
|
40 |
-
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
|
41 |
-
"requestTime": "09/Apr/2015:12:34:56 +0000",
|
42 |
-
"requestTimeEpoch": 1428582896000,
|
43 |
-
"identity": {
|
44 |
-
"cognitoIdentityPoolId": null,
|
45 |
-
"accountId": null,
|
46 |
-
"cognitoIdentityId": null,
|
47 |
-
"caller": null,
|
48 |
-
"accessKey": null,
|
49 |
-
"sourceIp": "127.0.0.1",
|
50 |
-
"cognitoAuthenticationType": null,
|
51 |
-
"cognitoAuthenticationProvider": null,
|
52 |
-
"userArn": null,
|
53 |
-
"userAgent": "Custom User Agent String",
|
54 |
-
"user": null
|
55 |
-
},
|
56 |
-
"path": "/prod/hello",
|
57 |
-
"resourcePath": "/hello",
|
58 |
-
"httpMethod": "POST",
|
59 |
-
"apiId": "1234567890",
|
60 |
-
"protocol": "HTTP/1.1"
|
61 |
-
}
|
62 |
}
|
|
|
|
1 |
{
|
2 |
+
"body": "{ \"title\": \"Sample Title\", \"keywords\": [\"keyword1\", \"keyword2\", \"keyword3\", \"keyword4\", \"keyword5\"], \"text\": \"This is a sample text for testing.\" }",
|
3 |
+
"httpMethod": "POST",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
"headers": {
|
5 |
+
"Content-Type": "application/json"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
},
|
7 |
+
"path": "/save",
|
8 |
+
"isBase64Encoded": false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
}
|
10 |
+
|
get.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
import json
|
3 |
+
from init import dynamodb_service
|
4 |
+
|
5 |
+
def get_data_from_dynamodb(item_id):
|
6 |
+
lambda_url = f'https://n0u3bakgmf.execute-api.us-east-1.amazonaws.com/dev/get-item/{item_id}'
|
7 |
+
|
8 |
+
response = requests.get(lambda_url)
|
9 |
+
|
10 |
+
if response.status_code == 200:
|
11 |
+
print("Data retrieved from DynamoDB successfully!")
|
12 |
+
return response.json()
|
13 |
+
else:
|
14 |
+
print(f"Error: {response.status_code}, {response.text}")
|
15 |
+
return None
|
16 |
+
|
17 |
+
def lambda_handler(event, context):
|
18 |
+
try:
|
19 |
+
|
20 |
+
item_id = event['pathParameters']['id']
|
21 |
+
|
22 |
+
|
23 |
+
response = dynamodb_service.get_item(
|
24 |
+
Key={
|
25 |
+
'id': item_id
|
26 |
+
}
|
27 |
+
)
|
28 |
+
|
29 |
+
# Eğer item varsa
|
30 |
+
item = response.get('Item', {})
|
31 |
+
|
32 |
+
if item:
|
33 |
+
|
34 |
+
return {
|
35 |
+
'statusCode': 200,
|
36 |
+
'body': json.dumps({
|
37 |
+
'id': item.get('id'),
|
38 |
+
'title': item.get('title'),
|
39 |
+
'keywords': item.get('keywords'),
|
40 |
+
'createdAt': item.get('createdAt')
|
41 |
+
})
|
42 |
+
}
|
43 |
+
else:
|
44 |
+
|
45 |
+
return {
|
46 |
+
'statusCode': 404,
|
47 |
+
'body': json.dumps({
|
48 |
+
'message': 'Item not found'
|
49 |
+
})
|
50 |
+
}
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
|
54 |
+
return {
|
55 |
+
'statusCode': 500,
|
56 |
+
'body': json.dumps({
|
57 |
+
'message': 'An error occurred',
|
58 |
+
'error': str(e)
|
59 |
+
})
|
60 |
+
}
|
init.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import boto3
|
3 |
+
|
4 |
+
class DynamoDBService:
|
5 |
+
def __init__(self, table_name):
|
6 |
+
|
7 |
+
self.dynamodb = boto3.resource('dynamodb')
|
8 |
+
self.table = self.dynamodb.Table(table_name)
|
9 |
+
return table_name
|
10 |
+
|
11 |
+
def put_item(self, item):
|
12 |
+
|
13 |
+
response = self.table.put_item(Item=item)
|
14 |
+
return response
|
15 |
+
|
16 |
+
def get_item(self, key):
|
17 |
+
|
18 |
+
response = self.table.get_item(Key=key)
|
19 |
+
return response
|
20 |
+
|
21 |
+
# Singleton olarak bir tabloya bağlanmak için aşağıdaki gibi tanımlayabilirsiniz
|
22 |
+
dynamodb_service = DynamoDBService('new_table')
|
23 |
+
|
model.py
CHANGED
@@ -1,59 +1,2 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from config import HUGGINGFACE_API_URL, HUGGINGFACE_API_KEY
|
4 |
-
from sentence_transformers import SentenceTransformer, util
|
5 |
-
from app.database import get_reference_data,connect_to_mongodb,insert_data_into_input_db
|
6 |
-
|
7 |
-
#input mongodb üzerindne çekilen veriler(kullanıcıdan alınan bilgilerin lamba üzerinden alınması gerekiyor, lamba fonksiyonu için dockerfile in gelişitirilmnesi atılması gerekiyor )
|
8 |
-
#sentenceTransformer modeli
|
9 |
-
#endpointin de eklenmesi gerekir.
|
10 |
-
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
11 |
-
|
12 |
-
#reference mongodb den çekilen veriler
|
13 |
-
def process_user_input(user_input):
|
14 |
-
"""
|
15 |
-
Kullanıcıdan gelen input verileriyle referans verilerini karşılaştırır ve en benzer metinleri döndürür.
|
16 |
-
"""
|
17 |
-
# MongoDB'den referans verileri çek
|
18 |
-
reference_data = get_reference_data()
|
19 |
-
|
20 |
-
# Kullanıcıdan gelen veriler için dynamo
|
21 |
-
insert_data_into_input_db(user_input)
|
22 |
-
|
23 |
-
# Model ile benzerlik hesaplama işlemi yap
|
24 |
-
similarity_scores = []
|
25 |
-
for reference_item in reference_data:
|
26 |
-
score = calculate_similarity(user_input["text"], reference_item["text"])
|
27 |
-
if score > 0.5: # Benzerlik eşiği
|
28 |
-
similarity_scores.append({
|
29 |
-
"reference_text": reference_item["text"],
|
30 |
-
"score": score
|
31 |
-
})
|
32 |
-
|
33 |
-
#alınan verilerin karşılaştırılmasının yapılması (title,keywords,text)
|
34 |
-
"""burada öncelikle refenence database ile input database in çekilmesi gerekiyor """
|
35 |
-
|
36 |
-
def calculate_similarity(text1, text2):
|
37 |
-
"""
|
38 |
-
İki metin arasındaki benzerlik oranını hesaplar.
|
39 |
-
"""
|
40 |
-
embedding1 = model.encode(text1, convert_to_tensor=True)
|
41 |
-
embedding2 = model.encode(text2, convert_to_tensor=True)
|
42 |
-
similarity_score = util.pytorch_cos_sim(embedding1, embedding2)
|
43 |
-
return similarity_score.item()
|
44 |
-
|
45 |
-
#benzerlik oranı belirlee (örneğin 0.5 in üzerinde yakaldığı benzerlikteki textler ve keywordler için yakalama)
|
46 |
-
|
47 |
-
|
48 |
-
"""def get_huggingface_prediction(input_data): #input_data'yı main.py ile entegre etmeliyim
|
49 |
-
headers = {
|
50 |
-
"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"
|
51 |
-
}
|
52 |
-
payload = {
|
53 |
-
"inputs": input_data
|
54 |
-
}
|
55 |
-
|
56 |
-
response = requests.post(HUGGINGFACE_API_URL, headers=headers, json=payload)
|
57 |
-
return response.json()"""
|
58 |
-
|
59 |
-
|
|
|
1 |
+
#--------------------------------------------------------------------- get items in dynamodb title and keywords
|
2 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
post.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import requests
|
3 |
+
import boto3
|
4 |
+
import uuid
|
5 |
+
from datetime import datetime
|
6 |
+
from init import dynamodb_service
|
7 |
+
|
8 |
+
def send_data_to_dynamodb(user_data):
|
9 |
+
try:
|
10 |
+
lambda_url = 'https://n0u3bakgmf.execute-api.us-east-1.amazonaws.com/dev/post-item'
|
11 |
+
|
12 |
+
headers = {
|
13 |
+
'Content-Type': 'application/json'
|
14 |
+
}
|
15 |
+
|
16 |
+
|
17 |
+
response = requests.post(lambda_url, data=json.dumps(user_data), headers=headers)
|
18 |
+
|
19 |
+
if response.status_code == 200:
|
20 |
+
print("Data sent to DynamoDB successfully!")
|
21 |
+
return response.json()
|
22 |
+
else:
|
23 |
+
print(f"Error: {response.status_code}, {response.text}")
|
24 |
+
return None
|
25 |
+
|
26 |
+
except Exception as e:
|
27 |
+
print(f"An error occurred while sending data: {str(e)}")
|
28 |
+
return None
|
29 |
+
|
30 |
+
def lambda_handler(event, context):
|
31 |
+
try:
|
32 |
+
|
33 |
+
body = json.loads(event['body'])
|
34 |
+
|
35 |
+
title = body.get('title')
|
36 |
+
keywords = body.get('keywords')
|
37 |
+
|
38 |
+
|
39 |
+
if not title or not keywords:
|
40 |
+
return {
|
41 |
+
'statusCode': 400,
|
42 |
+
'body': json.dumps({
|
43 |
+
'message': 'Missing title or keywords'
|
44 |
+
})
|
45 |
+
}
|
46 |
+
|
47 |
+
|
48 |
+
item_id = str(uuid.uuid4())
|
49 |
+
timestamp = datetime.now().isoformat()
|
50 |
+
|
51 |
+
|
52 |
+
response = dynamodb_service.put_item(
|
53 |
+
Item={
|
54 |
+
'id': item_id,
|
55 |
+
'title': title,
|
56 |
+
'keywords': keywords,
|
57 |
+
'createdAt': timestamp
|
58 |
+
}
|
59 |
+
)
|
60 |
+
|
61 |
+
|
62 |
+
return {
|
63 |
+
'statusCode': 200,
|
64 |
+
'body': json.dumps({
|
65 |
+
'message': 'Data successfully inserted',
|
66 |
+
'itemId': item_id
|
67 |
+
})
|
68 |
+
}
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
|
72 |
+
return {
|
73 |
+
'statusCode': 500,
|
74 |
+
'body': json.dumps({
|
75 |
+
'message': 'An error occurred',
|
76 |
+
'error': str(e)
|
77 |
+
})
|
78 |
+
}
|
requirements.txt
CHANGED
@@ -1 +1,4 @@
|
|
1 |
-
requests
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
pandas
|
3 |
+
boto3
|
4 |
+
pydantic
|
samconfig.toml
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
version = 0.1
|
4 |
|
5 |
[default.global.parameters]
|
6 |
-
stack_name = "sam-
|
7 |
|
8 |
[default.build.parameters]
|
9 |
cached = true
|
|
|
3 |
version = 0.1
|
4 |
|
5 |
[default.global.parameters]
|
6 |
+
stack_name = "sam-tutorial-demo"
|
7 |
|
8 |
[default.build.parameters]
|
9 |
cached = true
|
template.yaml
CHANGED
@@ -1,132 +1,22 @@
|
|
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 |
-
|
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:
|
18 |
-
Runtime:
|
19 |
-
|
20 |
-
- x86_64
|
21 |
MemorySize: 128
|
22 |
-
Timeout:
|
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 |
-
|
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 |
-
|
56 |
-
|
57 |
-
|
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 |
-
|
64 |
Type: Api
|
65 |
Properties:
|
66 |
-
Path: /
|
67 |
-
Method:
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
Resources:
|
2 |
+
MyLambdaFunction:
|
|
|
|
|
|
|
|
|
3 |
Type: AWS::Serverless::Function
|
4 |
Properties:
|
5 |
+
Handler: app.lambda_handler
|
6 |
+
Runtime: python3.10
|
7 |
+
CodeUri: sam_tutorial-demo/
|
|
|
8 |
MemorySize: 128
|
9 |
+
Timeout: 10
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
Environment:
|
11 |
Variables:
|
12 |
+
DYNAMODB_TABLE_NAME: input_to_users
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
Policies:
|
14 |
+
- DynamoDBCrudPolicy:
|
15 |
+
TableName: input_to_users
|
16 |
+
- AWSSecretsManagerReadWrite # Secrets Manager'dan erişim sağlamak için politika
|
|
|
|
|
|
|
|
|
17 |
Events:
|
18 |
+
ApiEvent:
|
19 |
Type: Api
|
20 |
Properties:
|
21 |
+
Path: /save
|
22 |
+
Method: post
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_app.py
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
|
3 |
+
from app import add_data_to_dynamodb, get_data_from_dynamodb
|
4 |
+
|
5 |
+
|
6 |
+
test_data = {
|
7 |
+
'id': '123',
|
8 |
+
'title': 'Example Title',
|
9 |
+
'keywords': 'example, test, boto3',
|
10 |
+
'createdAt': '2024-09-25T12:34:56'
|
11 |
+
}
|
12 |
+
|
13 |
+
|
14 |
+
add_data_to_dynamodb(test_data)
|
15 |
+
|
16 |
+
|
17 |
+
retrieved_data = get_data_from_dynamodb('123')
|
18 |
+
print(retrieved_data)
|