Home > Software engineering >  How to create DynamoDB table using docker CMD instruction and the AWS CLI inside running container?
How to create DynamoDB table using docker CMD instruction and the AWS CLI inside running container?

Time:02-10

Hello have a simple Dockerfile which has to create the DynamoDB tables:

FROM amazon/aws-cli AS seed                                                                                                                            
                                                                                                                                                       
CMD \                                                                                                                                                  
aws dynamodb --endpoint-url http://localhost:8080 create-table \                                                                                       
    --table-name mytable \                                                                                                           
    --attribute-definitions AttributeName=user_id,AttributeType=N AttributeName=order_id,AttributeType=N \
    --key-schema AttributeName=user_id,KeyType=HASH AttributeName=order_id,KeyType=RANGE \                                                             
    --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \                                                                                
    --region eu-west-2                                                                                                                                 

When I'm trying to run it I get the following output:

Attaching to my-container
my-container       | 
my-container       | usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
my-container       | To see help text, you can run:
my-container       | 
my-container       |   aws help
my-container       |   aws <command> help
my-container       |   aws <command> <subcommand> help
my-container       | 
my-container       | aws: error: argument command: Invalid choice, valid choices are:
my-container       | 
my-container       | accessanalyzer                           | account                                 
my-container       | acm                                      | acm-pca                                 
my-container       | alexaforbusiness                         | amp                                     
my-container       | amplify                                  | amplifybackend                          
my-container       | amplifyuibuilder                         | apigateway                              
my-container       | apigatewaymanagementapi                  | apigatewayv2                            

// Lot of services in here
                       
my-container       | s3                                       | ddb                                     
my-container       | configure                                | deploy                                  
my-container       | configservice                            | opsworks-cm                             
my-container       | history                                  | cli-dev                                 
my-container       | help                                    
my-container       | 
my-container exited with code 252

What am I doing wrong? Thanks for any help!

CodePudding user response:

With this particular image, the CMD must be in JSON-array syntax (one shell word to an array element) and must not contain the aws command.

FROM amazon/aws-cli
CMD ["dynamodb", "create-table", "..."]                                                                                                                                                  

This is because the aws-cli image declares ENTRYPOINT ["aws"], the ENTRYPOINT and CMD are combined into a single command, and string-format CMD inserts a shell wrapper. Your main container command in the current form is something like aws sh -c 'aws dynamodb ...' which the tool doesn't really understand.

(I might run this particular command as a standalone docker run command and not try to package it into an image, or possibly use the AWS SDK as part of your integration-test setup code. The explicit localhost endpoint also seems like it might not work well, since that will usually refer to the same container that's only running the AWS CLI.)

CodePudding user response:

In your command you must not have a space after \ that why maybe you recevie the error because the rest statements is ignored.

Try test again with this :

aws dynamodb --endpoint-url http://localhost:8080 create-table \
--table-name mytable \
--attribute-definitions AttributeName=user_id,AttributeType=N AttributeName=order_id,AttributeType=N \
--key-schema AttributeName=user_id,KeyType=HASH AttributeName=order_id,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--region eu-west-2

And also try to have a fake profile, because its need an access key and a secret key even if its locally

  • Related