

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 AWS CloudFormation 模板 GuardDuty 有条件地启用 Amazon
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates"></a>

*Ram Kandaswamy，Amazon Web Services*

## Summary
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-summary"></a>

[AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)是一款基础设施即代码 (IaC) 工具，可帮助您通过基于模板的部署来管理 AWS 资源。 CloudFormation 通常用于管理 AWS 资源。使用它来启用 AWS 服务（例如 [Amazon GuardDuty](https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html)）可能会带来独特的挑战。 GuardDuty 是一项威胁检测服务，可持续监控您的 AWS 账户 恶意活动和未经授权的行为。与可以多次创建的典型资源不同， GuardDuty 这是一项需要为每个账户启用一次的服务，而且 AWS 区域。传统 CloudFormation 条件仅支持静态值比较，因此很难检查服务的当前状态，例如 GuardDuty。如果您尝试在已处于活动状态的账户 CloudFormation 中启用 GuardDuty 直通，堆栈部署将失败。这可能会给管理多账户环境的 DevOps 团队带来运营挑战。

这种模式引入了应对这一挑战的解决方案。它使用由[AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html)函数支持的 CloudFormation 自定义资源来执行动态状态检查。条件逻辑 GuardDuty 只有在尚未启用的情况下才会启用。它使用堆栈输出来记录 GuardDuty 状态以备将来参考。

通过遵循这种模式，您可以自动 GuardDuty 部署整个 AWS 基础架构，同时保持干净、可预测的 CloudFormation 堆栈操作。这种方法对于符合以下条件的组织特别有价值：
+  AWS 账户 通过 IaC 管理多个
+ 大规模实施安全服务
+ 需要进行等性基础架构部署
+ 自动部署安全服务

## 先决条件和限制
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-prereqs"></a>

**先决条件**
+ 活跃的 AWS 账户
+ 有权创建、更新和删除 CloudFormation 堆栈的 AWS Identity and Access Management (IAM) 角色
+ AWS Command Line Interface (AWS CLI)，[已安装](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html#getting-started-install-instructions)并[配置](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)

**限制**

如果 GuardDuty 已为 AWS 账户 或手动禁用此模式 AWS 区域，则不会 GuardDuty 为该目标账户或区域启用此模式。

## 架构
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-architecture"></a>

**目标技术堆栈**

该模式 CloudFormation 用于基础设施即代码 (IaC)。您可以使用由 Lambda 函数支持的 CloudFormation 自定义资源来实现动态服务启用功能。

**目标架构**

以下高级架构图显示了 GuardDuty 通过部署 CloudFormation 模板实现启用的过程：

![\[使用 CloudFormation 堆栈在 AWS 账户 GuardDuty 中启用。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/3abd7cb7-0937-41fe-8eaa-79aedb182732/images/71624052-eebc-474a-9aa3-8606d87fc51d.png)


1. 您可以部署 CloudFormation 模板来创建 CloudFormation 堆栈。

1. 堆栈创建 IAM 角色和 Lambda 函数。

1. Lambda 函数代入 IAM 角色。

1. 如果尚未 GuardDuty 在目标上启用 AWS 账户，则 Lambda 函数将其启用。

**自动化和扩展**

您可以使用该 AWS CloudFormation StackSet 功能将此解决方案扩展到多个 AWS 账户 和 AWS 区域。有关更多信息，[请参阅 CloudFormation 文档 AWS CloudFormation StackSets中的使用](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)。

## 工具
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-tools"></a>
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一个开源工具，可帮助您 AWS 服务 通过命令行外壳中的命令进行交互。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html)帮助您设置 AWS 资源，快速一致地配置资源，并在资源的整个生命周期中跨地区对其 AWS 账户 进行管理。
+ [Amazon GuardDuty](https://docs.aws.amazon.com/guardduty/latest/ug/what-is-guardduty.html) 是一项持续的安全监控服务，可分析和处理日志，以识别您的 AWS 环境中意外和可能未经授权的活动。
+ [AWS Identity and Access Management (IAM)](https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html) 通过控制谁经过身份验证并有权使用 AWS 资源，从而帮助您安全地管理对资源的访问权限。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) 是一项计算服务，可帮助您运行代码，无需预调配或管理服务器。它只在需要时运行您的代码，并自动进行扩展，因此您只需为使用的计算时间付费。

## 操作说明
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-epics"></a>

### 创建 CloudFormation 模板并部署堆栈
<a name="create-the-cfnshort-template-and-deploy-the-stack"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 将代码存储在亚马逊 S3 中。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.html) | AWS DevOps | 
| 创建 CloudFormation 模板。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.html) | AWS DevOps | 
| 创建 CloudFormation 堆栈。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.html) | AWS DevOps | 
| 验证 GuardDuty 是否已为启用 AWS 账户。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates.html) | 云管理员、AWS 管理员 | 
| 配置其他账户或区域。 | 根据您的用例需要，使用该 CloudFormation StackSet 功能将此解决方案扩展到多个 AWS 账户 和 AWS 区域。有关更多信息，[请参阅 CloudFormation 文档 AWS CloudFormation StackSets中的使用](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/what-is-cfnstacksets.html)。 | 云管理员、AWS 管理员 | 

