Skip to content

Commit

Permalink
Merge pull request #3968 from mewa/master
Browse files Browse the repository at this point in the history
aws_api_gateway_rest_api: Add execute_arn computed attribute
  • Loading branch information
bflad authored May 11, 2018
2 parents e00f22f + 04690b5 commit 7d28327
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
15 changes: 15 additions & 0 deletions aws/resource_aws_api_gateway_rest_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/errwrap"
Expand Down Expand Up @@ -66,6 +67,10 @@ func resourceAwsApiGatewayRestApi() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"execution_arn": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -172,6 +177,16 @@ func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{})
d.Set("policy", policy)

d.Set("binary_media_types", api.BinaryMediaTypes)

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Service: "execute-api",
Region: meta.(*AWSClient).region,
AccountID: meta.(*AWSClient).accountid,
Resource: d.Id(),
}.String()
d.Set("execution_arn", arn)

if api.MinimumCompressionSize == nil {
d.Set("minimum_compression_size", -1)
} else {
Expand Down
4 changes: 4 additions & 0 deletions aws/resource_aws_api_gateway_rest_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "description", ""),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "minimum_compression_size", "0"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "created_date"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "execution_arn"),
resource.TestCheckNoResourceAttr("aws_api_gateway_rest_api.test", "binary_media_types"),
),
},
Expand All @@ -114,6 +115,7 @@ func TestAccAWSAPIGatewayRestApi_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "description", "test"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "minimum_compression_size", "10485760"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "created_date"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "execution_arn"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "binary_media_types.#", "1"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "binary_media_types.0", "application/octet-stream"),
),
Expand Down Expand Up @@ -178,6 +180,7 @@ func TestAccAWSAPIGatewayRestApi_openapi(t *testing.T) {
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "name", "test"),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "description", ""),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "created_date"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "execution_arn"),
resource.TestCheckNoResourceAttr("aws_api_gateway_rest_api.test", "binary_media_types"),
),
},
Expand All @@ -190,6 +193,7 @@ func TestAccAWSAPIGatewayRestApi_openapi(t *testing.T) {
testAccCheckAWSAPIGatewayRestAPIRoutes(&conf, []string{"/", "/update"}),
resource.TestCheckResourceAttr("aws_api_gateway_rest_api.test", "name", "test"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "created_date"),
resource.TestCheckResourceAttrSet("aws_api_gateway_rest_api.test", "execution_arn"),
),
},
},
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/api_gateway_rest_api.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ The following attributes are exported:
* `id` - The ID of the REST API
* `root_resource_id` - The resource ID of the REST API's root
* `created_date` - The creation date of the REST API
* `execution_arn` - The execution ARN part to be used in [`lambda_permission`](/docs/providers/aws/r/lambda_permission.html)'s `source_arn`
when allowing API Gateway to invoke a Lambda function,
e.g. `arn:aws:execute-api:eu-west-2:123456789012:z4675bid1j`, which can be concatenated with allowed stage, method and resource path.
20 changes: 20 additions & 0 deletions website/docs/r/lambda_permission.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,26 @@ EOF
}
```

## Specify Lambda permissions for API Gateway REST API

```hcl
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
resource "aws_lambda_permission" "lambda_permission" {
statement_id = "AllowMyDemoAPIInvoke"
action = "lambda:InvokeFunction"
function_name = "MyDemoFunction"
principal = "apigateway.amazonaws.com"
# The /*/*/* part allows invocation from any stage, method and resource path
# within API Gateway REST API.
source_arn = "${aws_api_gateway_rest_api.MyDemoAPI.execution_arn}/*/*/*"
}
```

## Argument Reference

* `action` - (Required) The AWS Lambda action you want to allow in this statement. (e.g. `lambda:InvokeFunction`)
Expand Down

0 comments on commit 7d28327

Please sign in to comment.