AWS ALB of a Go Lambda always returns "502 Bad Gateway" - amazon-web-services

I have an AWS Lambda implemented with Go lang. The Lambda is triggered by an ALB. When I invoke the ALB from outside it always returns this:
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>
In CloudWatch I can see that the Lambda was invoked. In this article I have read that the ALB expects a very specific response object from the Lambda. I have implemented that as a struct. Here is the Go Lambda code:
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"log"
)
type Request struct {
HttpMethod string `json:"httpMethod"`
Path string `json:"path"`
QueryStringParameters map[string]string `json:"queryStringParameters"`
IsBase64Encoded bool `json:"isBase64Encoded"`
Body string `json:"body"`
Headers RequestHeaders `json:"headers"`
}
type RequestHeaders struct {
Accept string `json:"accept"`
AcceptLanguage string `json:"accept-language"`
ContentType string `json:"Content-Type"`
Cookie string `json:"cookie"`
Host string `json:"host"`
UserAgent string `json:"user-agent"`
XAmznTraceId string `json:"x-amzn-trace-id"`
XForwardedFor string `json:"x-forwarded-for"`
XForwardedPort string `json:"x-forwarded-port"`
XForwardedProto string `json:"x-forwarded-proto"`
}
type Response struct {
IsBase64Encoded bool `json:"isBase64Encoded"`
StatusCode int `json:"statusCode"`
StatusDescription string `json:"statusDescription"`
Headers *ResponseHeaders `json:"headers"`
Body string `json:"body"`
}
type ResponseHeaders struct {
ContentType string `json:"Content-Type"`
}
func HandleRequest(ctx context.Context, request Request) (string, error) {
fmt.Println("Hello " + request.Body)
responseHeaders := new(ResponseHeaders)
responseHeaders.ContentType = "application/json"
response := new(Response)
response.IsBase64Encoded = false
response.StatusCode = 200
response.StatusDescription = "200 OK"
response.Headers = responseHeaders
response.Body = "{\"hello\":\"world\"}"
json, err := json.Marshal(response)
if err != nil {
log.Fatal(err)
}
responseString := string(json)
log.Println(responseString)
return responseString, nil
}
func main() {
lambda.Start( HandleRequest )
}
In Cloudwatch I can see that the Lambda is invoked and this the string it returns:
{
"isBase64Encoded": false,
"statusCode": 200,
"statusDescription": "200 OK",
"headers": {
"Content-Type": "application/json"
},
"body": "{\"hello\":\"world\"}"
}
As far as I can tell it looks like the described response specification in this article.
The logs from the ALB itself look like this:
http 2020-07-13T11:49:51.014327Z app/test-Lambda/3e92b31e6a921454 176.199.208.26:54486 - 0.006 0.021 -1 502 - 736 293 "POST http://test-lambda-999999999.eu-central-1.elb.amazonaws.com:80/ HTTP/1.1" "insomnia/7.1.1" - - arn:aws:elasticloadbalancing:eu-central-1:999999999:targetgroup/test-lambda-target/540454d9390da765 "Root=1-5f0c4a5e-ca4e4a43b6c48633dc4c5b3e" "-" "-" 0 2020-07-13T11:49:50.986000Z "forward" "-" "LambdaInvalidResponse" "-" "-"
I invested already a couple of hours in debugging but I really don't know why the ALB always returns a 502 error. Can you see the error? What I'm doing wrong?

Solved via debugging in comments: you need to return your actual Response structure from the handler, not a string containing JSON. The lambda library handles serializing the return value to JSON on its own.

Related

Which .NET Object corresponds to Lambda Proxt Integration in AWS

I've a lambda function that does some verification before inserting data into dynamoDB. I wish to return a 400 HttpStatus code if the passed payload is invalid and that's the reason I've googled and found
public async Task<APIGatewayProxyResponse> FunctionHandler(RequestItem request, ILambdaContext context)
{
But when I return the code as
private static APIGatewayProxyResponse GetBadRequestResponse()
{
var responseBadRequest = new APIGatewayProxyResponse
{
StatusCode = (int)HttpStatusCode.BadRequest,
Body = "Invalid payload sent",
// Headers = new Dictionary<string, string> { { "Content-Type", "text/plain" } }
};
return responseBadRequest;
}
It returns a 200 with the content of
{
"statusCode": 400,
"body": "Invalid payload sent",
"isBase64Encoded": false
}
In the AWS Console I did
But doing so If I put the APIGatewayProxyRequest as request, I've got null for Body field
Anyone has got a simila situation?

GoLang Mocking CSV Rest Response

I am trying to unit test an API which returns a CSV response. I am used to testing APIs that return Json response by doing something like this :
resBody := `{"access_token": "token","instance_url": "url","id": "id","token_type": "Bearer"}`
r := ioutil.NopCloser(bytes.NewReader([]byte(resBody)))
clientMock.GetMock = func(url string) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}
Here, the resBody is my mocked Json response, however unable to figure out what would be an equivalent response structure for testing APIs that return csv response.
Never mind, able to figure it out now.
Below can be used to mock CSV response :
resBody := `"access_token","instance_url","id","token_type"` + "\n" + `"token","url","id","Bearer"`
r := ioutil.NopCloser(bytes.NewReader([]byte(resBody)))
clientMock.GetMock = func(url string) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: r,
}, nil
}

