Skip to content

Instantly share code, notes, and snippets.

@lvthillo
Last active June 10, 2019 17:20
Show Gist options
  • Save lvthillo/ff2a5ff05ab762b1c415160d41d4be60 to your computer and use it in GitHub Desktop.
Save lvthillo/ff2a5ff05ab762b1c415160d41d4be60 to your computer and use it in GitHub Desktop.
ConfigSets
# Definition of json configuration of AmazonCloudWatchAgent
02_config-amazon-cloudwatch-agent:
files:
'/opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.json':
content: !Sub |
{
"metrics": {
"append_dimensions": {
"AutoScalingGroupName": "${!aws:AutoScalingGroupName}",
"ImageId": "${!aws:ImageId}",
"InstanceId": "${!aws:InstanceId}",
"InstanceType": "${!aws:InstanceType}"
},
"aggregation_dimensions" : [["AutoScalingGroupName"]],
"metrics_collected": {
"mem": {
"measurement": [
"mem_used_percent"
]
}
}
}
}
# Invoke amazon-cloudwatch-agent-ctl to restart the AmazonCloudWatchAgent.
03_restart_amazon-cloudwatch-agent:
commands:
01_stop_service:
command: /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a stop
02_start_service:
command: /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
# Cfn-hup setting, it is to monitor the change of metadata.
# When there is change in the contents of json file in the metadata section, cfn-hup will call cfn-init to restart the AmazonCloudWatchAgent.
01_setupCfnHup:
files:
'/etc/cfn/cfn-hup.conf':
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
mode: '000400'
owner: root
group: root
'/etc/cfn/hooks.d/amazon-cloudwatch-agent-auto-reloader.conf':
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.ASGLaunchConfiguration.Metadata.AWS::CloudFormation::Init.02_config-amazon-cloudwatch-agent
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackId} --resource ASGLaunchConfiguration --region ${AWS::Region} --configsets UpdateEnvironment
runas=root
mode: '000400'
owner: root
group: root
'/lib/systemd/system/cfn-hup.service':
content: |
[Unit]
Description=cfn-hup daemon
[Service]
Type=simple
ExecStart=/opt/aws/bin/cfn-hup
Restart=always
[Install]
WantedBy=multi-user.target
commands:
01enable_cfn_hup:
command: |
systemctl enable cfn-hup.service
02start_cfn_hup:
command: |
systemctl start cfn-hup.service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment