Comments (14)
@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.
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.
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.
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.
From the pre configured AWS test events for Cognito, the version should be passed as an int.
from aws-lambda-go.
Hey there, any update or workaround on this? I've run into the same issue
from aws-lambda-go.
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.
@dfalgout thank you
from aws-lambda-go.
@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.
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.
Hi @rayhaanq, thank you very much for sharing your solution. Submitted #59 for code review.
from aws-lambda-go.
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.
Merged #73
from aws-lambda-go.
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)
- feat: Add support for AWS VPC Lattice events
- The generic handler is slower than the dynamic handler
- Support for `AWS_LAMBDA_EXEC_WRAPPER`
- S3ObjectLambdaEvent is missed in codebase
- events.CodeCommitReference has missing Deleted field HOT 2
- ALB target grouped Lambda returns fine but alb return 502 error that "LambdaInvalidResponse" HOT 2
- Deserialisation of array values in Cognito events custom UserAttributes not working correctly HOT 1
- Support structured JSON logging for errors HOT 2
- please add a mocked example HOT 1
- Custom runtime provided.al2 broken on v1.42 HOT 1
- Add IoTCoreMQTTMessage to aws/aws-lambda-go/events
- Add disconnectStatusCode to event/APIGatewayWebsocketProxyRequestContext
- RequestContext.Authorizer differs between REST gateway vs HTTP gateway
- UnmarshalTypeError on CodePipelineEventBridgeEvent HOT 4
- Missing field enableSMSMFA on struct events.CognitoEventUserPoolsMigrateUserResponse
- Runtime.InvalidEntrypoint
- Add AWS CloudWatch Alarm Event
- Implement `MultiValueHeaders` for ``events.LambdaFunctionURLResponse``
- Small improvement in APIGatewayWebsocketProxyRequestContext.Authorizer
- [BUG] Traceability panic error memory address runtime al2023
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aws-lambda-go.