|
| 1 | +# Kinesis Poison Pill Pattern |
| 2 | + |
| 3 | +This pattern demonstrates how to handle a Lambda consumer failure when reading from a Kinesis Data Stream with Terraform |
| 4 | + |
| 5 | +Without proper handling of failure when working with Kinesis Data Streams, an iterator will get stuck and the only way for the data to clear the stream is for it to **Age Out** beyond the trim horizon. |
| 6 | + |
| 7 | +This will ultimately create wasteful invocations of a Lambda, wasted CPU cycles in a container and worst of all the downstream consumers will not get the data they need. |
| 8 | + |
| 9 | +Learn more about this pattern at Serverless Land Patterns: << Add the live URL here >> |
| 10 | + |
| 11 | +**Important**: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +- [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 16 | +- [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 17 | +- [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 18 | +- [Node and NPM](https://nodejs.org/en/download/) installed |
| 19 | +- [The Go Programming Langage](https://go.dev/doc/install) must be installed in order to build the Lambda |
| 20 | +- [Terraform](https://www.terraform.io) must be installed in order to provision infrastructure |
| 21 | + |
| 22 | +## Deployment Instructions |
| 23 | + |
| 24 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 25 | + ``` |
| 26 | + git clone https://github.com/aws-samples/serverless-patterns |
| 27 | + ``` |
| 28 | +2. Change directory to the lambda module directory: |
| 29 | + ``` |
| 30 | + cd terraform-kinesis-poison-pill/modules/lambda |
| 31 | + ``` |
| 32 | +3. Install the project dependencies |
| 33 | + ``` |
| 34 | + npm install |
| 35 | + ``` |
| 36 | +4. Build lambda using make command |
| 37 | + ``` |
| 38 | + make |
| 39 | + ``` |
| 40 | +5. Change directory to the pattern directory: |
| 41 | + ``` |
| 42 | + cd ../.. |
| 43 | +
|
| 44 | +6. Deploy the stack to your default AWS account and region using terraform |
| 45 | + ``` |
| 46 | + terraform init |
| 47 | + terraform plan |
| 48 | + terraform apply |
| 49 | + ``` |
| 50 | +
|
| 51 | +## How it works |
| 52 | +
|
| 53 | +This pattern is designed to highlight how to handle failure when processing records in a Lambda. The `Kinesis Event Source` turns a Lambda into a Data Consumer which will process a JSON event that looks like this in Go |
| 54 | +
|
| 55 | +```golang |
| 56 | +type SampleEvent struct { |
| 57 | + FieldA string `json:"fieldA"` |
| 58 | + FieldB string `json:"fieldB"` |
| 59 | + FieldC int `json:"fieldC"` |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +And like this in JSON |
| 64 | + |
| 65 | +```javascript |
| 66 | +// defintion |
| 67 | +{ |
| 68 | + fieldA: string; |
| 69 | + fieldB: string; |
| 70 | + fieldC: number |
| 71 | +} |
| 72 | + |
| 73 | +// example |
| 74 | +{ |
| 75 | + "fieldA": "ABC", |
| 76 | + "fieldB": "EFG", |
| 77 | + "fieldC": 123 |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +Once the pattern is deployed to AWS, you will have the following resources created with the described capabilities |
| 82 | + |
| 83 | +- Kinesis Data Stream where the data will be "sourced" |
| 84 | +- Lambda Consumer written in Golang that will read from Kinesis in an attempt to process the records |
| 85 | +- SQS Queue where failed Kinesis records will be put |
| 86 | + |
| 87 | +## Testing |
| 88 | + |
| 89 | + |
| 90 | +### Testing Success |
| 91 | + |
| 92 | + |
| 93 | +```bash |
| 94 | +aws kinesis put-record \ |
| 95 | + --stream-name kinesis-stream \ |
| 96 | + --data 'eyJmaWVsZEEiOiAiQUJDIiwgImZpZWxkQiI6ICJFRkciLCAiZmllbGRDIjogMTIzIH0K' \ |
| 97 | + --partition-key key --region us-west-1 |
| 98 | +``` |
| 99 | + |
| 100 | +This command will publish a JSON payload that will successfully be Unmarshalled in the Lambda. |
| 101 | + |
| 102 | +### Testing Failure |
| 103 | + |
| 104 | +```bash |
| 105 | +aws kinesis put-record \ |
| 106 | + --stream-name kinesis-stream \ |
| 107 | + --data 'eyJmaWVsZEEiOiAiQUJDIiwgImZpZWxkQiI6ICJFRkciLCAiZmllbGRDIjogIjEyMyIgfQo=' \ |
| 108 | + --partition-key key --region us-west-1 |
| 109 | +``` |
| 110 | + |
| 111 | +This command will publish a JSON payload that will not be Unmarshalled successfully in the Lambda. `FieldC` will be a `string` instead of a `number` |
| 112 | + |
| 113 | +Based upon the definition in the terraform code, this will be attempted 5 times and will be bisected out of batch should more records be in the read. |
| 114 | + |
| 115 | +### Inspecting in the AWS Console |
| 116 | + |
| 117 | +- Cloudwatch - for Logs |
| 118 | + |
| 119 | +Go to Cloudwatch log group /aws/lambda/kinesis_stream |
| 120 | +You should see two kind of log event for success and failure |
| 121 | +For Success event shows as |
| 122 | +``` |
| 123 | +level=info msg="All good, see you later |
| 124 | +``` |
| 125 | +For failure event shows as |
| 126 | +``` |
| 127 | +json: cannot unmarshal string into Go struct field SampleEvent.fieldC of type int: UnmarshalTypeError |
| 128 | +``` |
| 129 | + |
| 130 | +- SQS - for the failed message |
| 131 | +- Kinesis - DataViewer looking at the TRIM_HORIZON |
| 132 | + |
| 133 | +## Cleanup |
| 134 | + |
| 135 | +1. Delete the stack |
| 136 | + ```bash |
| 137 | + terraform destroy |
| 138 | + ``` |
| 139 | + |
| 140 | +## Documentation |
| 141 | + |
| 142 | +- [AWS Using Lambda with Kinesis](https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html) |
| 143 | +- [AWS Lambda Failure-Handling Features](https://www.amazonaws.cn/en/new/2019/aws-lambda-supports-failure-handling-features-for-kinesis-and-dynamodb-event-sources/) |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 148 | + |
| 149 | +SPDX-License-Identifier: MIT-0 |
0 commit comments