EventRule class

Provides an EventBridge Rule resource.

> Note: EventBridge was formerly known as CloudWatch Events. The functionality is identical.

Example Usage

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

const console = new aws.cloudwatch.EventRule("console", {
    name: "capture-aws-sign-in",
    description: "Capture each AWS Console Sign In",
    eventPattern: JSON.stringify({
        "detail-type": ["AWS Console Sign In via CloudTrail"],
    }),
});
const awsLogins = new aws.sns.Topic("aws_logins", {name: "aws-console-logins"});
const sns = new aws.cloudwatch.EventTarget("sns", {
    rule: console.name,
    targetId: "SendToSNS",
    arn: awsLogins.arn,
});
const snsTopicPolicy = aws.iam.getPolicyDocumentOutput({
    statements: [{
        principals: [{
            type: "Service",
            identifiers: ["events.amazonaws.com"],
        }],
        effect: "Allow",
        actions: ["SNS:Publish"],
        resources: [awsLogins.arn],
    }],
});
const _default = new aws.sns.TopicPolicy("default", {
    arn: awsLogins.arn,
    policy: snsTopicPolicy.json,
});
import pulumi
import json
import pulumi_aws as aws

console = aws.cloudwatch.EventRule("console",
    name="capture-aws-sign-in",
    description="Capture each AWS Console Sign In",
    event_pattern=json.dumps({
        "detail-type": ["AWS Console Sign In via CloudTrail"],
    }))
aws_logins = aws.sns.Topic("aws_logins", name="aws-console-logins")
sns = aws.cloudwatch.EventTarget("sns",
    rule=console.name,
    target_id="SendToSNS",
    arn=aws_logins.arn)
sns_topic_policy = aws.iam.get_policy_document_output(statements=[{
    "principals": [{
        "type": "Service",
        "identifiers": ["events.amazonaws.com"],
    }],
    "effect": "Allow",
    "actions": ["SNS:Publish"],
    "resources": [aws_logins.arn],
}])
default = aws.sns.TopicPolicy("default",
    arn=aws_logins.arn,
    policy=sns_topic_policy.json)
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using Pulumi;
using Aws = Pulumi.Aws;

return await Deployment.RunAsync(() =>
{
    var console = new Aws.CloudWatch.EventRule("console", new()
    {
        Name = "capture-aws-sign-in",
        Description = "Capture each AWS Console Sign In",
        EventPattern = JsonSerializer.Serialize(new Dictionary<string, object?>
        {
            ["detail-type"] = new[]
            {
                "AWS Console Sign In via CloudTrail",
            },
        }),
    });

    var awsLogins = new Aws.Sns.Topic("aws_logins", new()
    {
        Name = "aws-console-logins",
    });

    var sns = new Aws.CloudWatch.EventTarget("sns", new()
    {
        Rule = console.Name,
        TargetId = "SendToSNS",
        Arn = awsLogins.Arn,
    });

    var snsTopicPolicy = Aws.Iam.GetPolicyDocument.Invoke(new()
    {
        Statements = new[]
        {
            new Aws.Iam.Inputs.GetPolicyDocumentStatementInputArgs
            {
                Principals = new[]
                {
                    new Aws.Iam.Inputs.GetPolicyDocumentStatementPrincipalInputArgs
                    {
                        Type = "Service",
                        Identifiers = new[]
                        {
                            "events.amazonaws.com",
                        },
                    },
                },
                Effect = "Allow",
                Actions = new[]
                {
                    "SNS:Publish",
                },
                Resources = new[]
                {
                    awsLogins.Arn,
                },
            },
        },
    });

    var @default = new Aws.Sns.TopicPolicy("default", new()
    {
        Arn = awsLogins.Arn,
        Policy = snsTopicPolicy.Apply(getPolicyDocumentResult => getPolicyDocumentResult.Json),
    });

});
package main

