tutorial · 2022-04-09 · Updated 2022-04-09
Exploring AWS Lambda Function Urls
Intro
In this tutorial, we are going to be exploring the new AWS lambda function URLs.
These URLs provide direct HTTP(s) endpoints to invoke your functions through either the browser or external clients like cURL, Postman, etc.
Prerequisites
-
AWS Account
-
AWS CLI (installation link)
Verify installation withaws --version
Instructions
Create execution role
-
Select trusted entity:
Lambda -
Add permissions:
AWSLambdaBasicExecutionRole
Create lambda function
- Import boilerplate code into new file called
index.js
exports.handler = async (event) => {
return {
statusCode: 200,
body: 'AWS Lambda Function URLs!',
};
};
- Create deployment zip file through the following AWS CLI command
zip function.zip index.js
- Run AWS CLI
create-functioncommand to create a new lambda function with the code deployment zip file generated in the previous step. Make sure to replace the<FUNC_NAME>value with your desired function name and the<ROLE_ARN>value with the ARN from the previously create IAM role from above
aws lambda create-function \
--function-name <FUNC_NAME> \
--runtime nodejs14.x \
--zip-file fileb://function.zip \
--handler index.handler \
--role <ROLE_ARN>
- Run AWS CLI
create-function-url-configcommand to create a function URL for your newly created lambda function. Make sure to replace the<FUNC_NAME>value with the function name your set when creating your lambda function in the previous step
aws lambda create-function-url-config \
--function-name <FUNC_NAME> \
--auth-type NONE
Gotchas
Note: When creating the lambda function url config through AWS CLI with auth type of NONE, you must manually add the resource policy to your lambda function to allow public access
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": "*",
"Action": "lambda:InvokeFunctionUrl",
"Resource": "<FUNC_ARN>",
"Condition": {
"StringEquals": {
"lambda:FunctionUrlAuthType": "NONE"
}
}
}
]
}
Testing your new lambda function URL endpoint
Execute the following cURL command to test executing your new lambda function URL endpoint. (replacing <URL_ID> with yours)
curl https://<URL_ID>.lambda-url.us-west-2.on.aws
If all goes well, you should see the following response
{"body":"AWS Lambda Function URLs!"}