如何在AWS中部署并运行Windows Service以实现定时任务?
Hey there! Since you're new to AWS and need to run a .NET Windows Service or C# script on a daily schedule as a background process, let me walk you through a straightforward, step-by-step approach that fits your needs.
核心方案:EC2 Windows实例 + EventBridge + Systems Manager Run Command
This combo lets you keep your existing Windows-based workload while using AWS's scheduling tools to automate daily runs—no fancy CLI expertise required if you don't want it.
1. 准备你的EC2 Windows实例
- Spin up a Windows Server EC2 instance (start with a
t2.microif you qualify for the Free Tier—it's more than enough for most light scripts/services). - Upload your .NET Windows Service installer or C# script files to the instance: you can use RDP to connect and copy files directly, or use S3 to transfer them securely.
- Test manually first: Install the Windows Service and confirm it starts/runs correctly, or execute your C# script directly to make sure it works without errors. This saves you troubleshooting headaches later!
2. 设置EventBridge(调度触发器)
EventBridge is AWS's cloud-based task scheduler—think of it as a supercharged version of Windows Task Scheduler, but managed in the cloud.
- Open the AWS Console, search for EventBridge, and navigate to the service.
- Click Create rule:
- Name your rule (e.g., "Daily-DotNet-Service-Run") and add a simple description for future reference.
- Under Rule type, select Schedule.
- For the schedule, choose Cron expression and enter your desired daily time. For example:
0 9 * * ? *= Runs daily at 9 AM UTC (adjust the numbers to match your timezone—remember to account for UTC offsets!).
- Click Next to set up the target action.
3. 配置Run Command来执行你的任务
We'll use AWS Systems Manager Run Command to trigger your service/script on the EC2 instance securely:
- Under Target types, select AWS service, then choose Systems Manager Run Command as the target.
- For Document, pick
AWS-RunPowerShellScript(since we're working with Windows, PowerShell is our go-to tool for executing commands). - In Command parameters, enter the PowerShell command tailored to your workload:
- If it's a Windows Service:
Start-Service -Name "YourServiceName"(replace with your actual service name from the Windows Services list) - If it's a C# console script:
& "C:\Full\Path\To\YourScript.exe"
- If it's a Windows Service:
- Under Targets, select your EC2 instance (make sure the instance has an IAM role with
AmazonSSMManagedInstanceCorepermissions—this lets AWS communicate with the instance to run commands). - Finish creating the rule, and you're set!
4. 验证和测试
- To confirm everything works, go to your EventBridge rule and click Run command to trigger it manually.
- Check the Systems Manager > Run Command page to see if the command succeeded (look for a "Success" status).
- On your EC2 instance, verify the service started or script ran: check Event Viewer for service logs, or look for script output files if you've set up logging.
替代方案:无服务器Lambda(如果你的脚本可以适配)
If your C# script doesn't strictly need to run as a Windows Service, you can port it to AWS Lambda (supports .NET Core/.NET 6+), then use EventBridge to trigger it daily. This means you don't have to manage an EC2 instance at all—AWS handles all the infrastructure. Just keep in mind Lambda has a maximum execution time of 15 minutes, so it's best for shorter-running scripts.
内容的提问来源于stack exchange,提问作者vibhav bhavsar

