Tutorial: Creating a response streaming Lambda function with a function URL - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Tutorial: Creating a response streaming Lambda function with a function URL

In this tutorial, you create a Lambda function defined as a .zip file archive with a function URL endpoint that returns a response stream. For more information about configuring function URLs, see Creating and managing function URLs.

Prerequisites

This tutorial assumes that you have some knowledge of basic Lambda operations and the Lambda console. If you haven't already, follow the instructions in Create a Lambda function with the console to create your first Lambda function.

To complete the following steps, you need the Amazon Command Line Interface (Amazon CLI) version 2. Commands and the expected output are listed in separate blocks:

aws --version

You should see the following output:

aws-cli/2.13.27 Python/3.11.6 Linux/4.14.328-248.540.amzn2.x86_64 exe/x86_64.amzn.2

For long commands, an escape character (\) is used to split a command over multiple lines.

On Linux and macOS, use your preferred shell and package manager.

Note

In Windows, some Bash CLI commands that you commonly use with Lambda (such as zip) are not supported by the operating system's built-in terminals. To get a Windows-integrated version of Ubuntu and Bash, install the Windows Subsystem for Linux. Example CLI commands in this guide use Linux formatting. Commands which include inline JSON documents must be reformatted if you are using the Windows CLI.

Create an execution role

Create the execution role that gives your Lambda function permission to access Amazon resources.

To create an execution role
  1. Open the Roles page of the Amazon Identity and Access Management (IAM) console.

  2. Choose Create role.

  3. Create a role with the following properties:

    • Trusted entity typeAmazon service

    • Use caseLambda

    • PermissionsAWSLambdaBasicExecutionRole

    • Role nameresponse-streaming-role

The AWSLambdaBasicExecutionRole policy has the permissions that the function needs to write logs to Amazon CloudWatch Logs. After you create the role, note down the its Amazon Resource Name (ARN). You'll need it in the next step.

Create a response streaming function (Amazon CLI)

Create a response streaming Lambda function with a function URL endpoint using the Amazon Command Line Interface (Amazon CLI).

To create a function that can stream responses
  1. Copy the following code example into a file named index.mjs.

    import util from 'util'; import stream from 'stream'; const { Readable } = stream; const pipeline = util.promisify(stream.pipeline); /* global awslambda */ export const handler = awslambda.streamifyResponse(async (event, responseStream, _context) => { const requestStream = Readable.from(Buffer.from(JSON.stringify(event))); await pipeline(requestStream, responseStream); });
  2. Create a deployment package.

    zip function.zip index.mjs
  3. Create a Lambda function with the create-function command. Replace the value of --role with the role ARN from the previous step.

    aws lambda create-function \ --function-name my-streaming-function \ --runtime nodejs16.x \ --zip-file fileb://function.zip \ --handler index.handler \ --role arn:aws:iam::123456789012:role/response-streaming-role
To create a function URL
  1. Add a resource-based policy to your function to allow access to your function URL. Replace the value of --principal with your Amazon Web Services account ID.

    aws lambda add-permission \ --function-name my-streaming-function \ --action lambda:InvokeFunctionUrl \ --statement-id 12345 \ --principal 123456789012 \ --function-url-auth-type AWS_IAM \ --statement-id url
  2. Create a URL endpoint for the function with the create-function-url-config command.

    aws lambda create-function-url-config \ --function-name my-streaming-function \ --auth-type AWS_IAM \ --invoke-mode RESPONSE_STREAM

Test the function URL endpoint

Test your integration by invoking your function. You can open your function's URL in a browser, or you can use curl.

curl --request GET "<function_url>" --user "<key:token>" --aws-sigv4 "aws:amz:us-east-1:lambda" --no-buffer

Our function URL uses the IAM_AUTH authentication type. This means that you need to sign requests with both your Amazon access key and secret key. In the previous command, replace <key:token> with the Amazon access key ID. Enter your Amazon secret key when prompted. If you don't have your Amazon secret key, you can use temporary Amazon credentials instead.

Clean up your resources

You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting Amazon resources that you're no longer using, you prevent unnecessary charges to your Amazon Web Services account.

To delete the execution role
  1. Open the Roles page of the IAM console.

  2. Select the execution role that you created.

  3. Choose Delete.

  4. Enter the name of the role in the text input field and choose Delete.

To delete the Lambda function
  1. Open the Functions page of the Lambda console.

  2. Select the function that you created.

  3. Choose Actions, Delete.

  4. Type delete in the text input field and choose Delete.