How to remove .html extension using AWS Lambda & Cloudfront

I've my website's source code stored in AWS S3 and I'm using AWS Cloudfront to deliver my content.
I want to use AWS Lamda#Edge to remove .html extension from all the web links that's served through Cloudfront.
My required output should be www.example.com/foo instead of www.example.com/foo.html or example.com/foo1 instead of example.com/foo1.html.
Please help me to implement this as I can't find clear solution to use. I've referred the point 3 mentioned on this article: https://forums.aws.amazon.com/thread.jspa?messageID=796961&tstart=0. But it's not clear what I need to do.
PFB the lambda code, how can I modify it-
const config = {
suffix: '.html',
appendToDirs: 'index.html',
removeTrailingSlash: false,
};
const regexSuffixless = /\/[^/.]+$/; // e.g. "/some/page" but not "/", "/some/" or "/some.jpg"
const regexTrailingSlash = /.+\/$/; // e.g. "/some/" or "/some/page/" but not root "/"
exports.handler = function handler(event, context, callback) {
const { request } = event.Records[0].cf;
const { uri } = request;
const { suffix, appendToDirs, removeTrailingSlash } = config;
// Append ".html" to origin request
if (suffix && uri.match(regexSuffixless)) {
request.uri = uri + suffix;
callback(null, request);
return;
}
// Append "index.html" to origin request
if (appendToDirs && uri.match(regexTrailingSlash)) {
request.uri = uri + appendToDirs;
callback(null, request);
return;
}
// Redirect (301) non-root requests ending in "/" to URI without trailing slash
if (removeTrailingSlash && uri.match(/.+\/$/)) {
const response = {
// body: '',
// bodyEncoding: 'text',
headers: {
'location': [{
key: 'Location',
value: uri.slice(0, -1)
}]
},
status: '301',
statusDescription: 'Moved Permanently'
};
callback(null, response);
return;
}
// If nothing matches, return request unchanged
callback(null, request);
};
Please help me to remove .html extension from my website and what updated code do I need to paste in my AWS Lambda
Thanks in advance!!

API Gateway -> Lambda -> DynamoDB using Cognito. HTTP POST-> Unable to read response but returns a code 200

Scenario:
I query an HTTP POST (using Authorizer as Header parameter from Cognito).
When I try to fetch/read the query response, it triggers the error event. However, in the browser, I can see how 2 HTTP POST responses with 200 code and one of them returning the valid response. For example: if I make the request via POST man I receive the data in 1 response in a good way.
Problem:
I am unable to print the result because it launches the error event with not valid response data.
Browser images:
https://i.postimg.cc/MTMsxZjw/Screenshot-1.png
https://i.postimg.cc/3RstwMgv/Screenshot-2.png
Lambda code:
'use strict';
var AWS = require('aws-sdk'),
documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = function index(event, context, callback){
var params = {
TableName : "data-table"
};
documentClient.scan(params, function(err, data){
if(err){
callback(err, null);
}else{
console.log(JSON.stringify(data.Items));
callback(null, data.Items);
}
});
}
Client side JS code:
function requestData(pickupLocation) {
$.ajax({
type: 'POST',
url: _config.api.invokeUrl,
headers: {
Authorization: authToken,
},
data: "{}",
cache: false,
success: completeRequest,
error: errorRequest
});
}
function completeRequest(response) {
alert("hello");
alert(response.d);
}
function errorRequest(response) {
alert("hello1");
alert(response.status + ' ' + response.statusText);
}
According to further clarification based on the comments, this looks like API gateway has CORS disabled or enabled with incorrect header value returns.
The solution is to re-enable CORS through API gateway and in the advanced options add Access-Control-Allow-Origin to the header response (if not already on by default).
If you're proxying the response, you need to follow a specific format as described here
'use strict';
console.log('Loading hello world function');
exports.handler = async (event) => {
let name = "you";
let city = 'World';
let time = 'day';
let day = '';
let responseCode = 200;
console.log("request: " + JSON.stringify(event));
// This is a simple illustration of app-specific logic to return the response.
// Although only 'event.queryStringParameters' are used here, other request data,
// such as 'event.headers', 'event.pathParameters', 'event.body', 'event.stageVariables',
// and 'event.requestContext' can be used to determine what response to return.
//
if (event.queryStringParameters && event.queryStringParameters.name) {
console.log("Received name: " + event.queryStringParameters.name);
name = event.queryStringParameters.name;
}
if (event.pathParameters && event.pathParameters.proxy) {
console.log("Received proxy: " + event.pathParameters.proxy);
city = event.pathParameters.proxy;
}
if (event.headers && event.headers['day']) {
console.log("Received day: " + event.headers.day);
day = event.headers.day;
}
if (event.body) {
let body = JSON.parse(event.body)
if (body.time)
time = body.time;
}
let greeting = `Good ${time}, ${name} of ${city}. `;
if (day) greeting += `Happy ${day}!`;
let responseBody = {
message: greeting,
input: event
};
// The output from a Lambda proxy integration must be
// of the following JSON object. The 'headers' property
// is for custom response headers in addition to standard
// ones. The 'body' property must be a JSON string. For
// base64-encoded payload, you must also set the 'isBase64Encoded'
// property to 'true'.
let response = {
statusCode: responseCode,
headers: {
"x-custom-header" : "my custom header value"
},
body: JSON.stringify(responseBody)
};
console.log("response: " + JSON.stringify(response))
return response;
};
If you are using chrome you probably need the cors plugin .

