Search code examples
aws-api-gatewayaws-step-functionsaws-cdk

How to integrate Api Gateway and Step Fucnctions in CDK?


I have a statemachine.

        const task1 = new sfn.Task(this, 'Assign Case', {
            task: new tasks.InvokeFunction(Lambda1),
        });

        const task2 = new sfn.Task(this, 'Close Case', {
            task: new tasks.InvokeFunction(Lambda2),
        });

        const chain = sfn.Chain.start(task1)
            .next(task2);

      const StateMachine = new sfn.StateMachine(this, `StateMachine`, {
            definition: chain
        });

And I need to call this statemachine from Api-gateway resource.I have used the below code and it throws an error like 'statemacine is not assignable to paramaeter of type AwsIntegrationProps'

 const resource = this.api.root.addResource(path);
 resource.addMethod(method, new apigw.AwsIntegration(handler), { apiKeyRequired: true }); 
 //handler is above statemachine

My api gateway integration request looks like this in console.

enter image description here


Solution

  • You should use apigateway.LambdaIntegration which extends AwsIntegration.

    export declare class LambdaIntegration extends AwsIntegration {
        private readonly handler;
        private readonly enableTest;
        constructor(handler: lambda.IFunction, options?: LambdaIntegrationOptions);
        bind(method: Method): void;
    }
    

    For example :

    const getBookIntegration = new apigateway.LambdaIntegration(getBookHandler);
    
    

    Later, use the lambdaIntegration when creating a new method:

    book.addMethod('GET', getBookIntegration);
    

    More about LambdaIntegration.