Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ serverless deploy
| [AppSync GraphQL API with DynamoDB (NodeJS)](https://github.com/serverless/examples/tree/v4/aws-node-appsync-dynamodb) <br/> An AWS AppSync GraphQL API with API key authentication and two JavaScript resolvers that write and read posts in a DynamoDB table, from the built-in appSync block of serverless.yml. | nodeJS |
| [AWS Step Functions state machine (NodeJS)](https://github.com/serverless/examples/tree/v4/aws-node-step-functions) <br/> Deploy a Step Functions state machine that runs two Node.js Lambda functions with a retry, a wait and a choice, and start an execution with serverless invoke stepf. | nodeJS |
| [Node EventBridge Rule on AWS](https://github.com/serverless/examples/tree/v4/aws-node-eventbridge-rule) <br/> This example shows how an Amazon EventBridge rule on the default event bus invokes a Lambda function only for the events that match its content filter. | nodeJS |
| [Read Parameter Store through Cloud Extensions (Node.js)](https://github.com/serverless/examples/tree/v4/aws-node-cloud-extensions-parameter-store) <br/> Deploy a Node.js function that reads a Parameter Store parameter from a local cache with AWS Systems Manager Cloud Extensions, configured with provider.cloudExtensions. | nodeJS |
<!-- AUTO-GENERATED-CONTENT:END -->

## Community Examples
Expand Down
134 changes: 134 additions & 0 deletions aws-node-cloud-extensions-parameter-store/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<!--
title: 'Read Parameter Store through Cloud Extensions (Node.js)'
description: 'Deploy a Node.js function that reads a Parameter Store parameter from a local cache with AWS Systems Manager Cloud Extensions, configured with provider.cloudExtensions.'
layout: Doc
framework: v4
platform: AWS
language: nodeJS
authorLink: 'https://github.com/serverless'
authorName: 'Serverless, Inc.'
authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4'
-->

# Read Parameter Store through Cloud Extensions

[![Verified](https://raw.githubusercontent.com/serverless/examples/verified/badges/aws-node-cloud-extensions-parameter-store.svg)](https://github.com/serverless/examples/blob/verified/results.json)

[AWS Systems Manager Cloud Extensions](https://docs.aws.amazon.com/systems-manager/latest/userguide/cloud-extensions.html) runs a small runtime inside each Lambda execution environment that answers Parameter Store, Secrets Manager and AWS AppConfig reads from a local cache. Your code keeps its usual AWS SDK client and points it at `http://localhost:5333`; the runtime calls Parameter Store only when its cache has no fresh value. This example gives a Node.js function the Parameter Store extension with `provider.cloudExtensions` and reads one parameter through it.

## What it deploys

One CloudFormation stack per stage, `parameter-reader-<stage>`, with:

- A Lambda function, `reader`, on Node.js 26 and arm64, with the Cloud Extensions runtime layer and the variable `AWS_CLOUDEXTENSIONS_PROFILE`, an IAM role and a CloudWatch Logs log group.
- A Cloud Extensions profile, `parameter-reader-<stage>`, with one entry for the Parameter Store extension, `aws:parameterstore`.
- An IAM role for the extension, allowed to read only this example's parameter and trusted only by the function.
- A Parameter Store parameter, `/parameter-reader/<stage>/greeting`, with the value `Hello from Parameter Store`.

## Prerequisites

- [Node.js](https://nodejs.org/) 26.
- The Serverless Framework CLI: `npm i -g serverless`. See [Getting started](https://www.serverless.com/framework/docs/getting-started).
- An AWS account, with credentials configured on your machine. See [AWS credentials](https://www.serverless.com/framework/docs/providers/aws/guide/credentials). Besides what every deploy needs, the credentials need the Cloud Extensions permissions listed in [IAM permissions to deploy Cloud Extensions](https://www.serverless.com/framework/docs/providers/aws/guide/cloud-extensions#iam-permissions-to-deploy-cloud-extensions).
- A Serverless account, or a license key in `SERVERLESS_LICENSE_KEY`. The CLI asks you to sign in the first time you run it.
- A Region where Cloud Extensions is available. See [Regions and endpoints](https://docs.aws.amazon.com/systems-manager/latest/userguide/cloud-extensions-regions-endpoints.html).

## Deploy

Install the dependencies and deploy from this directory:

```bash
npm ci
serverless deploy
```

Without options, the deploy uses the stage `dev` and the Region `us-east-1`. To change them, pass `--stage` or `--region`, and pass the same options to every later command, including `serverless remove`.

You should see output like this:

```text
Deploying "parameter-reader" to stage "dev" (us-east-1)
✔ Service deployed to stack parameter-reader-dev (49s)
functions:
reader: parameter-reader-dev-reader (3.2 MB)

cloud extensions:
parameter-reader-dev: aws:parameterstore 1.0.0
```

The `cloud extensions` section lists the profile the function uses, the extension in it and the extension version it runs. `serverless info` prints the same section.

## Try it

Invoke the function:

```bash
serverless invoke -f reader
```

It returns the parameter it read through the extension:

```json
{
"name": "/parameter-reader/dev/greeting",
"value": "Hello from Parameter Store"
}
```

The function's own role has no Parameter Store permission, and nothing but the Cloud Extensions runtime answers on `localhost:5333`, so this value can only have come through the extension. Invoke it again: a warm execution environment answers from the extension's cache without calling Parameter Store.

## How it works

[`serverless.yml`](https://github.com/serverless/examples/blob/v4/aws-node-cloud-extensions-parameter-store/serverless.yml) gives every function the Parameter Store extension, with a role of its own that may read the one parameter:

```yaml
provider:
name: aws
runtime: nodejs26.x
architecture: arm64
cloudExtensions:
aws:parameterstore:
iam:
role:
statements:
- Effect: Allow
Action:
- ssm:GetParameter
- ssm:GetParameters
Resource: arn:aws:ssm:${aws:region}:${aws:accountId}:parameter${self:custom.parameterName}
```

From this, the Framework creates the profile, its entry and the extension's role, adds the Cloud Extensions runtime layer for the function's architecture, sets `AWS_CLOUDEXTENSIONS_PROFILE` to the profile's name, and gives the function's role `cloudextensions:SyncState`, which the runtime needs to read its profile. The extension calls Parameter Store with its own role, so the function's role needs no Parameter Store permission. The parameter itself is a CloudFormation resource under `resources`, because the Framework doesn't create parameters.

[`handler.mjs`](https://github.com/serverless/examples/blob/v4/aws-node-cloud-extensions-parameter-store/handler.mjs) uses the SSM client of the [AWS SDK for JavaScript v3](https://www.npmjs.com/package/@aws-sdk/client-ssm), pinned in `package.json`, with its endpoint set to the Parameter Store extension:

```javascript
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm'

const ssm = new SSMClient({ endpoint: 'http://localhost:5333/parameterstore' })

export const reader = async () => {
const { Parameter } = await ssm.send(
new GetParameterCommand({ Name: process.env.PARAMETER_NAME }),
)
return { name: Parameter.Name, value: Parameter.Value }
}
```

The extension serves `GetParameter` and `GetParameters`. For any other Parameter Store call, such as a write or `GetParametersByPath`, use a second client with the default endpoint and grant that call to the function's role.

Each execution environment has its own runtime and its own cache, and the runtime makes cold starts take longer. See [Cloud Extensions](https://www.serverless.com/framework/docs/providers/aws/guide/cloud-extensions) for the cold-start cost, per-function settings, Secrets Manager and AppConfig.

## Cost and cleanup

While the example is deployed, Lambda bills each invocation and the function's compute time, and a read the extension can't answer from its cache is a Parameter Store API call, billed at Parameter Store's rates ([Cloud Extensions pricing](https://docs.aws.amazon.com/systems-manager/latest/userguide/cloud-extensions.html#cloud-extensions-pricing)). Until you remove the stack, the function's logs stay in CloudWatch Logs, which bills for stored log data. See [Lambda pricing](https://aws.amazon.com/lambda/pricing/), [Systems Manager pricing](https://aws.amazon.com/systems-manager/pricing/) and [CloudWatch pricing](https://aws.amazon.com/cloudwatch/pricing/). Remove the stack, with the function, the profile, its entry, both roles, the parameter and the log group, when you are done:

```bash
serverless remove
```

## Next steps

- Read [Cloud Extensions](https://www.serverless.com/framework/docs/providers/aws/guide/cloud-extensions) for per-function extensions, version pinning, settings, Secrets Manager and AppConfig.
- Read a value once at deploy time instead with [SSM Parameter Store variables](https://www.serverless.com/framework/docs/guides/variables/aws/ssm).
- Start from a function with no extensions in the [AWS Node.js example](https://github.com/serverless/examples/tree/v4/aws-node).
30 changes: 30 additions & 0 deletions aws-node-cloud-extensions-parameter-store/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"title": "Read Parameter Store through Cloud Extensions (Node.js)",
"description": "Deploy a Node.js function that reads a Parameter Store parameter from a local cache with AWS Systems Manager Cloud Extensions, configured with provider.cloudExtensions.",
"language": "node",
"platform": "AWS",
"framework": "v4",
"author": {
"name": "Serverless, Inc.",
"link": "https://github.com/serverless",
"avatar": "https://avatars1.githubusercontent.com/u/13742415?s=200&v=4"
},
"services": [
"lambda",
"ssm",
"cloudextensions",
"iam",
"logs"
],
"test": {
"tier": "fast",
"checks": [
{
"invoke": {
"function": "reader",
"contains": "Hello from Parameter Store"
}
}
]
}
}
13 changes: 13 additions & 0 deletions aws-node-cloud-extensions-parameter-store/handler.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GetParameterCommand, SSMClient } from '@aws-sdk/client-ssm'

// The SSM client of the AWS SDK, pointed at the Parameter Store extension.
// The Cloud Extensions runtime answers on this address inside the execution
// environment and calls Parameter Store only when its cache has no fresh value.
const ssm = new SSMClient({ endpoint: 'http://localhost:5333/parameterstore' })

export const reader = async () => {
const { Parameter } = await ssm.send(
new GetParameterCommand({ Name: process.env.PARAMETER_NAME }),
)
return { name: Parameter.Name, value: Parameter.Value }
}
Loading
Loading