Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ARN attribute for aws_instance and data.aws_instance (#5425) #5432

Merged
merged 2 commits into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions aws/data_source_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log"

"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 +27,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 +322,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", 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", 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