Skip to content

Commit

Permalink
Merge branch 'main' into vite-app-polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
anitarua committed Aug 10, 2023
2 parents b18ef83 + a2bbba8 commit c3b594f
Show file tree
Hide file tree
Showing 24 changed files with 14,880 additions and 1 deletion.
65 changes: 65 additions & 0 deletions examples/nodejs/lambda-examples/simple-get/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<head>
<meta name="Momento Node.js Client Library Documentation" content="Node.js client software development kit for Momento Cache">
</head>
<img src="https://docs.momentohq.com/img/logo.svg" alt="logo" width="400"/>

[![project status](https://momentohq.github.io/standards-and-practices/badges/project-status-official.svg)](https://github.com/momentohq/standards-and-practices/blob/main/docs/momento-on-github.md)
[![project stability](https://momentohq.github.io/standards-and-practices/badges/project-stability-stable.svg)](https://github.com/momentohq/standards-and-practices/blob/main/docs/momento-on-github.md)

<br>

## Simple Get Lambda

This repo contains an example lambda, built using AWS CDK, that repeatedly calls get on a Momento cache.

The primary use is to provide a base for testing Momento performance in a lambda environment. The lambda creates a Momento client, and then calls get on a hard-coded key 100 times, with a 100ms wait between calls. The metric logging middleware is enabled, so detailed information about each call is logged.

## Prerequisites

- Node version 14 or higher is required
- To get started with Momento you will need a Momento Auth Token. You can get one from the [Momento Console](https://console.gomomento.com). Check out the [getting started](https://docs.momentohq.com/getting-started) guide for more information on obtaining an auth token.

## Deploying the Simple Get Lambda

First make sure to start Docker and install the dependencies in the `lambda` directory, which is where the AWS Lambda code lives.

```bash
cd lambda/simple-get
npm install
```

The source code for the CDK application lives in the `infrastructure` directory.
To build and deploy it you will first need to install the dependencies:

```bash
cd infrastructure
npm install
```

To deploy the CDK app you will need to have [configured your AWS credentials](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-authentication.html#cli-chap-authentication-precedence).

You will also need a superuser token generated from the [Momento Console](https://console.gomomento.com).

Then run:

```
npm run cdk -- deploy --parameters MomentoAuthToken=<YOUR_MOMENTO_AUTH_TOKEN>
```

The lambda does not set up a way to access itself externally, so to run it, you will have to go to MomentoSimpleGet in AWS Lambda and run a test.

The lambda is set up to make get calls for the key 'key' in the cache 'cache' by default. It does not create a cache or write anything to that key. While it still may give useful latency information if it can't find a cache or key, creating them will let you test in a more realistic way.

If you have the [Momento CLI](https://github.com/momentohq/momento-cli) installed, you can create a cache like this:

```commandline
momento cache create cache
```

You can then set a value for the key:

```commandline
momento cache set key value
```

You can edit [handler.ts](lambda/simple-get/handler.ts) to change the cache and key the lambda looks for.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
**/*.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"root": true,
"env": {
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:@typescript-eslint/recommended-requiring-type-checking",
"plugin:import/recommended",
"plugin:prettier/recommended",
"plugin:node/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"project": "./tsconfig.json"
},
"plugins": ["@typescript-eslint"],
"rules": {
"semi": ["error", "always"],
"import/no-extraneous-dependencies": ["error", {}],
"node/no-unsupported-features/es-syntax": "off",
"node/no-missing-import": [
"error",
{
"tryExtensions": [".js", ".ts", ".json", ".node"]
}
],
"prettier/prettier": "error",
"block-scoped-var": "error",
"eqeqeq": "error",
"no-var": "error",
"prefer-const": "error",
"eol-last": "error",
"prefer-arrow-callback": "error",
"no-trailing-spaces": "error",
"quotes": ["warn", "single", {"avoidEscape": true}],
"no-restricted-properties": [
"error",
{
"object": "describe",
"property": "only"
},
{
"object": "it",
"property": "only"
}
],
// async without await is often an error and in other uses it obfuscates
// the intent of the developer. Functions are async when they want to await.
"require-await": "error",
"import/no-duplicates": "error"
},
"settings": {
"import/resolver": {
"node": {
"extensions": [".js", ".jsx", ".ts", ".tsx"]
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.js
!jest.config.js
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"bracketSpacing": false,
"singleQuote": true,
"trailingComma": "es5",
"arrowParens": "avoid",
"printWidth": 120
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'source-map-support/register';
import * as cdk from 'aws-cdk-lib';
import {SimpleGetStack} from '../lib/simple-get-stack';

const app = new cdk.App();
new SimpleGetStack(app, 'MomentoSimpleGet', {
/* If you don't specify 'env', this stack will be environment-agnostic.
* Account/Region-dependent features and context lookups will not work,
* but a single synthesized template can be deployed anywhere. */
/* Uncomment the next line to specialize this stack for the AWS Account
* and Region that are implied by the current CLI configuration. */
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
/* Uncomment the next line if you know exactly what Account and Region you
* want to deploy the stack to. */
// env: { account: '123456789012', region: 'us-east-1' },
/* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
44 changes: 44 additions & 0 deletions examples/nodejs/lambda-examples/simple-get/infrastructure/cdk.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"app": "npx ts-node --prefer-ts-exts bin/infrastructure.ts",
"watch": {
"include": [
"**"
],
"exclude": [
"README.md",
"cdk*.json",
"**/*.d.ts",
"**/*.js",
"tsconfig.json",
"package*.json",
"yarn.lock",
"node_modules",
"test"
]
},
"context": {
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
"@aws-cdk/core:checkSecretUsage": true,
"@aws-cdk/core:target-partitions": [
"aws",
"aws-cn"
],
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
"@aws-cdk/aws-iam:minimizePolicies": true,
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
"@aws-cdk/core:enablePartitionLiterals": true,
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
"@aws-cdk/aws-route53-patters:useCertificate": true,
"@aws-cdk/customresources:installLatestAwsSdkDefault": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
testEnvironment: 'node',
roots: ['<rootDir>/test'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as path from 'path';
import * as cdk from 'aws-cdk-lib';
import {Construct} from 'constructs';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as lambdaNodejs from 'aws-cdk-lib/aws-lambda-nodejs';
import * as secrets from 'aws-cdk-lib/aws-secretsmanager';

export class SimpleGetStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);

const momentoAuthTokenParam = new cdk.CfnParameter(this, 'MomentoAuthToken', {
type: 'String',
description: 'The Momento Auth Token that will be used to read from the cache.',
noEcho: true,
});

const authTokenSecret = new secrets.Secret(this, 'MomentoSimpleGetAuthToken', {
secretName: 'MomentoSimpleGetAuthToken',
secretStringValue: new cdk.SecretValue(momentoAuthTokenParam.valueAsString),
});

const getLambda = new lambdaNodejs.NodejsFunction(this, 'MomentoSimpleGet', {
functionName: 'MomentoSimpleGet',
runtime: lambda.Runtime.NODEJS_18_X,
entry: path.join(__dirname, '../../lambda/simple-get/handler.ts'),
projectRoot: path.join(__dirname, '../../lambda/simple-get'),
depsLockFilePath: path.join(__dirname, '../../lambda/simple-get/package-lock.json'),
handler: 'handler',
timeout: cdk.Duration.seconds(30),
memorySize: 128,
environment: {
MOMENTO_AUTH_TOKEN_SECRET_NAME: authTokenSecret.secretName,
},
});

authTokenSecret.grantRead(getLambda);
}
}
Loading

0 comments on commit c3b594f

Please sign in to comment.