unable to create DynamoDB table from aws CLI - amazon-web-services

I am very new to aws. I was trying to create a database table by running following command:
``
aws dynamodb create-table --table-name pizza-order --attribute-definitions AttributeName=orderId, AttributeType=S --key-schema AttributeName=orderId,keyType=HASH --provisioned-throughput ReadCapacityUnit=1,WriteCapacityUnit=1 --region us-east-2 --query TableDescription.TableArn --output text
``
But I am getting a error like this:
Error parsing parameter '--attribute-definitions': Expected: '', received: '' for input:
AttributeName=orderId,

The command works with following modifications:
Remove space after orderId,
KeyType correctly capitalized
ReadCapacityUnits and WriteCapacityUnits correctly pluralized.
Here is the working command for your reference:
aws dynamodb create-table --table-name pizza-order --attribute-definitions AttributeName=orderId,AttributeType=S --key-schema AttributeName=orderId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --region us-east-2 --query TableDescription.TableArn --output text

aws dynamodb create-table --table-name pizza-order --attribute-definitions AttributeName=orderId,AttributeType=S --key-schema AttributeName=orderId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --region us-east-2 --query TableDescription.TableArn --output text
Docs for reference
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/getting-started-step-1.html
To verify that DynamoDB has finished creating the Music table, use the describe-table command.
aws dynamodb describe-table --table-name pizza-order | grep TableStatus
This command returns the following result. When DynamoDB finishes creating the table, the value of the TableStatus field is set to ACTIVE.
"TableStatus": "ACTIVE",
note:- whenever you are stuck or want to know the details of the command its a good practice to check aws cli doc for particular API for create table

Related

Creating DynamoDB Table in CLI

I created a DynamoDB table and was running the following command in the CLI
aws dynamodb put-item \
--table-name NBATeams \
--item '{"Team": {"S": "Team Name"},"Title": {"S": "Suns"}}' \
--region us-east-1
but I keep getting "An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Missing the key Team Name in the item" Not sure What am I missing since my partition key is City.
aws dynamodb put-item \
--table-name NBATeams \
--item '{"Team": {"S": "Team Name"},"Title": {"S": "Suns"}}' \
--region us-east-1
i believe problem is with your partition key, please double checked the name of your "PARTTION_KEY " make sure all attribute names, values, and keys are case sensitive.
When doing a put-item you must provide the exact keys for the table, which includes the partition key and sort key if one is defined.
If you still face issue, share the output of the describe-table command so we can understand your schema.
I believe your key is Team Name
aws dynamodb put-item \
--table-name NBATeams \
--item '{"Team Name": {"S": "Team Name"},"Title": {"S": "Suns"}}' \
--region us-east-1

How to avoid printing None by aws CLI when no data found?

I'm searching for values in DynamoDB like this:
aws dynamodb get-item --table-name table --key '{"name": {"S":"test"}}' --output text --query Item.value
If the item was not found, it prints None. How to avoid that and print an empty string instead?
You may want to have a default empty string value if nothing was found for the Item.value you are looking for:
aws dynamodb get-item --table-name table --key '{"name": {"S":"test"}}' --output text --query 'Item.value || ``'
You could re-direct to /dev/null?
aws dynamodb get-item --table-name table --key '{"name": {"S":"test"}}' --output text --query Item.value > /dev/null 2>&1
Be careful, because by doing so, you redirect both stdout and stderr to /dev/null. So if there is some error, you won't see it.

AWS Glue API not recognizing partitions with hyphen

