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

API Gateway private REST API - Support VPC Endpoint #10627

Merged
merged 4 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 38 additions & 1 deletion aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
}, false),
},
},
"vpc_endpoint_ids": {
Type: schema.TypeList,
Optional: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
Expand Down Expand Up @@ -335,6 +343,30 @@ func resourceAwsApiGatewayRestApiUpdateOperations(d *schema.ResourceData) []*api
}
}

if d.HasChange("endpoint_configuration.1.vpc_endpoint_ids") {
o, n := d.GetChange("endpoint_configuration.1.vpc_endpoint_ids")
prefix := "/endpointConfiguration/vpc_endpoint_ids"

old := o.([]interface{})
new := n.([]interface{})

for _, v := range old {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("remove"),
Path: aws.String(fmt.Sprintf("%s/%s", prefix, escapeJsonPointer(v.(string)))),
})
}

if len(new) > 0 {
for _, v := range new {
operations = append(operations, &apigateway.PatchOperation{
Op: aws.String("add"),
Path: aws.String(fmt.Sprintf("%s/%s", prefix, escapeJsonPointer(v.(string)))),
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
})
}
}
}

return operations
}

Expand Down Expand Up @@ -401,6 +433,10 @@ func expandApiGatewayEndpointConfiguration(l []interface{}) *apigateway.Endpoint
Types: expandStringList(m["types"].([]interface{})),
}

if endpointIds, ok := m["vpc_endpoint_ids"]; ok {
endpointConfiguration.VpcEndpointIds = expandStringList(endpointIds.([]interface{}))
}

return endpointConfiguration
}

Expand All @@ -410,7 +446,8 @@ func flattenApiGatewayEndpointConfiguration(endpointConfiguration *apigateway.En
}

m := map[string]interface{}{
"types": flattenStringList(endpointConfiguration.Types),
"types": flattenStringList(endpointConfiguration.Types),
"vpc_endpoint_ids": flattenStringList(endpointConfiguration.VpcEndpointIds),
DrFaust92 marked this conversation as resolved.
Show resolved Hide resolved
}

return []interface{}{m}
Expand Down
111 changes: 111 additions & 0 deletions aws/resource_aws_api_gateway_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,60 @@ func TestAccAWSAPIGatewayRestApi_EndpointConfiguration_Private(t *testing.T) {
})
}

func TestAccAWSAPIGatewayRestApi_EndpointConfiguration_VPCEndpoint(t *testing.T) {
var restApi apigateway.RestApi
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_api_gateway_rest_api.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSAPIGatewayRestAPIDestroy,
Steps: []resource.TestStep{
{
PreConfig: func() {
// Ensure region supports PRIVATE endpoint
// This can eventually be moved to a PreCheck function
conn := testAccProvider.Meta().(*AWSClient).apigateway
output, err := conn.CreateRestApi(&apigateway.CreateRestApiInput{
Name: aws.String(acctest.RandomWithPrefix("tf-acc-test-private-endpoint-precheck")),
EndpointConfiguration: &apigateway.EndpointConfiguration{
Types: []*string{aws.String("PRIVATE")},
},
})
if err != nil {
if isAWSErr(err, apigateway.ErrCodeBadRequestException, "Endpoint Configuration type PRIVATE is not supported in this region") {
t.Skip("Region does not support PRIVATE endpoint type")
}
t.Fatal(err)
}

// Be kind and rewind. :)
_, err = conn.DeleteRestApi(&apigateway.DeleteRestApiInput{
RestApiId: output.Id,
})
if err != nil {
t.Fatal(err)
}
},
Config: testAccAWSAPIGatewayRestAPIConfig_VPCEndpointConfiguration(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSAPIGatewayRestAPIExists(resourceName, &restApi),
resource.TestCheckResourceAttr(resourceName, "endpoint_configuration.#", "1"),
resource.TestCheckResourceAttr(resourceName, "endpoint_configuration.0.types.#", "1"),
resource.TestCheckResourceAttr(resourceName, "endpoint_configuration.0.types.0", "PRIVATE"),
resource.TestCheckResourceAttr(resourceName, "endpoint_configuration.0.vpc_endpoint_ids.#", "1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSAPIGatewayRestApi_api_key_source(t *testing.T) {
expectedAPIKeySource := "HEADER"
expectedUpdateAPIKeySource := "AUTHORIZER"
Expand Down Expand Up @@ -515,6 +569,63 @@ resource "aws_api_gateway_rest_api" "test" {
`, rName)
}

func testAccAWSAPIGatewayRestAPIConfig_VPCEndpointConfiguration(rName string) string {
return fmt.Sprintf(`
resource "aws_vpc" "test" {
cidr_block = "11.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true

tags = {
Name = %[1]q
}
}

data "aws_security_group" "test" {
vpc_id = "${aws_vpc.test.id}"
name = "default"
}

data "aws_availability_zones" "available" {}

resource "aws_subnet" "test" {
vpc_id = "${aws_vpc.test.id}"
cidr_block = "${aws_vpc.test.cidr_block}"
availability_zone = "${data.aws_availability_zones.available.names[0]}"

tags = {
Name = %[1]q
}
}

data "aws_region" "current" {}

resource "aws_vpc_endpoint" "test" {
vpc_id = "${aws_vpc.test.id}"
service_name = "com.amazonaws.${data.aws_region.current.name}.execute-api"
vpc_endpoint_type = "Interface"
private_dns_enabled = false

subnet_ids = [
"${aws_subnet.test.id}",
]

security_group_ids = [
"${data.aws_security_group.test.id}",
]
}

resource "aws_api_gateway_rest_api" "test" {
name = %[1]q

endpoint_configuration {
types = ["PRIVATE"]
vpc_endpoint_ids = ["${aws_vpc_endpoint.test.id}"]
}
}
`, rName)
}

const testAccAWSAPIGatewayRestAPIConfigWithAPIKeySource = `
resource "aws_api_gateway_rest_api" "test" {
name = "bar"
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/api_gateway_rest_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ __Note__: If the `body` argument is provided, the OpenAPI specification will be
### endpoint_configuration

* `types` - (Required) A list of endpoint types. This resource currently only supports managing a single value. Valid values: `EDGE`, `REGIONAL` or `PRIVATE`. If unspecified, defaults to `EDGE`. Must be declared as `REGIONAL` in non-Commercial partitions. Refer to the [documentation](https://docs.aws.amazon.com/apigateway/latest/developerguide/create-regional-api.html) for more information on the difference between edge-optimized and regional APIs.
* `vpc_endpoint_ids` - (Optional) A list of VPC Endpoint Ids. It is only supported for PRIVATE endpoint type.

## Attributes Reference

Expand Down