Esempio n. 1
0
func appendS3Lambda(api *sparta.API, lambdaFunctions []*sparta.LambdaAWSInfo) []*sparta.LambdaAWSInfo {
	lambdaFn := sparta.NewLambda(sparta.IAMRoleDefinition{}, echoS3Event, nil)
	apiGatewayResource, _ := api.NewResource("/hello/world/test", lambdaFn)
	apiGatewayResource.NewMethod("GET", http.StatusOK)

	lambdaFn.Permissions = append(lambdaFn.Permissions, sparta.S3Permission{
		BasePermission: sparta.BasePermission{
			SourceArn: s3Bucket,
		},
		Events: []string{"s3:ObjectCreated:*", "s3:ObjectRemoved:*"},
	})
	return append(lambdaFunctions, lambdaFn)
}
Esempio n. 2
0
////////////////////////////////////////////////////////////////////////////////
// Return the *[]sparta.LambdaAWSInfo slice
//
func imagerFunctions(api *sparta.API) ([]*sparta.LambdaAWSInfo, error) {

	//////////////////////////////////////////////////////////////////////////////
	// 1 - Lambda function that listens to S3 events and stamps images
	//////////////////////////////////////////////////////////////////////////////
	// Provision an IAM::Role as part of this application
	var iamRole = sparta.IAMRoleDefinition{}

	// Setup the ARN that includes all child keys
	resourceArn := fmt.Sprintf("%s/*", s3EventBroadcasterBucket)
	iamRole.Privileges = append(iamRole.Privileges, sparta.IAMRolePrivilege{
		Actions: []string{"s3:GetObject",
			"s3:PutObject",
		},
		Resource: resourceArn,
	})
	var lambdaFunctions []*sparta.LambdaAWSInfo

	// The default timeout is 3 seconds - increase that to 30 seconds s.t. the
	// transform lambda doesn't fail early.
	transformOptions := &sparta.LambdaFunctionOptions{
		Description: "Stamp assets in S3",
		MemorySize:  128,
		Timeout:     30,
	}
	lambdaFn := sparta.NewLambda(iamRole, transformImage, transformOptions)

	//////////////////////////////////////////////////////////////////////////////
	// S3 configuration
	//
	lambdaFn.Permissions = append(lambdaFn.Permissions, sparta.S3Permission{
		BasePermission: sparta.BasePermission{
			SourceArn: s3EventBroadcasterBucket,
		},
		Events: []string{"s3:ObjectCreated:*", "s3:ObjectRemoved:*"},
	})
	lambdaFunctions = append(lambdaFunctions, lambdaFn)

	//////////////////////////////////////////////////////////////////////////////
	// 2 - Lambda function that allows for querying of S3 information
	//////////////////////////////////////////////////////////////////////////////
	s3ItemInfoOptions := &sparta.LambdaFunctionOptions{
		Description: "Get information about an item in S3 via querystring params",
		MemorySize:  128,
		Timeout:     10,
	}
	var iamDynamicRole = sparta.IAMRoleDefinition{}
	iamDynamicRole.Privileges = append(iamDynamicRole.Privileges, sparta.IAMRolePrivilege{
		Actions:  []string{"s3:GetObject"},
		Resource: resourceArn,
	})
	s3ItemInfoLambdaFn := sparta.NewLambda(iamDynamicRole, s3ItemInfo, s3ItemInfoOptions)

	// Register the function with the API Gateway
	apiGatewayResource, _ := api.NewResource("/info", s3ItemInfoLambdaFn)
	method, err := apiGatewayResource.NewMethod("GET", http.StatusOK)
	if err != nil {
		return nil, err
	}
	// Whitelist query string params
	method.Parameters["method.request.querystring.keyName"] = true
	method.Parameters["method.request.querystring.bucketName"] = true
	lambdaFunctions = append(lambdaFunctions, s3ItemInfoLambdaFn)

	return lambdaFunctions, nil
}