Here is the code.
import boto3
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.put_metric_alarm(
AlarmName='EC2_CPU_Utilization_Alarm',
AlarmDescription='Alarm when server CPU exceeds 70%',
ActionsEnabled=True,
AlarmActions=[
'arn:aws:sns:us-west-2:123456789012:my-sns-topic'
],
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Statistic='Average',
Dimensions=[
{
'Name': 'InstanceId',
'Value': 'i-0123456789abcdef0'
},
],
Period=300,
EvaluationPeriods=1,
Threshold=70.0,
ComparisonOperator='GreaterThanThreshold'
)
print(response)