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

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 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 AWS 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 AWS resources.

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

  2. Choose Create role.

  3. Create a role with the following properties:

    • Trusted entity typeAWS 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 (AWS CLI)

Create a response streaming Lambda function with a function URL endpoint using the AWS Command Line Interface (AWS 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 AWS 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 AWS access key and secret key. In the previous command, replace <key:token> with the AWS access key ID. Enter your AWS secret key when prompted. If you don't have your AWS secret key, you can use temporary AWS 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 AWS resources that you're no longer using, you prevent unnecessary charges to your AWS 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.