Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Added limitation test cases for V2 resources #5708

Merged
merged 11 commits into from
Aug 5, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,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"),
Expand Down
2 changes: 2 additions & 0 deletions samcli/hook_packages/terraform/hooks/prepare/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
TF_AWS_API_GATEWAY_INTEGRATION_RESPONSE,
TF_AWS_API_GATEWAY_METHOD,
TF_AWS_API_GATEWAY_REST_API,
TF_AWS_API_GATEWAY_V2_API,
PropertyBuilderMapping,
)
from samcli.hook_packages.terraform.hooks.prepare.resource_linking import (
Expand Down Expand Up @@ -65,6 +66,7 @@

TRANSLATION_VALIDATORS: Dict[str, Type[ResourceTranslationValidator]] = {
TF_AWS_API_GATEWAY_REST_API: RESTAPITranslationValidator,
TF_AWS_API_GATEWAY_V2_API: RESTAPITranslationValidator,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,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):
Expand Down Expand Up @@ -189,6 +204,15 @@ def test_unsupported_limitations(self):
{
"terraform_application": "terraform-api-simple-local-variables-limitation",
},
{
"terraform_application": "terraform-v2-api-simple-multi-resource-link",
},
{
"terraform_application": "terraform-v2-api-simple-local-resource-link",
},
{
"terraform_application": "terraform-v2-openapi",
},
]
)
class TestStartApiTerraformApplicationLimitationsAfterApply(TerraformStartApiIntegrationApplyBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -22,15 +28,15 @@ 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"
source_code_hash = filebase64sha256("lambda-functions.zip")
}

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 = {
Expand Down Expand Up @@ -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 = <<EOF
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
provider "aws" {
}

resource "random_uuid" "unique_id" {
keepers = {
my_key = "my_key"
}
}

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
name = "iam_for_lambda_${random_uuid.unique_id.result}"

assume_role_policy = <<EOF
{
Expand All @@ -22,27 +28,27 @@ EOF
}

resource "aws_s3_bucket" "lambda_code_bucket" {
bucket = "lambda_code_bucket"
bucket = "lambda_code_bucket_${random_uuid.unique_id.result}"
}

resource "aws_s3_object" "s3_lambda_code" {
bucket = "lambda_code_bucket"
bucket = "lambda_code_bucke_${random_uuid.unique_id.result}t"
key = "s3_lambda_code_key"
source = "HelloWorldFunction.zip"
}

resource "aws_lambda_function" "HelloWorldFunction" {
s3_bucket = "lambda_code_bucket"
s3_bucket = "lambda_code_bucket_${random_uuid.unique_id.result}"
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction"
function_name = "HelloWorldFunction_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
}

resource "aws_apigatewayv2_api" "quick_create_api" {
name = "quick_create_api"
name = "quick_create_api_${random_uuid.unique_id.result}"
protocol_type = "HTTP"
target = aws_lambda_function.HelloWorldFunction.invoke_arn
route_key = "GET /hello"
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
provider "aws" {
}

locals {
api_function = aws_lambda_function.HelloWorldFunction.invoke_arn
}

resource "random_uuid" "unique_id" {
keepers = {
my_key = "my_key"
}
}

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda_${random_uuid.unique_id.result}"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_s3_bucket" "lambda_code_bucket" {
bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
}

resource "aws_s3_object" "s3_lambda_code" {
bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
key = "s3_lambda_code_key"
source = "HelloWorldFunction.zip"
depends_on = [aws_s3_bucket.lambda_code_bucket]
}

resource "aws_lambda_function" "HelloWorldFunction" {
s3_bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
depends_on = [aws_s3_bucket.lambda_code_bucket]
}

resource "aws_apigatewayv2_api" "my_api" {
name = "my_api_${random_uuid.unique_id.result}"
protocol_type = "HTTP"
}

resource "aws_apigatewayv2_route" "example" {
api_id = aws_apigatewayv2_api.my_api.id
target = "integrations/${aws_apigatewayv2_integration.example.id}"
route_key = "GET /hello"
operation_name = "my_operation"
depends_on = [aws_apigatewayv2_integration.example]
}

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

resource "aws_apigatewayv2_integration" "example" {
api_id = aws_apigatewayv2_api.my_api.id
integration_type = "AWS_PROXY"
integration_method = "POST"
integration_uri = local.api_function
payload_format_version = "2.0"
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
provider "aws" {
}

variable "stage" {
type = string
description = "stage"
default = "beta"
}

resource "random_uuid" "unique_id" {
keepers = {
my_key = "my_key"
}
}

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda_${random_uuid.unique_id.result}"

assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}

resource "aws_s3_bucket" "lambda_code_bucket" {
bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
}

resource "aws_s3_object" "s3_lambda_code" {
bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
key = "s3_lambda_code_key"
source = "HelloWorldFunction.zip"
depends_on = [aws_s3_bucket.lambda_code_bucket]
}

resource "aws_lambda_function" "HelloWorldFunction" {
s3_bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
depends_on = [aws_s3_object.s3_lambda_code]
}

resource "aws_lambda_function" "HelloWorldFunction2" {
s3_bucket = "lambda-code-bucket-${random_uuid.unique_id.result}"
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction2_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
depends_on = [aws_s3_object.s3_lambda_code]
}

resource "aws_apigatewayv2_api" "my_api" {
name = "my_api_${random_uuid.unique_id.result}"
protocol_type = "HTTP"
}

resource "aws_apigatewayv2_route" "example" {
api_id = aws_apigatewayv2_api.my_api.id
target = "integrations/${aws_apigatewayv2_integration.example.id}"
route_key = "GET /hello"
operation_name = "my_operation"
depends_on = [aws_apigatewayv2_integration.example]
}

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

resource "aws_apigatewayv2_integration" "example" {
api_id = aws_apigatewayv2_api.my_api.id
integration_type = "AWS_PROXY"
integration_method = "POST"
integration_uri = var.stage == "beta" ? aws_lambda_function.HelloWorldFunction.invoke_arn : aws_lambda_function.HelloWorldFunction2.invoke_arn
payload_format_version = "2.0"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
provider "aws" {
}

resource "random_uuid" "unique_id" {
keepers = {
my_key = "my_key"
}
}

resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
name = "iam_for_lambda_${random_uuid.unique_id.result}"

assume_role_policy = <<EOF
{
Expand All @@ -22,27 +28,27 @@ EOF
}

resource "aws_s3_bucket" "lambda_code_bucket" {
bucket = "lambda_code_bucket"
bucket = "lambda_code_bucket_${random_uuid.unique_id.result}"
}

resource "aws_s3_object" "s3_lambda_code" {
bucket = "lambda_code_bucket"
bucket = "lambda_code_bucket_${random_uuid.unique_id.result}"
key = "s3_lambda_code_key"
source = "HelloWorldFunction.zip"
}

resource "aws_lambda_function" "HelloWorldFunction" {
s3_bucket = "lambda_code_bucket"
s3_bucket = "lambda_code_bucket_${random_uuid.unique_id.result}"
s3_key = "s3_lambda_code_key"
handler = "app.lambda_handler"
runtime = "python3.8"
function_name = "HelloWorldFunction"
function_name = "HelloWorldFunction_${random_uuid.unique_id.result}"
timeout = 500
role = aws_iam_role.iam_for_lambda.arn
}

resource "aws_apigatewayv2_api" "my_api" {
name = "my_api"
name = "my_api_${random_uuid.unique_id.result}"
protocol_type = "HTTP"
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for resource "aws_apigatewayv2_route" "example" .. I think the target property value should be in format "integrations/${<integration id>}". If it is working using this format when we run terraform apply, please add another example to use the previous format as explained in the doc

Expand Down
Binary file not shown.
Loading
Loading