使用 AWS GuardDuty 模板有条件地启用 A CloudFormation mazon - AWS Prescriptive Guidance

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

使用 AWS GuardDuty 模板有条件地启用 A CloudFormation mazon

创建者:Ram Kandaswamy (AWS)

环境:生产

技术:安全、身份、合规; DevOps;运营

AWS 服务:AWS CloudFormation;亚马逊;AWS Lambda GuardDuty;AWS Identity and Access Management

Summary

您可以使用 AWS CloudFormation 模板 GuardDuty 在亚马逊网络服务 (AWS) 账户上启用亚马逊。默认情况下,如果您 GuardDuty 尝试使用 CloudFormation 开启堆栈时已启用,则堆栈部署将失败。但是,您可以使用 CloudFormation 模板中的条件来检查 GuardDuty 是否已启用。 CloudFormation 支持使用比较静态值的条件;它不支持在同一模板中使用其他资源属性的输出。有关更多信息,请参阅 CloudFormation 用户指南中的条件

在此模式中, GuardDuty 如果尚未启用,则使用由 AWS Lambda 函数支持的 CloudFormation 自定义资源有条件地启用。如果启 GuardDuty 用,堆栈将捕获状态并将其记录在堆栈的输出部分。如果 GuardDuty 未启用,则堆栈将其启用。

先决条件和限制

先决条件

  • 一个有效的 Amazon Web Services account。

  • 有权创建、更新和删除 CloudFormation 堆栈的 AWS Identity and Access Management (IAM) 角色

限制

  • 如果 GuardDuty 已为某个 AWS 账户或区域手动禁用此模式,则该目标账户或区域将无法启用 GuardDuty 此模式。

架构

目标技术堆栈

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

目标架构

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

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

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

  3. Lambda 函数代入 IAM 角色。

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

GuardDuty 通过部署 CloudFormation 模板实现启用的过程

自动化和扩展

您可以使用 AWS CloudFormation StackSet 功能将此解决方案扩展到多个 AWS 账户和 AWS 区域。有关更多信息,请参阅 CloudFormation 用户指南 CloudFormation StackSets中的使用 AWS

工具

  • AWS 命令行界面(AWS CLI)是一种开源工具,它可帮助您通过命令行 Shell 中的命令与 Amazon Web Services 交互。

  • AWS CloudFormation 可帮助您设置 AWS 资源,快速一致地配置这些资源,并在 AWS 账户和区域的整个生命周期中对其进行管理。

  • Amazon GuardDuty 是一项持续的安全监控服务,可分析和处理日志,以识别您的 AWS 环境中意外和可能未经授权的活动。

  • AWS Identity and Access Management(AWS IAM)通过控制验证和授权使用您 AWS 资源的用户,帮助您安全地管理对您 AWS 资源的访问。

  • AWS Lambda 是一项计算服务,可帮助您运行代码,而无需预置或管理服务器。它仅在需要时运行您的代码,并且能自动扩展,因此您只需为使用的计算时间付费。

操作说明

任务描述所需技能

创建 CloudFormation 模板。

  1. 将代码复制到附加信息部分的CloudFormation 模板中。

  2. 将代码粘贴至文本编辑器中。

  3. 在您的工作站上将文件另存为 sample.yaml

AWS DevOps

创建 CloudFormation 堆栈。

  1. 在 AWS CLI 中,输入以下命令。这将使用该sample.yaml文件创建一个新的 CloudFormation 堆栈。有关更多信息,请参阅 CloudFormation 用户指南中的创建堆栈

    aws cloudformation create-stack \ --stack-name guardduty-cf-stack \ --template-body file://sample.yaml
  2. 确认 AWS CLI 中出现以下值,表明堆栈已成功创建。创建堆栈所需时间量可能会有所不同。

    "StackStatus": "CREATE_COMPLETE",
AWS DevOps

验证 AWS 账户 GuardDuty 是否已启用。

  1. 登录 AWS 管理控制台并打开控制 GuardDuty 台,网址为 https://console.aws.amazon.com/guardduty/

  2. 确认该 GuardDuty 服务已启用。

云管理员、AWS 管理员

配置其他账户或者 Amazon Web Services Region。

根据您的用例需要,使用 AWS CloudFormation StackSet 功能将此解决方案扩展到多个 AWS 账户和 AWS 区域。有关更多信息,请参阅 CloudFormation 用户指南 CloudFormation StackSets中的使用 AWS

云管理员、AWS 管理员

相关资源

参考

教程和视频

其他信息

CloudFormation 模板

AWSTemplateFormatVersion: 2010-09-09 Resources: rLambdaLogGroup: Type: 'AWS::Logs::LogGroup' DeletionPolicy: Delete Properties: RetentionInDays: 7 LogGroupName: /aws/lambda/resource-checker rLambdaCheckerLambdaRole: Type: 'AWS::IAM::Role' Properties: RoleName: !Sub 'resource-checker-lambda-role-${AWS::Region}' AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: 'sts:AssumeRole' Path: / Policies: - PolicyName: !Sub 'resource-checker-lambda-policy-${AWS::Region}' PolicyDocument: Version: 2012-10-17 Statement: - Sid: CreateLogGroup Effect: Allow Action: - 'logs:CreateLogGroup' - 'logs:CreateLogStream' - 'logs:PutLogEvents' - 'iam:CreateServiceLinkedRole' - 'cloudformation:CreateStack' - 'cloudformation:DeleteStack' - 'cloudformation:Desc*' - 'guardduty:CreateDetector' - 'guardduty:ListDetectors' - 'guardduty:DeleteDetector' Resource: '*' resourceCheckerLambda: Type: 'AWS::Lambda::Function' Properties: Description: Checks for resource type enabled and possibly name to exist FunctionName: resource-checker Handler: index.lambda_handler Role: !GetAtt - rLambdaCheckerLambdaRole - Arn Runtime: python3.8 MemorySize: 128 Timeout: 180 Code: ZipFile: | 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": "A sample 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, {}) CheckResourceExist: Type: 'Custom::LambdaCustomResource' Properties: ServiceToken: !GetAtt - resourceCheckerLambda - Arn Outputs: status: Value: !GetAtt - CheckResourceExist - status

Lambda 资源的替代代码选项

提供的 CloudFormation 模板使用内联代码来引用 Lambda 资源,以便于参考和指导。或者,您可以将 Lambda 代码放入亚马逊简单存储服务 (Amazon S3) 存储桶中,并在模板中引用它。 CloudFormation 内联代码不支持数据包依赖项或库。您可以通过将 Lambda 代码放在 S3 存储桶中并在模板中引用它来支持这些操作。 CloudFormation

替换以下代码行:

Code: ZipFile: |

代码行如下:

Code: S3Bucket: <bucket name> S3Key: <python file name> S3ObjectVersion: <version>

如果您未在 S3 存储桶中使用版本控制,则可以省略 S3ObjectVersion属性。有关更多信息,请参阅 Amazon S3 用户指南中的在 S3 存储桶中使用版本控制