lambtrip

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2024 License: MIT Imports: 15 Imported by: 0

README

lambtrip

The package lambtrip is an adapter that converts APIs implemented in AWS Lambda into http.RoundTripper. No need to use AWS Lambda Function URLs, AWS Gateway, ALB, etc.

Synopsis

Here is a simple API implemented in AWS Lambda:

"use strict";

exports.handler = async (event) => {
  // Lambda handler code
  return {
    body: `Hello World`,
    statusCode: 200,
  };
};
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Lambda Function URL
Resources:
  FURLFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/
      Handler: app.handler
      Runtime: nodejs20.x
      Timeout: 3
      ### If you want to use Function URLs
      # FunctionUrlConfig:
      #   AuthType: AWS_IAM
      #   Cors:
      #     AllowOrigins: ["*"]

Outputs:
  FunctionURLEndpoint:
    Description: FURLFunction function name
    Value: !GetAtt FURLFunctionUrl.FunctionUrl
Use as a library

You can call the API by following code:

// initialize AWS SDK
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
    panic(err)
}
svc := lambda.NewFromConfig(cfg)

// register the lambda protocol
t := &http.Transport{}
t.RegisterProtocol("lambda", lambtrip.NewBufferedTransport(svc))
c := &http.Client{Transport: t}

// send a request to the lambda function
resp, err := c.Get("lambda://function-name/foo/bar")
if err != nil {
    panic(err)
}
defer resp.Body.Close()
Use function-url-local command

function-url-local is a minimum clone of AWS Lambda Function URLs.

$ go install github.com/shogo82148/lambtrip/cmd/function-url-local@latest
$ function-url-local function-name
{"time":"2024-02-05T22:28:57.781792+09:00","level":"INFO","msg":"starting the server","addr":":8080"}

Documentation

Overview

Example
package main

import (
	"context"
	"net/http"

	"github.com/aws/aws-sdk-go-v2/config"
	"github.com/aws/aws-sdk-go-v2/service/lambda"
	"github.com/shogo82148/lambtrip"
)

func main() {
	// Initialize AWS SDK to create a service client for AWS Lambda.
	cfg, err := config.LoadDefaultConfig(context.Background())
	if err != nil {
		panic(err)
	}
	svc := lambda.NewFromConfig(cfg)

	// Create a new HTTP transport and register the "lambda" protocol with a custom transport handler.
	t := &http.Transport{}
	t.RegisterProtocol("lambda", lambtrip.NewBufferedTransport(svc))
	// Create a new HTTP client using the custom transport to handle requests to Lambda functions.
	c := &http.Client{Transport: t}

	// Make an HTTP GET request to a specific Lambda function using the custom "lambda://" protocol.
	resp, err := c.Get("lambda://function-name/foo/bar")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BufferedTransport

type BufferedTransport struct {
	// contains filtered or unexported fields
}

func NewBufferedTransport

func NewBufferedTransport(c *lambda.Client) *BufferedTransport

func (*BufferedTransport) RoundTrip

func (t *BufferedTransport) RoundTrip(req *http.Request) (*http.Response, error)

type LambdaError

type LambdaError struct {
	StatusCode int
	Payload    []byte
}

LambdaError is an error returned by the lambda client.

func (*LambdaError) Error

func (e *LambdaError) Error() string

type ResponseStreamError

type ResponseStreamError struct {
	ErrorCode    string
	ErrorDetails string
}

ResponseStreamError is an error during response stream.

func (*ResponseStreamError) Error

func (e *ResponseStreamError) Error() string

type ResponseStreamTransport

type ResponseStreamTransport struct {
	// contains filtered or unexported fields
}

func NewResponseStreamTransport

func NewResponseStreamTransport(c *lambda.Client) *ResponseStreamTransport

func (*ResponseStreamTransport) RoundTrip

func (t *ResponseStreamTransport) RoundTrip(req *http.Request) (*http.Response, error)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL