Share Config Properties between Serverless Config and Lambda Functions

Photo by Dan Farrell on Unsplash

The Problem

Today, I was deploying a serverless backend to AWS with with Serverless. At one point, I ran into a cryptic error from the Lambda like “Server Error”. In the Lambda logs in the AWS console, I discovered the cause of the error: the DynamoDB table name in the serverless.yml config file was different from the table name in the Lambda function. So the Lambda was trying to access a DynamoDB table which didn’t exist. I thought to myself, there has to be a better way. Can the table name be shared between the config and the code? Of course it can!

The Solution

See corresponding code snippets below. Lines of interest are in bold.

  1. Create a constants.json file with an object containing the config property key-value
  2. In your lambda file, you can import a JSON file just like a normal JS file; Below we use destructing to grab the exact property we need
  3. In your serverless.yml, you can import a property with the syntax: ${file(<relative_path_to_file>):<object_key_in_file>} … Below we save the value in a top level custom section so it can be referenced later in the file
  4. Now to reference the property in the serverless.yml, use the syntax ${self:custom.<property_name>}
// constants.json
{ "dynamoTableName": "myTable" }

// lambda.js
const { dynamoTableName } = require('./constants');

const AWS = require('aws-sdk');

module.exports.getAllRecords = (event, context, callback) => {
const dynamo = new AWS.DynamoDB.DocumentClient();
const params = { TableName: dynamoTableName };
dynamo.scan(params, (error, data) => {
...

// serverless.yml
service: my-service

custom:
dynamoTableName: ${file(./constants.json):dynamoTableName}

provider:
name: aws
runtime: nodejs12.x

iamRoleStatements:
- Effect: 'Allow'
Action:
- 'dynamodb:*'
Resource:
Fn::Join:
- ':'
- - 'arn:aws:dynamodb'
- 'Ref': 'AWS::Region'
- 'Ref': 'AWS::AccountId'
- 'table/${self:custom.dynamoTableName}'

resources:
Resources:
MyDynamo:
Type: 'AWS::DynamoDB::Table'
Properties:
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:custom.dynamoTableName}

functions:
...

--

--

--

Software engineer crafting full-stack, cloud-native solutions for enterprise

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Keeping Catalog Models Up-to-Date with Shopify Webhooks

Intermediate : How to Create and Communicate with Service Ability in Harmony OS

How to propagate a change in Kubernetes Secrets by restarting dependent pods!

A stock photo for “Secret”

Turn on ALL warning messages in Codebase

How do we make people think that we are awesome?

🚀 GitHub Profile Views Counter

Designing a contextual chatbot in Telegram with Python, Github & Heroku — part 1

Linux Privilege Escalation exploiting Sudo Rights — Part I

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
David Good

David Good

Software engineer crafting full-stack, cloud-native solutions for enterprise

More from Medium

AWS Lambda

Amazon RDS Proxy makes it easier to use SQL in Serverless

AWS Step Functions Activities

Debugging AWS Lambda Logs 101

Lambda Monitoring and Debugging