Articles → AWS → Createtable, Updatetable And Deletetable Operations On The Dynamodb Using The Lambda Function

Createtable, Updatetable And Deletetable Operations On The Dynamodb Using The Lambda Function






Permissions




{
	"Version": "2012-10-17",
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"dynamodb:CreateTable",
				"dynamodb:UpdateTable",
				"dynamodb:DeleteTable"
			],
			"Resource": "*"
		}
	]
}



Create Table




import AWS from 'aws-sdk';

const dynamoDB = new AWS.DynamoDB();

export const handler = async (event, context) => {
  const params = {
    TableName: 'MyTable',
    KeySchema: [
      { AttributeName: 'primaryKey', KeyType: 'HASH' }, // HASH key
    ],
    AttributeDefinitions: [
      { AttributeName: 'primaryKey', AttributeType: 'S' }, // S for string
    ],
    ProvisionedThroughput: {
      ReadCapacityUnits: 5,
      WriteCapacityUnits: 5,
    },
  };

  try {
    await dynamoDB.createTable(params).promise();
    return { statusCode: 200, body: 'Table created successfully' };
  } catch (error) {
    return { statusCode: 500, body: JSON.stringify(error) };
  }
};


Picture showing the dynamodb table is created



Update Table




import AWS from 'aws-sdk';

const dynamoDB = new AWS.DynamoDB();

export const handler = async (event, context) => {
  const params = {
    TableName: 'MyTable',
    ProvisionedThroughput: {
      ReadCapacityUnits: 5,
      WriteCapacityUnits: 10, // Updated write capacity units
    },
  };

  try {
    await dynamoDB.updateTable(params).promise();
    return { statusCode: 200, body: 'Table updated successfully' };
  } catch (error) {
    return { statusCode: 500, body: JSON.stringify(error) };
  }
};


Picture showing the provisioned capacity is changed



Delete Table




import AWS from 'aws-sdk';

const dynamoDB = new AWS.DynamoDB();

export const handler = async (event, context) => {
  const params = {
    TableName: 'MyTable',
  };

  try {
    await dynamoDB.deleteTable(params).promise();
    return { statusCode: 200, body: 'Table deleted successfully' };
  } catch (error) {
    return { statusCode: 500, body: JSON.stringify(error) };
  }
};





Posted By  -  Karan Gupta
 
Posted On  -  Monday, December 25, 2023

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250