Lambda return only 200 respose code

I have created a sample lambda function for producing success & error responses. function is like below
exports.handler = (event, context, callback) => {
if(event.val1=="1")
{
callback(null, 'success');
}else
{
callback(true, 'fail');
}
};
When i have tested this function using API Gateway , I got different response body, But the response code is Same (always return 200 ok response code).
Is it possible to customize status code from lambda function(eg: need 500 for error responses & 200 for success responses)?
You may want to look at API Gateway's new simplified Lambda proxy feature.
Using this you can define your status codes, return headers and body content directly from your Lambda.
Example from docs:
'use strict';
console.log('Loading hello world function');
exports.handler = function(event, context) {
var name = "World";
var responseCode = 200;
console.log("request: " + JSON.stringify(event));
if (event.queryStringParameters !== null && event.queryStringParameters !== undefined) {
if (event.queryStringParameters.name !== undefined && event.queryStringParameters.name !== null && event.queryStringParameters.name !== "") {
console.log("Received name: " + event.queryStringParameters.name);
name = event.queryStringParameters.name;
}
if (event.queryStringParameters.httpStatus !== undefined && event.queryStringParameters.httpStatus !== null && event.queryStringParameters.httpStatus !== "") {
console.log("Received http status: " + event.queryStringParameters.httpStatus);
responseCode = event.queryStringParameters.httpStatus;
}
}
var responseBody = {
message: "Hello " + name + "!",
input: event
};
var response = {
statusCode: responseCode,
headers: {
"x-custom-header" : "my custom header value"
},
body: JSON.stringify(responseBody)
};
console.log("response: " + JSON.stringify(response))
context.succeed(response);
};
In order to send a custom error code from AWS API GW you should use response mapping template in integration response.
You basically define a regular expression for each status code you'd like to return from API GW.
Steps:
Define Method Response for each status code AWS Documentation
Define Integration Response RegEx for each status mapping to the Correct Method Response AWS Documentation
Using this configuration the HTTP return code returned by API GW to the client is the one matching the regular expression in "selectionPattern".
Finally I strongly suggest to use an API GW framework to handle these configuration, Serverless is a very good framework.
Using Servereless you can define a template as follows (serverless 0.5 snippet):
myResponseTemplate:
application/json;charset=UTF-8: |
#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage'))) {
"status" : $errorMessageObj.status,
"error":{
"error_message":"$errorMessageObj.error.message",
"details":"$errorMessageObj.error.custom_message"
}
}
responsesValues:
'202':
selectionPattern: '.*"status": 202.*'
statusCode: '202'
responseParameters: {}
responseModels: {}
responseTemplates: '$${myResponseTemplate}'
'400':
selectionPattern: '.*"status": 400.*'
statusCode: '400'
responseParameters: {}
responseModels: {}
responseTemplates: '$${myResponseTemplate}'
Then simply return a json object from your lambda, as in the following python snippet (you can use similar approach in nodejs):
def handler(event, context):
# Your function code ...
response = {
'status':400,
'error':{
'error_message' : 'your message',
'details' : 'your details'
}
}
return response
I hope this helps.
G.