Home Software Engineering Learn how to Create a DynamoDB Desk and Add Objects to it utilizing Python 3 from Lambda

Learn how to Create a DynamoDB Desk and Add Objects to it utilizing Python 3 from Lambda

0
Learn how to Create a DynamoDB Desk and Add Objects to it utilizing Python 3 from Lambda

[ad_1]

To create a DynamoDB desk and add gadgets to it utilizing Python 3 from AWS Lambda, you need to use the AWS SDK for Python, also referred to as Boto3. Right here’s a step-by-step information:

  1. Arrange your AWS setting:
    • Set up Boto3 by working pip set up boto3 in your native improvement setting.
    • Arrange your AWS credentials and configure your AWS CLI or setting variables. You could find detailed directions within the AWS documentation.
  2. Create a Lambda perform within the AWS Administration Console:
    • Go to the AWS Administration Console and navigate to the Lambda service.
    • Click on on “Create perform” and observe the directions to create a brand new Lambda perform.
    • Select the specified runtime as Python 3.x.
  3. Write the Python code to create the DynamoDB desk and add gadgets:
    • Within the Lambda perform code editor, enter the next code:
import boto3

def lambda_handler(occasion, context):
    # Create a DynamoDB consumer
    dynamodb = boto3.consumer('dynamodb')

    # Outline the desk identify and schema
    table_name = 'YourTableName'
    table_schema = [
        {
            'AttributeName': 'ID',
            'AttributeType': 'N'
        },
        {
            'AttributeName': 'Name',
            'AttributeType': 'S'
        }
    ]

    # Create the DynamoDB desk
    dynamodb.create_table(
        TableName=table_name,
        KeySchema=[
            {
                'AttributeName': 'ID',
                'KeyType': 'HASH'
            }
        ],
        AttributeDefinitions=table_schema,
        ProvisionedThroughput={
            'ReadCapacityUnits': 5,
            'WriteCapacityUnits': 5
        }
    )

    # Anticipate the desk to be created
    dynamodb.get_waiter('table_exists').wait(TableName=table_name)

    # Add gadgets to the desk
    gadgets = [
        {
            'ID': {'N': '1'},
            'Name': {'S': 'Item 1'}
        },
        {
            'ID': {'N': '2'},
            'Name': {'S': 'Item 2'}
        }
    ]

    with dynamodb.batch_writer(TableName=table_name) as batch:
        for merchandise in gadgets:
            batch.put_item(Merchandise=merchandise)

    return {
        'statusCode': 200,
        'physique': 'DynamoDB desk created and gadgets added efficiently.'
    }
  1. Configure the Lambda perform:
    • Within the AWS Lambda perform configuration, specify the next:
      • Handler: Enter the identify of the Python file and the lambda_handler perform. For instance, filename.lambda_handler.
      • Runtime: Python 3.x.
      • Timeout: Set an acceptable timeout based mostly on the anticipated execution time of your code.
      • Function: Select or create an execution position with acceptable DynamoDB permissions.
  2. Save and check the Lambda perform:
    • Save the Lambda perform by clicking on the “Save” button.
    • Take a look at the perform by clicking on the “Take a look at” button and configuring a check occasion.
    • Monitor the execution logs and test for any errors or exceptions.

If you invoke the Lambda perform, it’s going to create a DynamoDB desk with the desired schema and add the gadgets to it utilizing a batch write operation. Ensure that to exchange 'YourTableName' with the specified identify on your DynamoDB desk.

Be certain that the IAM position assigned to the Lambda perform has the mandatory permissions to create and write to DynamoDB tables.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here