Giter Site home page Giter Site logo

Comments (9)

moelasmar avatar moelasmar commented on August 31, 2024

Thanks @mikepianka for raising this issue.
Unfortunately I am not able to reproduce this issue, and I think the problem is I do not have a windows machine with the same setup you have. It seems you have mcafee firewall installed in your machine, and it is blocking the call to https://checkip.amazonaws.com

from aws-sam-cli-app-templates.

moelasmar avatar moelasmar commented on August 31, 2024

can you try to allow list the url https://checkip.amazonaws.com in your Mcafee

from aws-sam-cli-app-templates.

mikepianka avatar mikepianka commented on August 31, 2024

So I'm not actually running Mcafee and I did think that error was strange. I think the issue might actually just be with the https://checkip.amazonaws.com endpoint itself. It seems like its TLS cert is flakey.

I just tried again with curl, and I was able to successfully hit the endpoint, but then a few minutes later I tried again and hit an error. In Chrome, I tried visiting it and got redirected to my ISP's page with a Mcafee warning (that must be where the Mcafee message originates from). I tried refreshing in Chrome a few minutes later and it went through and showed my IP as expected.

I changed up the hello world example to use jsonplaceholder and it all works as expected.

package main

import (
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"net/http"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"
)

var (
	// DefaultHTTPGetAddress Default Address
	DefaultHTTPGetAddress = "https://jsonplaceholder.typicode.com/albums/1"

	// ErrNoTitle No album title found in response
	ErrNoTitle = errors.New("No album title in HTTP response")

	// ErrNon200Response non 200 status code in response
	ErrNon200Response = errors.New("Non 200 Response found")
)

type Album struct {
	UserID int    `json:"userId"`
	ID     int    `json:"id"`
	Title  string `json:"title"`
}

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
	resp, err := http.Get(DefaultHTTPGetAddress)
	if err != nil {
		return events.APIGatewayProxyResponse{}, err
	}

	if resp.StatusCode != 200 {
		return events.APIGatewayProxyResponse{}, ErrNon200Response
	}

	content, err := io.ReadAll(resp.Body)
	if err != nil {
		return events.APIGatewayProxyResponse{}, err
	}

	var album Album

	err = json.Unmarshal(content, &album)
	if err != nil {
		return events.APIGatewayProxyResponse{}, err
	}

	if len(album.Title) == 0 {
		return events.APIGatewayProxyResponse{}, ErrNoTitle
	}

	return events.APIGatewayProxyResponse{
		Body:       fmt.Sprintf("Hello! Have you listened to the new album <%s>?\n", album.Title),
		StatusCode: 200,
	}, nil
}

func main() {
	lambda.Start(handler)
}

from aws-sam-cli-app-templates.

moelasmar avatar moelasmar commented on August 31, 2024

thanks @mikepianka for the detailed explanation. As you mentioned, the problem is in https://checkip.amazonaws.com/ endpoint itself, which we could not fix. But I believe it is better to update our template to use some other endpoint or something else, as using this endpoint is not the purpose of our templates.

from aws-sam-cli-app-templates.

mikepianka avatar mikepianka commented on August 31, 2024

Sounds good. Do you want me to open a PR with my example? I could swap it out for a different endpoint if you have something else in mind.

from aws-sam-cli-app-templates.

mikepianka avatar mikepianka commented on August 31, 2024

Actually I just realized that since this example uses API Gateway proxy, you should be able to just get the caller's IP right from the identity sourceIP property on the request object. So the example could function the same but not rely on an extra web request. What do you think of that?

from aws-sam-cli-app-templates.

moelasmar avatar moelasmar commented on August 31, 2024

Actually the main purpose of this template is to show how to create a Go Lambda function with an API Gateway resource. Calling https://checkip.amazonaws.com endpoint or any other service is not part of the template purpose. So, I think it will be better to use the Request to get this IP, but make sure that if the Lambda function got invoked directly with different event, it will not fail. Your contribution is appreciated :)

from aws-sam-cli-app-templates.

mikepianka avatar mikepianka commented on August 31, 2024

Hi @moelasmar. It took a bit of time for me to get back to this, but I made a PR to fix the examples here - #431

from aws-sam-cli-app-templates.

mildaniel avatar mildaniel commented on August 31, 2024

This has been merged in #431, resolving.

from aws-sam-cli-app-templates.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.