Skip to content

Instantly share code, notes, and snippets.

@m-arrieta-r
Created January 6, 2024 17:08
Show Gist options
  • Save m-arrieta-r/f22b06dc8398babfa1f04c61daf36b0b to your computer and use it in GitHub Desktop.
Save m-arrieta-r/f22b06dc8398babfa1f04c61daf36b0b to your computer and use it in GitHub Desktop.
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