diff --git a/samcli/hook_packages/terraform/hooks/prepare/property_builder.py b/samcli/hook_packages/terraform/hooks/prepare/property_builder.py index 7cd6e05b27..efb88bedf3 100644 --- a/samcli/hook_packages/terraform/hooks/prepare/property_builder.py +++ b/samcli/hook_packages/terraform/hooks/prepare/property_builder.py @@ -385,7 +385,7 @@ def _add_property(cfn_prop, tf_prop): AWS_API_GATEWAY_V2_API_PROPERTY_BUILDER_MAPPING: PropertyBuilderMapping = { "Name": _get_property_extractor("name"), - "Body": _get_property_extractor("body"), + "Body": _get_json_body, "Target": _get_property_extractor("target"), "ProtocolType": _get_property_extractor("protocol_type"), "RouteKey": _get_property_extractor("route_key"), diff --git a/samcli/hook_packages/terraform/hooks/prepare/resource_linking.py b/samcli/hook_packages/terraform/hooks/prepare/resource_linking.py index cd8dba693a..819728d0c5 100644 --- a/samcli/hook_packages/terraform/hooks/prepare/resource_linking.py +++ b/samcli/hook_packages/terraform/hooks/prepare/resource_linking.py @@ -75,8 +75,8 @@ API_GATEWAY_REST_API_RESOURCE_ADDRESS_PREFIX = "aws_api_gateway_rest_api." API_GATEWAY_RESOURCE_RESOURCE_ADDRESS_PREFIX = "aws_api_gateway_resource." API_GATEWAY_AUTHORIZER_RESOURCE_ADDRESS_PREFIX = "aws_api_gateway_authorizer." -API_GATEWAY_V2_INTEGRATION_RESOURCE_ADDRESS_PREFIX = "aws_apigatewayv2_integration" -API_GATEWAY_V2_API_RESOURCE_ADDRESS_PREFIX = "aws_apigatewayv2_api" +API_GATEWAY_V2_INTEGRATION_RESOURCE_ADDRESS_PREFIX = "aws_apigatewayv2_integration." +API_GATEWAY_V2_API_RESOURCE_ADDRESS_PREFIX = "aws_apigatewayv2_api." TERRAFORM_LOCAL_VARIABLES_ADDRESS_PREFIX = "local." DATA_RESOURCE_ADDRESS_PREFIX = "data." diff --git a/samcli/hook_packages/terraform/hooks/prepare/resources/resource_links.py b/samcli/hook_packages/terraform/hooks/prepare/resources/resource_links.py index 13aae17eb2..721d6c5d3e 100644 --- a/samcli/hook_packages/terraform/hooks/prepare/resources/resource_links.py +++ b/samcli/hook_packages/terraform/hooks/prepare/resources/resource_links.py @@ -156,11 +156,3 @@ linking_func=_link_gateway_resources_to_parents, ), ] - -MULTIPLE_DESTINATIONS_RESOURCE_LINKS: List[LinkingMultipleDestinationsOptionsCaller] = [ - LinkingMultipleDestinationsOptionsCaller( - source=TF_AWS_API_GATEWAY_RESOURCE, - destinations=[TF_AWS_API_GATEWAY_REST_API, TF_AWS_API_GATEWAY_RESOURCE], - linking_func=_link_gateway_resources_to_parents, - ), -] diff --git a/samcli/hook_packages/terraform/hooks/prepare/translate.py b/samcli/hook_packages/terraform/hooks/prepare/translate.py index 46dcb15c97..09169214f1 100644 --- a/samcli/hook_packages/terraform/hooks/prepare/translate.py +++ b/samcli/hook_packages/terraform/hooks/prepare/translate.py @@ -15,6 +15,7 @@ TF_AWS_API_GATEWAY_INTEGRATION_RESPONSE, TF_AWS_API_GATEWAY_METHOD, TF_AWS_API_GATEWAY_REST_API, + TF_AWS_API_GATEWAY_V2_API, ) from samcli.hook_packages.terraform.hooks.prepare.enrich import enrich_resources_and_generate_makefile from samcli.hook_packages.terraform.hooks.prepare.property_builder import ( @@ -68,6 +69,7 @@ TRANSLATION_VALIDATORS: Dict[str, Type[ResourceTranslationValidator]] = { TF_AWS_API_GATEWAY_REST_API: RESTAPITranslationValidator, + TF_AWS_API_GATEWAY_V2_API: RESTAPITranslationValidator, } diff --git a/tests/integration/local/start_api/test_start_api_with_terraform_application.py b/tests/integration/local/start_api/test_start_api_with_terraform_application.py index c5410d4e65..3cfe6a2f8e 100644 --- a/tests/integration/local/start_api/test_start_api_with_terraform_application.py +++ b/tests/integration/local/start_api/test_start_api_with_terraform_application.py @@ -94,18 +94,6 @@ def tearDownClass(cls) -> None: not CI_OVERRIDE, "Skip Terraform test cases unless running in CI", ) -@parameterized_class( - [ - { - "terraform_application": "terraform-v1-nested-apis", - "testing_urls": ["parent/hello", "parent"], - }, - { - "terraform_application": "terraform-v1-api-simple", - "testing_urls": ["hello"], - }, - ] -) @pytest.mark.flaky(reruns=3) @parameterized_class( [ @@ -161,6 +149,21 @@ def test_successful_request(self): "expected_error_message": "Error: AWS SAM CLI could not process a Terraform project that uses local " "variables to define linked resources.", }, + { + "terraform_application": "terraform-v2-api-simple-multi-resource-link", + "expected_error_message": "Error: AWS SAM CLI could not process a Terraform project that contains a source " + "resource that is linked to more than one destination resource.", + }, + { + "terraform_application": "terraform-v2-api-simple-local-resource-link", + "expected_error_message": "Error: AWS SAM CLI could not process a Terraform project that uses local " + "variables to define linked resources.", + }, + { + "terraform_application": "terraform-v2-openapi", + "expected_error_message": "Error: AWS SAM CLI is unable to process a Terraform project that uses an OpenAPI" + " specification to define the API Gateway resource.", + }, ] ) class TestStartApiTerraformApplicationLimitations(TerraformStartApiIntegrationBase): @@ -219,6 +222,26 @@ def test_unsupported_limitations(self): "terraform_application": "terraform-api-simple-local-variables-limitation", "testing_urls": ["hello"], }, + { + "terraform_application": "terraform-v2-api-simple-multi-resource-link", + "testing_urls": ["hello"], + }, + { + "terraform_application": "terraform-v2-api-simple-local-resource-link", + "testing_urls": ["hello"], + }, + { + "terraform_application": "terraform-v2-openapi", + "testing_urls": ["hello"], + }, + { + "terraform_application": "terraform-v2-api-simple", + "testing_urls": ["hello"], + }, + { + "terraform_application": "terraform-v2-api-quick-create", + "testing_urls": ["hello"], + }, ] ) class TestStartApiTerraformApplicationLimitationsAfterApply(TerraformStartApiIntegrationApplyBase): diff --git a/tests/integration/testdata/start_api/terraform/lambda-auth-openapi/main.tf b/tests/integration/testdata/start_api/terraform/lambda-auth-openapi/main.tf index 8005fb4e95..f440840a94 100644 --- a/tests/integration/testdata/start_api/terraform/lambda-auth-openapi/main.tf +++ b/tests/integration/testdata/start_api/terraform/lambda-auth-openapi/main.tf @@ -2,8 +2,14 @@ provider "aws" {} data "aws_region" "current" {} +resource "random_uuid" "unique_id" { + keepers = { + my_key = "my_key" + } +} + resource "aws_api_gateway_authorizer" "header_authorizer" { - name = "header-authorizer-open-api" + name = "header-authorizer-open-api_${random_uuid.unique_id.result}" rest_api_id = aws_api_gateway_rest_api.api.id authorizer_uri = aws_lambda_function.authorizer.invoke_arn authorizer_credentials = aws_iam_role.invocation_role.arn @@ -13,7 +19,7 @@ resource "aws_api_gateway_authorizer" "header_authorizer" { resource "aws_lambda_function" "authorizer" { filename = "lambda-functions.zip" - function_name = "authorizer-open-api" + function_name = "authorizer-open-api_${random_uuid.unique_id.result}" role = aws_iam_role.invocation_role.arn handler = "handlers.auth_handler" runtime = "python3.8" @@ -22,7 +28,7 @@ resource "aws_lambda_function" "authorizer" { resource "aws_lambda_function" "hello_endpoint" { filename = "lambda-functions.zip" - function_name = "hello-lambda-open-api" + function_name = "hello-lambda-open-api_${random_uuid.unique_id.result}" role = aws_iam_role.invocation_role.arn handler = "handlers.hello_handler" runtime = "python3.8" @@ -30,7 +36,7 @@ resource "aws_lambda_function" "hello_endpoint" { } resource "aws_api_gateway_rest_api" "api" { - name = "api-open-api" + name = "api-open-api_${random_uuid.unique_id.result}" body = jsonencode({ swagger = "2.0" info = { @@ -92,7 +98,7 @@ resource "aws_api_gateway_rest_api" "api" { } resource "aws_iam_role" "invocation_role" { - name = "iam-lambda-open-api" + name = "iam-lambda-open-api_${random_uuid.unique_id.result}" path = "/" assume_role_policy = <