James_RajKumar
James_RajKumar

Reputation: 231

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

Upvotes: 2

Views: 1772

Answers (3)

Avi Cohen
Avi Cohen

Reputation: 3414

check here: api to state

and here:

state machine

Upvotes: 1

kylevoyto
kylevoyto

Reputation: 489

The error 'statemacine is not assignable to paramaeter of type AwsIntegrationProps' is referring to you instantiation.

The AwsIntegration class takes an AwsIntegrationProps struct as input.

new AwsIntegration(props: AwsIntegrationProps)

Getting API Gateway to kickoff Step Functions directly is a little strange. I found this Creating a Step Functions API Using API Gateway tutorial helpful. The State Machine ARN is passed in the request body of the call, so you need to pass a request template if you don't want to require the user to specify the State Machine.

resource.addMethod(
    method,
    new apigw.AwsIntegration({
        handler: 'states',
        action: 'StartExecution',
        options: {
            requestTemplates: {
                'application/json': `{
                    "stateMachineArn": "${handler.ref}",
                    "input": "$util.escapeJavaScript($input.body)"
                }`
            },
        },
    }),
    { apiKeyRequired: true }
);

(Note: I'm translating my code from Python so I'm not 100% on the strings in TypeScript.)

I also filled in credentialsRole, passthroughBehavior, and integrationResponses to the options to get mine setup the way I wanted.

Upvotes: 1

Amit Baranes
Amit Baranes

Reputation: 8122

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.

Upvotes: 1

Related Questions