Created
January 6, 2024 17:08
-
-
Save m-arrieta-r/f22b06dc8398babfa1f04c61daf36b0b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { AmplifyAuth } from '@aws-amplify/auth-construct-alpha'; | |
import { AmplifyData, AmplifyDataDefinition } from '@aws-amplify/data-construct'; | |
import { NodejsFunction, NodejsFunctionProps } from 'aws-cdk-lib/aws-lambda-nodejs'; | |
import { aws_lambda }from 'aws-cdk-lib'; | |
import * as cdk from 'aws-cdk-lib'; | |
import { UserPool } from 'aws-cdk-lib/aws-cognito'; | |
import { Construct } from 'constructs'; | |
import { DynamoEventSource, DynamoEventSourceProps } from 'aws-cdk-lib/aws-lambda-event-sources'; | |
type LambdaTriggerToDynamoProps = { | |
table: cdk.aws_dynamodb.ITable; | |
functionProps: NodejsFunctionProps; | |
dynamoEventSourceProps: DynamoEventSourceProps; | |
} | |
export class AppStack extends cdk.Stack { | |
private readonly dataDefaultName = 'amplifyData'; | |
constructor(scope: Construct, id: string, props?: cdk.StackProps) { | |
super(scope, id, props); | |
const data = new AmplifyData(this, 'amplify-data', { | |
definition: AmplifyDataDefinition.fromFiles('../amplify/data/schema.graphql'), | |
authorizationModes: { | |
userPoolConfig: { | |
userPool: UserPool.fromUserPoolId(this, 'ImportedUserPool', '<YOUR_USER_POOL_ID>'), | |
}, | |
}, | |
}); | |
this.addLambdaTriggerToDynamo(this, 'order-transaction', { | |
table: data.resources.tables.OrderTransaction, | |
functionProps: { | |
entry: 'app/notifications/order-transaction-notify/handler.ts', | |
handler: 'handler' | |
}, | |
dynamoEventSourceProps: { | |
startingPosition: aws_lambda.StartingPosition.TRIM_HORIZON, | |
batchSize: 10, | |
retryAttempts: 1 | |
} | |
}); | |
// https://docs.amplify.aws/javascript/build-a-backend/graphqlapi/set-up-graphql-api/ | |
new AmplifyAuth(this, 'amplify-auth', { | |
loginWith: { | |
email: true, | |
}, | |
multifactor: { | |
mode: 'OPTIONAL', | |
sms: { | |
smsMessage: (code: string) => `Your verification code is ${code}`, | |
}, | |
totp: false, | |
}, | |
}); | |
} | |
private addLambdaTriggerToDynamo(scope: Construct, id: string, props: LambdaTriggerToDynamoProps) { | |
const orderTransactionNotifyFn = new NodejsFunction(scope, `${id}-fn`, props.functionProps); | |
const dynamoEventSource = new DynamoEventSource(props.table, props.dynamoEventSourceProps); | |
orderTransactionNotifyFn.addEventSource(dynamoEventSource); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment