Skip to content

Commit 2453b5e

Browse files
committed
Merge pull request #14 from roylines/newrelic
allow newrelic instrumentation
2 parents b52ffa6 + b6b230a commit 2453b5e

File tree

4 files changed

+53
-23
lines changed

4 files changed

+53
-23
lines changed

cluster.tf

+10-20
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ resource "aws_autoscaling_group" "cluster" {
3737
}
3838
}
3939

40+
resource "template_file" "user_data" {
41+
template = "${file("${path.module}/user-data.sh")}"
42+
vars {
43+
vpc = "${var.vpc}"
44+
bucket_id = "${aws_s3_bucket.ecs_config.id}"
45+
newrelic_license_key = "${var.newrelic_license_key}"
46+
}
47+
}
48+
4049
resource "aws_launch_configuration" "cluster" {
4150
name_prefix = "${var.vpc}-cluster"
4251
image_id = "${var.image_id}"
@@ -45,24 +54,5 @@ resource "aws_launch_configuration" "cluster" {
4554
security_groups = ["${aws_security_group.cluster.id}", "${aws_security_group.microservices.id}"]
4655
key_name = "${aws_key_pair.instance.key_name}"
4756
depends_on = ["aws_s3_bucket_object.ecs_config", "aws_s3_bucket_object.awslogs_conf"]
48-
user_data = <<EOF
49-
#!/bin/bash
50-
yum -y update --security
51-
yum install -y aws-cli awslogs jq
52-
53-
# copy configurations
54-
aws s3 cp s3://${aws_s3_bucket.ecs_config.id}/ecs.config /etc/ecs/ecs.config
55-
aws s3 cp s3://${aws_s3_bucket.ecs_config.id}/awslogs.conf /etc/awslogs/awslogs.conf
56-
57-
# substitute container id
58-
sed -i -e "s/{container_instance_id}/$HOSTNAME/g" /etc/awslogs/awslogs.conf
59-
60-
# substitute region
61-
region=$(curl 169.254.169.254/latest/meta-data/placement/availability-zone | sed s'/.$//')
62-
sed -i -e "s/region = us-east-1/region = $region/g" /etc/awslogs/awscli.conf
63-
64-
# start aws logs
65-
service awslogs start
66-
chkconfig awslogs on
67-
EOF
57+
user_data = "${template_file.user_data.rendered}"
6858
}

s3.tf

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@ resource "aws_s3_bucket_object" "awslogs_conf" {
2020
key = "awslogs.conf"
2121
content = <<EOF
2222
[general]
23-
state_file = /var/lib/awslogs/agent-state
24-
23+
state_file = /var/lib/awslogs/agent-state
24+
25+
[/var/log/newrelic/nrsysmond.log]
26+
file = /var/log/newrelic/nrsysmond.log
27+
log_group_name = ${aws_cloudwatch_log_group.cluster.name}
28+
log_stream_name = {container_instance_id}/var/log/newrelic/nrsysmond.log
29+
2530
[/var/log/dmesg]
2631
file = /var/log/dmesg
2732
log_group_name = ${aws_cloudwatch_log_group.cluster.name}

user-data.sh

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/bash
2+
yum -y update
3+
yum install -y aws-cli awslogs jq
4+
5+
# copy configurations
6+
aws s3 cp s3://${bucket_id}/ecs.config /etc/ecs/ecs.config
7+
aws s3 cp s3://${bucket_id}/awslogs.conf /etc/awslogs/awslogs.conf
8+
9+
# substitute container id
10+
instanceId=$(curl 169.254.169.254/latest/meta-data/instance-id)
11+
sed -i -e "s/{container_instance_id}/$instanceId/g" /etc/awslogs/awslogs.conf
12+
13+
# substitute region
14+
region=$(curl 169.254.169.254/latest/meta-data/placement/availability-zone | sed s'/.$//')
15+
sed -i -e "s/region = us-east-1/region = $region/g" /etc/awslogs/awscli.conf
16+
17+
# start aws logs
18+
service awslogs start
19+
chkconfig awslogs on
20+
21+
# configure and start new relic server monitoring
22+
if ["${newrelic_license_key}" = "none" ]; then
23+
echo "no newrelic key set, skipping"
24+
else
25+
rpm -Uvh https://download.newrelic.com/pub/newrelic/el5/i386/newrelic-repo-5-3.noarch.rpm
26+
yum install -y newrelic-sysmond
27+
nrsysmond-config --set license_key=${newrelic_license_key}
28+
echo "labels=Type:cluster;VPC:${vpc};Instance:$instanceId" >> /etc/newrelic/nrsysmond.cfg
29+
30+
/etc/init.d/newrelic-sysmond start
31+
fi

variables.tf

+5-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ variable "to_port" {
4848
default = 8010
4949
}
5050

51+
variable "newrelic_license_key" {
52+
description = "new relic license key"
53+
default = "none"
54+
}
55+
5156
variable "ssh_public_key" {
5257
description = "public key to allow ssh to cluster instances"
5358
}
54-

0 commit comments

Comments
 (0)