How to integrate, by CloudFormation, Api Gateway w

2019-08-23 06:35发布

问题:

I'm creating a CloudFormation template for the platform I'm working on. I need to integrate Api Gateway and Step Functions, to make one of my step functions be executed by a call to a Api Gateway Method.

I'm not finding any documentation on this. I struggled to find the Integration/Uri, that should be

arn:aws:apigateway:${region}:states:action/StartExecution

but now I'm not sure on what to write in my RequestTemplates. I suppose that I could actually leave it empty, to make it act like a proxy, but I would really appreciate if you could give me any further information.

Thank you

回答1:

Obviously I could not leave RequestTemplates empty because it contains the information on what StateMachine is to be called. The URI itself doesn't contain that information, but it just points to the State Machine API's entrypoint.

The correct way comes from this documentation's page.

State Machine APIs expose various methods. The one to execute the Step Function is "StartExecution". To that entrypoint a body formed like this has to be passed

{
"input": "string",
"name": "string",
"stateMachineArn": "string"
}

So, in Cloud Formation:

"Integration": {
    "Type": "AWS",
    "IntegrationHttpMethod": "POST",
    "Uri": {
        "Fn::Join": ["",
            ["arn:aws:apigateway:",
            {
            "Ref": "AWS::Region"
            },
            ":states:action/StartExecution"]]
        },
    "RequestTemplates": {
        "application/json": {
            "Fn::Sub": ["{\"input\": \"$util.escapeJavaScript($input.json('$'))\",\"stateMachineArn\": \"${arn}\"}",
            {
            "arn": {
                "Ref": "[StepMachineResourceName]"
                }
            }]
        }
    }
}