Skip to content

Commit 65aa02b

Browse files
authored
provider/aws: DataSource for RedShift Account ID (hashicorp#8224)
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
1 parent 24e7c3b commit 65aa02b

5 files changed

Lines changed: 148 additions & 1 deletion

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package aws
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform/helper/schema"
7+
)
8+
9+
// See http://docs.aws.amazon.com/redshift/latest/mgmt/db-auditing.html#db-auditing-enable-logging
10+
var redshiftServiceAccountPerRegionMap = map[string]string{
11+
"us-east-1": "193672423079",
12+
"us-west-1": "262260360010",
13+
"us-west-2": "902366379725",
14+
"ap-south-1": "865932855811",
15+
"ap-northeast-2": "760740231472",
16+
"ap-southeast-1": "361669875840",
17+
"ap-southeast-2": "762762565011",
18+
"ap-northeast-1": "404641285394",
19+
"eu-central-1": "053454850223",
20+
"eu-west-1": "210876761215",
21+
}
22+
23+
func dataSourceAwsRedshiftServiceAccount() *schema.Resource {
24+
return &schema.Resource{
25+
Read: dataSourceAwsRedshiftServiceAccountRead,
26+
27+
Schema: map[string]*schema.Schema{
28+
"region": &schema.Schema{
29+
Type: schema.TypeString,
30+
Optional: true,
31+
},
32+
},
33+
}
34+
}
35+
36+
func dataSourceAwsRedshiftServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
37+
region := meta.(*AWSClient).region
38+
if v, ok := d.GetOk("region"); ok {
39+
region = v.(string)
40+
}
41+
42+
if accid, ok := redshiftServiceAccountPerRegionMap[region]; ok {
43+
d.SetId(accid)
44+
return nil
45+
}
46+
47+
return fmt.Errorf("Unknown region (%q)", region)
48+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package aws
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/resource"
7+
)
8+
9+
func TestAccAWSRedshiftServiceAccount_basic(t *testing.T) {
10+
resource.Test(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
Providers: testAccProviders,
13+
Steps: []resource.TestStep{
14+
resource.TestStep{
15+
Config: testAccCheckAwsRedshiftServiceAccountConfig,
16+
Check: resource.ComposeTestCheckFunc(
17+
resource.TestCheckResourceAttr("data.aws_redshift_service_account.main", "id", "902366379725"),
18+
),
19+
},
20+
resource.TestStep{
21+
Config: testAccCheckAwsRedshiftServiceAccountExplicitRegionConfig,
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttr("data.aws_redshift_service_account.regional", "id", "210876761215"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
const testAccCheckAwsRedshiftServiceAccountConfig = `
31+
data "aws_redshift_service_account" "main" { }
32+
`
33+
34+
const testAccCheckAwsRedshiftServiceAccountExplicitRegionConfig = `
35+
data "aws_redshift_service_account" "regional" {
36+
region = "eu-west-1"
37+
}
38+
`

builtin/providers/aws/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,12 @@ func Provider() terraform.ResourceProvider {
143143
"aws_ami": dataSourceAwsAmi(),
144144
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
145145
"aws_caller_identity": dataSourceAwsCallerIdentity(),
146+
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
146147
"aws_elb_account_id": dataSourceAwsElbAccountId(),
147148
"aws_iam_policy_document": dataSourceAwsIamPolicyDocument(),
148149
"aws_ip_ranges": dataSourceAwsIPRanges(),
150+
"aws_redshift_service_account": dataSourceAwsRedshiftServiceAccount(),
149151
"aws_s3_bucket_object": dataSourceAwsS3BucketObject(),
150-
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
151152
},
152153

153154
ResourcesMap: map[string]*schema.Resource{
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
layout: "aws"
3+
page_title: "AWS: aws_redshift_account_id"
4+
sidebar_current: "docs-aws-datasource-redshift-account-id"
5+
description: |-
6+
Get AWS Redshift Service Account ID for storing audit data in S3.
7+
---
8+
9+
# aws\_redshift\_service\_account
10+
11+
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)
12+
in a given region for the purpose of allowing Redshift to store audit data in S3.
13+
14+
## Example Usage
15+
16+
```
17+
data "aws_redshift_service_account" "main" { }
18+
19+
resource "aws_s3_bucket" "bucket" {
20+
bucket = "tf-redshift-logging-test-bucket"
21+
force_destroy = true
22+
policy = <<EOF
23+
{
24+
"Version": "2008-10-17",
25+
"Statement": [
26+
{
27+
"Sid": "Put bucket policy needed for audit logging",
28+
"Effect": "Allow",
29+
"Principal": {
30+
"AWS": "arn:aws:iam:${data.aws_redshift_account_id.main.id}:user/logs"
31+
},
32+
"Action": "s3:PutObject",
33+
"Resource": "arn:aws:s3:::tf-redshift-logging-test-bucket/*"
34+
},
35+
{
36+
"Sid": "Get bucket policy needed for audit logging ",
37+
"Effect": "Allow",
38+
"Principal": {
39+
"AWS": "arn:aws:iam:${data.aws_redshift_account_id.main.id}:user/logs"
40+
},
41+
"Action": "s3:GetBucketAcl",
42+
"Resource": "arn:aws:s3:::tf-redshift-logging-test-bucket"
43+
}
44+
]
45+
}
46+
EOF
47+
}
48+
```
49+
50+
## Argument Reference
51+
52+
* `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.
53+
54+
55+
## Attributes Reference
56+
57+
* `id` - The ID of the Redshift service Account in the selected region.

website/source/layouts/aws.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<li<%= sidebar_current("docs-aws-datasource-ip_ranges") %>>
3636
<a href="/docs/providers/aws/d/ip_ranges.html">aws_ip_ranges</a>
3737
</li>
38+
<li<%= sidebar_current("docs-aws-datasource-redshift-account-id") %>>
39+
<a href="/docs/providers/aws/d/redshift_service_account.html">aws_redshift_servcice_account</a>
40+
</li>
3841
<li<%= sidebar_current("docs-aws-datasource-s3-bucket-object") %>>
3942
<a href="/docs/providers/aws/d/s3_bucket_object.html">aws_s3_bucket_object</a>
4043
</li>

0 commit comments

Comments
 (0)