|
| 1 | +import { dedent } from 'ts-dedent'; |
| 2 | + |
| 3 | +import { AwsOptions } from '@/generators/addons/aws'; |
| 4 | +import { isAwsModuleAdded } from '@/generators/addons/aws/dependencies'; |
| 5 | +import { |
| 6 | + INFRA_CORE_V5_MAIN_PATH, |
| 7 | + INFRA_CORE_V5_VARIABLES_PATH, |
| 8 | + INFRA_CORE_V5_OUTPUTS_PATH, |
| 9 | +} from '@/generators/terraform/constants'; |
| 10 | +import { appendToFile, copy } from '@/helpers/file'; |
| 11 | + |
| 12 | +import { AWS_TEMPLATE_PATH } from '../constants'; |
| 13 | + |
| 14 | +const cloudtrailVariablesContent = dedent` |
| 15 | + variable "cloudtrail_trail_name" { |
| 16 | + description = "Name of the CloudTrail trail" |
| 17 | + type = string |
| 18 | + } |
| 19 | +
|
| 20 | + variable "cloudtrail_s3_bucket_name" { |
| 21 | + description = "Name for the S3 bucket to store CloudTrail logs" |
| 22 | + type = string |
| 23 | + } |
| 24 | +
|
| 25 | + variable "cloudtrail_s3_key_prefix" { |
| 26 | + description = "S3 key prefix for the CloudTrail logs" |
| 27 | + type = string |
| 28 | + default = "cloudtrail" |
| 29 | + } |
| 30 | +
|
| 31 | + variable "cloudtrail_event_type" { |
| 32 | + description = "Type of events that the trail will log. Valid values are 'All', 'Management', 'Data' and 'Insight'" |
| 33 | + type = string |
| 34 | + default = "All" |
| 35 | + } |
| 36 | +
|
| 37 | + variable "cloudtrail_log_retention_days" { |
| 38 | + description = "The number of days to retain CloudTrail logs in CloudWatch" |
| 39 | + type = number |
| 40 | + default = 365 |
| 41 | + } |
| 42 | +
|
| 43 | + variable "cloudtrail_sns_topic_name" { |
| 44 | + description = "Name for the SNS topic to send CloudTrail notifications" |
| 45 | + type = string |
| 46 | + } |
| 47 | +
|
| 48 | + variable "cloudtrail_enable_alerts" { |
| 49 | + description = "Enable CloudTrail alerts and monitoring" |
| 50 | + type = bool |
| 51 | + default = false |
| 52 | + } |
| 53 | +
|
| 54 | + variable "cloudtrail_slack_channel_id" { |
| 55 | + description = "Slack channel ID (starts with C)" |
| 56 | + type = string |
| 57 | + } |
| 58 | +
|
| 59 | + variable "cloudtrail_slack_team_id" { |
| 60 | + description = "Slack workspace/team ID (starts with T)" |
| 61 | + type = string |
| 62 | + }`; |
| 63 | + |
| 64 | +const cloudtrailModuleContent = dedent` |
| 65 | + module "cloudtrail_s3_bucket" { |
| 66 | + source = "../modules-v5/cloudtrail/s3-bucket-cloudtrail" |
| 67 | +
|
| 68 | + env_namespace = local.env_namespace |
| 69 | + s3_bucket_name = var.cloudtrail_s3_bucket_name |
| 70 | + log_retention_days = var.cloudtrail_log_retention_days |
| 71 | + trail_names = [var.cloudtrail_trail_name] |
| 72 | + s3_key_prefixes = [var.cloudtrail_s3_key_prefix] |
| 73 | + } |
| 74 | +
|
| 75 | + module "cloudtrail_sns" { |
| 76 | + source = "../modules-v5/cloudtrail/sns" |
| 77 | +
|
| 78 | + env_namespace = local.env_namespace |
| 79 | + sns_name = var.cloudtrail_sns_topic_name |
| 80 | + trail_names = [var.cloudtrail_trail_name] |
| 81 | + } |
| 82 | +
|
| 83 | + module "cloudtrail" { |
| 84 | + depends_on = [module.cloudtrail_s3_bucket, module.cloudtrail_sns] |
| 85 | + source = "../modules-v5/cloudtrail" |
| 86 | +
|
| 87 | + env_namespace = local.env_namespace |
| 88 | + trail_name = var.cloudtrail_trail_name |
| 89 | + cloudtrail_event_type = var.cloudtrail_event_type |
| 90 | + s3_bucket_name = var.cloudtrail_s3_bucket_name |
| 91 | + s3_bucket_id = module.cloudtrail_s3_bucket.s3_bucket_id |
| 92 | + s3_key_prefix = var.cloudtrail_s3_key_prefix |
| 93 | + sns_topic_arn = module.cloudtrail_sns.sns_topic_arn |
| 94 | + log_retention_days = var.cloudtrail_log_retention_days |
| 95 | + s3_ignore_data_bucket_arns = [module.cloudtrail_s3_bucket.s3_bucket_arn] |
| 96 | + } |
| 97 | +
|
| 98 | + # CloudTrail alerts and monitoring (optional) |
| 99 | + module "cloudtrail_alerts" { |
| 100 | + count = var.cloudtrail_enable_alerts ? 1 : 0 |
| 101 | + depends_on = [module.cloudtrail_sns] |
| 102 | + source = "../modules-v5/cloudtrail/alerts" |
| 103 | +
|
| 104 | + env_namespace = local.env_namespace |
| 105 | + cloudtrail_log_group_name = "/aws/cloudtrail/\${var.cloudtrail_trail_name}" |
| 106 | + sns_topic_arn = module.cloudtrail_sns.sns_topic_arn |
| 107 | + slack_channel_id = var.cloudtrail_slack_channel_id |
| 108 | + slack_team_id = var.cloudtrail_slack_team_id |
| 109 | + chatbot_role_name = "AWSChatbotRole" |
| 110 | + chatbot_configuration_name = "cloudtrail-alerts-channel" |
| 111 | + metric_filter_name = "FailedLoginAttempts" |
| 112 | + metric_namespace = "CloudTrailMetrics" |
| 113 | + alarm_name = "FailedLoginAttemptsAlarm" |
| 114 | + alarm_threshold = 4 |
| 115 | + alarm_period = 300 |
| 116 | + }`; |
| 117 | + |
| 118 | +const cloudtrailOutputsContent = dedent` |
| 119 | + output "cloudtrail_s3_bucket_id" { |
| 120 | + description = "The ID of the S3 bucket for CloudTrail logs" |
| 121 | + value = module.cloudtrail_s3_bucket.s3_bucket_id |
| 122 | + } |
| 123 | +
|
| 124 | + output "cloudtrail_s3_bucket_arn" { |
| 125 | + description = "The ARN of the S3 bucket for CloudTrail logs" |
| 126 | + value = module.cloudtrail_s3_bucket.s3_bucket_arn |
| 127 | + } |
| 128 | +
|
| 129 | + output "cloudtrail_arn" { |
| 130 | + description = "The ARN of the CloudTrail" |
| 131 | + value = module.cloudtrail.cloudtrail_arn |
| 132 | + } |
| 133 | +
|
| 134 | + output "cloudtrail_sns_topic_arn" { |
| 135 | + description = "The ARN of the SNS topic for CloudTrail notifications" |
| 136 | + value = module.cloudtrail_sns.sns_topic_arn |
| 137 | + } |
| 138 | +
|
| 139 | + output "cloudtrail_arm" { |
| 140 | + description = "The ARN of CloudTrail" |
| 141 | + value = module.cloudtrail.cloudtrail_arn |
| 142 | + }`; |
| 143 | + |
| 144 | +const applyAwsCloudtrail = async (options: AwsOptions) => { |
| 145 | + if (isAwsModuleAdded('cloudtrail', options.projectName)) { |
| 146 | + return; |
| 147 | + } |
| 148 | + |
| 149 | + copy( |
| 150 | + `${AWS_TEMPLATE_PATH}/modules-v5/cloudtrail`, |
| 151 | + 'modules-v5/cloudtrail', |
| 152 | + options.projectName |
| 153 | + ); |
| 154 | + appendToFile( |
| 155 | + INFRA_CORE_V5_VARIABLES_PATH, |
| 156 | + cloudtrailVariablesContent, |
| 157 | + options.projectName |
| 158 | + ); |
| 159 | + appendToFile( |
| 160 | + INFRA_CORE_V5_MAIN_PATH, |
| 161 | + cloudtrailModuleContent, |
| 162 | + options.projectName |
| 163 | + ); |
| 164 | + appendToFile( |
| 165 | + INFRA_CORE_V5_OUTPUTS_PATH, |
| 166 | + cloudtrailOutputsContent, |
| 167 | + options.projectName |
| 168 | + ); |
| 169 | +}; |
| 170 | + |
| 171 | +export default applyAwsCloudtrail; |
| 172 | +export { |
| 173 | + cloudtrailVariablesContent, |
| 174 | + cloudtrailModuleContent, |
| 175 | + cloudtrailOutputsContent, |
| 176 | +}; |
0 commit comments