CASE STUDY | CREYENTE INFOTECH

Run a Command Automatically When New EC2 Instances Created

Using EventBridge and Systems Manager

How Creyente designed an event-driven automation solution that automatically executes PowerShell or Shell scripts on EC2 instances immediately after creation using AWS EventBridge and Systems Manager.

100% Automated
10 Min Setup Time
Zero Manual Steps
Scroll to explore

Architecture Overview

The solution leverages AWS EventBridge to capture EC2 creation events and automatically trigger Systems Manager automation documents to execute commands on newly created instances.

EC2 EventBridge Automation Architecture

Introduction

This case study explains how to automatically run PowerShell or Shell Script commands on EC2 instances whenever they are created with a specific name prefix, using AWS Systems Manager Automation and EventBridge.

The solution provides a serverless, event-driven approach that eliminates manual intervention and ensures consistent setup across all new instances. EventBridge captures EC2 creation events and triggers Systems Manager to execute the required commands automatically.

This automation pattern is particularly valuable for organizations managing large-scale infrastructure where manual configuration becomes impractical and error-prone.

AWS Automation Architecture

What is EventBridge?

EventBridge is a serverless service that uses events to connect application components together, making it easier to construct scalable event-driven applications. Building loosely coupled software systems that communicate by emitting and reacting to events is known as event-driven architecture.

EventBridge routes events from sources such as AWS services, home-grown applications, and third-party software to consumer applications across your organization, providing simple and consistent ways to ingest, filter, transform, and deliver events.

Key Benefits

Solution Architecture Flow

The solution follows a structured event-driven flow that automatically detects EC2 creation events and executes predefined commands without manual intervention.

EC2 Instance Creation

Authorized AWS user creates an EC2 instance through console, CLI, or API with specific naming pattern

CloudTrail Event Logging

AWS API call (event) is automatically registered in CloudTrail and sent to EventBridge default bus

Event Pattern Matching

EventBridge evaluates the event against defined rules and matches instance name prefix and platform type

SSM Automation Trigger

Rule triggers SSM Automation document, passing AssumeRole and InstanceID parameters

Command Execution

SSM document executes RunCommand on EC2 instance, running predefined PowerShell or Shell scripts

Prerequisites

Before implementing this automation solution, ensure you have the necessary AWS resources and permissions in place. The solution requires specific IAM roles, policies, and service configurations to function properly.

AWS Account
Active AWS account with appropriate service limits
IAM Permissions
Create IAM Roles & Policies, EventBridge Rules, SSM Documents
EC2 Permissions
Launch EC2 instances and attach IAM roles
CloudTrail Access
View CloudTrail logs and event history

Implementation Steps

The implementation follows a structured approach with three main phases: IAM setup, SSM document creation, and EventBridge rule configuration. Each step builds upon the previous one to create a complete automation pipeline.

Step 1: Create IAM Policies and Roles

Create custom policies and three IAM roles: EventBridge role, SSM automation assume role, and EC2 instance role for SSM Agent communication

Step 2: Create SSM Document

Build SSM automation document with sleep step (10 minutes for instance registration) and RunCommandOnInstance step for script execution

Step 3: Create EventBridge Rule

Configure EventBridge rule with event pattern matching EC2 instance name prefix and platform, targeting the SSM document

Step 4: Testing & Validation

Create test EC2 instances with matching name pattern and verify automatic command execution

Step 5: Monitoring & Optimization

Monitor execution logs, optimize timing parameters, and extend patterns for additional use cases

Technologies / Services Used

The solution leveraged a carefully selected set of AWS services to deliver a robust, scalable, and cost-effective automation platform.

Compute & Scaling
Amazon EC2 Auto Scaling Groups
Lifecycle Hooks
Messaging
Amazon SNS
Amazon SQS
Compute
AWS Lambda
DNS
Amazon Route 53
Management
EC2 Metadata & Tagging APIs

Key Benefits

This automation solution delivers significant operational benefits by eliminating manual configuration steps and ensuring consistent instance setup across the entire infrastructure. The event-driven approach provides real-time responsiveness while maintaining reliability and scalability.

Fully Automated Instance Configuration
Eliminates manual intervention by automatically executing required scripts on every new EC2 instance matching the defined naming pattern.
Consistent Configuration Management
Ensures all instances receive identical configuration, reducing configuration drift and improving system reliability.
Real-time Event Processing
EventBridge processes EC2 creation events in real-time, triggering automation within seconds of instance launch.
Serverless and Cost-Effective
No infrastructure to manage, pay only for actual usage, and automatic scaling based on demand.
Flexible Pattern Matching
EventBridge rules can be customized to match specific instance names, platforms, or other attributes for targeted automation.
Enhanced Security and Compliance
Automated security configurations ensure compliance requirements are met consistently across all instances.

Conclusion

Instant Automation

Create any Windows EC2 instance with name "ssm-automation-ec2-ANYNAME" and watch automatic script execution

SSM Agent Required

Ensure SSM agent role is attached to EC2 instances for proper communication with Systems Manager

Scalable Solution

Architecture scales automatically with your infrastructure growth, handling hundreds of instances seamlessly

Framework

Easily extend to support different platforms, naming patterns, or additional automation workflows

Business Outcomes

The EventBridge automation solution delivers significant operational benefits by eliminating manual configuration steps and ensuring consistent instance setup across the entire infrastructure.

  • Fully Automated Configuration eliminates manual intervention by automatically executing required scripts on every new EC2 instance matching the defined naming pattern
  • Consistent Setup Management ensures all instances receive identical configuration, reducing configuration drift and improving system reliability
  • Real-time Event Processing with EventBridge processing EC2 creation events in real-time, triggering automation within seconds of instance launch
  • Serverless and Cost-Effective with no infrastructure to manage, pay only for actual usage, and automatic scaling based on demand
  • Enhanced Security and Compliance through automated security configurations that ensure compliance requirements are met consistently across all instances

Implementation Ready: This EventBridge automation solution is production-ready and can be deployed immediately. Create any Windows EC2 instance with name "ssm-automation-ec2-ANYNAME" and watch automatic script execution in action, ensuring consistent configuration across your entire infrastructure.

EventBridge
EventBridge
New Instance
Configuring
Configured

Creyente's Expertise

Creyente specializes in designing and implementing serverless automation solutions that solve complex infrastructure challenges. Our expertise in AWS services, event-driven architectures, and infrastructure automation enables us to deliver elegant, scalable solutions that reduce operational overhead while improving reliability.

This Auto Scaling Group automation project demonstrates our ability to understand client pain points, design appropriate technical solutions, and deliver production-ready implementations that provide immediate value while remaining maintainable and adaptable for future needs.

💬 No comments yet. Be the first to comment!

Write a comment
Your email address will not be published. Required fields are marked *
Security token: Active ✓
Scroll