Skip to content

Commit

Permalink
fixes hashicorp/terraform#25717, as well as , and
Browse files Browse the repository at this point in the history
  • Loading branch information
loganpowell committed Oct 26, 2023
1 parent 4d98eea commit 74976bc
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 130 deletions.
79 changes: 44 additions & 35 deletions examples/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { modulate, config, Provider, Terraform } from '../src/config'
import { AWS05200 as AWS } from '../registry/index'

import { rout53_zone, acm_cert, route53_record, acm_validation } from './route53'
import { lambda_invoke_cred } from './lambda'
import { acm_cert, route53_record, acm_validation } from './route53'

// ,e,
// /~~~8e 888-~88e "
Expand All @@ -10,10 +10,10 @@ import { rout53_zone, acm_cert, route53_record, acm_validation } from './route53
// C888 888 888 888P 888
// "88_-888 888-_88" 888
// 888
const api_domain = ({ subdomain = 'api', apex = 'chopshop-test.net', cert_arn }): AWS => ({
const api_domain = ({ full_domain, cert_arn }): AWS => ({
resource: {
apigatewayv2_domain_name: {
domain_name: `${subdomain}.${apex}`,
domain_name: full_domain,
/**
* Block type "domain_name_configuration" is represented by a list
* of objects, so it must be indexed using a numeric key, like
Expand All @@ -30,18 +30,18 @@ const api_domain = ({ subdomain = 'api', apex = 'chopshop-test.net', cert_arn })
},
],
tags: {
Name: `${subdomain}.${apex}`,
Name: full_domain,
BroughtToYouBy: '@-0/micro',
},
},
},
})

const api_gateway = ({ name }): AWS => ({
const api_gateway = ({ full_domain }): AWS => ({
resource: {
apigatewayv2_api: {
name,
description: `api for ${name}`,
name: full_domain,
description: `api for ${full_domain}`,
disable_execute_api_endpoint: false,
protocol_type: 'HTTP',
cors_configuration: {
Expand Down Expand Up @@ -111,6 +111,7 @@ interface Subdomains {
[key: string]: {
[key: string]: {
invoke_arn: string
function_name: string
}
}
}
Expand Down Expand Up @@ -139,37 +140,38 @@ interface Subdomains {
export const subdomains = (
{
apex = 'chopshop-test.net',
zone_id,
subdomainRoutes = {
test: {
'ANY /': {
invoke_arn: 'lambda_invoke_arn goes here 📌',
function_name: 'lambda function name goes here 📌',
},
},
},
}: {
apex: string
zone_id: string
subdomainRoutes: Subdomains
},
my: { [key: string]: AWS }
) => ({
zone: rout53_zone({ apex }), // 📌 outside module scope?
...Object.entries(subdomainRoutes).reduce(
) =>
Object.entries(subdomainRoutes).reduce(
(a, [sd, routes]) => ({
...a,
[`cert_${sd}`]: acm_cert({ apex, subdomain: sd }),
[`cert_${sd}`]: acm_cert({ full_domain: `${sd}.${apex}` }),
[`domain_${sd}`]: api_domain({
subdomain: sd,
apex,
full_domain: `${sd}.${apex}`,
cert_arn:
my?.[`validation_${sd}`]?.resource?.acm_certificate_validation?.certificate_arn,
}),
[`record_${sd}`]: route53_record({
route53_zone_id: my?.zone?.data?.route53_zone?.zone_id,
name: sd,
records: [
my?.[`cert_${sd}`]?.resource?.acm_certificate?.domain_validation_options[0]
?.resource_record_value,
],
route53_zone_id: zone_id,
full_domain: `${sd}.${apex}`,
//records: [
// my?.[`cert_${sd}`]?.resource?.acm_certificate?.domain_validation_options[0]
// ?.resource_record_value,
//],
api_domain_name:
my?.[`domain_${sd}`]?.resource?.apigatewayv2_domain_name
?.domain_name_configuration[0]?.target_domain_name,
Expand All @@ -178,42 +180,49 @@ export const subdomains = (
?.domain_name_configuration[0]?.hosted_zone_id,
}),
[`record_valid_${sd}`]: route53_record({
route53_zone_id: my?.zone?.data?.route53_zone?.zone_id,
route53_zone_id: zone_id,
records: [
my?.[`cert_${sd}`]?.resource?.acm_certificate?.domain_validation_options[0]
?.resource_record_value,
],
name: my?.[`cert_${sd}`]?.resource?.acm_certificate?.domain_validation_options[0]
?.resource_record_name,
full_domain:
my?.[`cert_${sd}`]?.resource?.acm_certificate?.domain_validation_options[0]
?.resource_record_name,
type: my?.[`cert_${sd}`]?.resource?.acm_certificate?.domain_validation_options[0]
?.resource_record_type,
}),
[`validation_${sd}`]: acm_validation({
cert_arn: my?.[`cert_${sd}`]?.resource?.acm_certificate?.arn,
fqdns: [my?.[`record_valid_${sd}`]?.resource?.route53_record?.fqdn],
}), // TODO
[`gateway_${sd}`]: api_gateway({ name: sd }),
[`gateway_${sd}`]: api_gateway({ full_domain: `${sd}.${apex}` }),
[`stage_${sd}`]: api_stage({
api_id: my?.[`gateway_${sd}`]?.resource?.apigatewayv2_api?.id,
}),
...Object.entries(routes).reduce(
(acc, [route, { invoke_arn }]) => ({
...Object.entries(routes).reduce((acc, [route, { invoke_arn, function_name }]) => {
const method = route.split(' ')[0]
return {
...acc,
[`integration_${sd}_${route.split(' ')[0]}`]: api_lambda_integration({
[`invoker_${sd}_${method}`]: lambda_invoke_cred({
function_name: function_name,
source_arn:
my?.[`gateway_${sd}`]?.resource?.apigatewayv2_api?.execution_arn,
principal: 'apigateway.amazonaws.com',
statement_id: 'AllowExecutionFromAPIGateway',
}),
[`integration_${sd}_${method}`]: api_lambda_integration({
api_id: my?.[`gateway_${sd}`]?.resource?.apigatewayv2_api?.id,
lambda_invoke_arn: invoke_arn,
}),
[`route_${sd}_${route.split(' ')[0]}`]: api_route({
[`route_${sd}_${method}`]: api_route({
api_id: my?.[`gateway_${sd}`]?.resource?.apigatewayv2_api?.id,
route_key: route,
integration_id:
my?.[`integration_${sd}_${route.split(' ')[0]}`]?.resource
?.apigatewayv2_integration?.id,
my?.[`integration_${sd}_${method}`]?.resource?.apigatewayv2_integration
?.id,
}),
}),
{}
),
}
}, {}),
}),
{}
),
})
)
Empty file added examples/cloudwatch.ts
Empty file.
81 changes: 42 additions & 39 deletions examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,44 @@ import { modulate, config, Provider, Terraform } from '../src/config'
import { AWS05200 as AWS } from '../registry/index'
import { lambda } from './lambda'
import { subdomains } from './api'

const provider: Provider = {
aws: {
region: 'us-east-2',
profile: 'chopshop',
},
}

const terraform: Terraform = {
required_providers: {
aws: {
source: 'hashicorp/aws',
version: '5.20.0',
},
},
}
import { route53_zone } from './route53'

/**
*
* my?.zone?.data?.route53_zone?.zone_id
*/

const module = modulate({ ms1: lambda })
const route53zone = ({ apex }) => ({
zone: route53_zone({ apex }),
})

const zoneMod = modulate({ zone: route53zone })
const lambdaMod = modulate({ ms1: lambda })

const [mod_lambda, out_lambda] = module({ name: 'throwaway-test-123' })
const apex = 'chopshop-test.net'

const [mod_zone, out_zone] = zoneMod({ apex })
const [mod_lambda, out_lambda] = lambdaMod({
name: 'throwaway-test-123',
file_path: '${path.root}/lambdas/template/zipped/handler.py.zip',
handler: 'handler.handler',
filter: { type: ['type1', 'type2'] },
})

const zone_id = out_zone?.zone?.data?.route53_zone?.zone_id
const functionInvokeArn = out_lambda?.lambda?.resource?.lambda_function?.invoke_arn
const functionName = out_lambda?.lambda?.resource?.lambda_function?.function_name

const moduleAPI = modulate({ subdomains })

const [mod_api, out_api] = moduleAPI({
apex: 'chopshop-test.net',
apex,
zone_id,
subdomainRoutes: {
test1: {
'ANY /': {
invoke_arn: functionInvokeArn,
function_name: functionName,
},
},
},
Expand All @@ -51,12 +53,27 @@ const [mod_api, out_api] = moduleAPI({
// "88__/ "88_-~ 888 888 888 888-_88" 888 888 "88___/ "88_/888
// 888

JSON.stringify(out_api, null, 4) //
JSON.stringify(mod_api, null, 4) //
const provider: Provider = {
aws: {
region: 'us-east-2',
profile: 'chopshop',
},
}

const terraform: Terraform = {
required_providers: {
aws: {
source: 'hashicorp/aws',
version: '5.20.0',
},
},
}

const compiler = config(provider, terraform, 'main.tf.json')
const compiled = compiler(mod_lambda, mod_api)
const compile = config(provider, terraform, 'main.tf.json')
const compiled = compile(mod_zone, mod_lambda, mod_api)

//JSON.stringify(out_api, null, 4) //
//JSON.stringify(mod_api, null, 4) //
JSON.stringify(compiled, null, 4) //?

/**
Expand All @@ -75,28 +92,14 @@ JSON.stringify(compiled, null, 4) //?
// 888 `88_-~ 888_-~ `88_-~

// - add ability to add tags at the module level
// - edify S3 bucket permissions
// - missing tick_groups - (top three) in route53_record
// - EFSAccessPoint - missing `file_system_arn` (not in docs)
// - resource: { lambda_function: { file_system_config
// - topic: sns_topic(name), // 📌 outside module scope?
// - topic: sns_topic(name), // 📌 outside module scope? TURN INTO INPUT TO LAMBDA
// - apigatewayv2_route 🐛 [2] `request_parameter_key` and `required` bug in docs (nested under section without heading)
// - apigatewayv2_integration: 🐛 [3] `status_code` and `mappings` bug in docs (nested under section without heading)

/*
================================================================================
Error: 1 error occurred:
* missing test1.chopshop-test.net DNS validation record:
_1c744958449a294d63143074447592fa.test1.chopshop-test.net
with aws_acm_certificate_validation.subdomains_validation_test1, on main.tf.json
line 198, in
resource.aws_acm_certificate_validation.subdomains_validation_test1: 198:
}
================================================================================
*/

/**
* Outline of microservice module:
Expand Down
Empty file added examples/iam.ts
Empty file.
10 changes: 8 additions & 2 deletions examples/lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const lambda_policy = ({ name, policy_json }): AWS => ({
},
})

const lambda_invoke_cred = ({
export const lambda_invoke_cred = ({
function_name,
source_arn,
principal = 'sns.amazonaws.com',
Expand Down Expand Up @@ -310,8 +310,14 @@ export const lambda = (
name = 'microservice',
file_path = '${path.root}/lambdas/template/zipped/handler.py.zip',
handler = 'handler.handler',
filter = { type: ['type1', 'type2'] },
env_vars = {},
filter, // { type: ['type1', 'type2'] },
}: {
name: string
file_path: string
handler: string
env_vars?: object
filter?: object
},
my: { [key: string]: AWS }
) => ({
Expand Down
Loading

0 comments on commit 74976bc

Please sign in to comment.