Skip to content

Commit

Permalink
Merge branch 'main' into helm-alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
ben851 authored Dec 12, 2024
2 parents ed5f60e + 620c2bd commit 4e66850
Show file tree
Hide file tree
Showing 6 changed files with 340 additions and 78 deletions.
64 changes: 36 additions & 28 deletions aws/newrelic/aws_integration.tf → aws/common/newrelic.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
provider "newrelic" {
account_id = var.new_relic_account_id
api_key = var.new_relic_api_key
region = "US"
}

data "aws_iam_policy_document" "newrelic_assume_policy" {
count = var.enable_new_relic ? 1 : 0

Expand All @@ -19,14 +25,14 @@ data "aws_iam_policy_document" "newrelic_assume_policy" {
}

resource "aws_iam_role" "newrelic_aws_role" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "NewRelicInfrastructure-Integrations-${var.env}"
description = "New Relic Cloud integration role"
assume_role_policy = data.aws_iam_policy_document.newrelic_assume_policy[0].json
}

resource "aws_iam_policy" "newrelic_aws_permissions" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "NewRelicCloudStreamReadPermissions-${var.env}"
description = ""
policy = <<EOF
Expand Down Expand Up @@ -67,13 +73,13 @@ EOF
}

resource "aws_iam_role_policy_attachment" "newrelic_aws_policy_attach" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
role = aws_iam_role.newrelic_aws_role[0].name
policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess"
}

resource "newrelic_cloud_aws_link_account" "newrelic_cloud_integration_push" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
account_id = var.new_relic_account_id
arn = aws_iam_role.newrelic_aws_role[0].arn
metric_collection_mode = "PUSH"
Expand All @@ -82,16 +88,15 @@ resource "newrelic_cloud_aws_link_account" "newrelic_cloud_integration_push" {
}

resource "newrelic_api_access_key" "newrelic_aws_access_key" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
account_id = var.new_relic_account_id
key_type = "INGEST"
ingest_type = "LICENSE"
name = "Metric Stream Key for ${var.env}"
notes = "AWS Cloud Integrations Metric Stream Key"
count = var.enable_new_relic && var.env != "production" ? 1 : 0
account_id = var.new_relic_account_id
key_type = "USER"
name = "notify_tf_provider"
notes = "Used by Notify Terraform Code to create New Relic Resources"
}

resource "aws_iam_role" "firehose_newrelic_role" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "firehose_newrelic_role_${var.env}"

assume_role_policy = <<EOF
Expand All @@ -112,28 +117,28 @@ EOF
}

resource "random_string" "s3-bucket-name" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
length = 8
special = false
upper = false
special = true
upper = true
}

resource "aws_s3_bucket" "newrelic_aws_bucket" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
bucket = "newrelic-aws-bucket-${random_string.s3-bucket-name[0].id}"
force_destroy = true
}

resource "aws_s3_bucket_ownership_controls" "newrelic_ownership_controls" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
bucket = aws_s3_bucket.newrelic_aws_bucket[0].id
rule {
object_ownership = "BucketOwnerEnforced"
}
}

resource "aws_kinesis_firehose_delivery_stream" "newrelic_firehose_stream" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "newrelic_firehose_stream_${var.env}"
destination = "http_endpoint"
http_endpoint_configuration {
Expand All @@ -158,7 +163,7 @@ resource "aws_kinesis_firehose_delivery_stream" "newrelic_firehose_stream" {
}

resource "aws_iam_role" "metric_stream_to_firehose" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "newrelic_metric_stream_to_firehose_role_${var.env}"

assume_role_policy = <<EOF
Expand All @@ -179,7 +184,7 @@ EOF
}

resource "aws_iam_role_policy" "metric_stream_to_firehose" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "default"
role = aws_iam_role.metric_stream_to_firehose[0].id

Expand All @@ -202,15 +207,15 @@ EOF

resource "aws_cloudwatch_metric_stream" "newrelic_metric_stream" {
# Disabled for now
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "newrelic-metric-stream-${var.env}"
role_arn = aws_iam_role.metric_stream_to_firehose[0].arn
firehose_arn = aws_kinesis_firehose_delivery_stream.newrelic_firehose_stream[0].arn
output_format = "opentelemetry0.7"
}

resource "newrelic_cloud_aws_link_account" "newrelic_cloud_integration_pull" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
account_id = var.new_relic_account_id
arn = aws_iam_role.newrelic_aws_role[0].arn
metric_collection_mode = "PULL"
Expand All @@ -219,22 +224,25 @@ resource "newrelic_cloud_aws_link_account" "newrelic_cloud_integration_pull" {
}

resource "newrelic_cloud_aws_integrations" "newrelic_cloud_integration_pull" {
count = var.env == "staging" ? 1 : 0
count = var.env != "production" ? 1 : 0
account_id = var.new_relic_account_id
linked_account_id = newrelic_cloud_aws_link_account.newrelic_cloud_integration_pull[0].id

lambda {}
lambda {
fetch_tags = true
metrics_polling_interval = 300
}

}

resource "aws_s3_bucket" "newrelic_configuration_recorder_s3" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
bucket = "newrelic-configuration-recorder-${random_string.s3-bucket-name[0].id}"
force_destroy = true
}

resource "aws_iam_role" "newrelic_configuration_recorder" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "newrelic_configuration_recorder-${var.env}"
assume_role_policy = <<EOF
{
Expand All @@ -254,7 +262,7 @@ EOF
}

resource "aws_iam_role_policy" "newrelic_configuration_recorder_s3" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = "newrelic-configuration-recorder-s3-${var.env}"
role = aws_iam_role.newrelic_configuration_recorder[0].id

Expand All @@ -278,14 +286,14 @@ POLICY
}

resource "aws_iam_role_policy_attachment" "newrelic_configuration_recorder" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
role = aws_iam_role.newrelic_configuration_recorder[0].name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWS_ConfigRole"
}


resource "aws_config_configuration_recorder_status" "newrelic_recorder_status" {
count = var.enable_new_relic && var.env == "staging" ? 1 : 0
count = var.enable_new_relic && var.env != "production" ? 1 : 0
name = var.aws_config_recorder_name
is_enabled = true
}
9 changes: 9 additions & 0 deletions aws/common/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "newrelic_account_region" {
type = string
default = "US"

validation {
condition = contains(["US", "EU"], var.newrelic_account_region)
error_message = "Valid values for region are 'US' or 'EU'."
}
}
115 changes: 67 additions & 48 deletions env/dev/common/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4e66850

Please sign in to comment.