github.com/mweagle/Sparta@v1.15.0/doc_newmaingateway_test.go (about) 1 package sparta 2 3 import ( 4 "context" 5 "net/http" 6 7 "github.com/aws/aws-lambda-go/lambdacontext" 8 "github.com/sirupsen/logrus" 9 ) 10 11 // NOTE: your application MUST use `package main` and define a `main()` function. The 12 // example text is to make the documentation compatible with godoc. 13 14 func echoAPIGatewayEvent(ctx context.Context, 15 props map[string]interface{}) error { 16 lambdaCtx, _ := lambdacontext.FromContext(ctx) 17 logger, _ := ctx.Value(ContextKeyLogger).(*logrus.Logger) 18 logger.WithFields(logrus.Fields{ 19 "RequestID": lambdaCtx.AwsRequestID, 20 "Properties": props, 21 }).Info("Lambda event") 22 return nil 23 } 24 25 // Should be main() in your application 26 func ExampleMain_apiGateway() { 27 28 // Create the MyEchoAPI API Gateway, with stagename /test. The associated 29 // Stage reesource will cause the API to be deployed. 30 stage := NewStage("test") 31 apiGateway := NewAPIGateway("MyEchoAPI", stage) 32 33 // Create a lambda function 34 echoAPIGatewayLambdaFn, _ := NewAWSLambda(LambdaName(echoAPIGatewayEvent), 35 echoAPIGatewayEvent, 36 IAMRoleDefinition{}) 37 38 // Associate a URL path component with the Lambda function 39 apiGatewayResource, _ := apiGateway.NewResource("/echoHelloWorld", echoAPIGatewayLambdaFn) 40 41 // Associate 1 or more HTTP methods with the Resource. 42 apiGatewayResource.NewMethod("GET", http.StatusOK) 43 44 // After the stack is deployed, the 45 // echoAPIGatewayEvent lambda function will be available at: 46 // https://{RestApiID}.execute-api.{AWSRegion}.amazonaws.com/test 47 // 48 // The dynamically generated URL will be written to STDOUT as part of stack provisioning as in: 49 // 50 // Outputs: [{ 51 // Description: "API Gateway URL", 52 // OutputKey: "URL", 53 // OutputValue: "https://zdjfwrcao7.execute-api.us-west-2.amazonaws.com/test" 54 // }] 55 // eg: 56 // curl -vs https://zdjfwrcao7.execute-api.us-west-2.amazonaws.com/test/echoHelloWorld 57 58 // Start 59 Main("HelloWorldLambdaService", "Description for Hello World Lambda", []*LambdaAWSInfo{echoAPIGatewayLambdaFn}, apiGateway, nil) 60 }