generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Datadog Service Log Forwarder Lambda #130
Open
DrizzlyOwl
wants to merge
1
commit into
main
Choose a base branch
from
feat/datadog-lambda-forwarder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,6 @@ backend.vars | |
|
||
# caches | ||
lambdas/.zip-cache/* | ||
|
||
# Junk files | ||
.DS_Store |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,283 @@ | ||
resource "aws_kms_key" "datadog_forwarder" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
description = "This key is used to encrypt the DataDog Forwarder Lambda logs (${local.project_name})" | ||
deletion_window_in_days = 10 | ||
enable_key_rotation = true | ||
policy = templatefile( | ||
"${path.root}/policies/kms-key-policy.json.tpl", | ||
{ | ||
statement = <<EOT | ||
[ | ||
${templatefile("${path.root}/policies/kms-key-policy-statements/root-allow-all.json.tpl", | ||
{ | ||
aws_account_id = local.aws_account_id | ||
} | ||
)}, | ||
${templatefile("${path.root}/policies/kms-key-policy-statements/cloudwatch-logs-allow.json.tpl", | ||
{ | ||
log_group_arn = "arn:aws:logs:${local.aws_region}:${local.aws_account_id}:log-group:/aws/lambda/datadog-forwarder" | ||
} | ||
)} | ||
] | ||
EOT | ||
} | ||
) | ||
} | ||
|
||
resource "aws_kms_alias" "datadog_forwarder" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "alias/${local.project_name}-datadog-forwarder-lambda" | ||
target_key_id = aws_kms_key.datadog_forwarder[0].key_id | ||
} | ||
|
||
resource "aws_cloudwatch_log_group" "datadog_forwarder_log_group" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "/aws/lambda/datadog-forwarder" | ||
kms_key_id = aws_kms_key.datadog_forwarder[0].arn | ||
retention_in_days = local.datadog_forwarder_log_retention | ||
} | ||
|
||
resource "aws_iam_role" "datadog_forwarder" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name}-${substr(sha512("datadog-forwarder"), 0, 6)}" | ||
description = "${local.project_name}-datadog-forwarder" | ||
assume_role_policy = templatefile( | ||
"${path.root}/policies/assume-roles/service-principle-standard.json.tpl", | ||
{ services = jsonencode(["lambda.amazonaws.com"]) } | ||
) | ||
} | ||
|
||
resource "aws_iam_policy" "datadog_forwarder_tags" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name}-datadog-forwarder-tags" | ||
policy = templatefile( | ||
"${path.root}/policies/datadog-forwarder.json.tpl", {} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "datadog_forwarder_tags" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
role = aws_iam_role.datadog_forwarder[0].name | ||
policy_arn = aws_iam_policy.datadog_forwarder_tags[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "datadog_forwarder" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name}-datadog-forwarder" | ||
policy = templatefile( | ||
"${path.root}/policies/lambda-default.json.tpl", | ||
{ | ||
region = local.aws_region | ||
account_id = local.aws_account_id | ||
function_name = "datadog-forwarder" | ||
} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "datadog_forwarder" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
role = aws_iam_role.datadog_forwarder[0].name | ||
policy_arn = aws_iam_policy.datadog_forwarder[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "datadog_forwarder_s3_object_read" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name}-datadog-forwarder-s3-object-read" | ||
policy = templatefile( | ||
"${path.root}/policies/s3-object-read.json.tpl", | ||
{ | ||
bucket_arn : "*" | ||
} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "datadog_forwarder_s3_object_read" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
role = aws_iam_role.datadog_forwarder[0].name | ||
policy_arn = aws_iam_policy.datadog_forwarder_s3_object_read[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "datadog_forwarder_s3_object_rw" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name}-datadog-forwarder-s3-object-rw" | ||
policy = templatefile( | ||
"${path.root}/policies/s3-object-rw.json.tpl", | ||
{ | ||
bucket_arn : aws_s3_bucket.datadog_lambda[0].arn | ||
} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "datadog_forwarder_s3_object_rw" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
role = aws_iam_role.datadog_forwarder[0].name | ||
policy_arn = aws_iam_policy.datadog_forwarder_s3_object_rw[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "datadog_forwarder_kms_encrypt" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name}-datadog-forwarder-kms-encrypt" | ||
policy = templatefile( | ||
"${path.root}/policies/kms-encrypt.json.tpl", | ||
{ | ||
kms_key_arn : aws_kms_key.datadog_forwarder[0].arn | ||
} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "datadog_forwarder_kms_encrypt" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
role = aws_iam_role.datadog_forwarder[0].name | ||
policy_arn = aws_iam_policy.datadog_forwarder_kms_encrypt[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "datadog_forwarder_kms_encrypt_wildcard" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name}-datadog-forwarder-kms-encrypt-all" | ||
policy = templatefile( | ||
"${path.root}/policies/kms-encrypt.json.tpl", | ||
{ | ||
kms_key_arn : "arn:aws:kms:${local.aws_region}:${local.aws_account_id}:key/*" | ||
} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "datadog_forwarder_kms_encrypt_wildcard" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
role = aws_iam_role.datadog_forwarder[0].name | ||
policy_arn = aws_iam_policy.datadog_forwarder_kms_encrypt_wildcard[0].arn | ||
} | ||
|
||
resource "aws_iam_policy" "datadog_forwarder_secret" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name}-datadog-forwarder-secrets-manager-get-secret-value" | ||
policy = templatefile( | ||
"${path.root}/policies/secrets-manager-get-secret-value.json.tpl", | ||
{ | ||
secret_name_arns : jsonencode([ | ||
aws_secretsmanager_secret.datadog_api_key[0].arn | ||
]) | ||
} | ||
) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "datadog_forwarder_secret" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
role = aws_iam_role.datadog_forwarder[0].name | ||
policy_arn = aws_iam_policy.datadog_forwarder_secret[0].arn | ||
} | ||
|
||
data "archive_file" "datadog_forwarder" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
type = "zip" | ||
source_dir = "lambdas/aws-dd-forwarder-3.127.0" | ||
output_path = "lambdas/.zip-cache/aws-dd-forwarder-3.127.0.zip" | ||
} | ||
|
||
resource "aws_lambda_function" "datadog_service_log_forwarder" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
filename = data.archive_file.datadog_forwarder[0].output_path | ||
function_name = "datadog-forwarder" | ||
description = "${local.project_name} DataDog AWS Service Log Forwarder" | ||
handler = "lambda_function.datadog_forwarder" | ||
runtime = "python3.11" | ||
role = aws_iam_role.datadog_forwarder[0].arn | ||
source_code_hash = data.archive_file.datadog_forwarder[0].output_base64sha256 | ||
memory_size = 128 | ||
package_type = "Zip" | ||
timeout = 900 | ||
|
||
environment { | ||
variables = { | ||
DD_STORE_FAILED_EVENTS : local.datadog_forwarder_store_failed_events, | ||
DD_API_KEY_SECRET_ARN : aws_secretsmanager_secret.datadog_api_key[0].arn, | ||
DD_ENHANCED_METRICS : local.datadog_forwarder_enhanced_metrics, | ||
DD_S3_BUCKET_NAME : aws_s3_bucket.datadog_lambda[0].bucket, | ||
DD_SITE : local.datadog_site, | ||
DD_API_URL : local.datadog_api_url, | ||
} | ||
} | ||
|
||
tracing_config { | ||
mode = "Active" | ||
} | ||
|
||
depends_on = [ | ||
aws_iam_role_policy_attachment.datadog_forwarder, | ||
aws_iam_role_policy_attachment.datadog_forwarder_s3_object_read, | ||
aws_iam_role_policy_attachment.datadog_forwarder_s3_object_rw, | ||
aws_iam_role_policy_attachment.datadog_forwarder_secret, | ||
] | ||
} | ||
|
||
#tfsec:ignore:aws-ssm-secret-use-customer-key | ||
resource "aws_secretsmanager_secret" "datadog_api_key" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
name = "${local.project_name_hash}/datadog/DD_API_KEY" | ||
} | ||
|
||
resource "aws_secretsmanager_secret_version" "datadog_api_key" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
secret_id = aws_secretsmanager_secret.datadog_api_key[0].id | ||
secret_string = local.datadog_api_key | ||
} | ||
|
||
resource "datadog_integration_aws_lambda_arn" "datadog_forwarder_arn" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
account_id = local.aws_account_id | ||
lambda_arn = aws_lambda_function.datadog_service_log_forwarder[0].arn | ||
} | ||
|
||
resource "datadog_integration_aws_log_collection" "datadog_forwarder" { | ||
count = local.enable_datadog_forwarder ? 1 : 0 | ||
|
||
account_id = local.aws_account_id | ||
services = ["cloudfront", "waf", "elbv2", "s3"] | ||
} | ||
|
||
resource "aws_lambda_permission" "datadog_forwarder_allow_cloudwatch" { | ||
statement_id = "AllowExecutionFromCloudWatch" | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.datadog_service_log_forwarder[0].function_name | ||
principal = "events.amazonaws.com" | ||
source_account = local.aws_account_id | ||
} | ||
|
||
resource "aws_lambda_permission" "datadog_forwarder_allow_sns" { | ||
statement_id = "AllowExecutionFromSNS" | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.datadog_service_log_forwarder[0].function_name | ||
principal = "sns.amazonaws.com" | ||
source_account = local.aws_account_id | ||
} | ||
Comment on lines
+269
to
+275
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
resource "aws_lambda_permission" "datadog_forwarder_allow_s3" { | ||
statement_id = "AllowExecutionFromS3" | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.datadog_service_log_forwarder[0].function_name | ||
principal = "s3.amazonaws.com" | ||
source_account = local.aws_account_id | ||
} | ||
Comment on lines
+277
to
+283
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
Binary file not shown.
1 change: 1 addition & 0 deletions
1
lambdas/aws-dd-forwarder-3.127.0/Deprecated-1.2.14.dist-info/INSTALLER
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pip |
21 changes: 21 additions & 0 deletions
21
lambdas/aws-dd-forwarder-3.127.0/Deprecated-1.2.14.dist-info/LICENSE.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017 Laurent LAPORTE | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aws-lambda-restrict-source-arn
:More information available here and here