Terraform module which creates EC2 security group within VPC on AWS.
This module aims to implement ALL combinations of arguments supported by AWS and latest stable version of Terraform:
- IPv4/IPv6 CIDR blocks
- VPC endpoint prefix lists (use data source aws_prefix_list)
- Access from source security groups
- Access from self
- Named rules (see the rules here)
- Named groups of rules with ingress (inbound) and egress (outbound) ports open for common scenarios (eg, ssh, http-80, mysql, see the whole list here)
- Conditionally create security group and/or all required security group rules.
Ingress and egress rules can be configured in a variety of ways. See inputs section for all supported arguments and complete example for the complete use-case.
If there is a missing feature or a bug - open an issue.
For Terraform 0.13 or later use any version from v4.5.0
of this module or newer.
For Terraform 0.12 use any version from v3.*
to v4.4.0
.
If you are using Terraform 0.11 you can use versions v2.*
.
There are two ways to create security groups using this module:
module "web_server_sg" {
source = "terraform-aws-modules/security-group/aws//modules/http-80"
name = "web-server"
description = "Security group for web-server with HTTP ports open within VPC"
vpc_id = "vpc-12345678"
ingress_cidr_blocks = ["10.10.0.0/16"]
}
module "vote_service_sg" {
source = "terraform-aws-modules/security-group/aws"
name = "user-service"
description = "Security group for user-service with custom ports open within VPC, and PostgreSQL publicly open"
vpc_id = "vpc-12345678"
ingress_cidr_blocks = ["10.10.0.0/16"]
ingress_rules = ["https-443-tcp"]
ingress_with_cidr_blocks = [
{
from_port = 8080
to_port = 8090
protocol = "tcp"
description = "User-service ports"
cidr_blocks = "10.10.0.0/16"
},
{
rule = "postgresql-tcp"
cidr_blocks = "0.0.0.0/0"
},
]
}
Terraform 0.11 has a limitation which does not allow computed values inside count
attribute on resources (issues: #16712, #18015, ...)
Computed values are values provided as outputs from module
. Non-computed values are all others - static values, values referenced as variable
and from data-sources.
When you need to specify computed value inside security group rule argument you need to specify it using an argument which starts with computed_
and provide a number of elements in the argument which starts with number_of_computed_
. See these examples:
module "http_sg" {
source = "terraform-aws-modules/security-group/aws"
# omitted for brevity
}
module "db_computed_source_sg" {
# omitted for brevity
vpc_id = "vpc-12345678" # these are valid values also - `module.vpc.vpc_id` and `local.vpc_id`
computed_ingress_with_source_security_group_id = [
{
rule = "mysql-tcp"
source_security_group_id = module.http_sg.security_group_id
}
]
number_of_computed_ingress_with_source_security_group_id = 1
}
module "db_computed_sg" {
# omitted for brevity
ingress_cidr_blocks = ["10.10.0.0/16", data.aws_security_group.default.id]
computed_ingress_cidr_blocks = [module.vpc.vpc_cidr_block]
number_of_computed_ingress_cidr_blocks = 1
}
module "db_computed_merged_sg" {
# omitted for brevity
computed_ingress_cidr_blocks = ["10.10.0.0/16", module.vpc.vpc_cidr_block]
number_of_computed_ingress_cidr_blocks = 2
}
Note that db_computed_sg
and db_computed_merged_sg
are equal, because it is possible to put both computed and non-computed values in arguments starting with computed_
.
Sometimes you need a way to conditionally create a security group. If you're using Terraform < 0.13 which lacks module support for count, you can instead specify the argument create
.
# This security group will not be created
module "vote_service_sg" {
source = "terraform-aws-modules/security-group/aws"
create = false
# ... omitted
}
- Complete Security Group example shows all available parameters to configure security group.
- Security Group "Rules Only" example shows how to manage just rules of a security group that is created outside.
- HTTP Security Group example shows more applicable security groups for common web-servers.
- Disable creation of Security Group example shows how to disable creation of security group.
- Dynamic values inside Security Group rules example shows how to specify values inside security group rules (data-sources and variables are allowed).
- Computed values inside Security Group rules example shows how to specify computed values inside security group rules (solution for
value of 'count' cannot be computed
problem).
Rules and groups are defined in rules.tf. Run update_groups.sh
when content of that file has changed to recreate content of all automatic modules.
No issue is creating limit on this module.
Name | Version |
---|---|
terraform | >= 0.13.1 |
aws | >= 3.29 |
Name | Version |
---|---|
aws | >= 3.29 |
No modules.
Name | Description | Type | Default | Required |
---|---|---|---|---|
auto_groups | Map of groups of security group rules to use to generate modules (see update_groups.sh) | map(map(list(string))) |
{ |
no |
computed_egress_rules | List of computed egress rules to create by name | list(string) |
[] |
no |
computed_egress_with_cidr_blocks | List of computed egress rules to create where 'cidr_blocks' is used | list(map(string)) |
[] |
no |
computed_egress_with_ipv6_cidr_blocks | List of computed egress rules to create where 'ipv6_cidr_blocks' is used | list(map(string)) |
[] |
no |
computed_egress_with_self | List of computed egress rules to create where 'self' is defined | list(map(string)) |
[] |
no |
computed_egress_with_source_security_group_id | List of computed egress rules to create where 'source_security_group_id' is used | list(map(string)) |
[] |
no |
computed_ingress_rules | List of computed ingress rules to create by name | list(string) |
[] |
no |
computed_ingress_with_cidr_blocks | List of computed ingress rules to create where 'cidr_blocks' is used | list(map(string)) |
[] |
no |
computed_ingress_with_ipv6_cidr_blocks | List of computed ingress rules to create where 'ipv6_cidr_blocks' is used | list(map(string)) |
[] |
no |
computed_ingress_with_self | List of computed ingress rules to create where 'self' is defined | list(map(string)) |
[] |
no |
computed_ingress_with_source_security_group_id | List of computed ingress rules to create where 'source_security_group_id' is used | list(map(string)) |
[] |
no |
create | Whether to create security group and all rules | bool |
true |
no |
create_sg | Whether to create security group | bool |
true |
no |
create_timeout | Time to wait for a security group to be created | string |
"10m" |
no |
delete_timeout | Time to wait for a security group to be deleted | string |
"15m" |
no |
description | Description of security group | string |
"Security Group managed by Terraform" |
no |
egress_cidr_blocks | List of IPv4 CIDR ranges to use on all egress rules | list(string) |
[ |
no |
egress_ipv6_cidr_blocks | List of IPv6 CIDR ranges to use on all egress rules | list(string) |
[ |
no |
egress_prefix_list_ids | List of prefix list IDs (for allowing access to VPC endpoints) to use on all egress rules | list(string) |
[] |
no |
egress_rules | List of egress rules to create by name | list(string) |
[] |
no |
egress_with_cidr_blocks | List of egress rules to create where 'cidr_blocks' is used | list(map(string)) |
[] |
no |
egress_with_ipv6_cidr_blocks | List of egress rules to create where 'ipv6_cidr_blocks' is used | list(map(string)) |
[] |
no |
egress_with_self | List of egress rules to create where 'self' is defined | list(map(string)) |
[] |
no |
egress_with_source_security_group_id | List of egress rules to create where 'source_security_group_id' is used | list(map(string)) |
[] |
no |
ingress_cidr_blocks | List of IPv4 CIDR ranges to use on all ingress rules | list(string) |
[] |
no |
ingress_ipv6_cidr_blocks | List of IPv6 CIDR ranges to use on all ingress rules | list(string) |
[] |
no |
ingress_prefix_list_ids | List of prefix list IDs (for allowing access to VPC endpoints) to use on all ingress rules | list(string) |
[] |
no |
ingress_rules | List of ingress rules to create by name | list(string) |
[] |
no |
ingress_with_cidr_blocks | List of ingress rules to create where 'cidr_blocks' is used | list(map(string)) |
[] |
no |
ingress_with_ipv6_cidr_blocks | List of ingress rules to create where 'ipv6_cidr_blocks' is used | list(map(string)) |
[] |
no |
ingress_with_self | List of ingress rules to create where 'self' is defined | list(map(string)) |
[] |
no |
ingress_with_source_security_group_id | List of ingress rules to create where 'source_security_group_id' is used | list(map(string)) |
[] |
no |
name | Name of security group - not required if create_sg is false | string |
null |
no |
number_of_computed_egress_rules | Number of computed egress rules to create by name | number |
0 |
no |
number_of_computed_egress_with_cidr_blocks | Number of computed egress rules to create where 'cidr_blocks' is used | number |
0 |
no |
number_of_computed_egress_with_ipv6_cidr_blocks | Number of computed egress rules to create where 'ipv6_cidr_blocks' is used | number |
0 |
no |
number_of_computed_egress_with_self | Number of computed egress rules to create where 'self' is defined | number |
0 |
no |
number_of_computed_egress_with_source_security_group_id | Number of computed egress rules to create where 'source_security_group_id' is used | number |
0 |
no |
number_of_computed_ingress_rules | Number of computed ingress rules to create by name | number |
0 |
no |
number_of_computed_ingress_with_cidr_blocks | Number of computed ingress rules to create where 'cidr_blocks' is used | number |
0 |
no |
number_of_computed_ingress_with_ipv6_cidr_blocks | Number of computed ingress rules to create where 'ipv6_cidr_blocks' is used | number |
0 |
no |
number_of_computed_ingress_with_self | Number of computed ingress rules to create where 'self' is defined | number |
0 |
no |
number_of_computed_ingress_with_source_security_group_id | Number of computed ingress rules to create where 'source_security_group_id' is used | number |
0 |
no |
putin_khuylo | Do you agree that Putin doesn't respect Ukrainian sovereignty and territorial integrity? More info: https://en.wikipedia.org/wiki/Putin_khuylo! | bool |
true |
no |
revoke_rules_on_delete | Instruct Terraform to revoke all of the Security Groups attached ingress and egress rules before deleting the rule itself. Enable for EMR. | bool |
false |
no |
rules | Map of known security group rules (define as 'name' = ['from port', 'to port', 'protocol', 'description']) | map(list(any)) |
{ |
no |
security_group_id | ID of existing security group whose rules we will manage | string |
null |
no |
tags | A mapping of tags to assign to security group | map(string) |
{} |
no |
use_name_prefix | Whether to use name_prefix or fixed name. Should be true to able to update security group name after initial creation | bool |
true |
no |
vpc_id | ID of the VPC where to create security group | string |
null |
no |
Name | Description |
---|---|
security_group_arn | The ARN of the security group |
security_group_description | The description of the security group |
security_group_id | The ID of the security group |
security_group_name | The name of the security group |
security_group_owner_id | The owner ID |
security_group_vpc_id | The VPC ID |
Module managed by Anton Babenko.
Apache 2 Licensed. See LICENSE for full details.
- Russia has illegally annexed Crimea in 2014 and brought the war in Donbas followed by full-scale invasion of Ukraine in 2022.
- Russia has brought sorrow and devastations to millions of Ukrainians, killed hundreds of innocent people, damaged thousands of buildings, and forced several million people to flee.
- Putin khuylo!