首页 > 其他 > 详细

[AWS] Lab: Lambda - build a Chaos monkey

时间:2021-04-18 21:38:28      阅读:23      评论:0      收藏:0      [点我收藏+]

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.

 

[AWS] Lab: Lambda - build a Chaos monkey

原文:https://www.cnblogs.com/Answer1215/p/14649756.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!