# Evidently Client-side Evaluation Lambda
<!--BEGIN STABILITY BANNER-->
---

![Stability: Stable](https://img.shields.io/badge/stability-Stable-success.svg?style=for-the-badge)

> **This is a stable example. It should successfully build out of the box**
>
> This examples is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build.

---
<!--END STABILITY BANNER-->

[Amazon CloudWatch Evidently](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Evidently.html)
is an AWS service that enables you to safely validate new features by serving them to a specified percentage of your users while you roll out the feature.
To do this, we create an Evidently [feature](https://docs.aws.amazon.com/cloudwatchevidently/latest/APIReference/API_CreateFeature.html),
which defines *what* we're rolling out. We also define a [launch](https://docs.aws.amazon.com/cloudwatchevidently/latest/APIReference/API_CreateLaunch.html),
which defines *how* we're rolling out the feature.

Your application can then call the [EvaluateFeature API](https://docs.aws.amazon.com/cloudwatchevidently/latest/APIReference/API_EvaluateFeature.html)
to determine which version (or variation) of the feature to show to an individual user.
Normally, we must call the Evidently server for this, but many applications cannot afford the latency of a network call for this API,
so [Client-side evaluation](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch-Evidently-client-side-evaluation.html)
removes this dependency by evaluating the feature locally using the [AWS AppConfig Lambda extension](https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-integration-lambda-extensions.html).

For this to work, we set up three things:

- An Evidently launch
- An AppConfig environment for Evidently to use
- A Lambda function which calls the Lambda extension locally to evaluate a Feature


## Build

To build this app, you need to be in this example's root folder. Then run the following:

```bash
npm install -g aws-cdk
npm install
npm run build
```

This will install the necessary CDK, then this example's dependencies, and then build your TypeScript files and your CloudFormation template.

## Deploy

Run `cdk deploy`. This will deploy / redeploy your Stack to your AWS Account.

After deploying, visit the Lambda function and perform a Test call with any input. You should see this response:
```json
{
  "details": "{\"group\":\"hideFeature\",\"launch\":\"ExampleFeatureLaunch\"}",
  "reason": "LAUNCH_RULE_MATCH",
  "value": {
    "boolValue": false
  },
  "variation": "hideFeature"
}
```

## Synthesize Cloudformation Template

To see the Cloudformation template generated by the CDK, run `cdk synth`, then check the output file in the "cdk.out" directory.