简体   繁体   中英

Localstack - AWS API Gateway enabling binary support using Terraform

I'm failing to enable binary support using the API Gateway with Terraform on Localstack.

The example is simple: a Lambda generates avatars similar to those of Github and returns the image as PNG.

The Lambda is (proxy) integrated with an API Gateway (the idea is: GET /avatars/{username} -> image/png)

When I invoke the published URL (I'm doing this on Localstack), the API returns always the Base64 Encoded image without applying the CONVERT_TO_BINARY .

Here are the basic steps...

  1. Create the ReST API:
resource "aws_api_gateway_rest_api" "api" {
  name = var.api_name
  
  # enable support for 'image/png'
  binary_media_types = [
    "image/png",
  ]
}
  1. Create the GET /avatars/{username} endpoint:
# ReST API endpoint 'avatars'
resource "aws_api_gateway_resource" "avatars" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  parent_id   = aws_api_gateway_rest_api.api.root_resource_id
  path_part   = "avatars"
}

# 'avatars' endpoint resource path parameter.
resource "aws_api_gateway_resource" "resource" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  parent_id   = aws_api_gateway_resource.avatars.id
  path_part   = "{username}"
}

# Defines the resource HTTP method (verb or action).
resource "aws_api_gateway_method" "get-avatar" {
  rest_api_id   = aws_api_gateway_rest_api.api.id
  resource_id   = aws_api_gateway_resource.resource.id
  http_method   = "GET"
  authorization = "NONE"

  request_parameters = {
    "method.request.path.username" = true
  }
}
  1. Integrate the GET /avatars/{username} endpoint into the ReST API:
resource "aws_api_gateway_integration" "resource_integration" {
  rest_api_id             = aws_api_gateway_rest_api.api.id
  resource_id             = aws_api_gateway_resource.resource.id
  http_method             = aws_api_gateway_method.get-avatar.http_method
  type                    = "AWS_PROXY"
  integration_http_method = "POST"
  uri                     = aws_lambda_function.lambda.invoke_arn
  passthrough_behavior    = "WHEN_NO_MATCH"

  request_parameters = {
    "integration.request.path.id" = "method.request.path.username"
  }
}
resource "aws_api_gateway_integration_response" "get-avatar-response" {
  rest_api_id      = aws_api_gateway_rest_api.api.id
  resource_id      = aws_api_gateway_resource.resource.id
  http_method      = aws_api_gateway_method.get-avatar.http_method
  status_code      = "200"
  content_handling = "CONVERT_TO_BINARY"
}
  1. Deploy the ReST API:
resource "aws_api_gateway_deployment" "deployment" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  stage_name  = "stage"
  depends_on  = [aws_api_gateway_method.get-avatar, aws_api_gateway_integration.resource_integration]
}

API Gateway assumes that the text data is a base64-encoded string and outputs the binary data as a base64-decoded blob.

Here the simple Lambda handler (in the awesome Go of course: :-) )

func handler(ctx context.Context, evt events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    log.Printf("Processing request data for request %s.\n", evt.RequestContext.RequestID)

    username := evt.PathParameters["username"]
    if len(username) == 0 {
        code := http.StatusBadRequest
        msg := http.StatusText(code)
        return events.APIGatewayProxyResponse{Body: msg, StatusCode: code}, nil
    }

    key := []byte{0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
    icon := identicon.New7x7(key)

    log.Printf("creating identicon for '%s'\n", username)

    pngdata := icon.Render([]byte(username))
    
    body := base64.StdEncoding.EncodeToString(pngdata) // <= Base64 Encoded Response

    return events.APIGatewayProxyResponse{
        Body: body,
        Headers: map[string]string{
            "Content-Type": "image/png",
        },
        IsBase64Encoded: true,  // <= Is Base64 Encoded? Yes!
        StatusCode:      200,
    }, nil
}

Invoking the endpoint with curl (for example):

curl 'http://localhost:4566/restapis/8nilx7bu49/stage/_user_request_/avatars/type-a-username-here'

...it responds with the Base64 encoded image. Infact, if I pipe the output to base64 -d saving the content, the image is right:

curl 'http://localhost:4566/restapis/8nilx7bu49/stage/_user_request_/avatars/type-a-username-here' -s | base64 -d > test.png

Could someone please point me out what I'm missing or confusing?

All the best, Luca

I got it working by using "*/*" as binary_media_types and nothing else. I read somewhere that if you specify something the client needs to send an identical accept content header.

I also don't have a aws_api_gateway_integration_response resource.

And finally I'm returning the raw image bytes in the form of a ByteArray. I'm using Kotlin to implement my lambda but the below should give you an idea how how to do it.

@Get("/{id}/avatar")
fun getImage(id: Long): HttpResponse<ByteArray> {
    logger.info { "AvatarController.getImage($id)" }
    val image = avatarService.getImage(id)
    val bytes: ByteArray = IOUtils.toByteArray(image)
    return HttpResponse.ok(bytes).contentType(MediaType.APPLICATION_OCTET_STREAM_TYPE)

Don't Base64 encode and don't use APIGatewayProxyResponse.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM