Skip to content

Instantly share code, notes, and snippets.

@donvito
Created November 21, 2019 11:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donvito/ab8d6a19a6dc25f4c477359e19efe003 to your computer and use it in GitHub Desktop.
Save donvito/ab8d6a19a6dc25f4c477359e19efe003 to your computer and use it in GitHub Desktop.
AWS Lambda Go example - retrieve environment variables
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
envVarKeys := []string{
"_HANDLER",
"AWS_REGION",
"AWS_EXECUTION_ENV",
"AWS_LAMBDA_FUNCTION_NAME",
"AWS_LAMBDA_FUNCTION_MEMORY_SIZE",
"AWS_LAMBDA_FUNCTION_VERSION",
"AWS_LAMBDA_LOG_GROUP_NAME",
"AWS_LAMBDA_LOG_STREAM_NAME",
"LANG",
"TZ",
"LAMBDA_TASK_ROOT",
"LAMBDA_RUNTIME_DIR",
"PATH",
"LD_LIBRARY_PATH",
"AWS_LAMBDA_RUNTIME_API",
}
m := make(map[string]string)
for _, v := range envVarKeys {
m[v] = os.Getenv(v)
}
jsonBytes, err := json.Marshal(m)
if err != nil {
log.Fatal("Error in converting map to json")
}
rstr := fmt.Sprint(string(jsonBytes))
return events.APIGatewayProxyResponse{
Body: rstr,
StatusCode: 200,
}, nil
}
func main() {
lambda.Start(handler)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment