constructor()

in cdk-stacks/lib/api/chatAPI-stack.ts [23:66]


    constructor(scope: cdk.Construct, id: string, props: ChatAPIStackProps) {
        super(scope, id, props);

        //create startChat Lambda
        const startChatLambda = new nodeLambda.NodejsFunction(this, 'StartChatLambda', {
            functionName: `${props.cdkAppName}-StartChatLambda`,
            runtime: lambda.Runtime.NODEJS_12_X,
            entry: 'lambdas/handlers/ChatAPI/startChat.js',
            timeout: cdk.Duration.seconds(20),
            environment: {
                ConnectInstanceId: props.SSMParams.connectInstanceARN.split('/')[1],
                DDB_TABLE: props.appTable.tableName,
            }
        });
        props.appTable.grantReadWriteData(startChatLambda);

        //Allow chatAPI to invoke Amazon Connect
        startChatLambda.role?.attachInlinePolicy(new iam.Policy(this, 'ConnectChatAPIAccess', {
            statements: [
                new iam.PolicyStatement({
                    effect: iam.Effect.ALLOW,
                    actions: ["connect:StartChatContact"],
                    resources: [props.SSMParams.connectInstanceARN + '/*']
                })
            ]
        }));

        const chatAPI = new apigw2.HttpApi(this, 'ChatAPI', {
            apiName: `${props.cdkAppName}-ChatAPI`,
            corsPreflight: {
                allowOrigins: props.SSMParams.websiteAPIAllowedOrigins.split(',').map((item: string) => item.trim()),
                allowMethods: [apigw2.CorsHttpMethod.POST],
                allowHeaders: apigw.Cors.DEFAULT_HEADERS
            }
        });

        const startChat_Route = new apigw2.HttpRoute(this, 'StartChat_Route', {
            httpApi: chatAPI,
            integration: new apigw2i.HttpLambdaIntegration('startChatLambda', startChatLambda),
            routeKey: apigw2.HttpRouteKey.with('/start', apigw2.HttpMethod.POST)
        });

        this.chatAPI = chatAPI;
    }