-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/aws: DataSource for RedShift Account ID
When you need to enable monitoring for Redshift, you need to create the correct policy in the bucket for logging. This needs to have the Redshift Account ID for a given region. This data source provides a handy lookup for this http://docs.aws.amazon.com/redshift/latest/mgmt/db-auditing.html#db-auditing-enable-logging % make testacc TEST=./builtin/providers/aws % TESTARGS='-run=TestAccAWSRedshiftAccountId_basic' 2 ↵ ✹ ✭ ==> Checking that code complies with gofmt requirements... /Users/stacko/Code/go/bin/stringer go generate $(go list ./... | grep -v /terraform/vendor/) 2016/08/16 14:39:35 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSRedshiftAccountId_basic -timeout 120m === RUN TestAccAWSRedshiftAccountId_basic --- PASS: TestAccAWSRedshiftAccountId_basic (19.47s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 19.483s
- Loading branch information
Showing
5 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
builtin/providers/aws/data_source_aws_redshift_service_account.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
// See http://docs.aws.amazon.com/redshift/latest/mgmt/db-auditing.html#db-auditing-enable-logging | ||
var redshiftServiceAccountPerRegionMap = map[string]string{ | ||
"us-east-1": "193672423079", | ||
"us-west-1": "262260360010", | ||
"us-west-2": "902366379725", | ||
"ap-south-1": "865932855811", | ||
"ap-northeast-2": "760740231472", | ||
"ap-southeast-1": "361669875840", | ||
"ap-southeast-2": "762762565011", | ||
"ap-northeast-1": "404641285394", | ||
"eu-central-1": "053454850223", | ||
"eu-west-1": "210876761215", | ||
} | ||
|
||
func dataSourceAwsRedshiftServiceAccount() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsRedshiftServiceAccountRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"region": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsRedshiftServiceAccountRead(d *schema.ResourceData, meta interface{}) error { | ||
region := meta.(*AWSClient).region | ||
if v, ok := d.GetOk("region"); ok { | ||
region = v.(string) | ||
} | ||
|
||
if accid, ok := redshiftServiceAccountPerRegionMap[region]; ok { | ||
d.SetId(accid) | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Unknown region (%q)", region) | ||
} |
38 changes: 38 additions & 0 deletions
38
builtin/providers/aws/data_source_aws_redshift_service_account_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSRedshiftServiceAccount_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckAwsRedshiftServiceAccountConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.aws_redshift_service_account.main", "id", "902366379725"), | ||
), | ||
}, | ||
resource.TestStep{ | ||
Config: testAccCheckAwsRedshiftServiceAccountExplicitRegionConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.aws_redshift_service_account.regional", "id", "210876761215"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccCheckAwsRedshiftServiceAccountConfig = ` | ||
data "aws_redshift_service_account" "main" { } | ||
` | ||
|
||
const testAccCheckAwsRedshiftServiceAccountExplicitRegionConfig = ` | ||
data "aws_redshift_service_account" "regional" { | ||
region = "eu-west-1" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
website/source/docs/providers/aws/d/redshift_service_account.html.markdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_redshift_account_id" | ||
sidebar_current: "docs-aws-datasource-redshift-account-id" | ||
description: |- | ||
Get AWS Redshift Service Account ID for storing audit data in S3. | ||
--- | ||
|
||
# aws\_redshift\_service\_account | ||
|
||
Use this data source to get the Service Account ID of the [AWS Redshift Account](http://docs.aws.amazon.com/redshift/latest/mgmt/db-auditing.html#db-auditing-enable-logging) | ||
in a given region for the purpose of allowing Redshift to store audit data in S3. | ||
|
||
## Example Usage | ||
|
||
``` | ||
data "aws_redshift_service_account" "main" { } | ||
resource "aws_s3_bucket" "bucket" { | ||
bucket = "tf-redshift-logging-test-bucket" | ||
force_destroy = true | ||
policy = <<EOF | ||
{ | ||
"Version": "2008-10-17", | ||
"Statement": [ | ||
{ | ||
"Sid": "Put bucket policy needed for audit logging", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "arn:aws:iam:${data.aws_redshift_account_id.main.id}:user/logs" | ||
}, | ||
"Action": "s3:PutObject", | ||
"Resource": "arn:aws:s3:::tf-redshift-logging-test-bucket/*" | ||
}, | ||
{ | ||
"Sid": "Get bucket policy needed for audit logging ", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"AWS": "arn:aws:iam:${data.aws_redshift_account_id.main.id}:user/logs" | ||
}, | ||
"Action": "s3:GetBucketAcl", | ||
"Resource": "arn:aws:s3:::tf-redshift-logging-test-bucket" | ||
} | ||
] | ||
} | ||
EOF | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `region` - (Optional) Name of the Region whose Redshift account id is desired. If not specified, default's to the region from the AWS provider configuration. | ||
|
||
|
||
## Attributes Reference | ||
|
||
* `id` - The ID of the Redshift service Account in the selected region. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters