Skip to content

I want to Automate Amazon CloudWatch Alarms with AWS Systems Manager

0

Hi everyone,

I'm automating CloudWatch custom metric alarms using AWS Lambda and Systems Manager, following this guide: "https://aws.amazon.com/blogs/mt/automating-amazon-cloudwatch-alarms-with-aws-systems-manager/". However, I'm encountering an issue with the Lambda handler version. Additionally, after manually editing the CloudFormation YAML file to version 3.12, I get a parsing error on line 143.3 of the YAML code.

2 Answers
3
Accepted Answer

Strange behavior of CloudFormation, even without changing Lambda runtime (just taking the original file), if you click Template source - Upload a template file if shows the error:

Template format error
Follow the standard JSON or YAML spec to format your template.Learn more 
Parser error
duplicated mapping key (143:3)

 140 |               - "TerminateCloudWa ...
 141 |               - "Arn"
 142 |           Id: "TerminateCloudWatc ...
 143 |   PermissionForEventsToInvokeLambda: 
---------^
 144 |     Type: AWS::Lambda::Permission
 145 |     Properties: 

but if you put the same template into Application Composer and validate it, it's completely fine and you can change Lambda runtime and successfully deploy the stack

Enter image description here

Enter image description here

also lines 111 and 143 have the same resource name PermissionForEventsToInvokeLambda, that's why parsing fails in the first case. Seems that Application composer ignores this.

You can rename one of PermissionForEventsToInvokeLambda to something else and Parsing error will be fixed

Maybe it was not a problem for CloudFormation in 2020 (when the mentioned blog post was written), but later AWS added some extra validation steps

EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Hi Oleksii,

    Thank you for your input. I have already resolved the initial issue, but I am encountering a new problem. When testing the code in Lambda, I am receiving an error. Additionally, the CloudWatch agent is not being configured on my Windows and Linux servers. When I test manually from Lambda, it sets up CPU, memory, and disk alerts, but custom metric alerts show insufficient data due to the lack of an automated CloudWatch agent installation.

    /var/task/lambda_cloudwatch_alarms.py:210: SyntaxWarning: invalid escape sequence '\P' "cd “C:\Program Files\Amazon\AmazonCloudWatchAgent\”", ".\amazon-cloudwatch-agent-ctl.ps1 -a fetch-config -m ec2 -c ssm:AmazonCloudWatch-windows", "" START RequestId: 9d1d00c3-e7b2-41b7-80cc-c1cb1b24be4c Version: $LATEST installing and configuring Cloudwatch agents on the instance : windows ========== Instances to install Cloudwatch Agents: i-0702a0a********** ============RunCommand sent successfully Creating Alarms for instance :windows END RequestId: 9d1d00c3-e7b2-41b7-80cc-c1cb1b24be4c REPORT RequestId: 9d1d00c3-e7b2-41b7-80cc-c1cb1b24be4c Duration: 1268.07 ms Billed Duration: 1269 ms Memory Size: 1024 MB Max Memory Used: 98 MB Init Duration: 550.49 ms

1

Can you send your yaml and the error message?

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.