To develop a DynamoDB table and include products to it utilizing Python 3 from AWS Lambda, you can utilize the AWS SDK for Python, likewise referred to as Boto3. Here’s a detailed guide:
- Establish your AWS environment:
- Set up Boto3 by running
pip set up boto3
in your regional advancement environment. - Establish your AWS qualifications and configure your AWS CLI or environment variables. You can discover in-depth directions in the AWS documents.
- Set up Boto3 by running
- Produce a Lambda function in the AWS Management Console:
- Go to the AWS Management Console and browse to the Lambda service.
- Click “Produce function” and follow the directions to develop a brand-new Lambda function.
- Pick the preferred runtime as Python 3.x.
- Compose the Python code to develop the DynamoDB table and include products:
- In the Lambda function code editor, get in the following code:
import boto3
def lambda_handler( occasion, context):.
# Produce a DynamoDB customer.
dynamodb = boto3.client(' dynamodb').
# Specify the table name and schema.
table_name='YourTableName'.
table_schema =[
{
'AttributeName': 'ID',
'AttributeType': 'N'
},
{
'AttributeName': 'Name',
'AttributeType': 'S'
}
]
# Produce the DynamoDB table.
dynamodb.create _ table(.
TableName= table_name,.
KeySchema =[
{
'AttributeName': 'ID',
'KeyType': 'HASH'
}
],.
AttributeDefinitions= table_schema,.
ProvisionedThroughput= {
' ReadCapacityUnits': 5,.
' WriteCapacityUnits': 5.
}
).
# Wait on the table to be produced.
dynamodb.get _ waiter(' table_exists'). wait( TableName= table_name).
# Include products to the table.
products =[
{
'ID': {'N': '1'},
'Name': {'S': 'Item 1'}
},
{
'ID': {'N': '2'},
'Name': {'S': 'Item 2'}
}
]
with dynamodb.batch _ author( TableName= table_name) as batch:.
for product in products:.
batch.put _ product( Product= product).
return {
' statusCode': 200,.
' body': 'DynamoDB table produced and products included effectively.'.
}
- Set up the Lambda function:
- In the AWS Lambda function setup, define the following:
- Handler: Go into the name of the Python file and the lambda_handler function. For instance,
filename.lambda _ handler
- Runtime: Python 3.x.
- Timeout: Set a suitable timeout based upon the predicted execution time of your code.
- Function: Pick or develop an execution function with proper DynamoDB consents.
- Handler: Go into the name of the Python file and the lambda_handler function. For instance,
- In the AWS Lambda function setup, define the following:
- Conserve and evaluate the Lambda function:
- Conserve the Lambda function by clicking the “Conserve” button.
- Check the function by clicking the “Test” button and setting up a test occasion.
- Screen the execution logs and look for any mistakes or exceptions.
When you conjure up the Lambda function, it will develop a DynamoDB table with the defined schema and include the products to it utilizing a batch compose operation. Make certain to change ' YourTableName'
with the preferred name for your DynamoDB table.
Make Sure that the IAM function designated to the Lambda function has the required consents to develop and compose to DynamoDB tables.