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.78k stars 9.13k forks source link

aws_apigatewayv2_integration for websocket does not allow changing to payload_format_version 2.0 #25280

Open jkirkpatrick opened 2 years ago

jkirkpatrick commented 2 years ago

Community Note

Terraform CLI and Terraform AWS Provider Version

Terraform v1.2.2

Affected Resource(s)

Terraform Configuration Files

resource "aws_apigatewayv2_api" "wsapi" {
  name                       = "websocketapi"
  description                = "my websocket api on apigateway v2"
  protocol_type              = "WEBSOCKET"
  route_selection_expression = "$request.body.action"
  version                    = "1" # our definition of the API version
}

resource "aws_apigatewayv2_route" "wsapi_route_default" {
  api_id    = aws_apigatewayv2_api.wsapi.id
  route_key = "$default"
  target = "integrations/${aws_apigatewayv2_integration.wss_ondefault_integration.id}"

  depends_on = [
    aws_apigatewayv2_integration.wss_ondefault_integration,
  ]    
}

resource "aws_apigatewayv2_deployment" "wsapi_deploy" {
  api_id      = aws_apigatewayv2_api.wsapi.id
  description = "Terraform managed api gateway deployed at ${timestamp()}"

  # Manually trigger redeployment https://www.terraform.io/cli/commands/taint 
  #   '$ terraform taint aws_apigatewayv2_deployment.wsapi_deploy' 
  triggers = {
    redeployment = sha1(join(",", tolist([
      jsonencode(aws_apigatewayv2_route.wsapi_route_default),
      jsonencode(aws_apigatewayv2_integration.wss_ondefault_integration),  
    ])))
  }

  depends_on = [
    aws_apigatewayv2_route.wsapi_route_default,
  ] 

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_apigatewayv2_stage" "wss_stg_default" {
  api_id = aws_apigatewayv2_api.wsapi.id
  name   = "$default"
  description = "default stage"
  deployment_id = aws_apigatewayv2_deployment.wsapi_deploy.id

  default_route_settings {
    data_trace_enabled = true
    logging_level = "INFO"
    detailed_metrics_enabled = true
    throttling_burst_limit = 10
    throttling_rate_limit = 10
  }
}

resource "aws_apigatewayv2_integration" "wss_ondefault_integration" {
  api_id           = aws_apigatewayv2_api.wsapi.id
  integration_type = "AWS_PROXY"

  connection_type           = "INTERNET"
  content_handling_strategy = "CONVERT_TO_TEXT"
  description               = "Lambda websocket default integration"
  integration_method        = "POST"
  integration_uri           = var.ondefault_invoke_arn
  passthrough_behavior      = "WHEN_NO_MATCH"

  payload_format_version = "2.0" # <== PROBLEM OCCURS when this line is added - but works when value is "1.0"
}

Debug Output

│ Error: error updating API Gateway v2 integration: BadRequestException: Unsupported PayloadFormatVersion: 2.0

Panic Output

Expected Behavior

Payload format version should be 2.0 - not default 1.0

Actual Behavior

Console error; integration payload format version is not changed

Steps to Reproduce

  1. Deploy a lambda (should return statusCode 200 for websocket support)
  2. Assign the ARN of the deployed lambda to ingetration_uri (var.ondefault_invoke_arn in the example code)
  3. terraform apply && terraform deploy
  4. Uncomment / change aws_apigatewayv2_integration.wss_ondefault_integration.payload_format_version from "1.0" to "2.0" after initial deployment
  5. terraform apply && terraform deploy

Important Factoids

References

Documentation indicates support for payload_format_version 1.0 or 2.0

justinretzolk commented 2 years ago

Hey @jkirkpatrick 👋 Thank you for taking the time to raise this! It looks like the provider supports setting payload_format_version to 2.0, so I suspect the error message that you're receiving is coming from the AWS API response. So that we have all of the necessary information in order to look into this, can you supply (redacted as needed) debug logs as well?

github-actions[bot] commented 4 months ago

Marking this issue as stale due to inactivity. This helps our maintainers find and focus on the active issues. If this issue receives no comments in the next 30 days it will automatically be closed. Maintainers can also remove the stale label.

If this issue was automatically closed and you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thank you!

Ugikie commented 4 months ago

Hey sorry to revive this old thread but was a solution ever found for this? I'm seeing the same thing with the exact same setup... does AWS just not support payload v2.0 for websocket apis?