We will create a Lambda function that reads all the data in the DynamoDB table.
Open template.yaml file in fcj-book-shop folder.
Add the following script at the end of the file.
BooksList:
Type: AWS::Serverless::Function
Properties:
CodeUri: fcj-book-shop/books_list
Handler: books_list.lambda_handler
Runtime: python3.11
FunctionName: books_list
Environment:
Variables:
TABLE_NAME: !Ref BooksTable
Architectures:
- x86_64
Policies:
- Statement:
- Sid: ReadDynamoDB
Effect: Allow
Action:
- dynamodb:Scan
- dynamodb:Query
Resource:
- !Sub arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${booksTableName}
The directory structure is as follows.
fcj-book-shop
├── fcj-book-shop
│ ├── books_list
│ └── books_list.py
└── template.yaml
Create fcj-book-shop/books_list folder in fcj-book-shop folder.
Create books_list.py file and copy the below code block to it.
import boto3
import os
import simplejson as json
TABLE = os.environ['TABLE_NAME']
# Get the service resource
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(TABLE)
header_res = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "OPTIONS,POST,GET,DELETE",
"Access-Control-Allow-Headers": "Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token",
}
secondary_index = "name-index"
def lambda_handler(event, context):
try:
books_data = table.scan(
TableName=TABLE,
IndexName=secondary_index
)
books = books_data.get('Items', [])
for book in books:
data_comment = table.query(
TableName=TABLE,
KeyConditionExpression="id = :id AND rv_id > :rv_id",
ExpressionAttributeValues={
":id": book['id'],
":rv_id": 0
}
)
book['comments'] = data_comment['Items']
return {
"statusCode": 200,
"headers": header_res,
"body": json.dumps(books, use_decimal=True)
}
except Exception as e:
print(f'Error getting items: {e}')
raise Exception(f'Error getting items: {e}')
Run the following command to deploy SAM.
If you have issues with Python version, following this instruction for setting up an virtual Python environment: pyenv Github page.
sam build
sam validate
sam deploy
Open AWS Lambda console.