Creating an AWS CloudWatch Monitoring Service

This reference shows how to use Pulumi to define an AWS CloudWatch resource using pure code which can then be deployed to AWS and managed as infrastructure as code.

What is AWS CloudWatch?


AWS CloudWatch is a monitoring service for AWS cloud resources and the applications you run on Amazon Web Services.. Find out more at AWS here.

Create an AWS CloudWatch resource using @pulumi/aws

The @pulumi/aws library enables fine-grained control over the AWS CloudWatch resource meaning it can be coded, deployed, and managed entirely in code.

const aws = require("@pulumi/aws");

const dashboard = new aws.cloudwatch.Dashboard("mydashboard", {
    dashboardName: "my-dashboard",
    dashboardBody: JSON.stringify({
        widgets: [
                type: "metric",
                x: 0,
                y: 0,
                width: 12,
                height: 6,
                properties: {
                    metrics: [
                    period: 300,
                    stat: "Average",
                    region: "us-east-1",
                    title: "EC2 Instance CPU"
                type: "text",
                x: 0,
                y: 7,
                width: 3,
                height: 3,
                properties: {
                    markdown: "Hello world"

const loginsTopic = new aws.sns.Topic("myloginstopic");

const eventRule = new aws.cloudwatch.EventRule("myeventrule", {
    eventPattern: JSON.stringify({
        "detail-type": [
            "AWS Console Sign In via CloudTrail"

const eventTarget = new aws.cloudwatch.EventTarget("myeventtarget", {
    targetId: "SendToSNS",
    arn: loginsTopic.arn

const logGroup = new aws.cloudwatch.LogGroup("myloggroup");

const logMetricFilter = new aws.cloudwatch.LogMetricFilter("mylogmetricfilter", {
    pattern: "",
    metricTransformation: {
        name: "EventCount",
        namespace: "YourNamespace",
        value: "1"

const logStream = new aws.cloudwatch.LogStream("mylogstream", {

const metricAlarm = new aws.cloudwatch.MetricAlarm("mymetricalarm", {
    comparisonOperator: "GreaterThanOrEqualToThreshold",
    evaluationPeriods: 2,
    metricName: "CPUUtilization",
    namespace: "AWS/EC2",
    period: 120,
    statistic: "Average",
    threshold: 80,
    alarmDescription: "This metric monitors ec2 cpu utilization"
Pulumi Deployments: the fastest way to go from code to cloud. Join the Preview