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

provider/aws: read iam_instance_profile for instance and save to state #3167

Merged
merged 1 commit into from
Sep 5, 2015
Merged
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
11 changes: 11 additions & 0 deletions builtin/providers/aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,13 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("public_ip", instance.PublicIpAddress)
d.Set("private_dns", instance.PrivateDnsName)
d.Set("private_ip", instance.PrivateIpAddress)

if instance.IamInstanceProfile != nil {
d.Set("iam_instance_profile", iamInstanceProfileArnToName(instance.IamInstanceProfile.Arn))
} else if _, ok := d.GetOk("iam_instance_profile"); ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I know, setting it to the empty string only when GetOk returns true is functionally equivalent to always setting it to the empty string, since for string schemata that is the "zero value" anyway. So I think this could just be a simple else block, rather than an else if.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this because if there's currently no attribute in the state file, I don't want to add one with an empty string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, interesting... I'd never noticed that Terraform made any distinction there before. I thought it always just wrote everything out in all cases, but I guess you learn something new every day! Thanks!

d.Set("iam_instance_profile", "")
}

if len(instance.NetworkInterfaces) > 0 {
d.Set("subnet_id", instance.NetworkInterfaces[0].SubnetId)
} else {
Expand Down Expand Up @@ -1070,3 +1077,7 @@ func awsTerminateInstance(conn *ec2.EC2, id string) error {

return nil
}

func iamInstanceProfileArnToName(arn *string) string {
return strings.Split(*arn, "/")[1]
}