import (
	"encoding/json"

	"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/cloudwatch"
	"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/iam"
	"github.com/pulumi/pulumi-aws/sdk/v7/go/aws/sns"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		tmpJSON0, err := json.Marshal(map[string][]string{
			"detail-type": []string{
				"AWS Console Sign In via CloudTrail",
			},
		})
		if err != nil {
			return err
		}
		json0 := string(tmpJSON0)
		console, err := cloudwatch.NewEventRule(ctx, "console", &cloudwatch.EventRuleArgs{
			Name:         pulumi.String("capture-aws-sign-in"),
			Description:  pulumi.String("Capture each AWS Console Sign In"),
			EventPattern: pulumi.String(json0),
		})
		if err != nil {
			return err
		}
		awsLogins, err := sns.NewTopic(ctx, "aws_logins", &sns.TopicArgs{
			Name: pulumi.String("aws-console-logins"),
		})
		if err != nil {
			return err
		}
		_, err = cloudwatch.NewEventTarget(ctx, "sns", &cloudwatch.EventTargetArgs{
			Rule:     console.Name,
			TargetId: pulumi.String("SendToSNS"),
			Arn:      awsLogins.Arn,
		})
		if err != nil {
			return err
		}
		snsTopicPolicy := iam.GetPolicyDocumentOutput(ctx, iam.GetPolicyDocumentOutputArgs{
			Statements: iam.GetPolicyDocumentStatementArray{
				&iam.GetPolicyDocumentStatementArgs{
					Principals: iam.GetPolicyDocumentStatementPrincipalArray{
						&iam.GetPolicyDocumentStatementPrincipalArgs{
							Type: pulumi.String("Service"),
							Identifiers: pulumi.StringArray{
								pulumi.String("events.amazonaws.com"),
							},
						},
					},
					Effect: pulumi.String("Allow"),
					Actions: pulumi.StringArray{
						pulumi.String("SNS:Publish"),
					},
					Resources: pulumi.StringArray{
						awsLogins.Arn,
					},
				},
			},
		}, nil)
		_, err = sns.NewTopicPolicy(ctx, "default", &sns.TopicPolicyArgs{
			Arn:    awsLogins.Arn,
			Policy: snsTopicPolicy.Json(),
		})
		if err != nil {
			return err
		}
		return nil
	})
}
pulumi {
  required_providers {
    aws = {
      source = "pulumi/aws"
    }
  }
}

data "aws_iam_getpolicydocument" "snsTopicPolicy" {
  statements {
    principals {
      type        = "Service"
      identifiers = ["events.amazonaws.com"]
    }
    effect    = "Allow"
    actions   = ["SNS:Publish"]
    resources = [aws_sns_topic.aws_logins.arn]
  }
}

resource "aws_cloudwatch_eventrule" "console" {
  name        = "capture-aws-sign-in"
  description = "Capture each AWS Console Sign In"
  event_pattern = jsonencode({
    "detail-type" = ["AWS Console Sign In via CloudTrail"]
  })
}
resource "aws_cloudwatch_eventtarget" "sns" {
  rule      = aws_cloudwatch_eventrule.console.name
  target_id = "SendToSNS"
  arn       = aws_sns_topic.aws_logins.arn
}
resource "aws_sns_topic" "aws_logins" {
  name = "aws-console-logins"
}
resource "aws_sns_topicpolicy" "default" {
  arn    = aws_sns_topic.aws_logins.arn
  policy = data.aws_iam_getpolicydocument.snsTopicPolicy.json
}
package generated_program;

