- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4.6k
feat: Add new sub-module for AWS flow log #1241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            bryantbiggs
  merged 2 commits into
  terraform-aws-modules:master
from
bryantbiggs:feat/flow-log
  
      
      
   
  Sep 26, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| # Examples | ||
|  | ||
| The examples provided demonstrate different cluster configurations that users can create with the modules provided. | ||
|  | ||
| Please do not mistake the examples provided as "best practices". It is up to users to consult the AWS service documentation for best practices, usage recommendations, etc. | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # AWS Flow Logs to S3 and CloudWatch logs | ||
|  | ||
| Configuration in this directory creates a set of VPC resources with VPC Flow Logs enabled in different configurations: | ||
|  | ||
| - Flow log to CloudWatch logs using module created CloudWatch log group and IAM role | ||
| - Flow log to CloudWatch logs using external CloudWatch log group and IAM role | ||
| - Flow log to S3 bucket in text format | ||
| - Flow log to S3 bucket in Parquet format | ||
|  | ||
| ## Usage | ||
|  | ||
| To run this example you need to execute: | ||
|  | ||
| ```bash | ||
| $ terraform init | ||
| $ terraform plan | ||
| $ terraform apply | ||
| ``` | ||
|  | ||
| Note that this example may create resources which can cost money (AWS Elastic IP, for example). Run `terraform destroy` when you don't need these resources. | ||
|  | ||
| <!-- BEGIN_TF_DOCS --> | ||
| ## Requirements | ||
|  | ||
| | Name | Version | | ||
| |------|---------| | ||
| | <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.5.7 | | ||
| | <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 6.5 | | ||
|  | ||
| ## Providers | ||
|  | ||
| | Name | Version | | ||
| |------|---------| | ||
| | <a name="provider_aws"></a> [aws](#provider\_aws) | >= 6.5 | | ||
|  | ||
| ## Modules | ||
|  | ||
| | Name | Source | Version | | ||
| |------|--------|---------| | ||
| | <a name="module_disabled"></a> [disabled](#module\_disabled) | ../../modules/flow-log | n/a | | ||
| | <a name="module_flow_log"></a> [flow\_log](#module\_flow\_log) | ../../modules/flow-log | n/a | | ||
| | <a name="module_flow_log_cloudwatch_external"></a> [flow\_log\_cloudwatch\_external](#module\_flow\_log\_cloudwatch\_external) | ../../modules/flow-log | n/a | | ||
| | <a name="module_flow_log_s3"></a> [flow\_log\_s3](#module\_flow\_log\_s3) | ../../modules/flow-log | n/a | | ||
| | <a name="module_flow_log_s3_parquet"></a> [flow\_log\_s3\_parquet](#module\_flow\_log\_s3\_parquet) | ../../modules/flow-log | n/a | | ||
| | <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | ~> 5.0 | | ||
| | <a name="module_vpc"></a> [vpc](#module\_vpc) | ../../ | n/a | | ||
|  | ||
| ## Resources | ||
|  | ||
| | Name | Type | | ||
| |------|------| | ||
| | [aws_cloudwatch_log_group.flow_log](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | | ||
| | [aws_iam_role.flow_log_cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource | | ||
| | [aws_iam_role_policy.flow_log_cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource | | ||
| | [aws_availability_zones.available](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones) | data source | | ||
|  | ||
| ## Inputs | ||
|  | ||
| No inputs. | ||
|  | ||
| ## Outputs | ||
|  | ||
| | Name | Description | | ||
| |------|-------------| | ||
| | <a name="output_arn"></a> [arn](#output\_arn) | The ARN of the Flow Log | | ||
| | <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | ARN of CloudWatch log group created | | ||
| | <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of CloudWatch log group created | | ||
| | <a name="output_iam_role_arn"></a> [iam\_role\_arn](#output\_iam\_role\_arn) | The Amazon Resource Name (ARN) specifying the IAM role | | ||
| | <a name="output_iam_role_name"></a> [iam\_role\_name](#output\_iam\_role\_name) | The name of the IAM role | | ||
| | <a name="output_iam_role_unique_id"></a> [iam\_role\_unique\_id](#output\_iam\_role\_unique\_id) | Stable and unique string identifying the IAM role | | ||
| | <a name="output_id"></a> [id](#output\_id) | The ID of the Flow Log | | ||
| <!-- END_TF_DOCS --> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| provider "aws" { | ||
| region = local.region | ||
| } | ||
|  | ||
| data "aws_availability_zones" "available" {} | ||
|  | ||
| locals { | ||
| name = "ex-${basename(path.cwd)}" | ||
| region = "eu-west-1" | ||
|  | ||
| vpc_cidr = "10.0.0.0/16" | ||
| azs = slice(data.aws_availability_zones.available.names, 0, 3) | ||
|  | ||
| tags = { | ||
| Example = local.name | ||
| GithubRepo = "terraform-aws-vpc" | ||
| GithubOrg = "terraform-aws-modules" | ||
| } | ||
| } | ||
|  | ||
| ################################################################################ | ||
| # Flow Log | ||
| ################################################################################ | ||
|  | ||
| module "flow_log" { | ||
| source = "../../modules/flow-log" | ||
|  | ||
| name = local.name | ||
| vpc_id = module.vpc.vpc_id | ||
|  | ||
| tags = local.tags | ||
| } | ||
|  | ||
| module "flow_log_cloudwatch_external" { | ||
| source = "../../modules/flow-log" | ||
|  | ||
| name = "${local.name}-cloudwatch-external" | ||
| vpc_id = module.vpc.vpc_id | ||
|  | ||
| create_cloudwatch_log_group = false | ||
| log_destination = aws_cloudwatch_log_group.flow_log.arn | ||
|  | ||
| create_iam_role = false | ||
| iam_role_arn = aws_iam_role.flow_log_cloudwatch.arn | ||
|  | ||
| tags = local.tags | ||
| } | ||
|  | ||
| module "flow_log_s3" { | ||
| source = "../../modules/flow-log" | ||
|  | ||
| name = "${local.name}-s3" | ||
| vpc_id = module.vpc.vpc_id | ||
|  | ||
| log_destination_type = "s3" | ||
| log_destination = module.s3_bucket.s3_bucket_arn | ||
|  | ||
| tags = local.tags | ||
| } | ||
|  | ||
| module "flow_log_s3_parquet" { | ||
| source = "../../modules/flow-log" | ||
|  | ||
| name = "${local.name}-s3-parquet" | ||
| vpc_id = module.vpc.vpc_id | ||
|  | ||
| log_destination_type = "s3" | ||
| log_destination = module.s3_bucket.s3_bucket_arn | ||
| destination_options = { | ||
| file_format = "parquet" | ||
| hive_compatible_partitions = true | ||
| per_hour_partition = true | ||
| } | ||
|  | ||
| tags = local.tags | ||
| } | ||
|  | ||
| module "disabled" { | ||
| source = "../../modules/flow-log" | ||
|  | ||
| create = false | ||
| } | ||
|  | ||
| ################################################################################ | ||
| # Supporting Resources | ||
| ################################################################################ | ||
|  | ||
| module "vpc" { | ||
| source = "../../" | ||
|  | ||
| name = local.name | ||
| cidr = local.vpc_cidr | ||
|  | ||
| azs = local.azs | ||
| private_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k)] | ||
| public_subnets = [for k, v in local.azs : cidrsubnet(local.vpc_cidr, 8, k + 4)] | ||
|  | ||
| tags = local.tags | ||
| } | ||
|  | ||
| module "s3_bucket" { | ||
| source = "terraform-aws-modules/s3-bucket/aws" | ||
| version = "~> 5.0" | ||
|  | ||
| bucket_prefix = "${local.name}-" | ||
| force_destroy = true | ||
|  | ||
| # Policy works for flow logs as well | ||
| attach_waf_log_delivery_policy = true | ||
|  | ||
| tags = local.tags | ||
| } | ||
|  | ||
| resource "aws_cloudwatch_log_group" "flow_log" { | ||
| name_prefix = "/aws/flow-log/vpc/${module.vpc.vpc_id}/${local.name}-external-" | ||
|  | ||
| retention_in_days = 7 | ||
|  | ||
| tags = local.tags | ||
| } | ||
|  | ||
| resource "aws_iam_role" "flow_log_cloudwatch" { | ||
| name_prefix = "${local.name}-external-" | ||
|  | ||
| assume_role_policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [ | ||
| { | ||
| Action = "sts:AssumeRole" | ||
| Effect = "Allow" | ||
| Sid = "VPCFlowLogsAssume" | ||
| Principal = { | ||
| Service = "vpc-flow-logs.amazonaws.com" | ||
| } | ||
| }, | ||
| ] | ||
| }) | ||
|  | ||
| tags = local.tags | ||
| } | ||
|  | ||
| resource "aws_iam_role_policy" "flow_log_cloudwatch" { | ||
| name_prefix = "${local.name}-external-" | ||
| role = aws_iam_role.flow_log_cloudwatch.id | ||
|  | ||
| policy = jsonencode({ | ||
| Version = "2012-10-17" | ||
| Statement = [ | ||
| { | ||
| Action = [ | ||
| "logs:CreateLogGroup", | ||
| "logs:CreateLogStream", | ||
| "logs:PutLogEvents", | ||
| "logs:DescribeLogGroups", | ||
| "logs:DescribeLogStreams", | ||
| ] | ||
| Effect = "Allow" | ||
| Resource = aws_cloudwatch_log_group.flow_log.arn | ||
| }, | ||
| ] | ||
| }) | ||
| } | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.