Skip to content
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

Create a default subnet in an Availability Zone that does not have one #2367

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions aws/resource_aws_default_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func resourceAwsDefaultSubnet() *schema.Resource {

func resourceAwsDefaultSubnetCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

req := &ec2.DescribeSubnetsInput{
Filters: []*ec2.Filter{
&ec2.Filter{
Expand All @@ -64,16 +65,28 @@ func resourceAwsDefaultSubnetCreate(d *schema.ResourceData, meta interface{}) er
},
}

resp, err := conn.DescribeSubnets(req)
var subnetId string
describeResp, err := conn.DescribeSubnets(req)
if err != nil {
return err
}

if len(resp.Subnets) != 1 || resp.Subnets[0] == nil {
return fmt.Errorf("Default subnet not found")
if len(describeResp.Subnets) != 1 || describeResp.Subnets[0] == nil {
log.Printf("[INFO] No default subnet found in the specified AZ, creating")

createResp, err := conn.CreateDefaultSubnet(&ec2.CreateDefaultSubnetInput{
AvailabilityZone: aws.String(d.Get("availability_zone").(string)),
})
if err != nil {
return fmt.Errorf("Error creating default subnet: %s", err)
}

subnetId = aws.StringValue(createResp.Subnet.SubnetId)
} else {
subnetId = aws.StringValue(describeResp.Subnets[0].SubnetId)
}

d.SetId(aws.StringValue(resp.Subnets[0].SubnetId))
d.SetId(subnetId)

return resourceAwsSubnetUpdate(d, meta)
}
Expand Down
73 changes: 63 additions & 10 deletions aws/resource_aws_default_subnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSDefaultSubnet_basic(t *testing.T) {
func TestAccAwsDefaultSubnet_basic(t *testing.T) {
var v ec2.Subnet

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDefaultSubnetDestroy,
CheckDestroy: testAccCheckAwsDefaultSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDefaultSubnetConfigBasic,
Config: testAccAwsDefaultSubnetConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists("aws_default_subnet.foo", &v),
resource.TestCheckResourceAttr(
Expand All @@ -36,20 +36,73 @@ func TestAccAWSDefaultSubnet_basic(t *testing.T) {
})
}

func testAccCheckAWSDefaultSubnetDestroy(s *terraform.State) error {
func TestAccAwsDefaultSubnet_createNew(t *testing.T) {
var v ec2.Subnet

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAwsDefaultSubnetDestroy,
Steps: []resource.TestStep{
{
Config: testAccAwsDefaultSubnetConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists("aws_default_subnet.foo", &v),
resource.TestCheckResourceAttr(
"aws_default_subnet.foo", "availability_zone", "us-west-2a"),
resource.TestCheckResourceAttr(
"aws_default_subnet.foo", "map_public_ip_on_launch", "true"),
resource.TestCheckResourceAttr(
"aws_default_subnet.foo", "assign_ipv6_address_on_creation", "false"),
resource.TestCheckResourceAttr(
"aws_default_subnet.foo", "tags.%", "1"),
resource.TestCheckResourceAttr(
"aws_default_subnet.foo", "tags.Name", "Default subnet for us-west-2a"),
testAccDeleteAwsDefaultSubnet(&v),
),
ExpectNonEmptyPlan: true,
},
{
Config: testAccAwsDefaultSubnetConfigBasic,
Check: resource.ComposeTestCheckFunc(
testAccCheckSubnetExists("aws_default_subnet.foo", &v),
resource.TestCheckResourceAttr(
"aws_default_subnet.foo", "availability_zone", "us-west-2a"),
),
},
},
})
}

func testAccCheckAwsDefaultSubnetDestroy(s *terraform.State) error {
// We expect subnet to still exist
return nil
}

const testAccAWSDefaultSubnetConfigBasic = `
func testAccDeleteAwsDefaultSubnet(sn *ec2.Subnet) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).ec2conn

_, err := conn.DeleteSubnet(&ec2.DeleteSubnetInput{
SubnetId: sn.SubnetId,
})
if err != nil {
return err
}

return nil
}
}

const testAccAwsDefaultSubnetConfigBasic = `
provider "aws" {
region = "us-west-2"
region = "us-west-2"
}

resource "aws_default_subnet" "foo" {
availability_zone = "us-west-2a"
tags {
Name = "Default subnet for us-west-2a"
}
availability_zone = "us-west-2a"
tags {
Name = "Default subnet for us-west-2a"
}
}
`
16 changes: 8 additions & 8 deletions website/docs/r/default_subnet.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ description: |-
Manage a default VPC subnet resource.
---

# aws\_default\_subnet
# aws_default_subnet

Provides a resource to manage a [default AWS VPC subnet](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/default-vpc.html#default-vpc-basics)
in the current region.

The `aws_default_subnet` behaves differently from normal resources, in that
Terraform does not _create_ this resource, but instead "adopts" it
into management.
Terraform does not _create_ this resource, but instead "adopts" it into management.
If no default subnet for an Availability Zone exits Terraform will [create a new one] (http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/default-vpc.html#create-default-subnet).

## Example Usage

Basic usage with tags:

```
```hcl
resource "aws_default_subnet" "default_az1" {
availability_zone = "us-west-2a"

tags {
Name = "Default subnet for us-west-2a"
}
tags {
Name = "Default subnet for us-west-2a"
}
}
```

Expand All @@ -34,7 +34,7 @@ resource "aws_default_subnet" "default_az1" {
The arguments of an `aws_default_subnet` differ from `aws_subnet` resources.
Namely, the `availability_zone` argument is required and the `vpc_id`, `cidr_block`, `ipv6_cidr_block`,
`map_public_ip_on_launch` and `assign_ipv6_address_on_creation` arguments are computed.
The following arguments are still supported:
The following arguments are still supported:

* `tags` - (Optional) A mapping of tags to assign to the resource.

Expand Down