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