Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: What is the uri-parameter for an aws_api_gateway_integration if target is an aws_sfn_state_machine

I want to create a step function API using API gateway using Terraforms api_gateway_integration and sfn_state_machine.

I am at the point where I have to fill the uri-parameter at the api_gateway_integration.

My step function was created, I can reference the id of the step function (something like arn:aws:states:*region*:*account*:stateMachine:*step-function-name*:stateMachine:*step-function-entry-point*).

Can anyone tell me the scheme or an example of how the uri-parameter will have to look, if an AWS step function is the target?

resource "aws_api_gateway_integration" "endpoint_integration" {
  ...
  integration_http_method = "POST"
  type = "AWS"
  uri = <<<<< What to place here???
}
like image 465
Markus Schulte Avatar asked Mar 05 '23 15:03

Markus Schulte


1 Answers

In contrast to Terraform-integrate an AWS lambda with an API gateway, you cannot point from an API gateway to a specific AWS state machine "directly" (using the "uri"-parameter"). Instead, the aws_api_gateway_integration-resource points to the AWS state machine in general, the specific AWS state machine will be referenced by AWS ARN as part of the request. You can use an API gateway request template for mapping from the API gateway to the specific state machine, so that you can omit die stateMachineArn when requesting your API. For detailed explanation have a look at AWS documentation "Creating a Step Functions API Using API Gateway".

Working example

# var.aws_region = eu-central-1
# var.sfn_orchestrater_arn = arn:aws:states:eu-central-1:*account*:stateMachine:*step-function-entry-point*
resource "aws_api_gateway_integration" "endpoint_integration" {
  http_method             = "POST"
  integration_http_method = "POST"
  type                    = "AWS"
  passthrough_behavior    = "NEVER"
  uri                     = "arn:aws:apigateway:${var.aws_region}:states:action/StartExecution"

  request_templates = {
    "application/json" = <<EOF
{
    "input": "$util.escapeJavaScript($input.json('$'))",
    "stateMachineArn": "${var.sfn_orchestrater_arn}"
}
EOF
  }
}
like image 96
Markus Schulte Avatar answered May 05 '23 10:05

Markus Schulte