Skip to content

Commit 26e6ee0

Browse files
author
Ubuntu
committed
Initial commit
1 parent 2862cd2 commit 26e6ee0

File tree

65 files changed

+2546
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+2546
-0
lines changed

README.md

100644100755
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ If you'd like to jump right into code, you can begin with a starter project in y
2222
- [Java starter](./java-test-samples/apigw-lambda-list-s3-buckets)
2323
- [TypeScript starter](./typescript-test-samples/typescript-test-intro)
2424
- [.NET starter](./dotnet-test-samples/apigw-lambda-list-s3-buckets)
25+
- [Local starter](./local-test-samples/apigateway-crud-lambda-dynamodb)
2526

2627
# Language Directories
2728
The repository is divided into several language directories. If you would like to browse by language, you can navigate to the main page of each language directory:
@@ -30,6 +31,8 @@ The repository is divided into several language directories. If you would like t
3031
- [Java main directory](./java-test-samples/)
3132
- [TypeScript main directory](./typescript-test-samples/)
3233
- [.NET main directory](./dotnet-test-samples/)
34+
- [Local starter](./local-test-samples/)
35+
3336

3437
# Workload Types
3538
This repository contains sample code for testing a variety of different types of workloads, including API's, Event-Driven Architectures, Service Orchestration, Data Processing, and AWS Partner Patterns.
@@ -45,6 +48,8 @@ This repository contains sample code for testing a variety of different types of
4548
| [API Gateway, Lambda Authorizer, Lambda, DynamoDB](https://github.com/aws-samples/serverless-samples/tree/main/serverless-rest-api/javascript-http-sam) [External] | Node.js |
4649
| [API Gateway, Lambda, S3](./dotnet-test-samples/apigw-lambda-list-s3-buckets)|.NET|
4750
| [API Gateway, Lambda, DynamoDB](./dotnet-test-samples/apigw-lambda-ddb)|.NET|
51+
| [Api Gateway, Lambda, DynamoDB](./local-test-samples/apigateway-crud-lambda-dynamodb)|Local|
52+
| [Api Gateway, MOCK](./local-test-samples/apigateway-mock)|Local|
4853
| [SQS, Lambda, DynamoDB](./dotnet-test-samples/sqs-lambda)|.NET|
4954
| [API Gateway, Lambda, DynamoDB](./java-test-samples/apigw-lambda-ddb)|Java|
5055
| [AppSync, DynamoDB](./java-test-samples/java-appsync-sam)|Java|
@@ -57,6 +62,8 @@ Event-driven architectures (EDA) are an architecture style that uses events and
5762
|[Schemas and Contracts](./typescript-test-samples/schema-and-contract-testing)|TypeScript|Event driven architectures decouple producers and consumers at the infrastructure layer, but these resources may still be coupled at the application layer by the event contract. Learn how to test for breaking changes in the contract.|
5863
|[S3, Lambda, DynamoDB](./dotnet-test-samples/async-lambda-dynamodb)|.NET|This example shows how to test async system by using DynamoDB during to store incoming asynchronous events during testing|
5964
|[S3, Lambda, SQS](./dotnet-test-samples/async-lambda-sqs)|.NET|An example to how to test asynchronous workflow by long polling the queue that resulting messages are sent to.|
65+
|[Lambda, SAM, HelloWorld](./local-test-samples/lambda-sam-helloworld)|Local| Local Lambda HelloWorld implementation |
66+
|[Lambda, SAM, Layers](./local-test-samples/lambda-sam-layers)|Local| Local Lambda emulator Layers implementation |
6067

6168
## Architectural patterns
6269
|Pattern|Services used|Language|Description|
@@ -67,13 +74,22 @@ Event-driven architectures (EDA) are an architecture style that uses events and
6774
|System Under Test|Language|Description|
6875
|---|---|---|
6976
| [Step Functions](./java-test-samples/step-functions-local) [External] |Java|This project shows a technique for testing an AWS Step Functions workflow in a local desktop environment.|
77+
| [Step Functions, HelloWorld](./local-test-samples/stepfunctions-helloworld) |Local| Local Hello World Step Functions implementation |
78+
| [Step Functions, Lambda](./local-test-samples/stepfunctions-lambda) |Local| Local Lambda execution integrated with local Hello World Step Functions implementation |
79+
| [Step Functions, MOCK](./local-test-samples/stepfunctions-mock) |Local| Mocking answers from local Hello World Step Functions implementation |
7080

7181
## Data Processing
7282
| System Under Test|Language|Description|
7383
|---|---|---|
7484
|[Kinesis Data Stream, Lambda](./typescript-test-samples/kinesis-lambda-dynamodb)|TypeScript|This project shows a technique for testing a streaming data processing system.|
7585
|[Kinesis Data Stream, Lambda, DynamoDB](./dotnet-test-samples/kinesis-lambda-dynamodb)|.NET|This pattern creates an AWS Lambda function that consumes messages from an Amazon Kinesis Data Streams and dumps them to Amazon DynamoDB using SAM and .NET 6.|
7686

87+
## Database
88+
| System Under Test|Language|Description|
89+
| [DynamoDB, aws cli](./local-test-samples/dynamodb-crud-cli)|Local| DynamoDB CRUD operations using local docker image and aws cli |
90+
| [DynamoDB, Lambda](./local-test-samples/dynamodb-crud-lambda)|Local| DynamoDB CRUD operations using docker image and local lambda local emulator |
91+
| [DynamoDB, StepFunctions](./local-test-samples/dynamodb-crud-stepfunctions)|Local| DynamoDB CRUD operations using docker image and step functions local emulator |
92+
7793
## AWS Partner Patterns
7894
| Partner |System Under Test|Language|Description|
7995
|---|---|---|---|
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
[![python: 3.9](https://img.shields.io/badge/Python-3.9-green)](https://img.shields.io/badge/Python-3.9-green)
2+
[![AWS: DynamoDB](https://img.shields.io/badge/AWS-DynamoDB-blueviolet)](https://img.shields.io/badge/AWS-DynamoDB-blueviolet)
3+
[![AWS: Lambda](https://img.shields.io/badge/AWS-Lambda-orange)](https://img.shields.io/badge/AWS-Lambda-orange)
4+
[![AWS: API Gateway](https://img.shields.io/badge/AWS-API%20Gateway-blue)](https://img.shields.io/badge/AWS-API%20Gateway-blue)
5+
[![test: local](https://img.shields.io/badge/Test-Local-red)](https://img.shields.io/badge/Test-Local-red)
6+
7+
# Local: Amazon Api Gateway, AWS Lambda, Amazon DynamoDB CRUD Operations
8+
9+
## Introduction
10+
This project demonstrates how to perform local testing of AWS serverless applications using SAM CLI and Docker. It implements a complete CRUD operation cycle through API Gateway, with Lambda functions processing the requests and DynamoDB storing the data.
11+
12+
---
13+
14+
## Contents
15+
- [Local: Amazon Api Gateway, AWS Lambda, Amazon DynamoDB CRUD Operations](#local-amazon-api-gateway-aws-lambda-amazon-dynamodb-crud-operations)
16+
- [Introduction](#introduction)
17+
- [Contents](#contents)
18+
- [Project Structure](#project-structure)
19+
- [Architecture Overview](#architecture-overview)
20+
- [Local Setup Requirements](#local-setup-requirements)
21+
- [Running the Tests](#running-the-tests)
22+
- [API Endpoints](#api-endpoints)
23+
- [Test Sequence](#test-sequence)
24+
- [Additional Resources](#additional-resources)
25+
26+
---
27+
28+
## Project Structure
29+
```
30+
├── apigateway-crud-lambda-dynamodb _# folder containing necessary code and template for CRUD operations with API Gateway, Lambda Functions and DynamoDB_
31+
│ ├── events _# folder containing json files for API Gateway CRUD input events_
32+
│ ├── img/apigateway-crud-lambda-dynamodb.png _# Architecture diagram_
33+
│ ├── lambda_crud_src _# folder containing code for different CRUD Lambda functions_
34+
│ ├── README.md _# instructions file_
35+
│ └── template.yaml _# sam yaml template file for necessary components test_
36+
37+
```
38+
39+
[Top](#contents)
40+
41+
---
42+
43+
## Architecture Overview
44+
45+
<p align="center">
46+
<img src="img/apigateway-crud-lambda-dynamodb.png" alt="API Gateway - CRUD + Lambda + DynamoDB" width="300"/>
47+
</p>
48+
49+
The application consists of:
50+
- API Gateway (emulated locally by SAM)
51+
- Lambda functions (Python 3.9) (emulated locally by SAM)
52+
- DynamoDB table (running locally via Docker)
53+
54+
[Top](#contents)
55+
56+
---
57+
58+
## Local Setup Requirements
59+
- Docker (SAM emulation)
60+
- AWS SAM CLI
61+
- Python 3.9
62+
- curl (for testing)
63+
64+
[Top](#contents)
65+
66+
---
67+
68+
## Running the Tests
69+
70+
1. Start DynamoDB locally:
71+
```sh
72+
docker run --rm -d --network host -p 8000:8000 amazon/dynamodb-local
73+
```
74+
75+
2. Start the API Gateway emulator:
76+
```sh
77+
sam local start-api --docker-network host &
78+
```
79+
80+
3. Initialize the DynamoDB table (though Api Gateway -> Lambda crud init function):
81+
```sh
82+
curl -X GET http://127.0.0.1:3000/init
83+
```
84+
85+
[Top](#contents)
86+
87+
---
88+
89+
## API Endpoints
90+
91+
| Endpoint | Method | Description |
92+
|----------|--------|-------------|
93+
| /init | GET | Creates DynamoDB CRUDLocalTable table |
94+
| /create | POST | Creates new item |
95+
| /read | GET | Retrieves an item |
96+
| /update | POST | Updates existing item |
97+
| /delete | GET | Deletes an item |
98+
99+
[Top](#contents)
100+
101+
---
102+
103+
## Test Sequence
104+
105+
1. Create initial item:
106+
```sh
107+
curl -X POST http://127.0.0.1:3000/create \
108+
-H 'Content-Type: application/json' \
109+
-d '{"Id": "123", "name": "Batman"}'
110+
```
111+
112+
2. Read item:
113+
```sh
114+
curl -X GET http://127.0.0.1:3000/read \
115+
-H 'Content-Type: application/json' \
116+
-d '{"Id": "123"}'
117+
```
118+
119+
3. Update initial item:
120+
```sh
121+
curl -X POST http://127.0.0.1:3000/update \
122+
-H 'Content-Type: application/json' \
123+
-d '{"Id": "123", "name": "Robin"}'
124+
```
125+
126+
4. Check updated item:
127+
```sh
128+
curl -X GET http://127.0.0.1:3000/read \
129+
-H 'Content-Type: application/json' \
130+
-d '{"Id": "123"}'
131+
```
132+
133+
5. Delete item:
134+
```sh
135+
curl -X GET http://127.0.0.1:3000/delete \
136+
-H 'Content-Type: application/json' \
137+
-d '{"Id": "123"}'
138+
```
139+
140+
6. Checking item does not exist:
141+
```sh
142+
curl -X GET http://127.0.0.1:3000/read \
143+
-H 'Content-Type: application/json' \
144+
-d '{"Id": "123"}'
145+
```
146+
147+
[Top](#contents)
148+
149+
---
150+
151+
## Additional Resources
152+
- [API Gateway Docs SAM] [AWS Serverless Application Model - Developer Guide - Locally run API Gateway with AWS SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-start-api.html)
153+
- [Lambda Docs SAM] [AWS Serverless Application Model - Developer Guide - Locally invoke Lambda functions with AWS SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-using-invoke.html)
154+
- [Amazon DynamoDB Developer Guide] [Amazon DynamoDB - Developer Guide - Deploying DynamoDB locally on your computer](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html)
155+
156+
157+
[Top](#contents)
158+
159+
---
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"body": "{\"Id\": \"123\", \"name\": \"Batman\"}"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Id": "123"
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"key1": "value1",
3+
"key2": "value2",
4+
"key3": "value3"
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Id": "123"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"body": "{\"Id\": \"123\", \"name\": \"Robin\"}"
3+
}
69.3 KB
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
import json
3+
import boto3
4+
def lambda_handler(event, context):
5+
# Checking if running locally
6+
if os.environ.get('AWS_SAM_LOCAL'):
7+
# Use local DynamoDB endpoint
8+
dynamodb = boto3.resource('dynamodb', endpoint_url='http://172.17.0.1:8000')
9+
else:
10+
# Use the default DynamoDB endpoint (AWS)
11+
dynamodb = boto3.resource('dynamodb')
12+
13+
# Adding an item to DynamoDB
14+
item = json.loads(event['body'])
15+
16+
table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
17+
response = table.put_item(Item=item)
18+
19+
return {
20+
'statusCode': 200,
21+
'body': json.dumps({'message': 'Item added', 'response': response})
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import json
3+
import boto3
4+
5+
def lambda_handler(event, context):
6+
# Checking if running locally
7+
if os.environ.get('AWS_SAM_LOCAL'):
8+
# Use local DynamoDB endpoint
9+
dynamodb = boto3.resource('dynamodb', endpoint_url='http://172.17.0.1:8000')
10+
else:
11+
# Use the default DynamoDB endpoint (AWS)
12+
dynamodb = boto3.resource('dynamodb')
13+
14+
# Parse the JSON body
15+
body = json.loads(event['body'])
16+
17+
# Access the Id element
18+
item_id = body['Id']
19+
20+
# Deleting item on DynamoDB
21+
table = dynamodb.Table(os.environ['DYNAMODB_TABLE'])
22+
response = table.delete_item(Key={'Id': item_id})
23+
24+
return {
25+
'statusCode': 200,
26+
'body': json.dumps({'message': 'Item deleted', 'response': response})
27+
}

0 commit comments

Comments
 (0)