From 1c4a7b60d43a721a0c639c49c81eeedefa0b7ebf Mon Sep 17 00:00:00 2001 From: loganpowell Date: Wed, 25 Oct 2023 07:21:03 -0400 Subject: [PATCH] uses actual args for refs instead of dummies --- examples/module.ts | 51 ++++++++++---------- main.tf.json | 115 ++++++++++++++++++++++++++++++++++++++------- src/config.ts | 7 +-- 3 files changed, 129 insertions(+), 44 deletions(-) diff --git a/examples/module.ts b/examples/module.ts index 1295e8e..4f0fca4 100644 --- a/examples/module.ts +++ b/examples/module.ts @@ -645,11 +645,6 @@ const subdomains = ( invoke_arn: 'lambda_invoke_arn goes here 📌', }, }, - test2: { - 'GET /': { - invoke_arn: 'lambda_invoke_arn goes here 📌', - }, - }, }, }: { apex: string @@ -659,43 +654,44 @@ const subdomains = ( ) => ({ zone: rout53_zone({ apex }), ...Object.entries(subdomainRoutes).reduce( - (a, [subdomain, routes], i) => ({ + (a, [subd, routes], i) => ({ ...a, - [`cert_${i}`]: acm_cert({ apex, subdomain }), - [`validation_${i}`]: acm_validation({ - cert_arn: my?.[`cert_${i}`]?.resource?.acm_certificate?.arn, + [`cert_${subd}`]: acm_cert({ apex, subdomain: subd }), + [`validation_${subd}`]: acm_validation({ + cert_arn: my?.[`cert_${subd}`]?.resource?.acm_certificate?.arn, }), // TODO - [`domain_${i}`]: api_domain({ - subdomain, + [`domain_${subd}`]: api_domain({ + subdomain: subd, apex, - cert_arn: my?.[`cert_${i}`]?.resource?.acm_certificate?.arn, + cert_arn: my?.[`cert_${subd}`]?.resource?.acm_certificate?.arn, }), - [`record_${i}`]: route53_record({ - name: subdomain, + [`record_${subd}`]: route53_record({ + name: subd, api_domain_name: - my?.[`domain_${i}`]?.resource?.apigatewayv2_domain_name + my?.[`domain_${subd}`]?.resource?.apigatewayv2_domain_name ?.domain_name_configuration[0]?.target_domain_name, api_hosted_zone_id: - my?.[`domain_${i}`]?.resource?.apigatewayv2_domain_name + my?.[`domain_${subd}`]?.resource?.apigatewayv2_domain_name ?.domain_name_configuration[0]?.hosted_zone_id, route53_zone_id: my?.zone?.data?.route53_zone?.zone_id, }), - [`gateway_${i}`]: api_gateway({ name: subdomain }), - [`stage_${i}`]: api_stage({ - api_id: my?.[`gateway_${i}`]?.resource?.apigatewayv2_api?.id, + [`gateway_${subd}`]: api_gateway({ name: subd }), + [`stage_${subd}`]: api_stage({ + api_id: my?.[`gateway_${subd}`]?.resource?.apigatewayv2_api?.id, }), ...Object.entries(routes).reduce( (acc, [route, { invoke_arn }], idx) => ({ ...acc, - [`integration_${i}_${idx}`]: api_lambda_integration({ - api_id: my?.[`gateway_${i}`]?.resource?.apigatewayv2_api?.id, + [`integration_${subd}_${route.split(' ')[0]}`]: api_lambda_integration({ + api_id: my?.[`gateway_${subd}`]?.resource?.apigatewayv2_api?.id, lambda_invoke_arn: invoke_arn, }), - [`route_${i}_${idx}`]: api_route({ - api_id: my?.[`gateway_${i}`]?.resource?.apigatewayv2_api?.id, + [`route_${subd}_${route.split(' ')[0]}`]: api_route({ + api_id: my?.[`gateway_${subd}`]?.resource?.apigatewayv2_api?.id, route_key: route, integration_id: - my?.[`integration_${i}_${idx}`]?.resource?.apigatewayv2_integration?.id, + my?.[`integration_${subd}_${route.split(' ')[0]}`]?.resource + ?.apigatewayv2_integration?.id, }), }), {} @@ -715,9 +711,14 @@ const [apiOutput, apiRefs] = moduleAPI({ invoke_arn: functionInvokeArn, }, }, + test2: { + 'GET /': { + invoke_arn: 'lambda_invoke_arn goes here 📌', + }, + }, }, }) -JSON.stringify(apiRefs, null, 4) // +JSON.stringify(apiRefs, null, 4) //? JSON.stringify(apiOutput, null, 4) // // ,e, 888 888 diff --git a/main.tf.json b/main.tf.json index b0ccfa2..bbe631b 100644 --- a/main.tf.json +++ b/main.tf.json @@ -133,7 +133,7 @@ } }, "aws_acm_certificate": { - "subdomains_cert_0": { + "subdomains_cert_test1": { "domain_name": "chopshop-test.net", "validation_method": "DNS", "subject_alternative_names": [ @@ -145,19 +145,35 @@ "lifecycle": { "create_before_destroy": true } + }, + "subdomains_cert_test2": { + "domain_name": "chopshop-test.net", + "validation_method": "DNS", + "subject_alternative_names": [ + "test2.chopshop-test.net" + ], + "tags": { + "BroughtToYouBy": "@-0/micro" + }, + "lifecycle": { + "create_before_destroy": true + } } }, "aws_acm_certificate_validation": { - "subdomains_validation_0": { - "certificate_arn": "${resource.aws_acm_certificate.subdomains_cert_0.arn}" + "subdomains_validation_test1": { + "certificate_arn": "${resource.aws_acm_certificate.subdomains_cert_test1.arn}" + }, + "subdomains_validation_test2": { + "certificate_arn": "${resource.aws_acm_certificate.subdomains_cert_test2.arn}" } }, "aws_apigatewayv2_domain_name": { - "subdomains_domain_0": { + "subdomains_domain_test1": { "domain_name": "test1.chopshop-test.net", "domain_name_configuration": [ { - "certificate_arn": "${resource.aws_acm_certificate.subdomains_cert_0.arn}", + "certificate_arn": "${resource.aws_acm_certificate.subdomains_cert_test1.arn}", "endpoint_type": "REGIONAL", "security_policy": "TLS_1_2" } @@ -166,22 +182,46 @@ "Name": "test1.chopshop-test.net", "BroughtToYouBy": "@-0/micro" } + }, + "subdomains_domain_test2": { + "domain_name": "test2.chopshop-test.net", + "domain_name_configuration": [ + { + "certificate_arn": "${resource.aws_acm_certificate.subdomains_cert_test2.arn}", + "endpoint_type": "REGIONAL", + "security_policy": "TLS_1_2" + } + ], + "tags": { + "Name": "test2.chopshop-test.net", + "BroughtToYouBy": "@-0/micro" + } } }, "aws_route53_record": { - "subdomains_record_0": { + "subdomains_record_test1": { "name": "test1", "zone_id": "${data.aws_route53_zone.subdomains_zone.zone_id}", "type": "A", "alias": { - "name": "${resource.aws_apigatewayv2_domain_name.subdomains_domain_0.domain_name_configuration[0].target_domain_name}", - "zone_id": "${resource.aws_apigatewayv2_domain_name.subdomains_domain_0.domain_name_configuration[0].hosted_zone_id}", + "name": "${resource.aws_apigatewayv2_domain_name.subdomains_domain_test1.domain_name_configuration[0].target_domain_name}", + "zone_id": "${resource.aws_apigatewayv2_domain_name.subdomains_domain_test1.domain_name_configuration[0].hosted_zone_id}", + "evaluate_target_health": false + } + }, + "subdomains_record_test2": { + "name": "test2", + "zone_id": "${data.aws_route53_zone.subdomains_zone.zone_id}", + "type": "A", + "alias": { + "name": "${resource.aws_apigatewayv2_domain_name.subdomains_domain_test2.domain_name_configuration[0].target_domain_name}", + "zone_id": "${resource.aws_apigatewayv2_domain_name.subdomains_domain_test2.domain_name_configuration[0].hosted_zone_id}", "evaluate_target_health": false } } }, "aws_apigatewayv2_api": { - "subdomains_gateway_0": { + "subdomains_gateway_test1": { "name": "test1", "description": "api for test1", "disable_execute_api_endpoint": false, @@ -203,32 +243,75 @@ ], "max_age": 300 } + }, + "subdomains_gateway_test2": { + "name": "test2", + "description": "api for test2", + "disable_execute_api_endpoint": false, + "protocol_type": "HTTP", + "cors_configuration": { + "allow_headers": [ + "content-type", + "x-amz-date", + "authorization", + "x-api-key", + "x-amz-security-token", + "x-amz-user-agent" + ], + "allow_methods": [ + "*" + ], + "allow_origins": [ + "*" + ], + "max_age": 300 + } } }, "aws_apigatewayv2_stage": { - "subdomains_stage_0": { - "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_0.id}", + "subdomains_stage_test1": { + "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_test1.id}", + "name": "$default", + "auto_deploy": true, + "description": "stage $default API" + }, + "subdomains_stage_test2": { + "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_test2.id}", "name": "$default", "auto_deploy": true, "description": "stage $default API" } }, "aws_apigatewayv2_integration": { - "subdomains_integration_0_0": { - "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_0.id}", + "subdomains_integration_test1_ANY": { + "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_test1.id}", "integration_uri": "${resource.aws_lambda_function.ms1_lambda.invoke_arn}", "integration_type": "AWS_PROXY", "integration_method": "POST", "connection_type": "INTERNET", "payload_format_version": "2.0", "timeout_milliseconds": 29000 + }, + "subdomains_integration_test2_GET": { + "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_test2.id}", + "integration_uri": "lambda_invoke_arn goes here 📌", + "integration_type": "AWS_PROXY", + "integration_method": "POST", + "connection_type": "INTERNET", + "payload_format_version": "2.0", + "timeout_milliseconds": 29000 } }, "aws_apigatewayv2_route": { - "subdomains_route_0_0": { - "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_0.id}", + "subdomains_route_test1_ANY": { + "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_test1.id}", "route_key": "ANY /", - "target": "integrations/${resource.aws_apigatewayv2_integration.subdomains_integration_0_0.id}" + "target": "integrations/${resource.aws_apigatewayv2_integration.subdomains_integration_test1_ANY.id}" + }, + "subdomains_route_test2_GET": { + "api_id": "${resource.aws_apigatewayv2_api.subdomains_gateway_test2.id}", + "route_key": "GET /", + "target": "integrations/${resource.aws_apigatewayv2_integration.subdomains_integration_test2_GET.id}" } } }, diff --git a/src/config.ts b/src/config.ts index b6ff299..d71d1d7 100644 --- a/src/config.ts +++ b/src/config.ts @@ -182,11 +182,12 @@ export const modulate = any }>( provider = 'aws' ) => { const [key, fn] = Object.entries(obj)[0] - const defaultArg = parseFirstArgObj(fn) - const ref = { [key]: fn(defaultArg) } - const refs = flattenPreservingPaths(ref, provider, [], {}, true) + + //const defaultArg = parseFirstArgObj(fn) return (...args: [FnParams[0], ...Partial>[]]) => { + const ref = { [key]: fn(...args) } + const refs = flattenPreservingPaths(ref, provider, [], {}, true) // TODO: consider just passing the same arguments to the reference function const obj = { [key]: fn(...args, refs) } const out = flattenPreservingPaths(obj, provider, [], {}, false)