[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.

 

posted @   Zhentiw  阅读(79)  评论(0编辑  收藏  举报
编辑推荐:
· 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)
点击右上角即可分享
微信分享提示