Thursday 15 September 2022

AWS Dynamo DB table using AWS CLI

1)Create Ec2 instance and get its key-pair for ssh to it .

 ssh ec2user@17.38.292.1 -i ec2key.pem  (Make sure key has right permission if not change it by               command . chmod 400  ec2key.pem

2)Once login can configure aws cli by running below command. Below command shall work in Amazon type Ec2.

    aws configure   (This works in amazon instance)

    Prompt for below ::

    Access key :

    Secret access key:

    Default region name: us-east-1

    Output format: json

3)create dynamco db tables

   a) After running AWS Configure, create a DynamoDB table using the following command:

aws dynamodb create-table --table-name ProductCatalog --attribute-definitions \

AttributeName=Id,AttributeType=N --key-schema \

AttributeName=Id,KeyType=HASH \

--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5

    b) This is the command to populate the table:

**** (make sure items.json is located in your working directory) ****

aws dynamodb batch-write-item --request-items file://items.json

where items.json contains: {

"ProductCatalog": [{

"PutRequest": {

"Item": {

"Id": {

"N": "201"

},

"ProductCategory": {

"S": "Bicycle"

},

"Description": {

"S": "Womens Road Bike"

},

"BicycleType": {

"S": "Road"

},

"Brand": {

"S": "Raleigh"

},

"Price": {

"N": "399"

},

"Color": {

"S": "Red"

}

}

}

},

{

"PutRequest": {

"Item": {

"Id": {

"N": "403"

},

"ProductCategory": {

"S": "Helmet"

},

"Description": {

"S": "Womens Cycling Helmet"

},

"Size": {

"S": "Small"

},

"Price": {

"S": "99"

},

"Color": {

"S": "Black"

}

}

}

},

{

"PutRequest": {

"Item": {

"Id": {

"N": "411"

},

"ProductCategory": {

"S": "Book"

},

"Description": {

"S": "The Read Aloud Cloud"

},

"Author": {

"S": "Forrest Brazeal"

},

"Price": {

"N": "19.99"

},

"Format": {

"S": "Hardback"

}

}

}

},

{

"PutRequest": {

"Item": {

"Id": {

"N": "563"

},

"ProductCategory": {

"S": "Helmet"

},

"Description": {

"S": "Mens Cycling Helmet"

},

"Size": {

"S": "Small"

},

"Price": {

"N": "75"

},

"Color": {

"S": "Blue"

}

}

}

},

{

"PutRequest": {

"Item": {

"Id": {

"N": "543"

},

"ProductCategory": {

"S": "Helmet"

},

"Description": {

"S": "Womens Cycling Helmet"

},

"Size": {

"S": "Medium"

},

"Price": {

"N": "199"

},

"Color": {

"S": "Red"

}

}

}

},

{

"PutRequest": {

"Item": {

"Id": {

"N": "493"

},

"ProductCategory": {

"S": "Helmet"

},

"Description": {

"S": "Childs Cycling Helmet"

},

"Size": {

"S": "Small"

},

"Price": {

"N": "99"

},

"Color": {

"S": "Black"

}

}

}

},

{

"PutRequest": {

"Item": {

"Id": {

"N": "347"

},

"ProductCategory": {

"S": "Helmet"

},

"Description": {

"S": "Womens Cycling Helmet"

},

"Size": {

"S": "Small"

},

"Price": {

"N": "79"

},

"Color": {

"S": "Blue"

}

}

}

},

{

"PutRequest": {

"Item": {

"Id": {

"N": "467"

},

"ProductCategory": {

"S": "Bicycle"

},

"Description": {

"S": "Mens Road Bike"

},

"BicycleType": {

"S": "Road"

},

"Brand": {

"S": "Raleigh"

},

"Price": {

"N": "250"

},

"Color": {

"S": "Blue"

}

}

}

},

{

"PutRequest": {

"Item": {

"Id": {

"N": "566"

},

"ProductCategory": {

"S": "Bicycle"

},

"Description": {

"S": "Mens Mountain Bike"

},

"BicycleType": {

"S": "Mountain"

},

"Brand": {

"S": "Raleigh"

},

"Price": {

"N": "599"

},

"Color": {

"S": "Black"

}

}

}

}

]

}


Response:

{

"UnprocessedItems":{}

}


c) This is the command to query Dynamodb from EC2 command line - make sure the region is correct. You should be working in us-east-1.


  aws dynamodb get-item --table-name ProductCatalog --region us-east-1  --key '{"Id":   {"N":"403"}}'


No comments:

Post a Comment