AWS Read/Write Value without using DynamoDB

Overview

AWS SSM Parameter Store allows you to store configuration data and secrets. It’s perfect for simple key-value storage without the overhead of setting up DynamoDB.

Prerequisites

  • AWS Lambda function
  • IAM role with SSM permissions (ssm:GetParameter, ssm:PutParameter)
  • Node.js 18+ runtime

Installation

First, install the AWS SDK v3 SSM client:

npm install @aws-sdk/client-ssm

Implementation

1. Import Required Modules

import { SSMClient, GetParameterCommand, PutParameterCommand } from "@aws-sdk/client-ssm";

2. Initialize SSM Client

const ssmClient = new SSMClient({
  region: "eu-central-1"
});

3. Read Parameter Function

const getParameterValue = async (parameterName) => {
  const command = new GetParameterCommand({
    Name: parameterName
  });
  const ssmResponse = await ssmClient.send(command);
  return parseInt(ssmResponse.Parameter.Value, 10);
};

4. Write Parameter Function

const updateParameterValue = async (parameterName, newValue) => {
  const command = new PutParameterCommand({
    Name: parameterName,
    Value: newValue.toString(),
    Type: "String",
    Overwrite: true
  });
  await ssmClient.send(command);
};

5. Lambda Handler Example

export const handler = async (event) => {
  const parameterName = "totalEmailSent";
  
  // Get the current value
  const currentValue = await getParameterValue(parameterName);
  
  // Increment and update the value
  const incrementedValue = currentValue + 1;
  await updateParameterValue(parameterName, incrementedValue);

  // Retrieve the updated value
  const updatedValue = await getParameterValue(parameterName);

  return {
    statusCode: 200,
    body: `${updatedValue}`
  };
};

Required IAM Permissions

Add this policy to your Lambda execution role:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ssm:GetParameter",
        "ssm:PutParameter"
      ],
      "Resource": "arn:aws:ssm:eu-central-1:*:parameter/totalEmailSent"
    }
  ]
}

Creating the Initial Parameter

Create the parameter via AWS CLI:

aws ssm put-parameter \
  --name "totalEmailSent" \
  --value "0" \
  --type "String" \
  --region eu-central-1

When to Use

  • Simple counters and flags
  • Configuration values
  • Low-frequency updates
  • Non-critical state storage

For high-concurrency scenarios or complex data structures, consider using DynamoD instead.

Follow me on Twitter! It's free!