github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/resource_aws_launch_configuration.go (about) 1 package aws 2 3 import ( 4 "crypto/sha1" 5 "encoding/hex" 6 "fmt" 7 "log" 8 "time" 9 10 "github.com/hashicorp/terraform/helper/hashcode" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/helper/schema" 13 "github.com/mitchellh/goamz/autoscaling" 14 ) 15 16 func resourceAwsLaunchConfiguration() *schema.Resource { 17 return &schema.Resource{ 18 Create: resourceAwsLaunchConfigurationCreate, 19 Read: resourceAwsLaunchConfigurationRead, 20 Delete: resourceAwsLaunchConfigurationDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "name": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "image_id": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 35 "instance_type": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 ForceNew: true, 39 }, 40 41 "iam_instance_profile": &schema.Schema{ 42 Type: schema.TypeString, 43 Optional: true, 44 ForceNew: true, 45 }, 46 47 "key_name": &schema.Schema{ 48 Type: schema.TypeString, 49 Optional: true, 50 Computed: true, 51 ForceNew: true, 52 }, 53 54 "user_data": &schema.Schema{ 55 Type: schema.TypeString, 56 Optional: true, 57 ForceNew: true, 58 StateFunc: func(v interface{}) string { 59 switch v.(type) { 60 case string: 61 hash := sha1.Sum([]byte(v.(string))) 62 return hex.EncodeToString(hash[:]) 63 default: 64 return "" 65 } 66 }, 67 }, 68 69 "security_groups": &schema.Schema{ 70 Type: schema.TypeSet, 71 Optional: true, 72 ForceNew: true, 73 Elem: &schema.Schema{Type: schema.TypeString}, 74 Set: func(v interface{}) int { 75 return hashcode.String(v.(string)) 76 }, 77 }, 78 79 "associate_public_ip_address": &schema.Schema{ 80 Type: schema.TypeBool, 81 Optional: true, 82 Default: false, 83 }, 84 }, 85 } 86 } 87 88 func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface{}) error { 89 autoscalingconn := meta.(*AWSClient).autoscalingconn 90 91 var createLaunchConfigurationOpts autoscaling.CreateLaunchConfiguration 92 createLaunchConfigurationOpts.Name = d.Get("name").(string) 93 createLaunchConfigurationOpts.IamInstanceProfile = d.Get("iam_instance_profile").(string) 94 createLaunchConfigurationOpts.ImageId = d.Get("image_id").(string) 95 createLaunchConfigurationOpts.InstanceType = d.Get("instance_type").(string) 96 createLaunchConfigurationOpts.KeyName = d.Get("key_name").(string) 97 createLaunchConfigurationOpts.UserData = d.Get("user_data").(string) 98 createLaunchConfigurationOpts.AssociatePublicIpAddress = d.Get("associate_public_ip_address").(bool) 99 100 if v, ok := d.GetOk("security_groups"); ok { 101 createLaunchConfigurationOpts.SecurityGroups = expandStringList( 102 v.(*schema.Set).List()) 103 } 104 105 log.Printf("[DEBUG] autoscaling create launch configuration: %#v", createLaunchConfigurationOpts) 106 _, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts) 107 if err != nil { 108 return fmt.Errorf("Error creating launch configuration: %s", err) 109 } 110 111 d.SetId(d.Get("name").(string)) 112 log.Printf("[INFO] launch configuration ID: %s", d.Id()) 113 114 // We put a Retry here since sometimes eventual consistency bites 115 // us and we need to retry a few times to get the LC to load properly 116 return resource.Retry(30*time.Second, func() error { 117 return resourceAwsLaunchConfigurationRead(d, meta) 118 }) 119 } 120 121 func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}) error { 122 autoscalingconn := meta.(*AWSClient).autoscalingconn 123 124 describeOpts := autoscaling.DescribeLaunchConfigurations{ 125 Names: []string{d.Id()}, 126 } 127 128 log.Printf("[DEBUG] launch configuration describe configuration: %#v", describeOpts) 129 describConfs, err := autoscalingconn.DescribeLaunchConfigurations(&describeOpts) 130 if err != nil { 131 return fmt.Errorf("Error retrieving launch configuration: %s", err) 132 } 133 if len(describConfs.LaunchConfigurations) == 0 { 134 d.SetId("") 135 return nil 136 } 137 138 // Verify AWS returned our launch configuration 139 if describConfs.LaunchConfigurations[0].Name != d.Id() { 140 return fmt.Errorf( 141 "Unable to find launch configuration: %#v", 142 describConfs.LaunchConfigurations) 143 } 144 145 lc := describConfs.LaunchConfigurations[0] 146 147 d.Set("key_name", lc.KeyName) 148 d.Set("iam_instance_profile", lc.IamInstanceProfile) 149 d.Set("image_id", lc.ImageId) 150 d.Set("instance_type", lc.InstanceType) 151 d.Set("name", lc.Name) 152 d.Set("security_groups", lc.SecurityGroups) 153 154 return nil 155 } 156 157 func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface{}) error { 158 autoscalingconn := meta.(*AWSClient).autoscalingconn 159 160 log.Printf("[DEBUG] Launch Configuration destroy: %v", d.Id()) 161 _, err := autoscalingconn.DeleteLaunchConfiguration( 162 &autoscaling.DeleteLaunchConfiguration{Name: d.Id()}) 163 if err != nil { 164 autoscalingerr, ok := err.(*autoscaling.Error) 165 if ok && autoscalingerr.Code == "InvalidConfiguration.NotFound" { 166 return nil 167 } 168 169 return err 170 } 171 172 return nil 173 }