Tutorial: Use EventBridge to relay events to AWS Systems Manager Run Command - Amazon EventBridge

Tutorial: Use EventBridge to relay events to AWS Systems Manager Run Command

You can use Amazon EventBridge to invoke AWS Systems Manager Run Command and perform actions on Amazon EC2 instances when specific events happen. In this tutorial, you set up Systems Manager Run Command to run shell commands and configure each new instance that is launched in an Amazon EC2 Auto Scaling group.


This tutorial assumes that you assigned a tag to the Amazon EC2 Auto Scaling group, with environment as the key and production as the value.

Step 1: Create a Rule

Create a rule to run shell commands and configure new Amazon EC2 instances.

To create the EventBridge rule

  1. Open the Amazon EventBridge console at https://console.aws.amazon.com/events/.

  2. In the navigation pane, choose Rules.

  3. Choose Create rule.

  4. Enter a name and description for the rule.

  5. For Define pattern, do the following:

    1. Choose Event pattern.

    2. Choose Pre-defined pattern by service.

    3. For Service provider, choose AWS.

    4. For Service Name, choose Auto Scaling.

    5. For Event type, choose Instance Launch and Terminate.

    6. Choose Specific instance event(s), EC2 Instance-launch Lifecycle Action.

    7. By default, the rule matches any Amazon EC2 Auto Scaling group in the Region. To make the rule match a specific group, choose Specific group name(s) and select one or more groups.

  6. For Select event bus, choose AWS default event bus. When an AWS service in your account emits an event, it goes to your account’s default event bus.

  7. For Target, select SSM Run Command .

  8. For Document, choose AWS-RunShellScript.

  9. For Target key, type tag:environment. For Target value(s), enter production and choose Add.

  10. Under Configure automation parameter(s), do the following:

    1. Choose Constant.

    2. For Commands, enter a shell command and choose Add. Repeat this step for all commands you want to run when an instance launches.

    3. If necessary, enter the appropriate information in WorkingDirectory and ExecutionTimeout.

  11. Do one of the following:

    • To create an IAM role automatically, choose Create a new role for this specific resource. EventBridge creates the IAM role needed for your event to run.

    • To use an IAM role that you created before, choose Use existing role

  12. Choose Create rule.

Step 2: Confirm success

If you see the rule in the list of rules, you've successfully created the rule.

Step 3: Clean up your resources

You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

To delete the EventBridge rule(s)

  1. Open the Rules page of the EventBridge console.

  2. Select the rule(s) that you created.

  3. Choose Delete.

  4. Choose Delete.