import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.cloudwatch.EventRule;
import com.pulumi.aws.cloudwatch.EventRuleArgs;
import com.pulumi.aws.sns.Topic;
import com.pulumi.aws.sns.TopicArgs;
import com.pulumi.aws.cloudwatch.EventTarget;
import com.pulumi.aws.cloudwatch.EventTargetArgs;
import com.pulumi.aws.iam.IamFunctions;
import com.pulumi.aws.iam.inputs.GetPolicyDocumentArgs;
import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementArgs;
import com.pulumi.aws.iam.inputs.GetPolicyDocumentStatementPrincipalArgs;
import com.pulumi.aws.sns.TopicPolicy;
import com.pulumi.aws.sns.TopicPolicyArgs;
import static com.pulumi.codegen.internal.Serialization.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class App {
    public static void main(String[] args) {
        Pulumi.run(App::stack);
    }

    public static void stack(Context ctx) {
        var console = new EventRule("console", EventRuleArgs.builder()
            .name("capture-aws-sign-in")
            .description("Capture each AWS Console Sign In")
            .eventPattern(serializeJson(
                jsonObject(
                    jsonProperty("detail-type", jsonArray("AWS Console Sign In via CloudTrail"))
                )))
            .build());

        var awsLogins = new Topic("awsLogins", TopicArgs.builder()
            .name("aws-console-logins")
            .build());

        var sns = new EventTarget("sns", EventTargetArgs.builder()
            .rule(console.name())
            .targetId("SendToSNS")
            .arn(awsLogins.arn())
            .build());

        final var snsTopicPolicy = IamFunctions.getPolicyDocument(GetPolicyDocumentArgs.builder()
            .statements(GetPolicyDocumentStatementArgs.builder()
                .principals(GetPolicyDocumentStatementPrincipalArgs.builder()
                    .type("Service")
                    .identifiers("events.amazonaws.com")
                    .build())
                .effect("Allow")
                .actions("SNS:Publish")
                .resources(awsLogins.arn())
                .build())
            .build());

        var default_ = new TopicPolicy("default", TopicPolicyArgs.builder()
            .arn(awsLogins.arn())
            .policy(snsTopicPolicy.applyValue(_snsTopicPolicy -> _snsTopicPolicy.json()))
            .build());

    }
}
resources:
  console:
    type: aws:cloudwatch:EventRule
    properties:
      name: capture-aws-sign-in
      description: Capture each AWS Console Sign In
      eventPattern:
        fn::toJSON:
          detail-type:
            - AWS Console Sign In via CloudTrail
  sns:
    type: aws:cloudwatch:EventTarget
    properties:
      rule: ${console.name}
      targetId: SendToSNS
      arn: ${awsLogins.arn}
  awsLogins:
    type: aws:sns:Topic
    name: aws_logins
    properties:
      name: aws-console-logins
  default:
    type: aws:sns:TopicPolicy
    properties:
      arn: ${awsLogins.arn}
      policy: ${snsTopicPolicy.json}
variables:
  snsTopicPolicy:
    fn::invoke:
      function: aws:iam:getPolicyDocument
      arguments:
        statements:
          - principals:
              - type: Service
                identifiers:
                  - events.amazonaws.com
            effect: Allow
            actions:
              - SNS:Publish
            resources:
              - ${awsLogins.arn}

Import

Identity Schema

Required

  • name (String) Name of the EventBridge rule.

Optional

  • accountId (String) AWS Account where this resource is managed.
  • region (String) Region where this resource is managed.

Using pulumi import, import EventBridge Rules using the event_bus_name/rule_name (if you omit eventBusName, the default event bus will be used). For example:

$ pulumi import aws:cloudwatch/eventRule:EventRule example example-event-bus/capture-console-sign-in

Constructors

EventRule(String name, {EventRuleArgs? args, CustomResourceOptions? options})
Creates a new EventRule. name The Pulumi resource name. args Arguments used to configure this EventRule. The set of arguments for EventRule. options Resource options controlling this resource's behavior.
EventRule.reference(String urn)
Creates a typed reference to an existing EventRule resource.

Properties

