Skip to content

Commit

Permalink
create seperate file for userdata
Browse files Browse the repository at this point in the history
  • Loading branch information
tajewole-moj committed Oct 11, 2023
1 parent f45b9df commit 2ea272b
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 15 deletions.
30 changes: 15 additions & 15 deletions terraform/environments/apex/ec2.tf
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
locals {
instance-userdata = <<EOF
#!/bin/bash
cd /tmp
yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
sudo systemctl start amazon-ssm-agent
sudo systemctl enable amazon-ssm-agent
echo "${aws_efs_file_system.efs.dns_name}:/ /backups nfs4 rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport" >> /etc/fstab
mount -a
# locals {
# instance-userdata = <<EOF
# #!/bin/bash
# cd /tmp
# yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
# sudo systemctl start amazon-ssm-agent
# sudo systemctl enable amazon-ssm-agent
# echo "${aws_efs_file_system.efs.dns_name}:/ /backups nfs4 rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport" >> /etc/fstab
# mount -a

# Setting up CloudWatch Agent
echo '${data.local_file.cloudwatch_agent.content}' > cloudwatch_agent_config.json
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:cloudwatch_agent_config.json
EOF
}
# # Setting up CloudWatch Agent
# echo '${data.local_file.cloudwatch_agent.content}' > cloudwatch_agent_config.json
# /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:cloudwatch_agent_config.json
# EOF
# }

resource "aws_instance" "apex_db_instance" {
ami = local.application_data.accounts[local.environment].ec2amiid
Expand All @@ -24,7 +24,7 @@ resource "aws_instance" "apex_db_instance" {
monitoring = true
subnet_id = data.aws_subnet.private_subnets_a.id
iam_instance_profile = aws_iam_instance_profile.ec2_instance_profile.id
user_data_base64 = base64encode(local.instance-userdata)
user_data = "${file("run.sh")}"

root_block_device {
delete_on_termination = false
Expand Down
88 changes: 88 additions & 0 deletions terraform/environments/apex/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
echo "${aws_efs_file_system.efs.dns_name}:/ /backups nfs4 rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport" >> /etc/fstab
mount -a

# Setting up CloudWatch Agent
cd /home
echo '{
"agent": {
"metrics_collection_interval": 60,
"run_as_user": "root"
},
"metrics": {
"aggregation_dimensions": [["InstanceId"]],
"append_dimensions": {
"InstanceId": "${aws:InstanceId}"
},
"metrics_collected": {
"cpu": {
"measurement": [
"cpu_usage_idle",
"cpu_usage_iowait",
"cpu_usage_user",
"cpu_usage_system"
],
"metrics_collection_interval": 60,
"resources": ["*"],
"totalcpu": false
},
"disk": {
"measurement": ["used_percent", "inodes_free"],
"metrics_collection_interval": 60,
"drop_device": true,
"resources": ["*"],
"ignore_file_system_types": ["tmpfs", "devtmpfs", "sysfs", "fuse.s3fs", "nfs4"]
},
"diskio": {
"measurement": [
"io_time",
"write_bytes",
"read_bytes",
"writes",
"reads"
],
"metrics_collection_interval": 60,
"resources": ["*"]
},
"mem": {
"measurement": ["mem_used_percent"],
"metrics_collection_interval": 60
},
"net": {
"measurement": [
"net_drop_in",
"net_drop_out",
"net_err_in",
"net_err_out"
],
"metrics_collection_interval": 60
},
"netstat": {
"measurement": ["tcp_established", "tcp_time_wait"],
"metrics_collection_interval": 60
},
"statsd": {
"metrics_aggregation_interval": 60,
"metrics_collection_interval": 60,
"service_address": ":8125"
},
"swap": {
"measurement": ["swap_used_percent"],
"metrics_collection_interval": 60
}
}
},
"logs": {
"logs_collected": {
"files": {
"collect_list": [
{
"file_path": "/var/log/messages",
"log_group_name": "${local.application_name}-EC2"
}
]
}
}
}
}
' > cloudwatch_agent_config.json
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/home/cloudwatch_agent_config.json

0 comments on commit 2ea272b

Please sign in to comment.