[AWS] Lab: Lambda - build a Chaos monkey
Solution Outline
To run our function we use a service called CloudWatch, which periodically generates events. Those events are sent to the Lambda function, and on each event, the Lambda function gets a list of instances from the environment and will randomly terminate one of them. (see below the summary diagram)
You can find the code for this demo in the course repo: https://github.com/udacity/cloud-developer/tree/master/course-04/exercises/c4-demos-master/04-chaos-monkey
In the 04-chaos-monkey
directory install the dependencies:
npm install
and then zip the folder so that it can be uploaded.
zip -r chaos-monkey.zip .
Create a Lambda function
1. Create a empty Node.js v10.x lambda function
2. Upload
chaos-monkey.zip
Get some running EC2 instances
Following the post: Host a Static Webpage, running two EC2 instance with public HTTP access.
Create a CloudWatch Event
So every 1 min, it will trigger Lambda `chaos-monkey` function.
After 1 min, we can see that Function has been triggered but with error result:
We can fix this by add EC2's List and TerminateInstance policy to the Role.
Fix IAM policy
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": "logs:CreateLogGroup", "Resource": "arn:aws:logs:us-east-1:645307551852:*" }, { "Effect": "Allow", "Action": [ "ec2:TerminateInstances", "ec2:DescribeInstances" ], "Resource": "*" } ] }
After that, it should work as expected. Every 1 min, cloudwatch will trigger a lambda function to turn off EC2 instances that are running. You can configure EC2 that it will automatcilly bring up again.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-04-18 [Nginx] Configuration for SPA
2019-04-18 [Unit Testing] Using Mockito Annotations - @Mock, @InjectMocks, @RunWith
2019-04-18 [Functional Programming] Using Lens to update nested object
2018-04-18 [ES6] Proxy & Reflect
2018-04-18 [Angular] Service Worker Version Management
2018-04-18 [Angular] Upgrade existing Angular app to Progressive Web App
2018-04-18 [HTML5] How Visible vs. Hidden Elements Affect Keyboard/Screen Reader Users (ARIA)