github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/data_source_aws_caller_identity.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/service/sts"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func dataSourceAwsCallerIdentity() *schema.Resource {
    13  	return &schema.Resource{
    14  		Read: dataSourceAwsCallerIdentityRead,
    15  
    16  		Schema: map[string]*schema.Schema{
    17  			"account_id": {
    18  				Type:     schema.TypeString,
    19  				Computed: true,
    20  			},
    21  
    22  			"arn": {
    23  				Type:     schema.TypeString,
    24  				Computed: true,
    25  			},
    26  
    27  			"user_id": {
    28  				Type:     schema.TypeString,
    29  				Computed: true,
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  func dataSourceAwsCallerIdentityRead(d *schema.ResourceData, meta interface{}) error {
    36  	client := meta.(*AWSClient).stsconn
    37  
    38  	res, err := client.GetCallerIdentity(&sts.GetCallerIdentityInput{})
    39  	if err != nil {
    40  		return fmt.Errorf("Error getting Caller Identity: %v", err)
    41  	}
    42  
    43  	log.Printf("[DEBUG] Received Caller Identity: %s", res)
    44  
    45  	d.SetId(time.Now().UTC().String())
    46  	d.Set("account_id", res.Account)
    47  	d.Set("arn", res.Arn)
    48  	d.Set("user_id", res.UserId)
    49  	return nil
    50  }