Example #1
0
////////////////////////////////////////////////////////////////////////////////
// SES handler
//
func echoSESEvent(event *json.RawMessage,
	context *sparta.LambdaContext,
	w http.ResponseWriter,
	logger *logrus.Logger) {

	logger.WithFields(logrus.Fields{
		"RequestID": context.AWSRequestID,
	}).Info("Request received")

	configuration, configErr := sparta.Discover()
	logger.WithFields(logrus.Fields{
		"Error":         configErr,
		"Configuration": configuration,
	}).Info("Discovery results")

	// The message bucket is an explicit `DependsOn` relationship, so it'll be in the
	// resources map.  We'll find it by looking for the dependent resource with the "AWS::S3::Bucket" type
	bucketName := ""
	for _, eachResource := range configuration.Resources {
		if eachResource.Properties[sparta.TagResourceType] == "AWS::S3::Bucket" {
			bucketName = eachResource.Properties["Ref"]
		}
	}
	if "" == bucketName {
		logger.Error("Failed to discover SES bucket from sparta.Discovery")
		http.Error(w, "Failed to discovery SES MessageBodyBucket", http.StatusInternalServerError)
	}
	// The bucket is in the configuration map, prefixed by
	// SESMessageStoreBucket

	var lambdaEvent spartaSES.Event
	err := json.Unmarshal([]byte(*event), &lambdaEvent)
	if err != nil {
		logger.Error("Failed to unmarshal event data: ", err.Error())
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}

	// Get the metdata about the item...
	svc := s3.New(session.New())
	for _, eachRecord := range lambdaEvent.Records {
		logger.WithFields(logrus.Fields{
			"Source":     eachRecord.SES.Mail.Source,
			"MessageID":  eachRecord.SES.Mail.MessageID,
			"BucketName": bucketName,
		}).Info("SES Event")

		if "" != bucketName {
			params := &s3.HeadObjectInput{
				Bucket: aws.String(bucketName),
				Key:    aws.String(eachRecord.SES.Mail.MessageID),
			}
			resp, err := svc.HeadObject(params)
			logger.WithFields(logrus.Fields{
				"Error":    err,
				"Metadata": resp,
			}).Info("SES MessageBody")
		}
	}
}
Example #2
0
////////////////////////////////////////////////////////////////////////////////
// CloudWatchEvent handler
//
func echoCloudWatchEvent(event *json.RawMessage, context *sparta.LambdaContext, w http.ResponseWriter, logger *logrus.Logger) {
	logger.WithFields(logrus.Fields{
		"RequestID": context.AWSRequestID,
	}).Info("Request received")

	config, _ := sparta.Discover()
	logger.WithFields(logrus.Fields{
		"RequestID":     context.AWSRequestID,
		"Event":         string(*event),
		"Configuration": config,
	}).Info("Request received")
	fmt.Fprintf(w, "Hello World!")
}
Example #3
0
// Standard AWS λ function
func helloWorld(event *json.RawMessage,
	context *sparta.LambdaContext,
	w http.ResponseWriter,
	logger *logrus.Logger) {

	configuration, _ := sparta.Discover()

	logger.WithFields(logrus.Fields{
		"Discovery": configuration,
	}).Info("Custom resource request")

	fmt.Fprint(w, "Hello World")
}
Example #4
0
////////////////////////////////////////////////////////////////////////////////
// Bucket handler
//
func echoS3DynamicBucketEvent(event *json.RawMessage,
	context *sparta.LambdaContext,
	w http.ResponseWriter,
	logger *logrus.Logger) {

	config, _ := sparta.Discover()
	logger.WithFields(logrus.Fields{
		"RequestID":     context.AWSRequestID,
		"Event":         string(*event),
		"Configuration": config,
	}).Info("Request received")

	fmt.Fprintf(w, string(*event))
}