Testing apps using AWS DynamoDB locally with the AWS CLI and JavaScript AWS SDK

One challenge when developing and testing code locally before you deploy is how you test against resources that are provisioned in the cloud. You can point directly to your cloud resources, but in some cases it’s easier if you can run and test code locally before you deploy.

DynamoDB has a local runtime that you can download and run locally from here.

Once downloaded run with:

java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb

You can use the AWS CLI to execute any of the DynamoDB commands against your local db by passing the –endpoint-url option like this:

aws dynamodb list-tables --endpoint-url http://localhost:8000

Docs for the available DynamoDB commands are here.

Other useful cli commands (append–endpoint-url http://localhost:8000 to use the local db):

List all tables:

aws dynamodb list-tables 

Delete a table:

aws dynamodb delete-table --table-name tablename

Scan a table (with no criteria)

aws dynamodb scan --table-name tablename

 

Running AWS Lambda functions on a timed schedule

AWS Lambdas can be called directly or triggered to execute based on a configured event. If you take a look at the ‘CloudWatch Events’ section, there is a configuration option for a Rule that can take a cron expression to trigger a Lambda based on a timed schedule. This could be useful for scheduling maintenance tasks or anything that needs to run on a periodic basis:

Scroll down to the ‘Configure trigger’ section and you’ll see the ‘Schedule Expression’ field where you can enter an expression like ‘rate(x minutes)’ (there’s a typo in the screenshot below it should be ‘minutes’ not ‘minute’) or define a cron style pattern:

Scroll further down and there’s an option to enable the schedule rule straight away, or disable it for testing, and you can enable it when you ready to start the schedule later:

After you’ve created the Rule, if you need to edit it you’ll find it over in the CloudWatch console page, under Events / Rules:

Revisiting AWS ECS: deploying Docker containers to ECS

A few months back I walked through the steps to build, tag and deploy Docker containers to AWS ECS. It’s been a while and I need to revisit the steps.

Although you can use the the AWS Console, using the aws cli works well complements the common steps with the docker cli.

The steps you need to connect and login the docker cli to aws are listed from the AWS ECS dashboard, from the Repositories tab. Press the ‘Push Commands’ button and it will show you the login command which looks like this:

aws ecr get-login --no-include-email --region us-east-1

and the output shows you a ‘docker login’ command – copy this and paste it to where you run your aws cli, to logon on aws.

Assuming you have a docker image already built (‘docker build -t yourimage .’), then you can tag it ready to push with the next command listed from the ‘Push Commands’ output:

docker tag yourimage:latest id-of-your-ecs-registry.dkr.ecr.us-east-1.amazonaws.com/yourimage:latest

Now push with:

docker push id-of-your-ecs-registry.dkr.ecr.us-east-1.amazonaws.com/yourimage:latest

 

AWS Lambda cost calculator webapp

AWS Lambda usage costs are a little tricky to understand, because the usage cost is per GB seconds of usage. This is calculated from the execution time of your Lambda by the GB of memory it is configured to use. For example, a single request to a Lambda configured to use 1GB that executes for 1 sec is 1 GB-sec.

AWS offers a free tier that includes the first 400,000 GB-s for free, and the first 1,000,000 requests a month for free. Above those you’re charged $0.00001667 for each GB-s and $0.20 for every 1M additions requests. Check the details here.

I put together a simple webapp that allows you to play with the numbers and see what your costs are going to look like. You can check it out (served from AWS S3) here:

 

https://s3-us-west-1.amazonaws.com/awslambdacostcalc/index.html

If you’re interested in taking a look at the source for the React app, it’s here on Github. Create me a ticket if you find any issues.