diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 2fb2b83ae..f486d2718 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -443,6 +443,7 @@ func Provider() terraform.ResourceProvider { "aws_subnet": resourceAwsSubnet(), "aws_volume_attachment": resourceAwsVolumeAttachment(), "aws_vpc_dhcp_options_association": resourceAwsVpcDhcpOptionsAssociation(), + "aws_default_vpc_dhcp_options": resourceAwsDefaultVpcDhcpOptions(), "aws_vpc_dhcp_options": resourceAwsVpcDhcpOptions(), "aws_vpc_peering_connection": resourceAwsVpcPeeringConnection(), "aws_vpc_peering_connection_accepter": resourceAwsVpcPeeringConnectionAccepter(), diff --git a/builtin/providers/aws/resource_aws_default_vpc_dhcp_options.go b/builtin/providers/aws/resource_aws_default_vpc_dhcp_options.go new file mode 100644 index 000000000..cb433ff4b --- /dev/null +++ b/builtin/providers/aws/resource_aws_default_vpc_dhcp_options.go @@ -0,0 +1,90 @@ +package aws + +import ( + "fmt" + "log" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsDefaultVpcDhcpOptions() *schema.Resource { + // reuse aws_vpc_dhcp_options schema, and methods for READ, UPDATE + dvpc := resourceAwsVpcDhcpOptions() + dvpc.Create = resourceAwsDefaultVpcDhcpOptionsCreate + dvpc.Delete = resourceAwsDefaultVpcDhcpOptionsDelete + + // domain_name is a computed value for Default Default DHCP Options Sets + dvpc.Schema["domain_name"] = &schema.Schema{ + Type: schema.TypeString, + Computed: true, + } + // domain_name_servers is a computed value for Default Default DHCP Options Sets + dvpc.Schema["domain_name_servers"] = &schema.Schema{ + Type: schema.TypeString, + Computed: true, + } + // ntp_servers is a computed value for Default Default DHCP Options Sets + dvpc.Schema["ntp_servers"] = &schema.Schema{ + Type: schema.TypeString, + Computed: true, + } + + return dvpc +} + +func resourceAwsDefaultVpcDhcpOptionsCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).ec2conn + + var domainName string + awsRegion := meta.(*AWSClient).region + if awsRegion == "us-east-1" { + domainName = "ec2.internal" + } else { + domainName = awsRegion + ".compute.internal" + } + req := &ec2.DescribeDhcpOptionsInput{ + Filters: []*ec2.Filter{ + &ec2.Filter{ + Name: aws.String("key"), + Values: aws.StringSlice([]string{"domain-name"}), + }, + &ec2.Filter{ + Name: aws.String("value"), + Values: aws.StringSlice([]string{domainName}), + }, + &ec2.Filter{ + Name: aws.String("key"), + Values: aws.StringSlice([]string{"domain-name-servers"}), + }, + &ec2.Filter{ + Name: aws.String("value"), + Values: aws.StringSlice([]string{"AmazonProvidedDNS"}), + }, + }, + } + + resp, err := conn.DescribeDhcpOptions(req) + if err != nil { + return err + } + + if len(resp.DhcpOptions) != 1 || resp.DhcpOptions[0] == nil { + return fmt.Errorf("Default DHCP Options Set not found") + } + + d.SetId(aws.StringValue(resp.DhcpOptions[0].DhcpOptionsId)) + + if err := resourceAwsVpcDhcpOptionsUpdate(d, meta); err != nil { + return err + } + + return resourceAwsVpcDhcpOptionsRead(d, meta) +} + +func resourceAwsDefaultVpcDhcpOptionsDelete(d *schema.ResourceData, meta interface{}) error { + log.Printf("[WARN] Cannot destroy Default DHCP Options Set. Terraform will remove this resource from the state file, however resources may remain.") + d.SetId("") + return nil +} diff --git a/builtin/providers/aws/resource_aws_default_vpc_dhcp_options_test.go b/builtin/providers/aws/resource_aws_default_vpc_dhcp_options_test.go new file mode 100644 index 000000000..8149d245f --- /dev/null +++ b/builtin/providers/aws/resource_aws_default_vpc_dhcp_options_test.go @@ -0,0 +1,53 @@ +// make testacc TEST=./builtin/providers/aws/ TESTARGS='-run=TestAccAWSDefaultVpc_' +package aws + +import ( + "testing" + + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSDefaultVpcDhcpOptions_basic(t *testing.T) { + var d ec2.DhcpOptions + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDefaultVpcDhcpOptionsDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDefaultVpcDhcpOptionsConfigBasic, + Check: resource.ComposeTestCheckFunc( + testAccCheckDHCPOptionsExists("aws_default_vpc_dhcp_options.foo", &d), + resource.TestCheckResourceAttr( + "aws_default_vpc_dhcp_options.foo", "domain_name", "us-west-2.compute.internal"), + resource.TestCheckResourceAttr( + "aws_default_vpc_dhcp_options.foo", "domain_name_servers", "AmazonProvidedDNS"), + resource.TestCheckResourceAttr( + "aws_default_vpc_dhcp_options.foo", "tags.%", "1"), + resource.TestCheckResourceAttr( + "aws_default_vpc_dhcp_options.foo", "tags.Name", "Default DHCP Option Set"), + ), + }, + }, + }) +} + +func testAccCheckAWSDefaultVpcDhcpOptionsDestroy(s *terraform.State) error { + // We expect DHCP Options Set to still exist + return nil +} + +const testAccAWSDefaultVpcDhcpOptionsConfigBasic = ` +provider "aws" { + region = "us-west-2" +} + +resource "aws_default_vpc_dhcp_options" "foo" { + tags { + Name = "Default DHCP Option Set" + } +} +` diff --git a/website/source/docs/providers/aws/r/default_vpc_dhcp_options.html.markdown b/website/source/docs/providers/aws/r/default_vpc_dhcp_options.html.markdown new file mode 100644 index 000000000..bc5a28383 --- /dev/null +++ b/website/source/docs/providers/aws/r/default_vpc_dhcp_options.html.markdown @@ -0,0 +1,55 @@ +--- +layout: "aws" +page_title: "AWS: aws_default_vpc_dhcp_options" +sidebar_current: "docs-aws-resource-default-vpc-dhcp-options" +description: |- + Manage the default VPC DHCP Options resource. +--- + +# aws\_default\_vpc\_dhcp\_options + +Provides a resource to manage the [default AWS DHCP Options Set](http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_DHCP_Options.html#AmazonDNS) +in the current region. + +Each AWS region comes with a default set of DHCP options. +**This is an advanced resource**, and has special caveats to be aware of when +using it. Please read this document in its entirety before using this resource. + +The `aws_default_vpc_dhcp_options` behaves differently from normal resources, in that +Terraform does not _create_ this resource, but instead "adopts" it +into management. + +## Example Usage + +Basic usage with tags: + +``` +resource "aws_default_vpc_dhcp_options" "default" { + tags { + Name = "Default DHCP Option Set" + } +} +``` + +## Argument Reference + +The arguments of an `aws_default_vpc_dhcp_options` differ slightly from `aws_vpc_dhcp_options` resources. +Namely, the `domain_name`, `domain_name_servers` and `ntp_servers` arguments are computed. +The following arguments are still supported: + +* `netbios_name_servers` - (Optional) List of NETBIOS name servers. +* `netbios_node_type` - (Optional) The NetBIOS node type (1, 2, 4, or 8). AWS recommends to specify 2 since broadcast and multicast are not supported in their network. For more information about these node types, see [RFC 2132](http://www.ietf.org/rfc/rfc2132.txt). +* `tags` - (Optional) A mapping of tags to assign to the resource. + +### Removing `aws_default_vpc_dhcp_options` from your configuration + +The `aws_default_vpc_dhcp_options` resource allows you to manage a region's default DHCP Options Set, +but Terraform cannot destroy it. Removing this resource from your configuration +will remove it from your statefile and management, but will not destroy the DHCP Options Set. +You can resume managing the DHCP Options Set via the AWS Console. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the DHCP Options Set. diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index 808d0c7b5..d6b2fe35b 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -1344,11 +1344,15 @@ > aws_default_security_group - + > aws_default_subnet + > + aws_default_vpc_dhcp_options + + > aws_egress_only_internet_gateway