mweagle / Sparta

go microservices, powered by AWS Lambda
https://gosparta.io
MIT License
716 stars 48 forks source link

Routing requests question #48

Closed karthequian closed 7 years ago

karthequian commented 7 years ago

Thanks for the awesome framework! I was able to get the helloworld example to work great, and started to play with adding more routes to the example when I ran into an error at deployment time..

Code:

func appendS3Lambda(api *sparta.API, lambdaFunctions []*sparta.LambdaAWSInfo) []*sparta.LambdaAWSInfo {
    options := new(sparta.LambdaFunctionOptions)
    options.Timeout = 30
    versionFn := sparta.NewLambda(sparta.IAMRoleDefinition{}, version, options)
    helloworldFn := sparta.NewLambda(sparta.IAMRoleDefinition{}, helloworld, options)

    versionGatewayResource, _ := api.NewResource(basePath+"/version", versionFn)
    versionGatewayResource.NewMethod("GET", http.StatusOK)

    helloworldGatewayResource, _ := api.NewResource(apiPath+"/helloworld", authFn)
    helloworldGatewayResource.NewMethod("GET", http.StatusOK)

    versionFn.Permissions = append(versionFn.Permissions, sparta.S3Permission{
        BasePermission: sparta.BasePermission{
            SourceArn: s3Bucket,
        },
        Events: []string{"s3:ObjectCreated:*", "s3:ObjectRemoved:*"},
    })
    helloworldFn.Permissions = append(helloworldFn.Permissions, sparta.S3Permission{
        BasePermission: sparta.BasePermission{
            SourceArn: s3Bucket,
        },
        Events: []string{"s3:ObjectCreated:*", "s3:ObjectRemoved:*"},
    })

    lambdaFunctions = append(lambdaFunctions, versionFn)
    lambdaFunctions = append(lambdaFunctions, helloworldFn)
    return lambdaFunctions
}

When I deploy this, I get a bunch of errors during stack provisioning...

ERRO[0113] Stack provisioning error
ERRO[0113]  Error ensuring AWS::ApiGateway::Method (GETmainxversionxapixv1xversionced93552d2efd44934590d5d266a772c3ae0b853): Resource creation cancelled
ERRO[0113]  Error ensuring AWS::ApiGateway::Method (POSTmainxattackxapixv1xuserxattacks945d626a5c0ad6e9133bbb3d1efb133952fa8aef): Resource creation cancelled

Any hints would be appreciated! Thanks in advance!

karthequian commented 7 years ago

Actually, I figured it out! If someone else runs into this issue, start here: https://godoc.org/github.com/mweagle/Sparta#example-Main--ApiGateway and then extend as shown below. The echo2APIGatewayLambdaFn function should return something else to test...

    // Create the MyEchoAPI API Gateway, with stagename /test.  The associated
    // Stage reesource will cause the API to be deployed.
    stage := sparta.NewStage("prod")
    apiGateway := sparta.NewAPIGateway("gauntlt", stage)

    // Create a lambda function
    echoAPIGatewayLambdaFn := sparta.NewLambda(sparta.IAMRoleDefinition{}, echoAPIGatewayEvent, nil)

    // 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)

    // Create a lambda function
    echo2APIGatewayLambdaFn := sparta.NewLambda(sparta.IAMRoleDefinition{}, echo2APIGatewayEvent, nil)

    // Associate a URL path component with the Lambda function
    api2GatewayResource, _ := apiGateway.NewResource("/echo2HelloWorld", echo2APIGatewayLambdaFn)

    // Associate 1 or more HTTP methods with the Resource.
    api2GatewayResource.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
    sparta.Main("HelloWorldLambdaService", "Description for Hello World Lambda", []*sparta.LambdaAWSInfo{echoAPIGatewayLambdaFn, echo2APIGatewayLambdaFn}, apiGateway, nil)