Coder Social home page Coder Social logo

Comments (14)

rayhaanq avatar rayhaanq commented on June 15, 2024 9

@cmello thanks for getting back to me. This is for the PostConfirmation event. I've been at this for a few hours now and I've finally got it to work with a successful response. This is the code that makes it functional. There's probably other fields missing but I guess they're not completely necessary.

These are the two structs that are necessary. CognitoEventRequest is a modified version of events.CognitoEvent. This was necessary as most of the fields don't work at all. The CognitoEventResponse is necessary to get an actual proper response without any errors.

type CognitoEventRequest struct {
	DatasetName    string                          `json:"datasetName"`
	DatasetRecords map[string]CognitoDatasetRecord `json:"datasetRecords"`
	TriggerSource  string                          `json:"triggerSource"`
	IdentityID     string                          `json:"identityId"`
	UserPoolID     string                          `json:"userPoolId"`
	Region         string                          `json:"region"`
	Version        string                          `json:"version"`
	Request        interface{}                     `json:"request"`
	Username       string                          `json:"userName"`
}

type CognitoEventResponse struct {
	TriggerSource string   `json:"triggerSource"`
	Username      string   `json:"userName"`
	UserPoolID    string   `json:"userPoolId"`
	Region        string   `json:"region"`
	Version       int      `json:"version"`
	Response      struct{} `json:"response"`
}

my handler definition
func Handler(ctx context.Context, cognitoEvent CognitoEventRequest) (interface{}, error)

within the function somewhere it's necessary to create the response object. This is what worked for me

res := CognitoEventResponse{}
res.Version = 1
res.TriggerSource = cognitoEvent.TriggerSource
res.Region = cognitoEvent.Region
res.UserPoolID = cognitoEvent.UserPoolID
res.Username = cognitoEvent.Username

and finally return res, nil when you're done.

It seems as if triggers are completely broken currently. Also the documentation is quite bare for doing this in golang. There should also be relevant response structs and obviously CognitoEvent needs updating. It might be possible to return the same CognitoEventRequest in which case it's just CognitoEvent that needs updating. Thanks

from aws-lambda-go.

adamo57 avatar adamo57 commented on June 15, 2024

what does the data that you are sending it look like?

also, have you tried looping through the data that you are sending it?

for datasetName, datasetRecord := range cognitoEvent.DatasetRecords {
    fmt.Printf("[%s -- %s] %s -> %s -> %s \n",
        cognitoEvent.EventType,
        datasetName,
        datasetRecord.OldValue,
        datasetRecord.Op,
        datasetRecord.NewValue)
} 

from aws-lambda-go.

 avatar commented on June 15, 2024

To be clear, my lambda handler doesn't get invoked. This is failing before that, which means I can't add any logic to iterate through the event.

My assumption is that maybe Cognito is sending version payload in "string" but aws-lambda-go is expecting int? https://github.com/aws/aws-lambda-go/blob/master/events/cognito.go#L13

from aws-lambda-go.

 avatar commented on June 15, 2024

Post-confirmation parameters sent to Lambda from Cognito: https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-lambda-trigger-syntax-post-confirmation.html

from aws-lambda-go.

adamo57 avatar adamo57 commented on June 15, 2024

From the pre configured AWS test events for Cognito, the version should be passed as an int.

from aws-lambda-go.

rayhaanq avatar rayhaanq commented on June 15, 2024

Hey there, any update or workaround on this? I've run into the same issue

from aws-lambda-go.

dfalgout avatar dfalgout commented on June 15, 2024

You could just define the event yourself and change the type of Version from int to string:

type CognitoEvent struct {
	DatasetName    string                          `json:"datasetName"`
	DatasetRecords map[string]CognitoDatasetRecord `json:"datasetRecords"`
	EventType      string                          `json:"eventType"`
	IdentityID     string                          `json:"identityId"`
	IdentityPoolID string                          `json:"identityPoolId"`
	Region         string                          `json:"region"`
	Version        string                          `json:"version"`
}

