本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 AWS 範本 GuardDuty 有條件地啟用 Amazon CloudFormation
由拉姆·康達斯瓦米(AWS)創建
環境:生產 | 技術:安全性、身分識別、合規性 DevOps;營運 | AWS 服務:AWS CloudFormation;Amazon GuardDuty;AWS Lambda;AWS Identity and Access Management |
Summary
您可以使用 AWS CloudFormation 範本 GuardDuty 在 Amazon 網路服務 (AWS) 帳戶上啟用亞馬遜。根據預設,如果 GuardDuty 在嘗試使用 CloudFormation 將其開啟時已啟用,堆疊部署會失敗。但是,您可以使用 CloudFormation 範本中的條件來檢查 GuardDuty 是否已啟用。 CloudFormation 支援使用比較靜態值的條件;它不支援在相同範本中使用其他資源屬性的輸出。如需詳細資訊,請參閱 CloudFormation 使用指南中的條件。
在此模式中, GuardDuty 如果尚未啟用,您可以使用 AWS Lambda 函數支援的 CloudFormation 自訂資源,有條件地啟用該資源。如果啟 GuardDuty 用,堆疊會擷取狀態並將其記錄在堆疊的輸出區段中。如果 GuardDuty 未啟用,堆疊會啟用它。
先決條件和限制
先決條件
有效的 AWS 帳戶
具有建立、更新和刪除 CloudFormation 堆疊許可的 AWS Identity and Access Management (IAM) 角色
限制
如果 AWS 帳戶或區域 GuardDuty 已手動停用,則該目標帳戶或區域無法啟用 GuardDuty 此模式。
架構
目標技術堆疊
該模式用 CloudFormation 於基礎架構即代碼(IaC)。您可以使用由 Lambda 函數支援的 CloudFormation 自訂資源來實現動態服務啟用功能。
目標架構
下列高階架構圖顯示透過部署 CloudFormation 範本 GuardDuty 來啟用的程序:
您部署 CloudFormation 範本以建立 CloudFormation 堆疊。
堆疊會建立 IAM 角色和 Lambda 函數。
Lambda 函數會擔任 IAM 角色。
如果 GuardDuty 目標 AWS 帳戶尚未啟用,則 Lambda 函數會啟用該功能。
![透過部署 CloudFormation 範本 GuardDuty 來啟用的程序](images/pattern-img/3abd7cb7-0937-41fe-8eaa-79aedb182732/images/b2d81a61-31f7-4cb9-9de3-f7db1b92ccd9.png)
自動化和規模
您可以使用 AWS CloudFormation StackSet 功能將此解決方案擴展到多個 AWS 帳戶和 AWS 區域。如需詳細資訊,請參閱使用 CloudFormation 者指南 CloudFormation StackSets中的使用 AWS。
工具
AWS Command Line Interface (AWS CLI) (AWS CLI) 是一種開放原始碼工具,可協助您透過命令列殼層中的命令與 AWS 服務互動。
AWS 可 CloudFormation協助您設定 AWS 資源、快速且一致地佈建 AWS 資源,並在 AWS 帳戶和區域的整個生命週期中進行管理。
Amazon GuardDuty 是一種持續的安全監控服務,可分析和處理日誌,以識別 AWS 環境中的未預期和潛在未經授權的活動。
AWS Identity and Access Management (IAM) 可透過控制誰經過身份驗證和授權使用 AWS 資源,協助您安全地管理對 AWS 資源的存取。
AWS Lambda 是一種運算服務,可協助您執行程式碼,而不需要佈建或管理伺服器。它只會在需要時執行程式碼並自動調整規模,因此您只需為使用的運算時間付費。
史诗
任務 | 描述 | 所需技能 |
---|---|---|
建立 CloudFormation 範本。 |
| AWS DevOps |
建立 CloudFormation 堆疊。 |
| AWS DevOps |
驗證已 GuardDuty 為 AWS 帳戶啟用。 |
| 雲端管理員、AWS 管理員 |
設定其他帳戶或 AWS 區域。 | 根據您的使用案例需要,使用 AWS CloudFormation StackSet 功能將此解決方案擴展到多個 AWS 帳戶和 AWS 區域。如需詳細資訊,請參閱使用 CloudFormation 者指南 CloudFormation StackSets中的使用 AWS。 | 雲端管理員、AWS 管理員 |
相關資源
參考
教學課程和影片
使用 Amazon GuardDuty 和 AWS Security Hub 保護多個帳戶
(AWS RE: 發明 2020) 建立 AWS 的最佳實務 CloudFormation
(AWS RE: 發明 2019)
其他資訊
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 儲存貯體中使用版本控制。