hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.74k stars 9.1k forks source link

Better documentation around the "target" parameter for aws_apigatewayv2_route #12972

Closed rayterrill closed 3 years ago

rayterrill commented 4 years ago

Community Note

Description

New or Affected Resource(s)

Potential Terraform Configuration

resource "aws_apigatewayv2_route" "Default" {
  api_id    = "${aws_apigatewayv2_api.API.id}"
  route_key = "$default"
  target = "integrations/${aws_apigatewayv2_integration.Default.id}"
}

References

bflad commented 4 years ago

Hi @rayterrill 👋 While the current argument description is very terse, can you please describe what you are looking for? This will allow others to update the documentation (that lives at website/docs/r/apigatewayv2_route.html.markdown) if they are interested in helping.

rayterrill commented 4 years ago

@bflad I included the code that worked for me above, found that by digging around on the AWS side.

I've always loved the little "breakouts" in the Terraform AWS docs that basically say: see this little block for examples of what can go here.

Happy to try to submit a PR for a documentation update for this - didn't know it was in the same repo. Really appreciate everyone's work on this Terraform module and would love to help out if that would be helpful.

benkraus commented 4 years ago

For context of something I ran into: I first used the target on aws_apigatewayv2_api to auto create the integration, route, etc. However, when I needed to customize a couple things, I had to configure the stage, route and integration myself.

When doing so, I kept hitting this error: Error: error creating API Gateway v2 deployment: BadRequestException: No integration defined for Route with ID xxxxxxx

Turns out I was missing the target in the route here, and finding this issue solved all my problems.

Perhaps a little more documentation as to what the target should be in various scenarios would help?

karl-cardenas-coding commented 4 years ago

I came here to open up this exact issue. The way I uncovered the the required string was using the Chrome debugger network tab and by inspecting the payload when adding an integration.

Alternatively, we could also add an attribute reference for the aws_apigatewayv2_integration that returns the expected string. Maybe something like route_integration_path

okgolove commented 4 years ago

Had the same issue. Wasn't able to get how and what I should specify as target parameter.

tarfeef102 commented 4 years ago

I ended up here after creating a POC using the AWS console in a sandbox and trying to convert that to terraform code. There was nothing obvious that translated from the UI to target (though I won't pretend the AWS console is the pinnacle of UI design).

I took this from the cloudformation docs, this hints at using target to specify an integration, it would be great if syntax and a "what is a target" definition could be put here.

{
    "MyRoute": {
        "Type": "AWS::ApiGatewayV2::Route",
        "DependsOn": [
            "MyIntegration"
        ],
        "Properties": {
            "ApiId": {
                "Ref": "MyApi"
            },
            "RouteKey": "routekey1",
            "AuthorizationType": "NONE",
            "Target": {
                "Fn::Join": [
                    "/",
                    [
                        "integrations",
                        {
                            "Ref": "MyIntegration"
                        }
                    ]
                ]
            }
        }
    }
}
mcalder-ccs commented 3 years ago

I know this thread is about documentation for the target parameter, but I have an issue with the module actually working at all. I used the example format included here (especially the target param), and while I did get what looked like a decent route created, whenever I hit that route using wscat, I got an Internal Server Error. If I used the console and deleted/recreated the integration, it worked fine. Does anyone have empirical evidence that we are able to create routes, connect them to lambda functions, and return the responses using this module?

karl-cardenas-coding commented 3 years ago

@MCalder-j2 It sounds like the lambda integration permissions is what is biting you. When you create the integration through the console then AWS adds the integration permission for you automatically. With terraform you need the aws_lambda_permission resource to the tie the lambda to the API Gateway route. If you enable API Gateway API logging then you can quickly see if it's the IAM Lambda integration that is biting you.

Example

resource "aws_lambda_permission" "lambda-permission" {
  statement_id  = "AllowAPIInvoke-${var.function-name}"
  action        = "lambda:InvokeFunction"
  function_name = aws_lambda_alias.lambda-alias.function_name
  principal     = "apigateway.amazonaws.com"
  qualifier     = aws_lambda_alias.lambda-alias.name

 # Must have the format below
  # The /*/*/* part allows invocation from any stage, method and resource path
  # within API Gateway REST API.
  source_arn = ${module.apigateway-core.main-api-execution-arn}/${module.apigateway-core.main-api-stage-id}/GET/v${var.api-version}/books/{book_id}

}
ghost commented 3 years ago

This has been released in version 3.28.0 of the Terraform AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template for triage. Thanks!

ghost commented 3 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!