-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdoc_newmaingateway_test.go
66 lines (55 loc) · 2.11 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
61
62
63
64
65
66
package sparta
import (
"context"
"net/http"
"github.com/aws/aws-lambda-go/lambdacontext"
"github.com/rs/zerolog"
)
// 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).(*zerolog.Logger)
logger.Info().
Str("RequestID", lambdaCtx.AwsRequestID).
Interface("Properties", props).
Msg("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, _ := NewAWSLambda(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.
_, methodErr := apiGatewayResource.NewMethod("GET", http.StatusOK)
if methodErr != nil {
panic("Failed to create API Method")
}
// 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
mainErr := Main("HelloWorldLambdaService", "Description for Hello World Lambda", []*LambdaAWSInfo{echoAPIGatewayLambdaFn}, apiGateway, nil)
if mainErr != nil {
panic("Failed to launch Main: " + mainErr.Error())
}
}