Enable Amazon GuardDuty conditionally by using AWS CloudFormation templates
Ram Kandaswamy, Amazon Web Services
Summary
Amazon GuardDuty
This pattern prevents that failure. A CloudFormation custom resource backed by an AWS Lambda
This approach is particularly valuable for organizations that are:
Managing multiple AWS accounts through infrastructure as code (IaC)
Implementing security services at scale
Requiring idempotent infrastructure deployments
Automating security service deployments
Prerequisites and limitations
Prerequisites
An active AWS account.
Permissions to create, update, and delete AWS CloudFormation stacks and AWS Identity and Access Management (IAM)
roles. AWS Command Line Interface (AWS CLI), installed
and configured An AI coding assistant, such as Kiro.
The following validation tools, installed: cfn-lint, Checkov, and Bandit.
Limitations
If GuardDuty is suspended in an AWS account or Region, its detector still exists. This pattern reports the status as AlreadyEnabled and doesn't resume monitoring. To resume monitoring, enable GuardDuty in the GuardDuty console or by using the aws guardduty update-detector command.
If you disabled GuardDuty in an AWS account or Region, GuardDuty deleted its detector and findings. This pattern creates a new detector. This pattern doesn't restore findings from before you disabled GuardDuty.
If the stack output shows Created, deleting the stack also deletes the GuardDuty detector and turns off threat detection in that Region. If the output shows AlreadyEnabled, deleting the stack doesn't change GuardDuty.
Generated code varies by AI model and version. Review and validate the generated code before you deploy it.
CloudFormation limits inline Lambda code in a template (using the
ZipFileproperty) to a 4 MB zip file.
Architecture
Target technology stack
The pattern uses CloudFormation for infrastructure as code (IaC). You use a CloudFormation custom resource backed by a Lambda function to achieve the dynamic service-enablement capability.
Target architecture
The following high-level architecture diagram shows the process of enabling GuardDuty by deploying a CloudFormation template:

You deploy a CloudFormation template to create a CloudFormation stack.
The stack creates an IAM role, a Lambda function, and a custom resource that invokes the function.
The Lambda function checks whether a GuardDuty detector exists in the account and Region.
If no detector exists, the function creates a separate stack, guardduty-cfn-stack, that contains the GuardDuty detector.
The function returns the GuardDuty status, which the stack shows as an output.
Automation and scale
You can use the AWS CloudFormation StackSet feature to extend this solution to multiple AWS accounts and AWS Regions. For more information, see Working with AWS CloudFormation StackSets
Tools
AWS Command Line Interface (AWS CLI)
is an open-source tool that helps you interact with AWS services through commands in your command-line shell. AWS CloudFormation
helps you set up AWS resources, provision them quickly and consistently, and manage them throughout their lifecycle across AWS accounts and Regions. Amazon GuardDuty
is a continuous security monitoring service that analyzes and processes logs to identify unexpected and potentially unauthorized activity in your AWS environment. AWS Identity and Access Management (IAM)
helps you securely manage access to your AWS resources by controlling who is authenticated and authorized to use them. AWS Lambda
is a compute service that helps you run code without needing to provision or manage servers. It runs your code only when needed and scales automatically, so you pay only for the compute time that you use. Kiro is an agentic AI engineering system available through an IDE
, CLI , web interface , mobile app , and Kiro Crew that helps developers and teams build software more efficiently. (Recommended) cfn-lint checks CloudFormation templates for syntax errors and property values that aren't valid. (Recommended)
Checkov is a static analysis tool that scans infrastructure as code for security and compliance misconfigurations. (Recommended)
Bandit is a static analysis tool that finds common security issues in Python code. (Recommended)
Best practices
Review the generated code and confirm that it meets every requirement in the prompt before you deploy it.
Grant the Lambda function's role only the permissions that the function needs, scoped to the specific stack, log group, account, and Region.
Don't log the full custom resource event. It contains a presigned URL that anyone can use to send a response to AWS CloudFormation.
Deploy the stack in a test account first, and confirm that both stack creation and stack deletion complete successfully.
Use the GuardDutyStatus stack output to identify the accounts and Regions where this stack enabled GuardDuty.
Epics
| Task | Description | Skills required |
|---|---|---|
Generate Python code using Kiro or another AI coding assistant | Use the following prompt, or a similar one, to generate the code. Save the file as index.py.
| AWS DevOps |
Generate an AWS CloudFormation template | In the same AI coding assistant session, enter the following prompt. If you use a new session, provide index.py with the prompt. Save the template as sample.yaml.
| AWS DevOps |
Validate code and template |
Resolve all errors and findings, and then save the updated files. | |
Create the CloudFormation stack. |
| AWS DevOps |
Validate that GuardDuty is enabled for the AWS account. |
| Cloud administrator, AWS administrator |
Configure additional accounts or Regions. | As needed for your use case, use the CloudFormation StackSet feature to extend this solution to multiple AWS accounts and AWS Regions. For more information, see Working with AWS CloudFormation StackSets | Cloud administrator, AWS administrator |
Related resources
References
Tutorials and videos
Simplify Your Infrastructure Management Using AWS CloudFormation
(Tutorial) Use Amazon GuardDuty and AWS Security Hub CSPM to secure multiple accounts
(AWS re:Invent 2020) Best practices for authoring AWS CloudFormation
(AWS re:Invent 2019) Threat Detection on AWS: An Introduction to Amazon GuardDuty
(AWS re:Inforce 2019)