|
| 1 | +import { dedent } from 'ts-dedent'; |
| 2 | + |
| 3 | +import { AwsOptions } from '@/generators/addons/aws'; |
| 4 | +import { |
| 5 | + isAwsModuleAdded, |
| 6 | + requireAwsModules, |
| 7 | +} from '@/generators/addons/aws/dependencies'; |
| 8 | +import { |
| 9 | + INFRA_CORE_MAIN_PATH, |
| 10 | + INFRA_CORE_VARIABLES_PATH, |
| 11 | + INFRA_CORE_OUTPUTS_PATH, |
| 12 | +} from '@/generators/terraform/constants'; |
| 13 | +import { appendToFile, copy } from '@/helpers/file'; |
| 14 | + |
| 15 | +import { AWS_TEMPLATE_PATH } from '../constants'; |
| 16 | + |
| 17 | +const vpcFlowLogVariablesContent = dedent` |
| 18 | + variable "vpc_flow_log_s3_bucket_name" { |
| 19 | + description = "The name of the S3 bucket to store VPC Flow Logs" |
| 20 | + type = string |
| 21 | + } |
| 22 | +
|
| 23 | + variable "vpc_flow_log_retention_days" { |
| 24 | + description = "The number of days to retain VPC Flow Logs in S3" |
| 25 | + type = number |
| 26 | + default = 90 |
| 27 | + }`; |
| 28 | + |
| 29 | +const vpcFlowLogModuleContent = dedent` |
| 30 | + module "vpc_flow_log" { |
| 31 | + source = "../modules/vpc_flow_log" |
| 32 | +
|
| 33 | + env_namespace = local.env_namespace |
| 34 | + vpc_id = module.vpc.vpc_id |
| 35 | + s3_bucket_name = var.vpc_flow_log_s3_bucket_name |
| 36 | + log_retention_days = var.vpc_flow_log_retention_days |
| 37 | + }`; |
| 38 | + |
| 39 | +const vpcFlowLogOutputsContent = dedent` |
| 40 | + output "vpc_flow_log_s3_bucket_id" { |
| 41 | + description = "The ID of the S3 bucket to store VPC Flow Logs" |
| 42 | + value = module.vpc_flow_log.s3_bucket_id |
| 43 | + } |
| 44 | +
|
| 45 | + output "vpc_flow_log_s3_bucket_name" { |
| 46 | + description = "The name of the S3 bucket to store VPC Flow Logs" |
| 47 | + value = module.vpc_flow_log.s3_bucket_name |
| 48 | + } |
| 49 | +
|
| 50 | + output "vpc_flow_log_arn" { |
| 51 | + description = "The ARN of the VPC Flow Log" |
| 52 | + value = module.vpc_flow_log.vpc_flow_log_arn |
| 53 | + } |
| 54 | +
|
| 55 | + output "vpc_flow_log_athena_table_name" { |
| 56 | + description = "The name of the Athena table for VPC Flow Logs" |
| 57 | + value = module.vpc_flow_log.vpc_flow_log_athena_table_name |
| 58 | + } |
| 59 | +
|
| 60 | + output "vpc_flow_log_athena_table_arn" { |
| 61 | + description = "The arn of the Athena table for VPC Flow Logs" |
| 62 | + value = module.vpc_flow_log.vpc_flow_log_athena_table_arn |
| 63 | + }`; |
| 64 | + |
| 65 | +const applyAwsVpcFlowLog = async (options: AwsOptions) => { |
| 66 | + if (isAwsModuleAdded('vpcFlowLog', options.projectName)) { |
| 67 | + return; |
| 68 | + } |
| 69 | + await requireAwsModules('vpcFlowLog', 'vpc', options); |
| 70 | + |
| 71 | + copy( |
| 72 | + `${AWS_TEMPLATE_PATH}/modules/vpc_flow_log`, |
| 73 | + 'modules/vpc_flow_log', |
| 74 | + options.projectName |
| 75 | + ); |
| 76 | + appendToFile( |
| 77 | + INFRA_CORE_VARIABLES_PATH, |
| 78 | + vpcFlowLogVariablesContent, |
| 79 | + options.projectName |
| 80 | + ); |
| 81 | + appendToFile( |
| 82 | + INFRA_CORE_MAIN_PATH, |
| 83 | + vpcFlowLogModuleContent, |
| 84 | + options.projectName |
| 85 | + ); |
| 86 | + appendToFile( |
| 87 | + INFRA_CORE_OUTPUTS_PATH, |
| 88 | + vpcFlowLogOutputsContent, |
| 89 | + options.projectName |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +export default applyAwsVpcFlowLog; |
| 94 | +export { |
| 95 | + vpcFlowLogVariablesContent, |
| 96 | + vpcFlowLogModuleContent, |
| 97 | + vpcFlowLogOutputsContent, |
| 98 | +}; |
0 commit comments