2014-07-23 22:32:33 +02:00
---
layout: "aws"
page_title: "AWS: aws_security_group"
sidebar_current: "docs-aws-resource-security-group"
2014-10-22 05:21:56 +02:00
description: |-
2015-05-06 19:08:30 +02:00
Provides a security group resource.
2014-07-23 22:32:33 +02:00
---
# aws\_security\_group
2015-04-20 20:38:21 +02:00
Provides a security group resource.
~> **NOTE on Security Groups and Security Group Rules:** Terraform currently
provides both a standalone [Security Group Rule resource ](security_group_rule.html ) (a single `ingress` or
`egress` rule), and a Security Group resource with `ingress` and `egress` rules
defined in-line. At this time you cannot use a Security Group with in-line rules
in conjunction with any Security Group Rule resources. Doing so will cause
a conflict of rule settings and will overwrite rules.
2014-07-23 22:32:33 +02:00
## Example Usage
2014-10-14 23:07:01 +02:00
Basic usage
2017-04-17 12:17:54 +02:00
```hcl
2014-07-23 22:32:33 +02:00
resource "aws_security_group" "allow_all" {
2017-02-18 23:48:50 +01:00
name = "allow_all"
2015-03-18 19:45:32 +01:00
description = "Allow all inbound traffic"
2014-08-03 04:39:22 +02:00
2014-10-14 23:07:01 +02:00
ingress {
2017-02-18 23:48:50 +01:00
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
2014-10-14 23:07:01 +02:00
}
2015-02-17 19:23:10 +01:00
egress {
2017-02-18 23:48:50 +01:00
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
prefix_list_ids = ["pl-12c4e678"]
2015-02-17 19:23:10 +01:00
}
2014-10-14 23:07:01 +02:00
}
```
Basic usage with tags:
2017-05-05 02:00:17 +02:00
```hcl
2014-10-14 23:07:01 +02:00
resource "aws_security_group" "allow_all" {
2017-02-18 23:48:50 +01:00
name = "allow_all"
2014-10-14 23:07:01 +02:00
description = "Allow all inbound traffic"
ingress {
2017-02-18 23:48:50 +01:00
from_port = 0
to_port = 65535
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
2014-10-14 23:07:01 +02:00
}
tags {
Name = "allow_all"
}
2014-07-23 22:32:33 +02:00
}
```
## Argument Reference
The following arguments are supported:
2016-02-29 19:06:33 +01:00
* `name` - (Optional, Forces new resource) The name of the security group. If omitted, Terraform will
2015-12-02 17:28:17 +01:00
assign a random, unique name
2016-02-29 19:06:33 +01:00
* `name_prefix` - (Optional, Forces new resource) Creates a unique name beginning with the specified
2015-12-04 15:21:08 +01:00
prefix. Conflicts with `name` .
2016-02-29 19:06:33 +01:00
* `description` - (Optional, Forces new resource) The security group description. Defaults to
"Managed by Terraform". Cannot be "". __NOTE__ : This field maps to the AWS
`GroupDescription` attribute, for which there is no Update API. If you'd like
to classify your security groups in a way that can be updated, use `tags` .
2014-12-08 08:52:04 +01:00
* `ingress` - (Optional) Can be specified multiple times for each
2014-07-23 22:32:33 +02:00
ingress rule. Each ingress block supports fields documented below.
2015-05-05 23:22:18 +02:00
* `egress` - (Optional, VPC only) Can be specified multiple times for each
2015-02-17 19:23:10 +01:00
egress rule. Each egress block supports fields documented below.
2016-02-29 19:06:33 +01:00
* `vpc_id` - (Optional, Forces new resource) The VPC ID.
2015-04-10 21:29:31 +02:00
* `tags` - (Optional) A mapping of tags to assign to the resource.
2014-07-23 22:32:33 +02:00
The `ingress` block supports:
2016-03-04 09:28:37 +01:00
* `cidr_blocks` - (Optional) List of CIDR blocks.
2017-03-14 12:02:50 +01:00
* `ipv6_cidr_blocks` - (Optional) List of IPv6 CIDR blocks.
2016-03-11 02:22:47 +01:00
* `from_port` - (Required) The start port (or ICMP type number if protocol is "icmp")
2015-05-05 06:04:21 +02:00
* `protocol` - (Required) The protocol. If you select a protocol of
2017-01-05 12:31:51 +01:00
"-1" (semantically equivalent to `"all"` , which is not a valid value here), you must specify a "from_port" and "to_port" equal to 0. If not icmp, tcp, udp, or "-1" use the [protocol number ](https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml )
2015-04-09 21:41:41 +02:00
* `security_groups` - (Optional) List of security group Group Names if using
2016-05-21 00:01:13 +02:00
EC2-Classic, or Group IDs if using a VPC.
2014-09-30 23:19:16 +02:00
* `self` - (Optional) If true, the security group itself will be added as
a source to this ingress rule.
2016-08-10 21:32:18 +02:00
* `to_port` - (Required) The end range port (or ICMP code if protocol is "icmp").
2014-07-23 22:32:33 +02:00
2015-02-17 19:23:10 +01:00
The `egress` block supports:
2016-03-04 09:28:37 +01:00
* `cidr_blocks` - (Optional) List of CIDR blocks.
2017-03-14 12:02:50 +01:00
* `ipv6_cidr_blocks` - (Optional) List of IPv6 CIDR blocks.
2016-06-06 12:07:19 +02:00
* `prefix_list_ids` - (Optional) List of prefix list IDs (for allowing access to VPC endpoints)
2016-03-11 02:22:47 +01:00
* `from_port` - (Required) The start port (or ICMP type number if protocol is "icmp")
2015-05-05 06:04:21 +02:00
* `protocol` - (Required) The protocol. If you select a protocol of
2017-01-05 12:31:51 +01:00
"-1" (semantically equivalent to `"all"` , which is not a valid value here), you must specify a "from_port" and "to_port" equal to 0. If not icmp, tcp, udp, or "-1" use the [protocol number ](https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml )
2015-04-09 21:41:41 +02:00
* `security_groups` - (Optional) List of security group Group Names if using
2016-05-21 00:18:51 +02:00
EC2-Classic, or Group IDs if using a VPC.
2015-02-17 19:23:10 +01:00
* `self` - (Optional) If true, the security group itself will be added as
a source to this egress rule.
2016-08-10 21:32:18 +02:00
* `to_port` - (Required) The end range port (or ICMP code if protocol is "icmp").
2015-02-17 19:23:10 +01:00
2015-05-01 17:07:46 +02:00
~> **NOTE on Egress rules:** By default, AWS creates an `ALLOW ALL` egress rule when creating a
new Security Group inside of a VPC. When creating a new Security
Group inside a VPC, **Terraform will remove this default rule** , and require you
specifically re-create it if you desire that rule. We feel this leads to fewer
surprises in terms of controlling your egress rules. If you desire this rule to
be in place, you can use this `egress` block:
2017-05-05 02:00:17 +02:00
```hcl
2015-05-01 17:07:46 +02:00
egress {
from_port = 0
to_port = 0
protocol = "-1"
2015-05-14 01:41:41 +02:00
cidr_blocks = ["0.0.0.0/0"]
2015-05-01 17:07:46 +02:00
}
2017-05-05 02:00:17 +02:00
```
2015-05-01 17:07:46 +02:00
2016-06-06 12:07:19 +02:00
## Usage with prefix list IDs
2016-11-03 22:14:28 +01:00
Prefix list IDs are managed by AWS internally. Prefix list IDs
2016-06-06 12:07:19 +02:00
are associated with a prefix list name, or service name, that is linked to a specific region.
Prefix list IDs are exported on VPC Endpoints, so you can use this format:
2017-05-05 02:00:17 +02:00
```hcl
2017-02-18 23:48:50 +01:00
# ...
2016-06-06 12:07:19 +02:00
egress {
from_port = 0
to_port = 0
protocol = "-1"
prefix_list_ids = ["${aws_vpc_endpoint.my_endpoint.prefix_list_id}"]
}
2017-02-18 23:48:50 +01:00
# ...
2016-06-06 12:07:19 +02:00
resource "aws_vpc_endpoint" "my_endpoint" {
2017-02-18 23:48:50 +01:00
# ...
2016-06-06 12:07:19 +02:00
}
```
2014-07-23 22:32:33 +02:00
## Attributes Reference
The following attributes are exported:
* `id` - The ID of the security group
* `vpc_id` - The VPC ID.
* `owner_id` - The owner ID.
* `name` - The name of the security group
* `description` - The description of the security group
* `ingress` - The ingress rules. See above for more.
2015-02-17 19:23:10 +01:00
* `egress` - The egress rules. See above for more.
2016-07-21 00:28:59 +02:00
## Import
2016-09-21 20:52:23 +02:00
Security Groups can be imported using the `security group id` , e.g.
2016-07-21 00:28:59 +02:00
```
$ terraform import aws_security_group.elb_sg sg-903004f8
2016-08-10 21:32:18 +02:00
```