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

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

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

由 Ram Kandaswamy 创作 () AWS

摘要

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

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

先决条件和限制

先决条件

  • 一个活跃的AWS账户

  • 具有创建、更新和删除 CloudFormation 堆栈权限的 Ident AWS ity 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 用户指南AWS CloudFormation StackSets中的使用

工具

  • AWSCommand Line Interface (AWSCLI) 是一个开源工具,可帮助您通过命令行外壳中的命令与AWS服务进行交互。

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

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

  • AWSIdentity and Access Management (IAM) 通过控制谁经过身份验证并有权使用AWS资源,从而帮助您安全地管理对资源的访问权限。

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

操作说明

任务描述所需技能

创建 CloudFormation 模板。

  1. 在 “其他信息” 部分的CloudFormation 模板中复制代码。

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

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

AWS DevOps

创建 CloudFormation 堆栈。

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

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

    "StackStatus": "CREATE_COMPLETE",
AWS DevOps

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

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

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

云管理员、AWS管理员

配置其他账户或AWS区域。

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

云管理员、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 存储桶中使用版本控制