From c8d5ea4d9d8f803aa440d93be39dc3b179fcfa57 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Mon, 28 Jul 2014 12:47:40 -0400 Subject: [PATCH] provider/aws: fixing instance for EC2-VPC /cc:@pearkes --- .../providers/aws/resource_aws_instance.go | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 71ee08e93..10fe9455e 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "strconv" + "strings" "time" "github.com/hashicorp/terraform/flatmap" @@ -263,10 +264,28 @@ func resource_aws_instance_update_state( s.Attributes["subnet_id"] = instance.SubnetId s.Dependencies = nil + // Extract the existing security groups + useID := false + if raw := flatmap.Expand(s.Attributes, "security_groups"); raw != nil { + if sgs, ok := raw.([]interface{}); ok { + for _, sg := range sgs { + str, ok := sg.(string) + if !ok { + continue + } + + if strings.Contains(str, "sg-") { + useID = true + break + } + } + } + } + // Build up the security groups sgs := make([]string, len(instance.SecurityGroups)) for i, sg := range instance.SecurityGroups { - if instance.SubnetId != "" { + if instance.SubnetId != "" && useID { sgs[i] = sg.Id } else { sgs[i] = sg.Name