How to pass a variable to put-item using AWS CLI dynamoDB - amazon-web-services

I'm trying to pass in a variable value to map Jarfile name in dynamoDB table using AWS CLI.
aws dynamodb put-item --table-name epis-deployment-history --item "{\"JarFile\":{\"S\":$JarFile}}" --return-consumed-capacity TOTAL
It threw this error. It substitutes the value correctly but the CLI commands fail to run.
Error parsing parameter '--item': Invalid JSON: Expecting value: line 1 column 17 (char 16)
JSON received: {"JarFile":{"S":medallia-dealertrack-93311b0-20210301-133510.jar}}
Deploy to preprod Complete.
Thank you

Related

AWS Printing DynamoDB Table Via CLI

I'm trying to find the right command to use in the CLI to print the contents of a table within DynamoDB.
I've tried using the following command but it gives me a "parameter validation failed" error.
`
aws dynamodb get-item \
--table-name Traffic \
--key file://traffic.json \
--return-consumed-capacity TOTAL
`
The AWS website is giving me a 403 error, at the moment, so I can't search for the solution through the official site.
To get all items in a table, use a scan operation, not a get item operation. This basic scan operation works fine with the CLI:
aws dynamodb scan --table-name Work
You can find all valid options here:
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/scan.html
You can run the Scan API to output how the table looks in DynamoDB JSON format.
aws dynamodb scan \
--table-name test \
--output text
If you have a list of keys to fetch in your traffic.json file then you should use batch-get-item.
If it's a single item you need then please share the contents of traffic.json file.

Why I get error usage command amazon cli?

¿How are you?
I'm trying to use this command in Amazon CLI:
aws autoscaling describe-launch-configurations \
--query "LaunchConfigurations[?contains(LaunchConfigurationName,'Extra')].LaunchConfigurationName\
| sort_by(LaunchConfigurations, &CreatedTime)[0].[CreatedTime,LaunchConfigurationName]
But I have an error:
In function sort_by(), invalid type for value: None, expected one of: ['array'], received: "null"
In the first part, I get a list of Launch configuration on Amazon EC2. Why in sort_by, I receive a null output?
When I execute the command without sort_by, I don't have any problem....
if your list returned by the query contains no results. the sort command will respond 'null'

How to create dynamodb table only if not exists via cli

I know the syntax for creating a dynamodb table on the cli, but how to create it only if doesn't exist? I want to do this via cli because it will be running on CodePipeline in AWS
What are the best options?
Thanks
You can use below's snippet for shell script, if describe table fails then create new table
DB_NAME=table_name
if aws dynamodb describe-table --table-name $DB_NAME 2>/dev/null; then
echo "DynamoDB Table: $DB_NAME found, Skipping DynamoDB table creation ..."
else
echo "DynamoDB Table: $DB_NAME found, Creating DynamoDB table ..."
aws dynamodb create-table --table-name $DB_NAME --attribute-definitions AttributeName=LockID,AttributeType=S --key-schema AttributeName=LockID,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
fi

UnrecognizedClientException error when I try to enable "time to live" on local DynamoDB

I use local DynamoDB on Docker and I want to set up a time to live (TTL) feature for the table.
To table creates I use:
aws dynamodb create-table \
--table-name activity \
--attribute-definitions \
AttributeName=deviceId,AttributeType=S \
AttributeName=time,AttributeType=S \
--key-schema \
AttributeName=deviceId,KeyType=HASH \
AttributeName=time,KeyType=RANGE \
--billing-mode 'PAY_PER_REQUEST' \
--endpoint-url http://dynamo:8000
And it works as need.
But when I try to enable TTL:
aws dynamodb update-time-to-live \
--table-name activity \
--time-to-live-specification Enabled=true,AttributeName=ttl
I got the error: An error occurred (UnrecognizedClientException) when calling the UpdateTimeToLive operation: The security token included in the request is invalid
Dummy credentials for the Docker I sent using docker-compose environment:
AWS_ACCESS_KEY_ID: 0
AWS_SECRET_ACCESS_KEY: 0
AWS_DEFAULT_REGION: eu-central-1
Used Docker images:
For DynamoDB - dwmkerr/dynamodb
For internal AWS CLI - garland/aws-cli-docker
What is wrong? How can I enable the feature using local Docker?
Thanks for any answer.
Best.
After an extra a few hours of failures, I have an answer. I hope it helps somebody save a bit of time:
Even if you use a local environment, you should use real AWS
credentials (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY). You can get it here after register.
If you use --endpoint-url parameter
for creating DB, then you should use it with the same value for
update-time-to-live or any other action for the DB.
Cheers!

How pass json as parameter to aws cli?

I am trying to update crawler using this command:
aws glue update-crawler --name my-crawler --configuration '{"Version":1.0,"CrawlerOutput":{"Partitions":{"AddOrUpdateBehavior":"InheritFromTable"}}}' --region us-west-2
As described here
Instead of update I got:
An error occurred (InvalidInputException) when calling the UpdateCrawler operation: Crawler configuration not valid: Error parsing JSON: Received JsonParseException: Unexpected character (''' (code 39)): expected a valid value (number, String, array, object, 'true', 'false' or 'null'). Check that your JSON is well formed. For more information about the crawler configuration structure, see http://docs.aws.amazon.com/glue/latest/dg/aws-glue-api-crawler-crawling.html.
The jsonlint tells me that json is ok.
What is wrong? How pass json as parameter for aws cli?
cli is used under windows 10
You have to escape the quotes under Windows:
aws glue update-crawler --name my-crawler --configuration "{\"Version\":1.0,\"CrawlerOutput\":{\"Partitions\":{\"AddOrUpdateBehavior\":\"InheritFromTable\"}}}" --region us-west-2
For Windows, you have to do some "special" escaping, which I've learned the hard way. Take the following JSON snippet...
{ "#t": "timestamp" }`
Here's how you'd enter it on Windows...
DOS
aws dynamodb scan --table-name MyTable --region "us-east-1" --profile dev --projection-expression "failureKey, #t" --expression-attribute-names "{ ""#t"": ""timestamp"" }"
For Powershell, it's a little different...
Powershell
aws dynamodb scan --table-name "MyTable" --region "us-east-1" --profile "dev" --projection-expression "failureKey, #t" --expression-attribute-names '{ \"#t\": \"timestamp\" }'
Used an example with a shorter JSON snippet, but you get the idea. Apply the same concept to your string based on the shell your using.