[{"data":1,"prerenderedAt":101},["ShallowReactive",2],{"snippet-integration-kinesis-to-lambda":3},{"id":4,"title":5,"contributors":6,"description":11,"extension":12,"gitHub":13,"introBox":16,"languages":20,"meta":28,"resources":29,"services":34,"snippets":37,"stem":97,"tags":98,"type":99,"__hash__":100},"snippets\u002Fsnippets\u002Fintegration-kinesis-to-lambda.json","Using AWS Lambda with Amazon Kinesis without Batch Item Handling",[7,8,9,10],"content\u002Fcontributors\u002Fmohammed-tomeh.json","content\u002Fcontributors\u002Fananthan-rudran.json","content\u002Fcontributors\u002Fumang-aggarwal.json","content\u002Fcontributors\u002Fjan-richter.json","Using AWS Lambda with Amazon Kinesis without Batch Item Handling.","json",{"template":14},{"repoURL":15},"https:\u002F\u002Fgithub.com\u002Faws-samples\u002Fserverless-snippets\u002Ftree\u002Fmain\u002Fintegration-kinesis-to-lambda",{"headline":17,"text":18},"How it works",[19],"The Lambda function retrieves Kinesis Records from the event parameter that it receives. The function retrieves Kinesis payload, decode from Base64 and log them to cloudwatch. The function processes Kinesis Records without batch item handling.",[21,22,23,24,25,26,27],"Node.js","TypeScript",".NET","Java","Go","Python","PHP",{},{"bullets":30},[31],{"text":32,"link":33},"Sample function code","https:\u002F\u002Fdocs.aws.amazon.com\u002Flambda\u002Flatest\u002Fdg\u002Fwith-kinesis-create-package.html",[35,36],"lambda-event-functions","kinesis",[38],{"title":39,"codeTabs":40},"Runtimes",[41,48,55,62,69,76,84,91],{"id":21,"title":42,"description":43,"snippets":44},"Usage Example with Node:","Consuming Kinesis event with Lambda using JavaScript without batch item handling.",[45],{"language":46,"code":47},"js","exports.handler = async (event, context) =&#62; {\n  for (const record of event.Records) {\n    try {\n      console.log(`Processed Kinesis Event - EventID: ${record.eventID}`);\n      const recordData = await getRecordDataAsync(record.kinesis);\n      console.log(`Record Data: ${recordData}`);\n      \u002F\u002F TODO: Do interesting work based on the new data\n    } catch (err) {\n      console.error(`An error occurred ${err}`);\n      throw err;\n    }\n  }\n  console.log(`Successfully processed ${event.Records.length} records.`);\n};\n\nasync function getRecordDataAsync(payload) {\n  var data = Buffer.from(payload.data, \"base64\").toString(\"utf-8\");\n  await Promise.resolve(1); \u002F\u002FPlaceholder for actual async work\n  return data;\n}\n",{"id":22,"title":49,"description":50,"snippets":51},"Usage Example with TypeScript:","Consuming Kinesis event with Lambda using TypeScript without batch item handling.",[52],{"language":53,"code":54},"ts","import {\n  KinesisStreamEvent,\n  Context,\n  KinesisStreamHandler,\n  KinesisStreamRecordPayload,\n} from \"aws-lambda\";\nimport { Buffer } from \"buffer\";\nimport { Logger } from \"@aws-lambda-powertools\u002Flogger\";\n\nconst logger = new Logger({\n  logLevel: \"INFO\",\n  serviceName: \"kinesis-stream-handler-sample\",\n});\n\nexport const functionHandler: KinesisStreamHandler = async (\n  event: KinesisStreamEvent,\n  context: Context\n): Promise&#60;void&#62; =&#62; {\n  for (const record of event.Records) {\n    try {\n      logger.info(`Processed Kinesis Event - EventID: ${record.eventID}`);\n      const recordData = await getRecordDataAsync(record.kinesis);\n      logger.info(`Record Data: ${recordData}`);\n      \u002F\u002F TODO: Do interesting work based on the new data\n    } catch (err) {\n      logger.error(`An error occurred ${err}`);\n      throw err;\n    }\n    logger.info(`Successfully processed ${event.Records.length} records.`);\n  }\n};\n\nasync function getRecordDataAsync(\n  payload: KinesisStreamRecordPayload\n): Promise&#60;string&#62; {\n  var data = Buffer.from(payload.data, \"base64\").toString(\"utf-8\");\n  await Promise.resolve(1); \u002F\u002FPlaceholder for actual async work\n  return data;\n}\n",{"id":23,"title":56,"description":57,"snippets":58},"Usage Example with .NET:","Consuming Kinesis event with Lambda using .NET without batch item handling.",[59],{"language":60,"code":61},"dotnet","﻿using System.Text;\nusing Amazon.Lambda.Core;\nusing Amazon.Lambda.KinesisEvents;\nusing AWS.Lambda.Powertools.Logging;\n\n\u002F\u002F Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.\n[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]\n\nnamespace KinesisIntegrationSampleCode;\n\npublic class Function\n{\n    \u002F\u002F Powertools Logger requires an environment variables against your function\n    \u002F\u002F POWERTOOLS_SERVICE_NAME\n    [Logging(LogEvent = true)]\n    public async Task FunctionHandler(KinesisEvent evnt, ILambdaContext context)\n    {\n        if (evnt.Records.Count == 0)\n        {\n            Logger.LogInformation(\"Empty Kinesis Event received\");\n            return;\n        }\n\n        foreach (var record in evnt.Records)\n        {\n            try\n            {\n                Logger.LogInformation($\"Processed Event with EventId: {record.EventId}\");\n                string data = await GetRecordDataAsync(record.Kinesis, context);\n                Logger.LogInformation($\"Data: {data}\");\n                \u002F\u002F TODO: Do interesting work based on the new data\n            }\n            catch (Exception ex)\n            {\n                Logger.LogError($\"An error occurred {ex.Message}\");\n                throw;\n            }\n        }\n        Logger.LogInformation($\"Successfully processed {evnt.Records.Count} records.\");\n    }\n\n    private async Task&#60;string&#62; GetRecordDataAsync(KinesisEvent.Record record, ILambdaContext context)\n    {\n        byte[] bytes = record.Data.ToArray();\n        string data = Encoding.UTF8.GetString(bytes);\n        await Task.CompletedTask; \u002F\u002FPlaceholder for actual async work\n        return data;\n    }\n}",{"id":24,"title":63,"description":64,"snippets":65},"Usage Example with Java","Consuming Kinesis event with Lambda using Java without batch item handling.",[66],{"language":67,"code":68},"java","package example;\n\nimport com.amazonaws.services.lambda.runtime.Context;\nimport com.amazonaws.services.lambda.runtime.LambdaLogger;\nimport com.amazonaws.services.lambda.runtime.RequestHandler;\nimport com.amazonaws.services.lambda.runtime.events.KinesisEvent;\n\npublic class Handler implements RequestHandler&#60;KinesisEvent, Void&#62; {\n    @Override\n    public Void handleRequest(final KinesisEvent event, final Context context) {\n        LambdaLogger logger = context.getLogger();\n        if (event.getRecords().isEmpty()) {\n            logger.log(\"Empty Kinesis Event received\");\n            return null;\n        }\n        for (KinesisEvent.KinesisEventRecord record : event.getRecords()) {\n            try {\n                logger.log(\"Processed Event with EventId: \"+record.getEventID());\n                String data = new String(record.getKinesis().getData().array());\n                logger.log(\"Data:\"+ data);\n                \u002F\u002F TODO: Do interesting work based on the new data\n            }\n            catch (Exception ex) {\n                logger.log(\"An error occurred:\"+ex.getMessage());\n                throw ex;\n            }\n        }\n        logger.log(\"Successfully processed:\"+event.getRecords().size()+\" records\");\n        return null;\n    }\n\n}\n",{"id":25,"title":70,"description":71,"snippets":72},"Usage Example with Go:","Consuming Kinesis event with Lambda using Go without batch item handling.",[73],{"language":74,"code":75},"go","package main\n\nimport (\n\t\"context\"\n\t\"log\"\n\n\t\"github.com\u002Faws\u002Faws-lambda-go\u002Fevents\"\n\t\"github.com\u002Faws\u002Faws-lambda-go\u002Flambda\"\n)\n\nfunc handler(ctx context.Context, kinesisEvent events.KinesisEvent) error {\n\tif len(kinesisEvent.Records) == 0 {\n\t\tlog.Printf(\"empty Kinesis event received\")\n\t\treturn nil\n\t}\n\n\tfor _, record := range kinesisEvent.Records {\n\t\tlog.Printf(\"processed Kinesis event with EventId: %v\", record.EventID)\n\t\trecordDataBytes := record.Kinesis.Data\n\t\trecordDataText := string(recordDataBytes)\n\t\tlog.Printf(\"record data: %v\", recordDataText)\n\t\t\u002F\u002F TODO: Do interesting work based on the new data\n\t}\n\tlog.Printf(\"successfully processed %v records\", len(kinesisEvent.Records))\n\treturn nil\n}\n\nfunc main() {\n\tlambda.Start(handler)\n}\n",{"id":77,"title":78,"description":79,"snippets":80},"Rust","Usage Example with Rust:","Consuming Kinesis event with Lambda using Rust without batch item handling.",[81],{"language":82,"code":83},"rust","use aws_lambda_events::event::kinesis::KinesisEvent;\nuse lambda_runtime::{run, service_fn, Error, LambdaEvent};\n\nasync fn function_handler(event: LambdaEvent&#60;KinesisEvent&#62;) -&#62; Result&#60;(), Error&#62; {\n    if event.payload.records.is_empty() {\n        tracing::info!(\"No records found. Exiting.\");\n        return Ok(());\n    }\n\n    event.payload.records.iter().for_each(|record| {\n        tracing::info!(\"EventId: {}\",record.event_id.as_deref().unwrap_or_default());\n\n        let record_data = std::str::from_utf8(&record.kinesis.data);\n\n        match record_data {\n            Ok(data) =&#62; {\n                \u002F\u002F log the record data\n                tracing::info!(\"Data: {}\", data);\n            }\n            Err(e) =&#62; {\n                tracing::error!(\"Error: {}\", e);\n            }\n        }\n    });\n\n    tracing::info!(\n        \"Successfully processed {} records\",\n        event.payload.records.len()\n    );\n\n    Ok(())\n}\n\n#[tokio::main]\nasync fn main() -&#62; Result&#60;(), Error&#62; {\n    tracing_subscriber::fmt()\n        .with_max_level(tracing::Level::INFO)\n        \u002F\u002F disable printing the name of the module in every log line.\n        .with_target(false)\n        \u002F\u002F disabling time is handy because CloudWatch will add the ingestion time.\n        .without_time()\n        .init();\n\n    run(service_fn(function_handler)).await\n}",{"id":26,"title":85,"description":86,"snippets":87},"Usage Example with Python","Consuming Kinesis event with Lambda using Python without batch item handling.",[88],{"language":89,"code":90},"python","import base64\ndef lambda_handler(event, context):\n\n    for record in event['Records']:\n        try:\n            print(f\"Processed Kinesis Event - EventID: {record['eventID']}\")\n            record_data = base64.b64decode(record['kinesis']['data']).decode('utf-8')\n            print(f\"Record Data: {record_data}\")\n            # TODO: Do interesting work based on the new data\n        except Exception as e:\n            print(f\"An error occurred {e}\")\n            raise e\n    print(f\"Successfully processed {len(event['Records'])} records.\")\n",{"id":27,"title":92,"description":93,"snippets":94},"Usage Example with PHP","Consuming Kinesis event with Lambda using PHP without batch item handling.",[95],{"language":27,"code":96},"&#60;?php\n\n# using bref\u002Fbref and bref\u002Flogger for simplicity\n\nuse Bref\\Context\\Context;\nuse Bref\\Event\\Kinesis\\KinesisEvent;\nuse Bref\\Event\\Kinesis\\KinesisHandler;\nuse Bref\\Logger\\StderrLogger;\n\nrequire __DIR__ . '\u002Fvendor\u002Fautoload.php';\n\nclass Handler extends KinesisHandler\n{\n    private StderrLogger $logger;\n    public function __construct(StderrLogger $logger)\n    {\n        $this-&#62;logger = $logger;\n    }\n\n    \u002F**\n     * @throws JsonException\n     * @throws \\Bref\\Event\\InvalidLambdaEvent\n     *\u002F\n    public function handleKinesis(KinesisEvent $event, Context $context): void\n    {\n        $this-&#62;logger-&#62;info(\"Processing records\");\n        $records = $event-&#62;getRecords();\n        foreach ($records as $record) {\n            try {\n                $data = $record-&#62;getData();\n                $this-&#62;logger-&#62;info(json_encode($data));\n                \u002F\u002F TODO: Do interesting work based on the new data\n            } catch (Exception $e) {\n                $this-&#62;logger-&#62;error($e-&#62;getMessage());\n                throw $e;\n            }\n        }\n        $totalRecords = count($records);\n        $this-&#62;logger-&#62;info(\"Successfully processed $totalRecords records\");\n    }\n}\n\n$logger = new StderrLogger();\nreturn new Handler($logger);\n","snippets\u002Fintegration-kinesis-to-lambda",[],"Integration","8iTUi-9Y2RkDN54V-D1HI9F3VIVFeRZw1PkLtn4QyDY",1792578864330]