Skip to content

Commit

Permalink
Add ARN attribute for aws_instance and data.aws_instance (hashicorp#5425
Browse files Browse the repository at this point in the history
)
  • Loading branch information
blckct committed Aug 2, 2018
1 parent f2af45c commit ecc2a66
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
16 changes: 16 additions & 0 deletions aws/data_source_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package aws
import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)
Expand All @@ -26,6 +28,10 @@ func dataSourceAwsInstance() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"instance_type": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -317,6 +323,16 @@ func dataSourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("password_data", passwordData)
}

// ARN
arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "ec2",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("instance/%s", strings.TrimPrefix(d.Id(), "/")),
}
d.Set("arn", arn.String())

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions aws/data_source_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"fmt"
"regexp"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
Expand All @@ -20,6 +21,7 @@ func TestAccAWSInstanceDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr("data.aws_instance.web-instance", "ami", "ami-4fccb37f"),
resource.TestCheckResourceAttr("data.aws_instance.web-instance", "tags.%", "1"),
resource.TestCheckResourceAttr("data.aws_instance.web-instance", "instance_type", "m1.small"),
resource.TestMatchResourceAttr("data.aws_instance.web-instance", "arn", regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:instance/i-.+`)),
),
},
},
Expand Down
17 changes: 17 additions & 0 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,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/ec2"
"github.com/hashicorp/terraform/helper/hashcode"
Expand Down Expand Up @@ -45,6 +46,11 @@ func resourceAwsInstance() *schema.Resource {
ForceNew: true,
},

"arn": {
Type: schema.TypeString,
Computed: true,
},

"associate_public_ip_address": {
Type: schema.TypeBool,
ForceNew: true,
Expand Down Expand Up @@ -802,6 +808,17 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("ephemeral_block_device", []interface{}{})
}

// ARN

arn := arn.ARN{
Partition: meta.(*AWSClient).partition,
Region: meta.(*AWSClient).region,
Service: "ec2",
AccountID: meta.(*AWSClient).accountid,
Resource: fmt.Sprintf("instance/%s", strings.TrimPrefix(d.Id(), "/")),
}
d.Set("arn", arn.String())

// Instance attributes
{
attr, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{
Expand Down
4 changes: 4 additions & 0 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ func TestAccAWSInstance_basic(t *testing.T) {
"3dc39dda39be1205215e776bad998da361a5955d"),
resource.TestCheckResourceAttr(
"aws_instance.foo", "ebs_block_device.#", "0"),
resource.TestMatchResourceAttr(
"aws_instance.foo",
"arn",
regexp.MustCompile(`^arn:[^:]+:ec2:[^:]+:\d{12}:instance/i-.+`)),
),
},

Expand Down
1 change: 1 addition & 0 deletions website/docs/d/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ are exported:
interpolation.

* `ami` - The ID of the AMI used to launch the instance.
* `arn` - The ARN of the instance.
* `associate_public_ip_address` - Whether or not the Instance is associated with a public IP address or not (Boolean).
* `availability_zone` - The availability zone of the Instance.
* `ebs_block_device` - The EBS block device mappings of the Instance.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ resource "aws_instance" "foo" {
In addition to all arguments above, the following attributes are exported:

* `id` - The instance ID.
* `arn` - The ARN of the instance.
* `availability_zone` - The availability zone of the instance.
* `placement_group` - The placement group of the instance.
* `key_name` - The key name of the instance
Expand Down

0 comments on commit ecc2a66

Please sign in to comment.