// CognitoDatasetRecord represents a record from an AWS Cognito event
type CognitoDatasetRecord struct {
	NewValue string `json:"newValue"`
	OldValue string `json:"oldValue"`
	Op       string `json:"op"`
}

type Response struct {
	Message string `json:"message"`
}

func Handler(ctx context.Context, event CognitoEvent) (Response, error) {
	for datasetName, datasetRecord := range event.DatasetRecords {
		fmt.Printf("[%s -- %s] %s -> %s -> %s \n",
			event.EventType,
			datasetName,
			datasetRecord.OldValue,
			datasetRecord.Op,
			datasetRecord.NewValue)
	}

	return Response{
		Message: "Testing",
	}, nil
}

func main() {
	lambda.Start(Handler)
}

from aws-lambda-go.

rayhaanq avatar rayhaanq commented on June 15, 2024

@dfalgout thank you

from aws-lambda-go.

rayhaanq avatar rayhaanq commented on June 15, 2024

@dfalgout the lambda function executes but when trying to add a response i keep getting an error when clicking the verify link in the email Unrecognizable lambda output. This is using the same code as above

from aws-lambda-go.

cmello avatar cmello commented on June 15, 2024

Hi @rayhaanq, the response for the Custom Message event should contain the emailSubject, emailMessage and smsMessage attributes. Please see https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-lambda-trigger-examples.html#aws-lambda-triggers-custom-message-example and https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-lambda-trigger-syntax-custom-message.html . Thanks!

from aws-lambda-go.

cmello avatar cmello commented on June 15, 2024

Hi @rayhaanq, thank you very much for sharing your solution. Submitted #59 for code review.

from aws-lambda-go.

marchinram avatar marchinram commented on June 15, 2024

thanks @rayhaanq! based on your code I got a pre signup trigger working to auto confirm in go. I agree the docs are quite bare for golang and it took a lot of trial and error.

from aws-lambda-go.

bmoffatt avatar bmoffatt commented on June 15, 2024

Merged #73

from aws-lambda-go.

brydavis avatar brydavis commented on June 15, 2024

For those who stumble upon this in future, here is the entire solution altogether... (Credit given to everyone above, thanks!)

package main

import (
	"context"
	"fmt"

	"github.com/aws/aws-lambda-go/lambda"
)

type CognitoEventRequest struct {
	DatasetName    string                          `json:"datasetName"`
	DatasetRecords map[string]CognitoDatasetRecord `json:"datasetRecords"`
	TriggerSource  string                          `json:"triggerSource"`
	IdentityID     string                          `json:"identityId"`
	UserPoolID     string                          `json:"userPoolId"`
	Region         string                          `json:"region"`
	Version        string                          `json:"version"`
	Request        interface{}                     `json:"request"`
	Username       string                          `json:"userName"`
}

type CognitoDatasetRecord struct {
	NewValue string `json:"newValue"`
	OldValue string `json:"oldValue"`
	Op       string `json:"op"`
}

type CognitoEventResponse struct {
	TriggerSource string   `json:"triggerSource"`
	Username      string   `json:"userName"`
	UserPoolID    string   `json:"userPoolId"`
	Region        string   `json:"region"`
	Version       int      `json:"version"`
	Response      struct{} `json:"response"`
}

func Handler(ctx context.Context, cognitoEvent CognitoEventRequest) (interface{}, error) {
	for datasetName, datasetRecord := range cognitoEvent.DatasetRecords {
		fmt.Printf("[%s] %s -> %s -> %s \n",
			datasetName,
			datasetRecord.OldValue,
			datasetRecord.Op,
			datasetRecord.NewValue)
	}

	res := CognitoEventResponse{}
	res.Version = 1
	res.TriggerSource = cognitoEvent.TriggerSource
	res.Region = cognitoEvent.Region
	res.UserPoolID = cognitoEvent.UserPoolID
	res.Username = cognitoEvent.Username

	return res, nil
}

func main() {
	lambda.Start(Handler)
}

from aws-lambda-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.