When building serverless applications on AWS, AWS Lambda is often the go-to solution for running code without provisioning or managing servers. Traditionally, AWS API Gateway has been used to expose Lambda functions as RESTful APIs. However, AWS introduced Lambda Function URLs, a simpler way to invoke Lambda functions via HTTPS without the overhead of configuring an API Gateway. In this post, we'll explore how to use Lambda Function URLs to handle different HTTP methods—GET, POST, PUT, and DELETE—while incorporating security authentication.
Lambda Function URLs provide a dedicated HTTP(S) endpoint for your Lambda function. This feature is particularly useful for single-function microservices, lightweight APIs, or when you need to expose a Lambda function to the public with minimal setup.
First, let's create a Lambda function and configure its URL. You can do this via the AWS Management Console, AWS CLI, or Infrastructure as Code (IaC) tools like AWS CloudFormation or Terraform.
Create a Lambda Function:
Create a Function URL:
Secure Your Function URL:
Lambda functions triggered by Function URLs can handle multiple HTTP methods—GET, POST, PUT, and DELETE—within a single function. Here’s a simple example of how to implement this:
import json def lambda_handler(event, context): # Determine the HTTP method http_method = event['httpMethod'] if http_method == 'GET': return handle_get(event) elif http_method == 'POST': return handle_post(event) elif http_method == 'PUT': return handle_put(event) elif http_method == 'DELETE': return handle_delete(event) else: return { 'statusCode': 405, 'body': json.dumps({'message': 'Method Not Allowed'}) } def handle_get(event): # Handle GET request logic return { 'statusCode': 200, 'body': json.dumps({'message': 'GET request received'}) } def handle_post(event): # Handle POST request logic return { 'statusCode': 200, 'body': json.dumps({'message': 'POST request received'}) } def handle_put(event): # Handle PUT request logic return { 'statusCode': 200, 'body': json.dumps({'message': 'PUT request received'}) } def handle_delete(event): # Handle DELETE request logic return { 'statusCode': 200, 'body': json.dumps({'message': 'DELETE request received'}) }
If you opted to use AWS_IAM for securing your function URL, clients will need to sign requests using AWS SigV4 (Signature Version 4). Here’s a brief overview of how to make authenticated requests:
For example, with the AWS CLI:
aws lambda invoke-url https://.lambda-url. .on.aws/ \ --http-method POST \ --body '{ "key": "value" }' \ --region \ --profile
AWS Lambda Function URLs offer a streamlined way to expose Lambda functions via HTTP without the need for an API Gateway. By handling different HTTP methods (GET, POST, PUT, DELETE) within the Lambda function and securing access with AWS IAM, you can build lightweight, secure APIs quickly. Whether you’re developing a simple microservice or a more complex application, Lambda Function URLs are a powerful addition to your AWS toolkit.
The above is the detailed content of Simplifying API Access with AWS Lambda Function URLs: Handling GET, POST, PUT, and DELETE with Built-in Security. For more information, please follow other related articles on the PHP Chinese website!