Skip to content

Commit 380aef9

Browse files
committed
Tighten up documentation, same-account acceptance test, better error handling.
1 parent ea642dd commit 380aef9

4 files changed

Lines changed: 94 additions & 29 deletions

File tree

builtin/providers/aws/resource_aws_vpc_peering_connection_accepter.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"errors"
55
"log"
66

7+
"fmt"
8+
79
"github.com/hashicorp/terraform/helper/schema"
810
)
911

@@ -49,17 +51,22 @@ func resourceAwsVpcPeeringConnectionAccepter() *schema.Resource {
4951
}
5052

5153
func resourceAwsVPCPeeringAccepterCreate(d *schema.ResourceData, meta interface{}) error {
52-
d.SetId(d.Get("vpc_peering_connection_id").(string))
54+
id := d.Get("vpc_peering_connection_id").(string)
55+
d.SetId(id)
5356

54-
if err := resourceAwsVPCPeeringUpdate(d, meta); err != nil {
57+
if err := resourceAwsVPCPeeringRead(d, meta); err != nil {
5558
return err
5659
}
60+
if d.Id() == "" {
61+
return fmt.Errorf("VPC Peering Connection %q not found", id)
62+
}
5763

5864
// Ensure that this IS as cross-account VPC peering connection.
5965
if d.Get("peer_owner_id").(string) == meta.(*AWSClient).accountid {
6066
return errors.New("aws_vpc_peering_connection_accepter can only adopt into management cross-account VPC peering connections")
6167
}
62-
return nil
68+
69+
return resourceAwsVPCPeeringUpdate(d, meta)
6370
}
6471

6572
func resourceAwsVPCPeeringAccepterDelete(d *schema.ResourceData, meta interface{}) error {

builtin/providers/aws/resource_aws_vpc_peering_connection_accepter_test.go

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,77 @@
22
package aws
33

44
import (
5+
"regexp"
56
"testing"
67

78
"github.com/hashicorp/terraform/helper/resource"
89
"github.com/hashicorp/terraform/terraform"
910
)
1011

11-
func TestAccAwsVPCPeeringConnectionAccepter_basic(t *testing.T) {
12+
func TestAccAwsVPCPeeringConnectionAccepter_sameAccount(t *testing.T) {
1213
resource.Test(t, resource.TestCase{
13-
PreCheck: func() { testAccPreCheck(t) },
14-
Providers: testAccProviders,
14+
PreCheck: func() { testAccPreCheck(t) },
15+
Providers: testAccProviders,
16+
CheckDestroy: testAccAwsVPCPeeringConnectionAccepterDestroy,
1517
Steps: []resource.TestStep{
16-
{
17-
Config: testAccAwsVPCPeeringConnectionAccepterConfig,
18-
Check: resource.ComposeTestCheckFunc(
19-
testAccAwsVPCPeeringConnectionAccepterCheckSomething(""),
20-
),
18+
resource.TestStep{
19+
Config: testAccAwsVPCPeeringConnectionAccepterSameAccountConfig,
20+
ExpectError: regexp.MustCompile(`aws_vpc_peering_connection_accepter can only adopt into management cross-account VPC peering connections`),
2121
},
2222
},
2323
})
2424
}
2525

26-
func testAccAwsVPCPeeringConnectionAccepterCheckSomething(name string) resource.TestCheckFunc {
27-
return func(s *terraform.State) error {
28-
return nil
29-
}
26+
func testAccAwsVPCPeeringConnectionAccepterDestroy(s *terraform.State) error {
27+
// We don't destroy the underlying VPC Peering Connection.
28+
return nil
3029
}
3130

32-
const testAccAwsVPCPeeringConnectionAccepterConfig = `
31+
const testAccAwsVPCPeeringConnectionAccepterSameAccountConfig = `
32+
provider "aws" {
33+
region = "us-west-2"
34+
// Requester's credentials.
35+
}
36+
37+
provider "aws" {
38+
alias = "peer"
39+
region = "us-west-2"
40+
// Accepter's credentials.
41+
}
42+
43+
resource "aws_vpc" "main" {
44+
cidr_block = "10.0.0.0/16"
45+
}
46+
47+
resource "aws_vpc" "peer" {
48+
provider = "aws.peer"
49+
cidr_block = "10.1.0.0/16"
50+
}
51+
52+
data "aws_caller_identity" "peer" {
53+
provider = "aws.peer"
54+
}
55+
56+
// Requester's side of the connection.
57+
resource "aws_vpc_peering_connection" "peer" {
58+
vpc_id = "${aws_vpc.main.id}"
59+
peer_vpc_id = "${aws_vpc.peer.id}"
60+
peer_owner_id = "${data.aws_caller_identity.peer.account_id}"
61+
auto_accept = false
62+
63+
tags {
64+
Side = "Requester"
65+
}
66+
}
67+
68+
// Accepter's side of the connection.
69+
resource "aws_vpc_peering_connection_accepter" "peer" {
70+
provider = "aws.peer"
71+
vpc_peering_connection_id = "${aws_vpc_peering_connection.peer.id}"
72+
auto_accept = true
73+
74+
tags {
75+
Side = "Accepter"
76+
}
77+
}
3378
`

website/source/docs/providers/aws/r/vpc_peering.html.markdown

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ layout: "aws"
33
page_title: "AWS: aws_vpc_peering_connection"
44
sidebar_current: "docs-aws-resource-vpc-peering"
55
description: |-
6-
Provides a VPC Peering Connection resource.
6+
Manage a VPC Peering Connection resource.
77
---
88

99
# aws\_vpc\_peering\_connection
1010

11-
Provides a VPC Peering Connection resource.
11+
Provides a resource to manage a VPC Peering Connection resource.
12+
13+
-> **Note:** For cross-account (requester's AWS account differs from the accepter's AWS account) VPC Peering Connections
14+
use the `aws_vpc_peering_connection` resource to manage the requester's side of the connection and
15+
use the `aws_vpc_peering_connection_accepter` resource to manage the accepter's side of the connection.
1216

1317
## Example Usage
1418

@@ -112,9 +116,9 @@ The following attributes are exported:
112116

113117
AWS only supports VPC peering within the same AWS region.
114118

115-
If both VPCs are not in the same AWS account do not enable the `auto_accept` attribute. You will still
116-
have to accept the VPC Peering Connection request manually using the AWS Management Console, AWS CLI,
117-
through SDKs, etc.
119+
If both VPCs are not in the same AWS account do not enable the `auto_accept` attribute.
120+
The accepter can manage its side of the connection using the `aws_vpc_peering_connection_accepter` resource
121+
or accept the connection manually using the AWS Management Console, AWS CLI, through SDKs, etc.
118122

119123
## Import
120124

website/source/docs/providers/aws/r/vpc_peering_accepter.html.markdown

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ layout: "aws"
33
page_title: "AWS: aws_vpc_peering_connection_accepter"
44
sidebar_current: "docs-aws-resource-vpc-peering-accepter"
55
description: |-
6-
Manage the accepter's side of a cross-account VPC peering connection.
6+
Manage the accepter's side of a cross-account VPC Peering Connection.
77
---
88

99
# aws\_vpc\_peering\_connection\_accepter
1010

11-
Provides a resource to manage the accepter's side of a cross-account VPC peering connection.
11+
Provides a resource to manage the accepter's side of a cross-account VPC Peering Connection.
1212

13-
When a cross-account (requester's AWS account differs from the accepter's) VPC peering connection is created,
14-
a VPC peering connection resource is automatically created in the accepter's account. The requester can use
15-
the `aws_vpc_peering_connection` resource to manage its side of the connection and the accepter can use the
16-
`aws_vpc_peering_connection_accepter` resource to "adopt" its side of the connection into management.
13+
When a cross-account (requester's AWS account differs from the accepter's AWS account) VPC Peering Connection
14+
is created, a VPC Peering Connection resource is automatically created in the accepter's account.
15+
The requester can use the `aws_vpc_peering_connection` resource to manage its side of the connection
16+
and the accepter can use the `aws_vpc_peering_connection_accepter` resource to "adopt" its side of the
17+
connection into management.
1718

1819
## Example Usage
1920

@@ -72,6 +73,14 @@ The following arguments are supported:
7273
* `auto_accept` - (Optional) Whether or not to accept the peering request. Defaults to `false`.
7374
* `tags` - (Optional) A mapping of tags to assign to the resource.
7475

76+
### Removing `aws_vpc_peering_connection_accepter` from your configuration
77+
78+
AWS allows a cross-account VPC Peering Connection to be deleted from either the requester's or accepter's side.
79+
However, Terraform only allows the VPC Peering Connection to be deleted from the requester's side
80+
by removing the corresponding `aws_vpc_peering_connection` resource from your configuration.
81+
Removing a `aws_vpc_peering_connection_accepter` resource from your configuration will remove it
82+
from your statefile and management, **but will not destroy the VPC Peering Connection.**
83+
7584
## Attributes Reference
7685

7786
All of the argument attributes except `auto_accept` are also exported as result attributes.
@@ -91,6 +100,6 @@ All of the argument attributes except `auto_accept` are also exported as result
91100
* `allow_remote_vpc_dns_resolution` - Indicates whether a local VPC can resolve public DNS hostnames to
92101
private IP addresses when queried from instances in a peer VPC.
93102
* `allow_classic_link_to_remote_vpc` - Indicates whether a local ClassicLink connection can communicate
94-
with the peer VPC over the VPC peering connection.
103+
with the peer VPC over the VPC Peering Connection.
95104
* `allow_vpc_to_remote_classic_link` - Indicates whether a local VPC can communicate with a ClassicLink
96-
connection in the peer VPC over the VPC peering connection.
105+
connection in the peer VPC over the VPC Peering Connection.

0 commit comments

Comments
 (0)