Skip to content

Instantly share code, notes, and snippets.

@fsaravia
Last active December 15, 2023 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fsaravia/13f4b94d5a370b1198f8474422c8b862 to your computer and use it in GitHub Desktop.
Save fsaravia/13f4b94d5a370b1198f8474422c8b862 to your computer and use it in GitHub Desktop.
What's My IP for AWS (Golang)
module whats-my-ip
go 1.21.0
require github.com/aws/aws-lambda-go v1.41.0
github.com/aws/aws-lambda-go v1.41.0 h1:l/5fyVb6Ud9uYd411xdHZzSf2n86TakxzpvIoz7l+3Y=
github.com/aws/aws-lambda-go v1.41.0/go.mod h1:jwFe2KmMsHmffA1X2R09hH6lFzJQxzI8qK17ewzbQMM=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
package main
import (
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"net/http"
)
type response struct {
Ip string `json:"ip_address"`
}
func handler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
requestContext := req.RequestContext
requestIdentity := requestContext.Identity
sourceIp := requestIdentity.SourceIP
response := &response{sourceIp}
json, err := json.Marshal(response)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: http.StatusInternalServerError,
}, err
}
return events.APIGatewayProxyResponse{
StatusCode: http.StatusOK,
Body: string(json),
}, nil
}
func main() {
lambda.Start(handler)
}

What's My IP for AWS Lambda

This is a Go script that can be deployed to AWS Lambda and returns the caller's IP address formatted in JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment