main.tf 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. # Kenisis firehose stream
  2. # Record Transformation Required, called "processing_configuration" in Terraform
  3. resource "aws_kinesis_firehose_delivery_stream" "kinesis_firehose" {
  4. name = var.firehose_name
  5. destination = "splunk"
  6. s3_configuration {
  7. role_arn = aws_iam_role.kinesis_firehose.arn
  8. prefix = var.s3_prefix
  9. bucket_arn = aws_s3_bucket.kinesis_firehose_s3_bucket.arn
  10. buffer_size = var.kinesis_firehose_buffer
  11. buffer_interval = var.kinesis_firehose_buffer_interval
  12. compression_format = var.s3_compression_format
  13. }
  14. splunk_configuration {
  15. hec_endpoint = var.hec_url
  16. #hec_token = data.aws_kms_secrets.splunk_hec_token.plaintext["hec_token"]
  17. hec_token = var.hec_token
  18. hec_acknowledgment_timeout = var.hec_acknowledgment_timeout
  19. hec_endpoint_type = var.hec_endpoint_type
  20. s3_backup_mode = var.s3_backup_mode
  21. processing_configuration {
  22. enabled = "true"
  23. processors {
  24. type = "Lambda"
  25. parameters {
  26. parameter_name = "LambdaArn"
  27. parameter_value = "${aws_lambda_function.firehose_lambda_transform.arn}:$LATEST"
  28. }
  29. parameters {
  30. parameter_name = "RoleArn"
  31. parameter_value = aws_iam_role.kinesis_firehose.arn
  32. }
  33. }
  34. }
  35. cloudwatch_logging_options {
  36. enabled = var.enable_fh_cloudwatch_logging
  37. log_group_name = aws_cloudwatch_log_group.kinesis_logs.name
  38. log_stream_name = aws_cloudwatch_log_stream.kinesis_logs.name
  39. }
  40. }
  41. tags = var.tags
  42. }
  43. # S3 Bucket for Kinesis Firehose s3_backup_mode
  44. resource "aws_s3_bucket" "kinesis_firehose_s3_bucket" {
  45. bucket = var.s3_bucket_name
  46. # new version of aws doesn't let you specify the region
  47. #region = var.region
  48. acl = "private"
  49. server_side_encryption_configuration {
  50. rule {
  51. apply_server_side_encryption_by_default {
  52. sse_algorithm = "AES256"
  53. }
  54. }
  55. }
  56. tags = var.tags
  57. }
  58. # Cloudwatch logging group for Kinesis Firehose
  59. resource "aws_cloudwatch_log_group" "kinesis_logs" {
  60. name = "/aws/kinesisfirehose/${var.firehose_name}"
  61. retention_in_days = var.cloudwatch_log_retention
  62. tags = var.tags
  63. }
  64. # Create the stream
  65. resource "aws_cloudwatch_log_stream" "kinesis_logs" {
  66. name = var.log_stream_name
  67. log_group_name = aws_cloudwatch_log_group.kinesis_logs.name
  68. }
  69. ## handle the sensitivity of the hec_token variable
  70. #data "aws_kms_secrets" "splunk_hec_token" {
  71. # secret {
  72. # name = "hec_token"
  73. # payload = var.hec_token
  74. #
  75. # context = var.encryption_context
  76. # }
  77. #}
  78. # Role for the transformation Lambda function attached to the kinesis stream
  79. resource "aws_iam_role" "kinesis_firehose_lambda" {
  80. name = var.kinesis_firehose_lambda_role_name
  81. path = "/lambda/"
  82. description = "Role for Lambda function to transformation CloudWatch logs into Splunk compatible format"
  83. force_detach_policies = true
  84. assume_role_policy = <<POLICY
  85. {
  86. "Statement": [
  87. {
  88. "Effect": "Allow",
  89. "Action": "sts:AssumeRole",
  90. "Principal": {
  91. "Service": "lambda.amazonaws.com"
  92. }
  93. }
  94. ],
  95. "Version": "2012-10-17"
  96. }
  97. POLICY
  98. tags = var.tags
  99. }
  100. data "aws_iam_policy_document" "lambda_policy_doc" {
  101. statement {
  102. actions = [
  103. "logs:GetLogEvents",
  104. ]
  105. resources = [
  106. var.arn_cloudwatch_logs_to_ship,
  107. ]
  108. effect = "Allow"
  109. }
  110. statement {
  111. actions = [
  112. "firehose:PutRecordBatch",
  113. ]
  114. resources = [
  115. aws_kinesis_firehose_delivery_stream.kinesis_firehose.arn,
  116. ]
  117. }
  118. statement {
  119. actions = [
  120. "logs:PutLogEvents",
  121. ]
  122. resources = [
  123. "*",
  124. ]
  125. effect = "Allow"
  126. }
  127. statement {
  128. actions = [
  129. "logs:CreateLogGroup",
  130. ]
  131. resources = [
  132. "*",
  133. ]
  134. effect = "Allow"
  135. }
  136. statement {
  137. actions = [
  138. "logs:CreateLogStream",
  139. ]
  140. resources = [
  141. "*",
  142. ]
  143. effect = "Allow"
  144. }
  145. }
  146. resource "aws_iam_policy" "lambda_transform_policy" {
  147. name = var.lambda_iam_policy_name
  148. policy = data.aws_iam_policy_document.lambda_policy_doc.json
  149. }
  150. resource "aws_iam_role_policy_attachment" "lambda_policy_role_attachment" {
  151. role = aws_iam_role.kinesis_firehose_lambda.name
  152. policy_arn = aws_iam_policy.lambda_transform_policy.arn
  153. }
  154. # Create the lambda function
  155. # The lambda function to transform data from compressed format in Cloudwatch to something Splunk can handle (uncompressed)
  156. resource "aws_lambda_function" "firehose_lambda_transform" {
  157. function_name = var.lambda_function_name
  158. description = "Transform data from CloudWatch format to Splunk compatible format"
  159. filename = data.archive_file.lambda_function.output_path
  160. role = aws_iam_role.kinesis_firehose_lambda.arn
  161. handler = "kinesis-firehose-cloudwatch-logs-processor.handler"
  162. source_code_hash = data.archive_file.lambda_function.output_base64sha256
  163. runtime = var.nodejs_runtime
  164. timeout = var.lambda_function_timeout
  165. tags = var.tags
  166. }
  167. # kinesis-firehose-cloudwatch-logs-processor.js was taken by copy/paste from the AWS UI. It is predefined blueprint
  168. # code supplied to AWS by Splunk.
  169. data "archive_file" "lambda_function" {
  170. type = "zip"
  171. source_file = "${path.module}/files/kinesis-firehose-cloudwatch-logs-processor.js"
  172. output_path = "${path.module}/files/kinesis-firehose-cloudwatch-logs-processor.zip"
  173. }
  174. # Role for Kenisis Firehose
  175. resource "aws_iam_role" "kinesis_firehose" {
  176. name = var.kinesis_firehose_role_name
  177. path = "/aws_services/"
  178. description = "IAM Role for Kenisis Firehose"
  179. force_detach_policies = true
  180. assume_role_policy = <<POLICY
  181. {
  182. "Version": "2012-10-17",
  183. "Statement": [
  184. {
  185. "Principal": {
  186. "Service": "firehose.amazonaws.com"
  187. },
  188. "Action": "sts:AssumeRole",
  189. "Effect": "Allow"
  190. }
  191. ]
  192. }
  193. POLICY
  194. tags = var.tags
  195. }
  196. data "aws_iam_policy_document" "kinesis_firehose_policy_document" {
  197. statement {
  198. actions = [
  199. "s3:AbortMultipartUpload",
  200. "s3:GetBucketLocation",
  201. "s3:GetObject",
  202. "s3:ListBucket",
  203. "s3:ListBucketMultipartUploads",
  204. "s3:PutObject",
  205. ]
  206. resources = [
  207. aws_s3_bucket.kinesis_firehose_s3_bucket.arn,
  208. "${aws_s3_bucket.kinesis_firehose_s3_bucket.arn}/*",
  209. ]
  210. effect = "Allow"
  211. }
  212. statement {
  213. actions = [
  214. "lambda:InvokeFunction",
  215. "lambda:GetFunctionConfiguration",
  216. ]
  217. resources = [
  218. "${aws_lambda_function.firehose_lambda_transform.arn}:$LATEST",
  219. ]
  220. }
  221. statement {
  222. actions = [
  223. "logs:PutLogEvents",
  224. ]
  225. resources = [
  226. aws_cloudwatch_log_group.kinesis_logs.arn,
  227. aws_cloudwatch_log_stream.kinesis_logs.arn,
  228. ]
  229. effect = "Allow"
  230. }
  231. }
  232. resource "aws_iam_policy" "kinesis_firehose_iam_policy" {
  233. name = var.kinesis_firehose_iam_policy_name
  234. policy = data.aws_iam_policy_document.kinesis_firehose_policy_document.json
  235. }
  236. resource "aws_iam_role_policy_attachment" "kenisis_fh_role_attachment" {
  237. role = aws_iam_role.kinesis_firehose.name
  238. policy_arn = aws_iam_policy.kinesis_firehose_iam_policy.arn
  239. }
  240. resource "aws_iam_role" "cloudwatch_to_firehose_trust" {
  241. name = var.cloudwatch_to_firehose_trust_iam_role_name
  242. description = "Role for CloudWatch Log Group subscription"
  243. path = "/aws_services/"
  244. force_detach_policies = true
  245. assume_role_policy = <<ROLE
  246. {
  247. "Statement": [
  248. {
  249. "Effect": "Allow",
  250. "Action": "sts:AssumeRole",
  251. "Principal": {
  252. "Service": "logs.${var.region}.amazonaws.com"
  253. }
  254. }
  255. ],
  256. "Version": "2012-10-17"
  257. }
  258. ROLE
  259. }
  260. data "aws_iam_policy_document" "cloudwatch_to_fh_access_policy" {
  261. statement {
  262. actions = [
  263. "firehose:*",
  264. ]
  265. effect = "Allow"
  266. resources = [
  267. aws_kinesis_firehose_delivery_stream.kinesis_firehose.arn,
  268. ]
  269. }
  270. statement {
  271. actions = [
  272. "iam:PassRole",
  273. ]
  274. effect = "Allow"
  275. resources = [
  276. aws_iam_role.cloudwatch_to_firehose_trust.arn,
  277. ]
  278. }
  279. }
  280. resource "aws_iam_policy" "cloudwatch_to_fh_access_policy" {
  281. name = var.cloudwatch_to_fh_access_policy_name
  282. description = "Cloudwatch to Firehose Subscription Policy"
  283. policy = data.aws_iam_policy_document.cloudwatch_to_fh_access_policy.json
  284. }
  285. resource "aws_iam_role_policy_attachment" "cloudwatch_to_fh" {
  286. role = aws_iam_role.cloudwatch_to_firehose_trust.name
  287. policy_arn = aws_iam_policy.cloudwatch_to_fh_access_policy.arn
  288. }
  289. resource "aws_cloudwatch_log_subscription_filter" "cloudwatch_log_filter" {
  290. name = var.cloudwatch_log_filter_name
  291. role_arn = aws_iam_role.cloudwatch_to_firehose_trust.arn
  292. destination_arn = aws_kinesis_firehose_delivery_stream.kinesis_firehose.arn
  293. log_group_name = var.name_cloudwatch_logs_to_ship
  294. filter_pattern = var.subscription_filter_pattern
  295. }