I have data in S3 that is partitioned by category and date as follows:
s3://mybucket/category=1/date=2018-08-30/data1.json
s3://mybucket/category=1/date=2018-08-31/data2.json
s3://mybucket/category=2/date=2018-08-30/data3.json
s3://mybucket/category=2/date=2018-08-31/data4.json
After running the crawler, I have two partition keys in my metadata table: one for category, the other for date. I want to retrieve partitions that match certain keys using the GetPartitions API so I began experimenting with the AWS CLI. If I run this command:
aws glue get-partitions --database-name mydb --table-name mytable --expression "category=1" --region us-west-2
I successfully retrieve the partition as expected. However, I tried the following command:
aws glue get-partitions --database-name mydb --table-name mytable --expression "category=1 AND date=2018-08-30" --region us-west-2
and the response was
An error occurred (InvalidInputException) when calling the
GetPartitions operation: Unsupported expression '2018 - 08 - 30'
Another command that produced this error was
aws glue get-partitions --database-name mydb --table-name mytable --expression category=1\ AND\ date=2018-08-30 --region us-west-2
I also tried modifying the call by using the following command:
aws glue get-partitions --database-name mydb --table-name mytable --expression "category=1 AND date=2018\-08\-30" --region us-west-2
which gave me the error
An error occurred (InvalidInputException) when calling the GetPartitions operation: Lexical error at line 1, column 35. Encountered: "\" (92), after : ""
Is the GetPartitions API able to handle expressions for partitions that contain hyphens? If so, what is the correct syntax?
Partitions that are initially generated by a crawler in AWS Glue will have type String in the metadata catalog. While some of my categories contained hyphens, they were in uuids (i.e. category=so36-fkw1-...) so they were not interpreted as expressions. On the other hand, the dates contain only numeric characters and - which was the root of the problem. I was able to fix it by enclosing the dates in singular quotes as follows:
aws glue get-partitions --database-name mydb --table-name mytable --expression category=1\ AND\ date=\'2018-08-30\' --region us-west-2

Delete Item on DynamoDB CLI localhost

I have a DynamoDB's Table called "ZombieSession" and the "SessionId" primary key with "S" type.
The local service is running in http://localhost:8181.
For local tests, I'm trying execute these commands:
(1)
aws dynamodb delete-item --table-name ZombieSession --key
'4ae40a08-007c-4785-babd-caff0ed12d1d' --endpoint-url
http://localhost:8181 --region us-east-1
That results in:
Error parsing parameter '--key': Invalid JSON:
'4ae40a08-007c-4785-babd-caff0ed12d1d'
and
(2)
aws dynamodb delete-item --table-name ZombieSession --key
'{"SessionId":{"S":"4ae40a08-007c-4785-babd-caff0ed12d1d"}}'
--endpoint-url http://localhost:8181 --region us-east-1
That results in:
Error parsing parameter '--key': Invalid JSON:
'{SessionId:{S:4ae40a08-007c-4785-babd-caff0ed12d1d}}'
I don't found any documentation example about this.
What's the appropriate command for this operation?
I discovered that the value of --key parameter need have the quotation mark with escape:
aws dynamodb delete-item --table-name ZombieSession --key
"{\"SessionId\":{\"S\":\"4ae40a08-007c-4785-babd-caff0ed12d1d\"}}"
--endpoint-url http://localhost:8181 --region us-east-1

AWS CLI DynamoDB command

I am writing a shell script file for doing some operation on dynamoDB using AWS CLI. I am trying to update an attribute in an item in a dynamodb table if the attribute already exists.
However, I am not comfortable with the syntax of the update-item command. I want to update an attribute named 'conf' with some value. However, I am not able to figure out the syntax for SET in thie command. This is what I have got till now :
aws dynamodb update-item --table-name MY_TABLE_NAME --key '{"AccountId": {"S": accountId}}'
I know the above has to followed by the SET option.
Any help would be appreciated.
I think it would look something like this:
aws dynamodb update-item --table-name MY_TABLE_NAME --key file://update-key.json --update-expression "SET conf = :newconf" --expression-attribute-values file://update-attr-values.json --condition-expression "attribute_exists(conf)" --return-values ALL_NEW
update-key.json
{
"AccountId": {
"S": "account123"
}
}
update-attr-values.json
{
":newconf": {
"S": "new conf value"
}
}
It is possible to do this without files but the answer is hidden in the 700+ pages developer guide of dynamodb:
aws dynamodb update-item \
--region MY_REGION \
--table-name MY_TABLE_NAME \
--key='{"AccountId": {"S": accountId}}' \
--update-expression 'SET conf=:newconf' \
--expression-attribute-values '{":newconf":{"S":"new conf value"}}'
The developer guide of Dynamo DB can be found here: Dynamo DB Developer guide
On page 206 by Atomic Counters, there is an example of how to use --expression-attribute-values without a file