This template creates an AWS Events Rule that invokes a specified Lambda function when any EC2 instance's state changes to stopping. The rule is configured with an event pattern that filters for EC2 instance state-change notifications and a target that specifies the Lambda function to invoke.

CloudFormation Template