Skip to content

Commit dd66af0

Browse files
authored
Merge pull request hashicorp#8701 from steveh/feature/aws-billing-service-account
provider/aws: Add AWS Billing & Cost Management service account
2 parents 8a793ce + 0cf3300 commit dd66af0

4 files changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package aws
2+
3+
import (
4+
"github.com/hashicorp/terraform/helper/schema"
5+
)
6+
7+
// See http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-getting-started.html#step-2
8+
var billingAccountId = "386209384616"
9+
10+
func dataSourceAwsBillingServiceAccount() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceAwsBillingServiceAccountRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"arn": &schema.Schema{
16+
Type: schema.TypeString,
17+
Computed: true,
18+
},
19+
},
20+
}
21+
}
22+
23+
func dataSourceAwsBillingServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
24+
d.SetId(billingAccountId)
25+
26+
d.Set("arn", "arn:aws:iam::"+billingAccountId+":root")
27+
28+
return nil
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package aws
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform/helper/resource"
7+
)
8+
9+
func TestAccAWSBillingServiceAccount_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: testAccCheckAwsBillingServiceAccountConfig,
16+
Check: resource.ComposeTestCheckFunc(
17+
resource.TestCheckResourceAttr("data.aws_billing_service_account.main", "id", "386209384616"),
18+
resource.TestCheckResourceAttr("data.aws_billing_service_account.main", "arn", "arn:aws:iam::386209384616:root"),
19+
),
20+
},
21+
},
22+
})
23+
}
24+
25+
const testAccCheckAwsBillingServiceAccountConfig = `
26+
data "aws_billing_service_account" "main" { }
27+
`

builtin/providers/aws/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func Provider() terraform.ResourceProvider {
145145
DataSourcesMap: map[string]*schema.Resource{
146146
"aws_ami": dataSourceAwsAmi(),
147147
"aws_availability_zones": dataSourceAwsAvailabilityZones(),
148+
"aws_billing_service_account": dataSourceAwsBillingServiceAccount(),
148149
"aws_caller_identity": dataSourceAwsCallerIdentity(),
149150
"aws_cloudformation_stack": dataSourceAwsCloudFormationStack(),
150151
"aws_ecs_container_definition": dataSourceAwsEcsContainerDefinition(),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
layout: "aws"
3+
page_title: "AWS: aws_billing_service_account"
4+
sidebar_current: "docs-aws-datasource-billing-service-account"
5+
description: |-
6+
Get AWS Billing Service Account
7+
---
8+
9+
# aws\_billing\_service\_account
10+
11+
Use this data source to get the Account ID of the [AWS Billing and Cost Management Service Account](http://docs.aws.amazon.com/awsaccountbilling/latest/aboutv2/billing-getting-started.html#step-2) for the purpose of whitelisting in S3 bucket policy.
12+
13+
## Example Usage
14+
15+
```
16+
data "aws_billing_service_account" "main" { }
17+
18+
resource "aws_s3_bucket" "billing_logs" {
19+
bucket = "my-billing-tf-test-bucket"
20+
acl = "private"
21+
policy = <<POLICY
22+
{
23+
"Id": "Policy",
24+
"Version": "2012-10-17",
25+
"Statement": [
26+
{
27+
"Action": [
28+
"s3:GetBucketAcl", "s3:GetBucketPolicy"
29+
],
30+
"Effect": "Allow",
31+
"Resource": "arn:aws:s3:::my-billing-tf-test-bucket",
32+
"Principal": {
33+
"AWS": [
34+
"${data.aws_billing_service_account.main.id}"
35+
]
36+
}
37+
},
38+
{
39+
"Action": [
40+
"s3:PutObject"
41+
],
42+
"Effect": "Allow",
43+
"Resource": "arn:aws:s3:::my-billing-tf-test-bucket/AWSLogs/*",
44+
"Principal": {
45+
"AWS": [
46+
"${data.aws_billing_service_account.main.id}"
47+
]
48+
}
49+
}
50+
]
51+
}
52+
POLICY
53+
}
54+
```
55+
56+
57+
## Attributes Reference
58+
59+
* `id` - The ID of the AWS billing service account.
60+
* `arn` - The ARN of the AWS billing service account.

0 commit comments

Comments
 (0)