diff --git a/README.md b/README.md index fb135a728..8da30fea7 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,7 @@ serverless deploy | [AppSync GraphQL API with DynamoDB (NodeJS)](https://github.com/serverless/examples/tree/v4/aws-node-appsync-dynamodb)
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)
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)
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)
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 | ## Community Examples diff --git a/aws-node-cloud-extensions-parameter-store/README.md b/aws-node-cloud-extensions-parameter-store/README.md new file mode 100644 index 000000000..2ce6bff9e --- /dev/null +++ b/aws-node-cloud-extensions-parameter-store/README.md @@ -0,0 +1,134 @@ + + +# 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-`, 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-`, 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//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). diff --git a/aws-node-cloud-extensions-parameter-store/example.json b/aws-node-cloud-extensions-parameter-store/example.json new file mode 100644 index 000000000..910f4aecd --- /dev/null +++ b/aws-node-cloud-extensions-parameter-store/example.json @@ -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" + } + } + ] + } +} diff --git a/aws-node-cloud-extensions-parameter-store/handler.mjs b/aws-node-cloud-extensions-parameter-store/handler.mjs new file mode 100644 index 000000000..6fea121cb --- /dev/null +++ b/aws-node-cloud-extensions-parameter-store/handler.mjs @@ -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 } +} diff --git a/aws-node-cloud-extensions-parameter-store/package-lock.json b/aws-node-cloud-extensions-parameter-store/package-lock.json new file mode 100644 index 000000000..ce8e7d382 --- /dev/null +++ b/aws-node-cloud-extensions-parameter-store/package-lock.json @@ -0,0 +1,380 @@ +{ + "name": "aws-node-cloud-extensions-parameter-store", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "aws-node-cloud-extensions-parameter-store", + "version": "1.0.0", + "dependencies": { + "@aws-sdk/client-ssm": "3.1180.0" + } + }, + "node_modules/@aws-sdk/client-ssm": { + "version": "3.1180.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/client-ssm/-/client-ssm-3.1180.0.tgz", + "integrity": "sha512-B1Sm+oce2dPQhmK7Y3xUBuNimKGBAkR6kJIDpun+KW/pj4a62N5Ca6fqSvwb+sIDyL2yXwqpN5Dj75ILturKug==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/credential-provider-node": "^3.972.99", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/fetch-http-handler": "^5.8.1", + "@smithy/node-http-handler": "^4.14.2", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/core": { + "version": "3.981.2", + "resolved": "https://registry.npmjs.org/@aws-sdk/core/-/core-3.981.2.tgz", + "integrity": "sha512-N3XKZdfa+Dcm7Kl8NJnaaHLx860Ts3QYHOraj/wuKOYHqqCApQBIKwzSxrpwZdwrqfbPt7BvJdYVhVMPpsRoGA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/types": "^3.975.1", + "@aws-sdk/xml-builder": "^3.972.43", + "@aws/lambda-invoke-store": "^0.3.0", + "@smithy/core": "^3.41.1", + "@smithy/signature-v4": "^5.7.5", + "@smithy/types": "^4.20.0", + "bowser": "^2.11.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-env": { + "version": "3.972.86", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-env/-/credential-provider-env-3.972.86.tgz", + "integrity": "sha512-ZFIkIzLAiOLOakfrzUuOz938+AF+q4nc/MYINYwFlgBbl8CsI8ofJ8Ueh0/Pp7hXMJfpq10eJdnQ7aaE6kfikQ==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-http": { + "version": "3.972.88", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-http/-/credential-provider-http-3.972.88.tgz", + "integrity": "sha512-92dpM3lwCyDjcDI3R+olUI2IG8R+qfUdnMQgxWZB4XM37P+N+oBaRd5xGc7uOH7FqMQY03XsrVKQeuWbPfIrKg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/fetch-http-handler": "^5.8.1", + "@smithy/node-http-handler": "^4.14.2", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-ini": { + "version": "3.973.32", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-ini/-/credential-provider-ini-3.973.32.tgz", + "integrity": "sha512-MdIAf70d701Hj04Yo7AKqI1RfjTW5dFf+tEvUirYFMbs3NRHWiwqQj/C9wGFptZtmX+z/c57H3gQfzTt5Bsn8A==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/credential-provider-env": "^3.972.86", + "@aws-sdk/credential-provider-http": "^3.972.88", + "@aws-sdk/credential-provider-login": "^3.972.94", + "@aws-sdk/credential-provider-process": "^3.972.86", + "@aws-sdk/credential-provider-sso": "^3.973.30", + "@aws-sdk/credential-provider-web-identity": "^3.972.92", + "@aws-sdk/nested-clients": "^3.998.5", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/credential-provider-imds": "^4.5.2", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-login": { + "version": "3.972.94", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-login/-/credential-provider-login-3.972.94.tgz", + "integrity": "sha512-sSq1PxdXT1kqR11/dRvE5AC2dEWMbYJCXzRU03Ulu4stu6vv08zju8uUNhVdqiIX+OrMIcEHHT1maLrUZDBGDA==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/nested-clients": "^3.998.5", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-node": { + "version": "3.972.99", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-node/-/credential-provider-node-3.972.99.tgz", + "integrity": "sha512-AgS2qV8P9XmT4M44LfGcFtKbbt/yLgBydETKxPvqN9y7gMnKYv8GY+QfH7JsNvu6eQgDl600uAjvzC6K7ilkJQ==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/credential-provider-env": "^3.972.86", + "@aws-sdk/credential-provider-http": "^3.972.88", + "@aws-sdk/credential-provider-ini": "^3.973.32", + "@aws-sdk/credential-provider-process": "^3.972.86", + "@aws-sdk/credential-provider-sso": "^3.973.30", + "@aws-sdk/credential-provider-web-identity": "^3.972.92", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/credential-provider-imds": "^4.5.2", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-process": { + "version": "3.972.86", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-process/-/credential-provider-process-3.972.86.tgz", + "integrity": "sha512-9x/+gtSDNxMQrRXgwIdwdNCVGxzUZtTZArjEeLfsn9xDvyXdsgs4my1g7JN5wKxT61ZLx4bKPREaDXnO8j7gkg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-sso": { + "version": "3.973.30", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-sso/-/credential-provider-sso-3.973.30.tgz", + "integrity": "sha512-sMvwUIaKQEeprbMT5LXrI6nhM1IUEBDRpJAydEkAMrI7brrsRl7pxnOAJ6nweg/Hn5pJOVS7JgieJIv6CvJ7Sw==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/nested-clients": "^3.998.5", + "@aws-sdk/token-providers": "3.1177.0", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/credential-provider-web-identity": { + "version": "3.972.92", + "resolved": "https://registry.npmjs.org/@aws-sdk/credential-provider-web-identity/-/credential-provider-web-identity-3.972.92.tgz", + "integrity": "sha512-AKaUwpy1wPN3B/byFWuvL6v+Qk8Gm840InZJw/bA1A+ciCL3zkzBUyEgxdeghJPEe79VKLs6BvbME74MQa3YKg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/nested-clients": "^3.998.5", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/nested-clients": { + "version": "3.998.5", + "resolved": "https://registry.npmjs.org/@aws-sdk/nested-clients/-/nested-clients-3.998.5.tgz", + "integrity": "sha512-9w5mkwVLOuGqlJkPR1uhod6ZP+TOtaxrKtvK2pY9G/+FEx0e8wYRLIDeg30dYQjFDx27jHGSzEA+OaMqxeyTPg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/signature-v4-multi-region": "^3.996.52", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/fetch-http-handler": "^5.8.1", + "@smithy/node-http-handler": "^4.14.2", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/signature-v4-multi-region": { + "version": "3.996.52", + "resolved": "https://registry.npmjs.org/@aws-sdk/signature-v4-multi-region/-/signature-v4-multi-region-3.996.52.tgz", + "integrity": "sha512-6W6SjGc8OXUhMzNxU1X922ghD+PP1XvzmCk5Scui0jaYsLezycMYR1RaLp5xTIOUrhTXyb4DnAbPr3fBgmdnzg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/types": "^3.975.1", + "@smithy/signature-v4": "^5.7.5", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/token-providers": { + "version": "3.1177.0", + "resolved": "https://registry.npmjs.org/@aws-sdk/token-providers/-/token-providers-3.1177.0.tgz", + "integrity": "sha512-KFQ/2YqE7OIGNu05hys6y9ZUyoNxxLJVHM7NCvjfyjV0Mki0u0Euwjduj5M/HMurdbZJfBV0NjMNQQkGlJEQqg==", + "license": "Apache-2.0", + "dependencies": { + "@aws-sdk/core": "^3.981.2", + "@aws-sdk/nested-clients": "^3.998.5", + "@aws-sdk/types": "^3.975.1", + "@smithy/core": "^3.41.1", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/types": { + "version": "3.975.1", + "resolved": "https://registry.npmjs.org/@aws-sdk/types/-/types-3.975.1.tgz", + "integrity": "sha512-7rGxUkCB2QPoQOeJmonmAICTN2zrH+fpmi1p4Iwu72lnxNaMhOgqcbpyddifnM1DBb19TWQ1u6z/XjkaFNEDWw==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws-sdk/xml-builder": { + "version": "3.972.43", + "resolved": "https://registry.npmjs.org/@aws-sdk/xml-builder/-/xml-builder-3.972.43.tgz", + "integrity": "sha512-rG8QVx4DOrJWptsogu1Aq+CDsnPKaBrAlJpKLxrJrIDXAq1ElnW0yQjjBnlWSXll0cDakCG6UDvaqwz0A9d9Og==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=20.0.0" + } + }, + "node_modules/@aws/lambda-invoke-store": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/@aws/lambda-invoke-store/-/lambda-invoke-store-0.3.0.tgz", + "integrity": "sha512-sl4Bm6yiMNYrZKkqqDFWN0UfnWhlS8ivKxrYl+6t0gCLrqr8y3B2IqZZbFRkfaVVp7C/baApyh71P+LeE1A2sQ==", + "license": "Apache-2.0", + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/core": { + "version": "3.41.1", + "resolved": "https://registry.npmjs.org/@smithy/core/-/core-3.41.1.tgz", + "integrity": "sha512-24LyiHSY0R/MLcbTylurWXU6Uy4lzI6KJgsdzxkNnjT9/OLWc2zGPb/3k5AM1f0vcgCGrExanUG339t+TBiIrA==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/credential-provider-imds": { + "version": "4.5.2", + "resolved": "https://registry.npmjs.org/@smithy/credential-provider-imds/-/credential-provider-imds-4.5.2.tgz", + "integrity": "sha512-A9uSdn72ozbRUSit0eib0TW7nXuNPlaeM0zcGkJ+nE6tFcSDbnmtwoxbTCFBukVQcszDAyvsd7+rTduPTXpygg==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.33.2", + "@smithy/types": "^4.17.2", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/fetch-http-handler": { + "version": "5.8.1", + "resolved": "https://registry.npmjs.org/@smithy/fetch-http-handler/-/fetch-http-handler-5.8.1.tgz", + "integrity": "sha512-h8BdMUrUBhMhMaeK7QqAdINRZezCGuYOCpcOZZ1ewxbHST9rdTd09xwNpvLTYfzDRehxQiReK4SKbdYZxBA+ew==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.39.0", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/node-http-handler": { + "version": "4.14.2", + "resolved": "https://registry.npmjs.org/@smithy/node-http-handler/-/node-http-handler-4.14.2.tgz", + "integrity": "sha512-i1uORV1v3qBqP43ayRKzIdm1q5x14tf8N4Dd06/QUX1Szm6VxHMrTsu20X5E63UFsaNZCRjI4SXG0jDmqYa+3w==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.39.0", + "@smithy/types": "^4.20.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/signature-v4": { + "version": "5.7.5", + "resolved": "https://registry.npmjs.org/@smithy/signature-v4/-/signature-v4-5.7.5.tgz", + "integrity": "sha512-WPjdr5oWOk1LArpI4pavu+fSo+/evi0Re1olxGvqwuZopd4gYR1UGCi+Soyn/IjmafqbWs+ZC+3AKxJS3ZpwUg==", + "license": "Apache-2.0", + "dependencies": { + "@smithy/core": "^3.38.0", + "@smithy/types": "^4.19.0", + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/@smithy/types": { + "version": "4.20.0", + "resolved": "https://registry.npmjs.org/@smithy/types/-/types-4.20.0.tgz", + "integrity": "sha512-lCA6QG22dPk9ecHbv91jDQZsmtPUjdS1aE2Il6NNPqe7DrYc0dZQi6DhcESk2KYXH2sp5xbAcGdYMZDFnuzA2A==", + "license": "Apache-2.0", + "dependencies": { + "tslib": "^2.6.2" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/bowser": { + "version": "2.15.1", + "resolved": "https://registry.npmjs.org/bowser/-/bowser-2.15.1.tgz", + "integrity": "sha512-ma47ABeUiTsmG4McKDtGCgQSiTCatM97Mlbn13K42N1kPSFE14oN3WFMjtyL4KubhGvQTqYheKl4Qx2bJtMXLQ==", + "license": "MIT" + }, + "node_modules/tslib": { + "version": "2.9.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.9.1.tgz", + "integrity": "sha512-a4Ux9VKuIMHsOS7ENbEq6hK2ZGYIT27Rtl0E4eD9KqwxhKiQeMZE/9xb3IdYwWaSOyIBQLExhQgXDTYoiCxnWA==", + "license": "0BSD" + } + } +} diff --git a/aws-node-cloud-extensions-parameter-store/package.json b/aws-node-cloud-extensions-parameter-store/package.json new file mode 100644 index 000000000..36cee5759 --- /dev/null +++ b/aws-node-cloud-extensions-parameter-store/package.json @@ -0,0 +1,10 @@ +{ + "name": "aws-node-cloud-extensions-parameter-store", + "version": "1.0.0", + "private": true, + "description": "A Node.js function that reads Parameter Store through AWS Systems Manager Cloud Extensions, deployed with the Serverless Framework", + "type": "module", + "dependencies": { + "@aws-sdk/client-ssm": "3.1180.0" + } +} diff --git a/aws-node-cloud-extensions-parameter-store/serverless.yml b/aws-node-cloud-extensions-parameter-store/serverless.yml new file mode 100644 index 000000000..481ae5f1c --- /dev/null +++ b/aws-node-cloud-extensions-parameter-store/serverless.yml @@ -0,0 +1,45 @@ +# A Node.js function that reads a Parameter Store parameter through AWS Systems +# Manager Cloud Extensions: a runtime in the function's execution environment +# that answers the read from a local cache at http://localhost:5333. +service: parameter-reader + +provider: + name: aws + runtime: nodejs26.x + architecture: arm64 + # Every function gets the Parameter Store extension. The Framework creates + # the Cloud Extensions profile, its entry and the role below, adds the + # runtime layer and sets AWS_CLOUDEXTENSIONS_PROFILE on each function. + cloudExtensions: + aws:parameterstore: + iam: + # The role the extension calls Parameter Store with. The function's + # own role gets no Parameter Store permission. + role: + statements: + - Effect: Allow + Action: + - ssm:GetParameter + - ssm:GetParameters + Resource: arn:aws:ssm:${aws:region}:${aws:accountId}:parameter${self:custom.parameterName} + +custom: + # One parameter per stage, so two stages can live in one account + parameterName: /${self:service}/${sls:stage}/greeting + +functions: + reader: + handler: handler.reader + environment: + PARAMETER_NAME: ${self:custom.parameterName} + +resources: + Resources: + # The parameter the function reads. The Framework doesn't create + # parameters, so it comes from CloudFormation. + GreetingParameter: + Type: AWS::SSM::Parameter + Properties: + Name: ${self:custom.parameterName} + Type: String + Value: Hello from Parameter Store diff --git a/examples.json b/examples.json index 63c4a61ca..635711b2c 100644 --- a/examples.json +++ b/examples.json @@ -2923,5 +2923,17 @@ "authorLink": "https://github.com/serverless", "authorName": "Serverless, Inc.", "authorAvatar": "https://avatars1.githubusercontent.com/u/13742415?s=200&v=4" + }, + { + "title": "Read Parameter Store through Cloud Extensions (Node.js)", + "name": "aws-node-cloud-extensions-parameter-store", + "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.", + "githubUrl": "https://github.com/serverless/examples/tree/v4/aws-node-cloud-extensions-parameter-store", + "framework": "v4", + "language": "node", + "platform": "aws", + "authorLink": "https://github.com/serverless", + "authorName": "Serverless, Inc.", + "authorAvatar": "https://avatars1.githubusercontent.com/u/13742415?s=200&v=4" } ]