Skip to content

Commit

Permalink
Merge pull request #14 from eemperor/cw-agent-lx
Browse files Browse the repository at this point in the history
Added CloudWatch Logs option to Linux templates
  • Loading branch information
eemperor authored Mar 26, 2018
2 parents 3315348 + 6ea6de4 commit cd5195b
Show file tree
Hide file tree
Showing 9 changed files with 491 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/lx-autoscale/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ resource "aws_cloudformation_stack" "watchmaker-lx-autoscale" {
AppVolumeMountPath = "${var.AppVolumeMountPath}"
AppVolumeType = "${var.AppVolumeType}"
AppVolumeSize = "${var.AppVolumeSize}"
CloudWatchAgentUrl = "${var.CloudWatchAgentUrl}"
KeyPairName = "${var.KeyPairName}"
InstanceType = "${var.InstanceType}"
InstanceRole = "${var.InstanceRole}"
Expand Down
6 changes: 6 additions & 0 deletions modules/lx-autoscale/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ variable "WatchmakerAdminUsers" {
default = ""
}

variable "CloudWatchAgentUrl" {
type = "string"
description = "(Optional) S3 URL to CloudWatch Agent installer. Example: s3://amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip"
default = ""
}

variable "CfnEndpointUrl" {
type = "string"
description = "(Optional) URL to the CloudFormation Endpoint. e.g. https://cloudformation.us-east-1.amazonaws.com"
Expand Down
231 changes: 231 additions & 0 deletions modules/lx-autoscale/watchmaker-lx-autoscale.cfn.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@
}
]
},
"InstallCloudWatchAgent": {
"Fn::Not": [
{
"Fn::Equals": [
{
"Ref": "CloudWatchAgentUrl"
},
""
]
}
]
},
"InstallUpdates": {
"Fn::Not": [
{
Expand Down Expand Up @@ -290,6 +302,7 @@
"CfnEndpointUrl",
"CfnGetPipUrl",
"CfnBootstrapUtilsUrl",
"CloudWatchAgentUrl",
"ToggleCfnInitUpdate",
"ToggleNewInstances"
]
Expand All @@ -305,6 +318,27 @@
}
}
},
"Outputs": {
"WatchmakerAutoScalingGroupId": {
"Description": "Autoscaling Group ID",
"Value": {
"Ref": "WatchmakerAutoScalingGroup"
}
},
"WatchmakerLaunchConfigId": {
"Description": "Launch Configuration ID",
"Value": {
"Ref": "WatchmakerLaunchConfig"
}
},
"WatchmakerLaunchConfigLogGroupName": {
"Condition": "InstallCloudWatchAgent",
"Description": "Log Group Name",
"Value": {
"Ref": "WatchmakerLaunchConfigLogGroup"
}
}
},
"Parameters": {
"AmiDistro": {
"AllowedValues": [
Expand Down Expand Up @@ -393,6 +427,12 @@
"Description": "URL to get-pip.py",
"Type": "String"
},
"CloudWatchAgentUrl": {
"AllowedPattern": "^$|^s3://.*$",
"Default": "",
"Description": "(Optional) S3 URL to CloudWatch Agent installer. Example: s3://amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip",
"Type": "String"
},
"DesiredCapacity": {
"Default": "1",
"Description": "Desired number of instances in the Autoscaling Group",
Expand Down Expand Up @@ -589,6 +629,15 @@
"configSets": {
"launch": [
"setup",
{
"Fn::If": [
"InstallCloudWatchAgent",
"install-cloudwatch-agent",
{
"Ref": "AWS::NoValue"
}
]
},
"watchmaker-install",
"watchmaker-launch",
{
Expand Down Expand Up @@ -704,6 +753,171 @@
}
}
},
"install-cloudwatch-agent": {
"commands": {
"01-get-cloudwatch-agent": {
"command": {
"Fn::Join": [
"",
[
"mkdir -p /etc/cfn/scripts/ &&",
" aws s3 cp ",
{
"Ref": "CloudWatchAgentUrl"
},
" /etc/cfn/scripts/AmazonCloudWatchAgent.zip",
" --region ",
{
"Ref": "AWS::Region"
},
" &&",
" chown root:root /etc/cfn/scripts/AmazonCloudWatchAgent.zip &&",
" chmod 700 /etc/cfn/scripts/AmazonCloudWatchAgent.zip"
]
]
}
},
"02-extract-cloudwatch-agent": {
"command": {
"Fn::Join": [
"",
[
"yum -y install unzip &&",
"unzip /etc/cfn/scripts/AmazonCloudWatchAgent.zip -d /etc/cfn/scripts/aws-cw-agent"
]
]
}
},
"10-install-cloudwatch-agent": {
"command": {
"Fn::Join": [
"",
[
" bash -xe install.sh &&",
" systemctl enable amazon-cloudwatch-agent.service &&",
" systemctl start amazon-cloudwatch-agent.service &&",
" /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl",
" -a fetch-config -m ec2 -c",
" file:/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json -s"
]
]
},
"cwd": "/etc/cfn/scripts/aws-cw-agent"
}
},
"files": {
"/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json": {
"content": {
"Fn::Join": [
"",
[
"{",
" \"logs\": {\n",
" \"logs_collected\": {\n",
" \"files\": {\n",
" \"collect_list\": [\n",
" {\n",
" \"file_path\": \"/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log\",\n",
" \"log_group_name\": \"",
{
"Fn::If": [
"InstallCloudWatchAgent",
{
"Ref": "WatchmakerLaunchConfigLogGroup"
},
{
"Ref": "AWS::NoValue"
}
]
},
"\",\n",
" \"log_stream_name\": \"cloudwatch_agent_logs_{instance_id}\",\n",
" \"timestamp_format\": \"%H:%M:%S %y %b %-d\"\n",
" },\n",
" {\n",
" \"file_path\": \"/var/log/cfn-init.log\",\n",
" \"log_group_name\": \"",
{
"Fn::If": [
"InstallCloudWatchAgent",
{
"Ref": "WatchmakerLaunchConfigLogGroup"
},
{
"Ref": "AWS::NoValue"
}
]
},
"\",\n",
" \"log_stream_name\": \"cfn_init_logs_{instance_id}\",\n",
" \"timestamp_format\": \"%H:%M:%S %y %b %-d\"\n",
" },\n",
" {\n",
" \"file_path\": \"/var/log/messages\",\n",
" \"log_group_name\": \"",
{
"Fn::If": [
"InstallCloudWatchAgent",
{
"Ref": "WatchmakerLaunchConfigLogGroup"
},
{
"Ref": "AWS::NoValue"
}
]
},
"\",\n",
" \"log_stream_name\": \"messages_logs_{instance_id}\",\n",
" \"timestamp_format\": \"%H:%M:%S %y %b %-d\"\n",
" },\n",
" {\n",
" \"file_path\": \"/var/log/watchmaker/watchmaker.log\",\n",
" \"log_group_name\": \"",
{
"Fn::If": [
"InstallCloudWatchAgent",
{
"Ref": "WatchmakerLaunchConfigLogGroup"
},
{
"Ref": "AWS::NoValue"
}
]
},
"\",\n",
" \"log_stream_name\": \"watchmaker_logs_{instance_id}\",\n",
" \"timestamp_format\": \"%H:%M:%S %y %b %-d\"\n",
" },\n",
" {\n",
" \"file_path\": \"/var/log/watchmaker/salt_call.debug.log\",\n",
" \"log_group_name\": \"",
{
"Fn::If": [
"InstallCloudWatchAgent",
{
"Ref": "WatchmakerLaunchConfigLogGroup"
},
{
"Ref": "AWS::NoValue"
}
]
},
"\",\n",
" \"log_stream_name\": \"salt_call_debug_logs_{instance_id}\",\n",
" \"timestamp_format\": \"%H:%M:%S %y %b %-d\"\n",
" }\n",
" ]\n",
" }\n",
" },\n",
" \"log_stream_name\": \"default_logs_{instance_id}\"\n",
" }\n",
"}\n"
]
]
}
}
}
},
"install-updates": {
"commands": {
"10-install-updates": {
Expand Down Expand Up @@ -1470,6 +1684,23 @@
}
},
"Type": "AWS::AutoScaling::LaunchConfiguration"
},
"WatchmakerLaunchConfigLogGroup": {
"Condition": "InstallCloudWatchAgent",
"Properties": {
"LogGroupName": {
"Fn::Join": [
"",
[
"/aws/ec2/lx/",
{
"Ref": "AWS::StackName"
}
]
]
}
},
"Type": "AWS::Logs::LogGroup"
}
}
}
4 changes: 4 additions & 0 deletions modules/lx-autoscale/watchmaker-lx-autoscale.params.cfn.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
"ParameterKey": "WatchmakerAdminGroups",
"ParameterValue": "__WATCHMAKERADMINGROUPS__"
},
{
"ParameterKey": "CloudWatchAgentUrl",
"ParameterValue": "__CWAGENTURL__"
},
{
"ParameterKey": "CfnEndpointUrl",
"ParameterValue": "__CFNENDPOINTURL__"
Expand Down
1 change: 1 addition & 0 deletions modules/lx-instance/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ resource "aws_cloudformation_stack" "watchmaker-lx-instance" {
AppVolumeMountPath = "${var.AppVolumeMountPath}"
AppVolumeType = "${var.AppVolumeType}"
AppVolumeSize = "${var.AppVolumeSize}"
CloudWatchAgentUrl = "${var.CloudWatchAgentUrl}"
KeyPairName = "${var.KeyPairName}"
InstanceType = "${var.InstanceType}"
InstanceRole = "${var.InstanceRole}"
Expand Down
6 changes: 6 additions & 0 deletions modules/lx-instance/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ variable "WatchmakerAdminUsers" {
default = ""
}

variable "CloudWatchAgentUrl" {
type = "string"
description = "(Optional) S3 URL to CloudWatch Agent installer. Example: s3://amazoncloudwatch-agent/linux/amd64/latest/AmazonCloudWatchAgent.zip"
default = ""
}

variable "CfnEndpointUrl" {
type = "string"
description = "(Optional) URL to the CloudFormation Endpoint. e.g. https://cloudformation.us-east-1.amazonaws.com"
Expand Down
Loading

0 comments on commit cd5195b

Please sign in to comment.