Skip to content

Commit

Permalink
Merge pull request #38 from rackspace-infrastructure-automation/mc_cu…
Browse files Browse the repository at this point in the history
…stom_cw_config

Add custom cw agent configuration parmater store input
  • Loading branch information
Michael Cardenas committed Jan 15, 2019
2 parents e560d06 + 706206e commit 4683211
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Full working references are available at [examples](examples)
| backup\_tag\_value | Value of the 'Backup' tag, used to assign te EBSSnapper configuration | string | `"False"` | no |
| cloudwatch\_log\_retention | The number of days to retain Cloudwatch Logs for this instance. | string | `"30"` | no |
| creation\_policy\_timeout | Time to wait for the number of signals for the creation policy. H/M/S Hours/Minutes/Seconds | string | `"20m"` | no |
| custom\_cw\_agent\_config\_ssm\_param | SSM Parameter Store name that contains a custom CloudWatch agent configuration that you would like to use as an alternative to the default provided. | string | `""` | no |
| cw\_cpu\_high\_evaluations | The number of periods over which data is compared to the specified threshold. | string | `"15"` | no |
| cw\_cpu\_high\_operator | Math operator used by CloudWatch for alarms and triggers. | string | `"GreaterThanThreshold"` | no |
| cw\_cpu\_high\_period | Time the specified statistic is applied. Must be in seconds that is also a multiple of 60. | string | `"60"` | no |
Expand Down Expand Up @@ -57,6 +58,7 @@ Full working references are available at [examples](examples)
| primary\_ebs\_volume\_size | EBS Volume Size in GB | string | `"60"` | no |
| primary\_ebs\_volume\_type | EBS Volume Type. e.g. gp2, io1, st1, sc1 | string | `"gp2"` | no |
| private\_ip\_address | A list of static private IP addresses to be configured on the instance. This IP should be in the assigned subnet and if the instance is replaced, a new IP would need to be assigned. If used, one private IP needs to be provided per instance. | list | `<list>` | no |
| provide\_custom\_cw\_agent\_config | Set to true if a custom cloudwatch agent configuration has been provided in variable custom_cw_agent_config_ssm_param. | string | `"false"` | no |
| rackspace\_managed | Boolean parameter controlling if instance will be fully managed by Rackspace support teams, created CloudWatch alarms that generate tickets, and utilize Rackspace managed SSM documents. | string | `"true"` | no |
| resource\_name | Name to be used for the provisioned EC2 instance(s) and other resources provisioned in this module | string | n/a | yes |
| secondary\_ebs\_volume\_iops | Iops value required for use with io1 EBS volumes. This value should be 3 times the EBS volume size | string | `"0"` | no |
Expand Down
73 changes: 73 additions & 0 deletions examples/custom_cw_agent_config.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
provider "aws" {
region = "us-west-2"
}

resource "random_string" "res_name" {
length = 8
upper = false
lower = true
special = false
number = false
}

module "vpc" {
source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-vpc_basenetwork?ref=v0.0.6"
vpc_name = "EC2-AR-BaseNetwork-Test1"
}

data "aws_region" "current_region" {}

module "ec2_ar_with_codedeploy" {
source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-ec2_autorecovery?ref=v0.0.8"
ec2_os = "rhel6"
instance_count = "1"
subnets = "${module.vpc.private_subnets}"

security_group_list = [
"${module.vpc.default_sg}",
]

key_pair = "CircleCI"
instance_type = "t2.micro"
resource_name = "ar_ec2_codedeploy-${random_string.res_name.result}"
install_codedeploy_agent = true
enable_ebs_optimization = false
tenancy = "default"
backup_tag_value = "False"
detailed_monitoring = true
ssm_patching_group = "Group1Patching"
primary_ebs_volume_size = "60"
primary_ebs_volume_iops = "0"
primary_ebs_volume_type = "gp2"
encrypt_secondary_ebs_volume = "False"
environment = "Development"
perform_ssm_inventory_tag = true
cloudwatch_log_retention = "30"
ssm_association_refresh_rate = "rate(1 day)"
alarm_notification_topic = ""
disable_api_termination = false
t2_unlimited_mode = "standard"
creation_policy_timeout = "20m"
cw_cpu_high_operator = "GreaterThanThreshold"
cw_cpu_high_threshold = "90"
cw_cpu_high_evaluations = "15"
cw_cpu_high_period = "60"
provide_custom_cw_agent_config = true
custom_cw_agent_config_ssm_param = "${aws_ssm_parameter.custom_cwagentparam.name}"
}

resource "aws_ssm_parameter" "custom_cwagentparam" {
name = "custom_cw_param-${random_string.res_name.result}"
description = "Custom Cloudwatch Agent configuration"
type = "String"
value = "${data.template_file.custom_cwagentparam.rendered}"
}

data "template_file" "custom_cwagentparam" {
template = "${file("./text/linux_cw_agent_param.json")}"

vars {
application_log_group_name = "custom_app_log_group_name"
system_log_group_name = "custom_system_log_group_name"
}
}
2 changes: 1 addition & 1 deletion examples/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data "aws_ami" "amazon_centos_7" {
}

module "ec2_ar" {
source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-ec2_autorecovery?ref=v0.0.6"
source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-ec2_autorecovery?ref=v0.0.8"
ec2_os = "centos7"
instance_count = "3"
subnets = "${module.vpc.public_subnets}"
Expand Down
1 change: 1 addition & 0 deletions examples/text/linux_cw_agent_param.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"metrics":{"append_dimensions":{"InstanceId":"$${aws:InstanceId}","AutoScalingGroupName":"$${aws:AutoScalingGroupName}"},"aggregation_dimensions":[["InstanceId"],["AutoScalingGroupName"],["InstanceId","device"]],"namespace":"System/Linux","metrics_collected":{"mem":{"metrics_collection_interval":60,"measurement":[{"rename":"MemoryUtilization","name":"mem_used_percent","unit":"Percent"}]},"disk":{"ignore_file_system_types":["devtmpfs","tmpfs","devfs","rootfs"],"metrics_collection_interval":60,"resources":["*"],"measurement":["used_percent"]}}},"logs":{"logs_collected":{"files":{"collect_list":[{"file_path":"/var/log/cloud-init-output.log","log_group_name":"${system_log_group_name}","log_stream_name":"{instance_id}/cloud-init-output.log"},{"timestamp_format":"%b %d %H:%M:%S","file_path":"/var/log/cloud-init.log","log_group_name":"${system_log_group_name}","log_stream_name":"{instance_id}/cloud-init.log"},{"timestamp_format":"%Y-%m-%d %H:%M:%S","multi_line_start_pattern":"{timestamp_format}","file_path":"/var/log/amazon/ssm/amazon-ssm-agent.log","log_group_name":"${system_log_group_name}","log_stream_name":"{instance_id}/amazon-ssm-agent.log"},{"timestamp_format":"%Y-%m-%d %H:%M:%S","multi_line_start_pattern":"{timestamp_format}","file_path":"/var/log/amazon/ssm/errors.log","log_group_name":"${system_log_group_name}","log_stream_name":"{instance_id}/amazon-ssm-errors.log"},{"timestamp_format":"%d/%b/%Y:%H:%M:%S","file_path":"/var/log/httpd/access*","log_group_name":"${application_log_group_name}","log_stream_name":"{instance_id}/httpd-access"},{"timestamp_format":"%d/%b/%Y:%H:%M:%S","file_path":"/var/log/httpd/error*","log_group_name":"${application_log_group_name}","log_stream_name":"{instance_id}/httpd-error"},{"timestamp_format":"%d/%b/%Y:%H:%M:%S","file_path":"/var/log/apache2/access*","log_group_name":"${application_log_group_name}","log_stream_name":"{instance_id}/apache2-access"},{"timestamp_format":"%d/%b/%Y:%H:%M:%S","file_path":"/var/log/apache2/error*","log_group_name":"${application_log_group_name}","log_stream_name":"{instance_id}/apache2-error"},{"timestamp_format":"%d/%b/%Y:%H:%M:%S","file_path":"/var/log/nginx/access*","log_group_name":"${application_log_group_name}","log_stream_name":"{instance_id}/nginx-access"},{"timestamp_format":"%d/%b/%Y:%H:%M:%S","file_path":"/var/log/nginx/error*","log_group_name":"${application_log_group_name}","log_stream_name":"{instance_id}/nginx-error"},{"timestamp_format":"%a %b %d %H:%M:%S","file_path":"/etc/openvpn/openvpn.log*","log_group_name":"${application_log_group_name}","log_stream_name":"{instance_id}/openvpn.log"}]}}}}
2 changes: 1 addition & 1 deletion examples/unmanaged.tf
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module "sns" {
}

module "unmanaged_ar" {
source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-ec2_autorecovery?ref=v0.0.6"
source = "git@github.com:rackspace-infrastructure-automation/aws-terraform-ec2_autorecovery?ref=v0.0.8"

ec2_os = "centos7"
instance_count = "1"
Expand Down
7 changes: 5 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ EOF
windows2012R2 = "Windows_Server-2012-R2_RTM-English-64Bit-Base*"
windows2016 = "Windows_Server-2016-English-Full-Base*"
}

cw_config_parameter_name = "CWAgent-${var.resource_name}"
}

# Lookup the correct AMI based on the region specified
Expand Down Expand Up @@ -321,7 +323,7 @@ data "template_file" "ssm_bootstrap_template" {
template = "${file("${path.module}/text/ssm_bootstrap_template.json")}"

vars {
cw_agent_param = "${aws_ssm_parameter.cwagentparam.name}"
cw_agent_param = "${var.provide_custom_cw_agent_config ? var.custom_cw_agent_config_ssm_param : local.cw_config_parameter_name}"
managed_ssm_docs = "${var.rackspace_managed ? data.template_file.ssm_managed_commands.rendered : ""}"
codedeploy_doc = "${local.ssm_codedeploy_include[local.codedeploy_install]}"
nfs_doc = "${local.ssm_nfs_include[local.nfs_install]}"
Expand All @@ -337,7 +339,8 @@ resource "aws_ssm_document" "ssm_bootstrap_doc" {
}

resource "aws_ssm_parameter" "cwagentparam" {
name = "CWAgent-${var.resource_name}"
count = "${var.provide_custom_cw_agent_config ? 0 : 1}"
name = "${local.cw_config_parameter_name}"
description = "${var.resource_name} Cloudwatch Agent configuration"
type = "String"
value = "${replace(replace(file("${path.module}/text/${local.cwagent_config}"),"((SYSTEM_LOG_GROUP_NAME))",aws_cloudwatch_log_group.system_logs.name),"((APPLICATION_LOG_GROUP_NAME))",aws_cloudwatch_log_group.application_logs.name)}"
Expand Down
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,18 @@ variable "cw_cpu_high_threshold" {
default = "90"
}

variable "provide_custom_cw_agent_config" {
description = "Set to true if a custom cloudwatch agent configuration has been provided in variable custom_cw_agent_config_ssm_param."
type = "string"
default = false
}

variable "custom_cw_agent_config_ssm_param" {
description = "SSM Parameter Store name that contains a custom CloudWatch agent configuration that you would like to use as an alternative to the default provided."
type = "string"
default = ""
}

#
# Roles and Policies
#
Expand Down

0 comments on commit 4683211

Please sign in to comment.