arn ↔ Output<String>
ARN of the rule.
latefinal
childResources Set<Resource>
finalinherited
completionSources Map<String, IOutputCompletionSource>
latefinalinherited
description ↔ Output<String?>
The description of the rule.
latefinal
eventBusName ↔ Output<String?>
The name or ARN of the event bus to associate with this rule. If you omit this, the default event bus is used.
latefinal
eventPattern ↔ Output<String?>
The event pattern described a JSON object. At least one of scheduleExpression or eventPattern is required. See full documentation of Events and Event Patterns in EventBridge for details. Note: The event pattern size is 2048 by default but it is adjustable up to 4096 characters by submitting a service quota increase request. See Amazon EventBridge quotas for details.
latefinal
forceDestroy ↔ Output<bool?>
Used to delete managed rules created by AWS. Defaults to false.
latefinal
hashCode int
The hash code for this object.
no setterinherited
id ↔ Output<String>
getter/setter pairinherited
isCustom bool
Returns whether this resource is provider-managed.
no setterinherited
isEnabled ↔ Output<bool?>
Whether the rule should be enabled. Defaults to true. Conflicts with state.
latefinal
isProtected bool
Returns whether this resource is protected from deletion.
no setterinherited
isRemote bool
Whether this resource is registered as remote.
no setterinherited
isResourceReference bool
Whether this instance represents a resource value returned over RPC.
finalinherited
name ↔ Output<String>
The name of the rule. If omitted, this provider will assign a random, unique name. Conflicts with namePrefix.
latefinal
namePrefix ↔ Output<String>
Creates a unique name beginning with the specified prefix. Conflicts with name. Note: Due to the length of the generated suffix, must be 38 characters or less.
latefinal
region ↔ Output<String>
Region where this resource will be managed. Defaults to the Region set in the provider configuration.
latefinal
resourceTransforms List<ResourceTransform>
Inherited/explicit async transforms.
no setterinherited
roleArn ↔ Output<String?>
ARN associated with the role that is used for target invocation.
latefinal
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
scheduleExpression ↔ Output<String?>
The scheduling expression. For example, cron(0 20 * * ? *) or rate(5 minutes). At least one of scheduleExpression or eventPattern is required. Can only be used on the default event bus. For more information, refer to the AWS documentation Schedule Expressions for Rules.
latefinal
state ↔ Output<String?>
State of the rule. Valid values are DISABLED, ENABLED, and ENABLED_WITH_ALL_CLOUDTRAIL_MANAGEMENT_EVENTS. When state is ENABLED, the rule is enabled for all events except those delivered by CloudTrail. To also enable the rule for events delivered by CloudTrail, set state to ENABLED_WITH_ALL_CLOUDTRAIL_MANAGEMENT_EVENTS. Defaults to ENABLED. Conflicts with isEnabled.
latefinal
tags ↔ Output<Map<String, String>?>
A map of tags to assign to the resource. .If configured with a provider defaultTags configuration block present, tags with matching keys will overwrite those defined at the provider-level.
latefinal
tagsAll ↔ Output<Map<String, String>>
A map of tags assigned to the resource, including those inherited from the provider defaultTags configuration block.
latefinal
transformations List<ResourceTransformation>
Inherited/explicit legacy transformations.
no setterinherited
urn ↔ Output<String>
latefinalinherited

Methods

failId(Object error) → void
Completes this resource ID with an error when registration fails.
inherited
failOutputs(Object error) → void
Completes all output properties with error.
inherited
failUrn(Object error) → void
Completes this resource URN with an error when registration fails.
inherited
getProvider(String moduleMember) → ProviderResource?
Returns provider for moduleMember's package, if configured.
inherited
getResourceName() String
Returns this resource's logical name.
inherited
getResourceType() String
Returns this resource's Pulumi type token.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
registerOutput<T>(String propertyName, {Object? decoder(Object?)?, bool isSecret = false}) → Output<T>
Registers a dynamic output property for this resource.
inherited
resolveId(String? value, {required bool isKnown}) → void
Resolves the provider-assigned ID for this resource.
inherited
resolveOutputs(Struct outputs) → void
Resolves all output properties from a monitor response payload.
inherited
resolveUrn(String value) → void
Resolves this resource's URN once assigned by the engine.
inherited
serializeProperties(Map<String, dynamic> properties) Future<Struct>
Serializes resource properties for RPC transmission.
inherited
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

get(String name, Input<String> id, {EventRuleState? state, CustomResourceOptions? options}) EventRule
Gets an existing EventRule resource's state with the given name and id.