Skip to content

Instantly share code, notes, and snippets.

@brunopk
Last active June 30, 2021 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunopk/8d372dcf272647eacc91d6f15eba4547 to your computer and use it in GitHub Desktop.
Save brunopk/8d372dcf272647eacc91d6f15eba4547 to your computer and use it in GitHub Desktop.
Running serverless offline with a local DynamoDB database

Running serverless offline (with a local DynamoDB database)

On console:

$ export AWS_ACCESS_KEY_ID='any_value'
$ export AWS_SECRET_ACCESS_KEY='any_value'
$ export AWS_SERVICE_REGION='us-west-2'
$ export IS_LOCAL=1
$ serverless offline start

If using TypeScript, invoke tsc to compile and generate dist/ folder (from where serverless will run the code).

To config DynamoDB in JavaScript:

import * as AWS from 'aws-sdk';
AWS.config.update({ region: process.env.AWS_SERVICE_REGION });

let config = { convertEmptyValues: true } as unknown;


if (process.env.IS_LOCAL) {
   config = {
     region: 'localhost',
     endpoint: 'http://localhost:8000',
     accessKeyId: 'DEFAULT_ACCESS_KEY', // needed if you don't have aws credentials at all in env
     secretAccessKey: 'DEFAULT_SECRET', // needed if you don't have aws credentials at all in env
     convertEmptyValues: true
   }
};;

export const client = new AWS.DynamoDB.DocumentClient(config);

Useful links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment