3

I am trying to use Terraform to deploy API Gateway that routes traffic into my domain google.com as an example.

I use aws_apigatewayv2_api for creating HTTP_PROXY type for integrations. I went through the docs but still cannot find the way to attach an integration into a route GET /sitemap.xml. How to deal with this?

resource "aws_api_gateway_deployment" "_" {
  rest_api_id = aws_api_gateway_rest_api._.id
  stage_name  = ""

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_apigatewayv2_api" "_" {
  name          = "example"
  protocol_type = "HTTP"
}

resource "aws_apigatewayv2_route" "apigateway_route" {
  api_id    = aws_apigatewayv2_api._.id
  route_key = "GET /sitemap.xml"
}

resource "aws_apigatewayv2_integration" "apigateway_intergration" {
  api_id           = aws_apigatewayv2_api._.id
  integration_type = "HTTP_PROXY"

  connection_type      = "INTERNET"
  description          = "Gateway intergration for EC2"
  integration_method   = "ANY"
  integration_uri      = "https://www.google.com"
  passthrough_behavior = "WHEN_NO_MATCH"
}

# resource "aws_apigatewayv2_deployment" "apigateway_deployment" {
#   api_id      = aws_apigatewayv2_route.apigateway_route.api_id
#   description = "Example deployment"

#   lifecycle {
#     create_before_destroy = true
#   }
# }

resource "aws_apigatewayv2_stage" "apigateway_stage" {
  api_id = aws_apigatewayv2_api._.id
  name   = "example-stage"
}

UI API Gateway integration

1 Answer 1

4

You are missing several components. Most importantly there is no link between your aws_apigatewayv2_route and aws_apigatewayv2_integration.

The link is established using target argument.

Similarly, there is no link between aws_apigatewayv2_stage and aws_apigatewayv2_deployment.

You can have a look at the following version of the code:


resource "aws_apigatewayv2_deployment" "example" {
  api_id      = aws_apigatewayv2_api._.id
  description = "Example deployment"

  lifecycle {
    create_before_destroy = true
  }
  
  depends_on = [
    aws_apigatewayv2_route.apigateway_route
  ]
}


resource "aws_apigatewayv2_api" "_" {
  name          = "example"
  protocol_type = "HTTP"
}

resource "aws_apigatewayv2_route" "apigateway_route" {
  api_id    = aws_apigatewayv2_api._.id
  route_key = "GET /sitemap.xml"
  
  target = "integrations/${aws_apigatewayv2_integration.apigateway_intergration.id}"
  
}

resource "aws_apigatewayv2_integration" "apigateway_intergration" {
  api_id           = aws_apigatewayv2_api._.id
  integration_type = "HTTP_PROXY"

  connection_type      = "INTERNET"
  description          = "Gateway intergration for EC2"
  integration_method   = "ANY"
  integration_uri      = "https://www.google.com"
  passthrough_behavior = "WHEN_NO_MATCH"
}

resource "aws_apigatewayv2_stage" "apigateway_stage" {
  api_id = aws_apigatewayv2_api._.id
  name   = "example-stage"
  deployment_id = aws_apigatewayv2_deployment.example.id  
}

The above code correctly creates integration:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.