## 相关资源
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-resources"></a>

**参考**
+ [AWS CloudFormation 文档](https://docs.aws.amazon.com/cloudformation/index.html)
+ [AWS Lambda 资源类型参考](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/AWS_Lambda.html)
+ [CloudFormation 资源类型:: AWS: IAM:: 角色](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-iam-role.html)
+ [CloudFormation 资源类型:: AWS:GuardDuty: 探测器](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-guardduty-detector.html)
+ [使用 AWS CloudFormation（博客文章）检索任何 AWS 服务属性的四种方法](https://aws.amazon.com/blogs/mt/four-ways-to-retrieve-any-aws-service-property-using-aws-cloudformation-part-1/)

**教程和视频**
+ [使用 AWS CloudFormation简化基础设施管理](https://www.youtube.com/watch?v=1h-GPXQrLZw)（教程）
+ [使用亚马逊 GuardDuty 并 AWS Security Hub CSPM 保护多个账户](https://www.youtube.com/watch?v=Rg2ZzAAi1nY)（re AWS : Invent 2020）
+ [创作最佳实践 AWS CloudFormation（re AWS : Inv](https://www.youtube.com/watch?v=bJHHQM7GGro) ent 2019）
+ [威胁检测开启 AWS：亚马逊简介 GuardDuty（r](https://www.youtube.com/watch?v=czsuZXQvD8E) e AWS : inForce 2019）

## 附加信息
<a name="enable-amazon-guardduty-conditionally-by-using-aws-cloudformation-templates-additional"></a>

**Python 代码**

```
import boto3
import os
import json
from botocore.exceptions import ClientError
import cfnresponse
guardduty=boto3.client('guardduty')
cfn=boto3.client('cloudformation')
def lambda_handler(event, context):
    print('Event: ', event)
    if 'RequestType' in event:    
      if event['RequestType'] in ["Create","Update"]:
          enabled=False
          try:  
            response=guardduty.list_detectors()
            if "DetectorIds" in response and len(response["DetectorIds"])>0:
              enabled="AlreadyEnabled"
            elif "DetectorIds" in response and len(response["DetectorIds"])==0:
              cfn_response=cfn.create_stack(
                StackName='guardduty-cfn-stack',
                TemplateBody='{ "AWSTemplateFormatVersion": "2010-09-09",    "Description": "Guard duty creation template",    "Resources": { "IRWorkshopGuardDutyDetector": {  "Type": "AWS::GuardDuty::Detector",    "Properties": {   "Enable": true  }   } } }'
                )
              enabled="True"
          except Exception as e:
              print("Exception: ",e)
          responseData = {}
          responseData['status'] = enabled
          cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData, "CustomResourcePhysicalID" )
      elif event['RequestType'] == "Delete":
          cfn_response=cfn.delete_stack(
                  StackName='guardduty-cfn-stack')
          cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
```

