2014-06-27 18:47:19 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-10-10 08:58:48 +02:00
|
|
|
"bytes"
|
2014-06-27 18:47:19 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-08-31 13:12:44 +02:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2015-06-05 17:12:32 +02:00
|
|
|
"strings"
|
2015-11-12 22:15:47 +01:00
|
|
|
"time"
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-11-05 23:43:49 +01:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/elb"
|
2014-10-10 08:58:48 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
2015-06-30 14:01:07 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2014-10-10 08:58:48 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2014-06-27 18:47:19 +02:00
|
|
|
)
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
func resourceAwsElb() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsElbCreate,
|
|
|
|
Read: resourceAwsElbRead,
|
|
|
|
Update: resourceAwsElbUpdate,
|
|
|
|
Delete: resourceAwsElbDelete,
|
2016-05-13 20:13:47 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2014-10-10 08:58:48 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
2015-09-21 23:00:51 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validateElbName,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"internal": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
2014-10-10 09:17:35 +02:00
|
|
|
Computed: true,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-12-10 07:07:08 +01:00
|
|
|
"cross_zone_load_balancing": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
2016-06-02 13:08:46 +02:00
|
|
|
Default: true,
|
2014-12-10 07:07:08 +01:00
|
|
|
},
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"availability_zones": &schema.Schema{
|
2014-12-16 13:13:59 +01:00
|
|
|
Type: schema.TypeSet,
|
2014-10-10 08:58:48 +02:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
2014-12-25 23:12:54 +01:00
|
|
|
Computed: true,
|
2015-04-09 15:38:16 +02:00
|
|
|
Set: schema.HashString,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-07-03 00:55:28 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"instances": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
2014-10-17 03:24:58 +02:00
|
|
|
Computed: true,
|
2015-04-09 15:38:16 +02:00
|
|
|
Set: schema.HashString,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"security_groups": &schema.Schema{
|
2014-12-17 23:53:01 +01:00
|
|
|
Type: schema.TypeSet,
|
2014-10-10 08:58:48 +02:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
2014-10-10 09:17:35 +02:00
|
|
|
Computed: true,
|
2015-04-09 15:38:16 +02:00
|
|
|
Set: schema.HashString,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-07-28 03:20:03 +02:00
|
|
|
|
2015-04-28 16:40:19 +02:00
|
|
|
"source_security_group": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-11-05 23:43:49 +01:00
|
|
|
"source_security_group_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"subnets": &schema.Schema{
|
2014-10-20 22:34:14 +02:00
|
|
|
Type: schema.TypeSet,
|
2014-10-10 08:58:48 +02:00
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Optional: true,
|
2014-10-10 09:17:35 +02:00
|
|
|
Computed: true,
|
2015-04-09 15:38:16 +02:00
|
|
|
Set: schema.HashString,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-09-17 23:56:27 +02:00
|
|
|
|
2015-04-13 22:14:26 +02:00
|
|
|
"idle_timeout": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 60,
|
|
|
|
ValidateFunc: validateIntegerInRange(1, 3600),
|
2015-04-13 22:14:26 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"connection_draining": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
"connection_draining_timeout": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 300,
|
|
|
|
},
|
|
|
|
|
2015-11-03 23:30:18 +01:00
|
|
|
"access_logs": &schema.Schema{
|
2016-02-09 07:19:30 +01:00
|
|
|
Type: schema.TypeList,
|
2015-11-03 23:30:18 +01:00
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"interval": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 60,
|
|
|
|
ValidateFunc: validateAccessLogsInterval,
|
2015-11-03 23:30:18 +01:00
|
|
|
},
|
|
|
|
"bucket": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"bucket_prefix": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
2016-08-24 11:07:47 +02:00
|
|
|
"enabled": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: true,
|
|
|
|
},
|
2015-11-03 23:30:18 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"listener": &schema.Schema{
|
2014-10-10 08:58:48 +02:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"instance_port": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateIntegerInRange(1, 65535),
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"instance_protocol": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateListenerProtocol,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"lb_port": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateIntegerInRange(1, 65535),
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"lb_protocol": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateListenerProtocol,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"ssl_certificate_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsElbListenerHash,
|
|
|
|
},
|
2014-08-08 02:14:48 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
"health_check": &schema.Schema{
|
2016-02-09 08:08:35 +01:00
|
|
|
Type: schema.TypeList,
|
2014-10-10 08:58:48 +02:00
|
|
|
Optional: true,
|
2014-10-10 09:17:35 +02:00
|
|
|
Computed: true,
|
2016-07-05 11:56:17 +02:00
|
|
|
MaxItems: 1,
|
2014-10-10 08:58:48 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"healthy_threshold": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateIntegerInRange(2, 10),
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"unhealthy_threshold": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateIntegerInRange(2, 10),
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"target": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateHeathCheckTarget,
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"interval": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateIntegerInRange(5, 300),
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"timeout": &schema.Schema{
|
2016-08-31 13:12:44 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
ValidateFunc: validateIntegerInRange(2, 60),
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
"dns_name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2015-03-24 19:37:42 +01:00
|
|
|
|
2015-04-30 23:58:09 +02:00
|
|
|
"zone_id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2015-03-24 19:37:42 +01:00
|
|
|
"tags": tagsSchema(),
|
2014-10-10 08:58:48 +02:00
|
|
|
},
|
2014-08-08 02:14:48 +02:00
|
|
|
}
|
2014-10-10 08:58:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsElbCreate(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
elbconn := meta.(*AWSClient).elbconn
|
2014-07-01 20:56:04 +02:00
|
|
|
|
2015-04-14 23:41:36 +02:00
|
|
|
// Expand the "listener" set to aws-sdk-go compat []*elb.Listener
|
2015-04-16 22:28:18 +02:00
|
|
|
listeners, err := expandListeners(d.Get("listener").(*schema.Set).List())
|
2014-07-01 20:56:04 +02:00
|
|
|
if err != nil {
|
2014-10-10 08:58:48 +02:00
|
|
|
return err
|
2014-07-01 20:56:04 +02:00
|
|
|
}
|
|
|
|
|
2015-06-30 14:01:07 +02:00
|
|
|
var elbName string
|
|
|
|
if v, ok := d.GetOk("name"); ok {
|
|
|
|
elbName = v.(string)
|
|
|
|
} else {
|
|
|
|
elbName = resource.PrefixedUniqueId("tf-lb-")
|
|
|
|
d.Set("name", elbName)
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:37:42 +01:00
|
|
|
tags := tagsFromMapELB(d.Get("tags").(map[string]interface{}))
|
2014-10-10 08:58:48 +02:00
|
|
|
// Provision the elb
|
2015-04-14 23:41:36 +02:00
|
|
|
elbOpts := &elb.CreateLoadBalancerInput{
|
2015-06-30 14:01:07 +02:00
|
|
|
LoadBalancerName: aws.String(elbName),
|
2014-10-10 08:58:48 +02:00
|
|
|
Listeners: listeners,
|
2015-03-24 19:37:42 +01:00
|
|
|
Tags: tags,
|
2015-03-02 16:44:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if scheme, ok := d.GetOk("internal"); ok && scheme.(bool) {
|
|
|
|
elbOpts.Scheme = aws.String("internal")
|
2014-10-10 08:58:48 +02:00
|
|
|
}
|
2014-07-01 20:56:04 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
if v, ok := d.GetOk("availability_zones"); ok {
|
2015-04-16 22:28:18 +02:00
|
|
|
elbOpts.AvailabilityZones = expandStringList(v.(*schema.Set).List())
|
2014-10-10 08:58:48 +02:00
|
|
|
}
|
2014-07-05 01:03:01 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
if v, ok := d.GetOk("security_groups"); ok {
|
2015-04-16 22:28:18 +02:00
|
|
|
elbOpts.SecurityGroups = expandStringList(v.(*schema.Set).List())
|
2014-10-10 08:58:48 +02:00
|
|
|
}
|
2014-07-05 01:03:01 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
if v, ok := d.GetOk("subnets"); ok {
|
2015-04-16 22:28:18 +02:00
|
|
|
elbOpts.Subnets = expandStringList(v.(*schema.Set).List())
|
2014-10-10 08:58:48 +02:00
|
|
|
}
|
2014-07-05 01:03:01 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
log.Printf("[DEBUG] ELB create configuration: %#v", elbOpts)
|
2016-03-09 23:53:32 +01:00
|
|
|
err = resource.Retry(1*time.Minute, func() *resource.RetryError {
|
2015-11-12 22:15:47 +01:00
|
|
|
_, err := elbconn.CreateLoadBalancer(elbOpts)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
// Check for IAM SSL Cert error, eventual consistancy issue
|
|
|
|
if awsErr.Code() == "CertificateNotFound" {
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(
|
|
|
|
fmt.Errorf("[WARN] Error creating ELB Listener with SSL Cert, retrying: %s", err))
|
2015-11-12 22:15:47 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2015-11-12 22:15:47 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-05 01:03:01 +02:00
|
|
|
}
|
2014-07-30 16:15:22 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
// Assign the elb's unique identifier for use later
|
2015-06-30 14:01:07 +02:00
|
|
|
d.SetId(elbName)
|
2014-10-10 08:58:48 +02:00
|
|
|
log.Printf("[INFO] ELB ID: %s", d.Id())
|
|
|
|
|
|
|
|
// Enable partial mode and record what we set
|
|
|
|
d.Partial(true)
|
|
|
|
d.SetPartial("name")
|
|
|
|
d.SetPartial("internal")
|
|
|
|
d.SetPartial("availability_zones")
|
2014-10-17 03:02:03 +02:00
|
|
|
d.SetPartial("listener")
|
2014-10-10 08:58:48 +02:00
|
|
|
d.SetPartial("security_groups")
|
|
|
|
d.SetPartial("subnets")
|
|
|
|
|
2015-03-24 19:37:42 +01:00
|
|
|
d.Set("tags", tagsToMapELB(tags))
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
return resourceAwsElbUpdate(d, meta)
|
2014-06-27 18:47:19 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsElbRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
elbconn := meta.(*AWSClient).elbconn
|
2015-04-13 22:14:26 +02:00
|
|
|
elbName := d.Id()
|
2014-11-21 17:58:34 +01:00
|
|
|
|
|
|
|
// Retrieve the ELB properties for updating the state
|
2015-04-14 23:41:36 +02:00
|
|
|
describeElbOpts := &elb.DescribeLoadBalancersInput{
|
2015-04-22 08:38:19 +02:00
|
|
|
LoadBalancerNames: []*string{aws.String(elbName)},
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
|
|
|
|
if err != nil {
|
2015-04-29 18:31:23 +02:00
|
|
|
if isLoadBalancerNotFound(err) {
|
2014-11-21 17:58:34 +01:00
|
|
|
// The ELB is gone now, so just remove it from the state
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error retrieving ELB: %s", err)
|
|
|
|
}
|
2015-03-02 16:44:06 +01:00
|
|
|
if len(describeResp.LoadBalancerDescriptions) != 1 {
|
|
|
|
return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions)
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
2015-04-13 22:14:26 +02:00
|
|
|
describeAttrsOpts := &elb.DescribeLoadBalancerAttributesInput{
|
|
|
|
LoadBalancerName: aws.String(elbName),
|
|
|
|
}
|
|
|
|
describeAttrsResp, err := elbconn.DescribeLoadBalancerAttributes(describeAttrsOpts)
|
|
|
|
if err != nil {
|
2015-04-29 18:31:23 +02:00
|
|
|
if isLoadBalancerNotFound(err) {
|
2015-04-13 22:14:26 +02:00
|
|
|
// The ELB is gone now, so just remove it from the state
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Error retrieving ELB: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lbAttrs := describeAttrsResp.LoadBalancerAttributes
|
|
|
|
|
2015-03-02 16:44:06 +01:00
|
|
|
lb := describeResp.LoadBalancerDescriptions[0]
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2016-07-13 11:07:56 +02:00
|
|
|
d.Set("name", lb.LoadBalancerName)
|
|
|
|
d.Set("dns_name", lb.DNSName)
|
|
|
|
d.Set("zone_id", lb.CanonicalHostedZoneNameID)
|
|
|
|
|
|
|
|
var scheme bool
|
|
|
|
if lb.Scheme != nil {
|
|
|
|
scheme = *lb.Scheme == "internal"
|
|
|
|
}
|
|
|
|
d.Set("internal", scheme)
|
2016-01-10 00:15:57 +01:00
|
|
|
d.Set("availability_zones", flattenStringList(lb.AvailabilityZones))
|
2015-04-16 22:28:18 +02:00
|
|
|
d.Set("instances", flattenInstances(lb.Instances))
|
|
|
|
d.Set("listener", flattenListeners(lb.ListenerDescriptions))
|
2016-01-10 01:09:14 +01:00
|
|
|
d.Set("security_groups", flattenStringList(lb.SecurityGroups))
|
2015-04-28 16:40:19 +02:00
|
|
|
if lb.SourceSecurityGroup != nil {
|
2016-03-04 09:28:37 +01:00
|
|
|
group := lb.SourceSecurityGroup.GroupName
|
|
|
|
if lb.SourceSecurityGroup.OwnerAlias != nil && *lb.SourceSecurityGroup.OwnerAlias != "" {
|
|
|
|
group = aws.String(*lb.SourceSecurityGroup.OwnerAlias + "/" + *lb.SourceSecurityGroup.GroupName)
|
|
|
|
}
|
|
|
|
d.Set("source_security_group", group)
|
2015-11-05 23:43:49 +01:00
|
|
|
|
|
|
|
// Manually look up the ELB Security Group ID, since it's not provided
|
|
|
|
var elbVpc string
|
|
|
|
if lb.VPCId != nil {
|
|
|
|
elbVpc = *lb.VPCId
|
2015-11-26 15:32:21 +01:00
|
|
|
sgId, err := sourceSGIdByName(meta, *lb.SourceSecurityGroup.GroupName, elbVpc)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("[WARN] Error looking up ELB Security Group ID: %s", err)
|
|
|
|
} else {
|
|
|
|
d.Set("source_security_group_id", sgId)
|
|
|
|
}
|
2015-11-05 23:43:49 +01:00
|
|
|
}
|
2015-04-28 16:40:19 +02:00
|
|
|
}
|
2016-01-10 00:15:57 +01:00
|
|
|
d.Set("subnets", flattenStringList(lb.Subnets))
|
2015-11-03 23:30:18 +01:00
|
|
|
d.Set("idle_timeout", lbAttrs.ConnectionSettings.IdleTimeout)
|
|
|
|
d.Set("connection_draining", lbAttrs.ConnectionDraining.Enabled)
|
|
|
|
d.Set("connection_draining_timeout", lbAttrs.ConnectionDraining.Timeout)
|
2016-04-22 02:47:19 +02:00
|
|
|
d.Set("cross_zone_load_balancing", lbAttrs.CrossZoneLoadBalancing.Enabled)
|
2015-11-04 18:50:34 +01:00
|
|
|
if lbAttrs.AccessLog != nil {
|
|
|
|
if err := d.Set("access_logs", flattenAccessLog(lbAttrs.AccessLog)); err != nil {
|
2015-11-10 23:58:24 +01:00
|
|
|
return err
|
2015-11-04 18:50:34 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-21 17:58:34 +01:00
|
|
|
|
2015-11-03 23:30:18 +01:00
|
|
|
resp, err := elbconn.DescribeTags(&elb.DescribeTagsInput{
|
|
|
|
LoadBalancerNames: []*string{lb.LoadBalancerName},
|
2015-03-24 19:37:42 +01:00
|
|
|
})
|
|
|
|
|
2015-04-14 23:41:36 +02:00
|
|
|
var et []*elb.Tag
|
2015-03-24 19:37:42 +01:00
|
|
|
if len(resp.TagDescriptions) > 0 {
|
|
|
|
et = resp.TagDescriptions[0].Tags
|
|
|
|
}
|
|
|
|
d.Set("tags", tagsToMapELB(et))
|
2016-02-09 08:08:35 +01:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
// There's only one health check, so save that to state as we
|
|
|
|
// currently can
|
2015-03-02 16:44:06 +01:00
|
|
|
if *lb.HealthCheck.Target != "" {
|
2015-04-16 22:28:18 +02:00
|
|
|
d.Set("health_check", flattenHealthCheck(lb.HealthCheck))
|
2014-11-21 17:58:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
func resourceAwsElbUpdate(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
elbconn := meta.(*AWSClient).elbconn
|
2014-07-03 07:40:55 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
d.Partial(true)
|
2014-07-07 20:03:29 +02:00
|
|
|
|
2015-04-22 00:17:17 +02:00
|
|
|
if d.HasChange("listener") {
|
|
|
|
o, n := d.GetChange("listener")
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
|
|
|
|
remove, _ := expandListeners(os.Difference(ns).List())
|
|
|
|
add, _ := expandListeners(ns.Difference(os).List())
|
|
|
|
|
|
|
|
if len(remove) > 0 {
|
|
|
|
ports := make([]*int64, 0, len(remove))
|
|
|
|
for _, listener := range remove {
|
|
|
|
ports = append(ports, listener.LoadBalancerPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteListenersOpts := &elb.DeleteLoadBalancerListenersInput{
|
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
|
|
|
LoadBalancerPorts: ports,
|
|
|
|
}
|
|
|
|
|
2015-11-12 23:20:54 +01:00
|
|
|
log.Printf("[DEBUG] ELB Delete Listeners opts: %s", deleteListenersOpts)
|
2015-04-22 00:17:17 +02:00
|
|
|
_, err := elbconn.DeleteLoadBalancerListeners(deleteListenersOpts)
|
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure removing outdated ELB listeners: %s", err)
|
2015-04-22 00:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(add) > 0 {
|
|
|
|
createListenersOpts := &elb.CreateLoadBalancerListenersInput{
|
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
|
|
|
Listeners: add,
|
|
|
|
}
|
|
|
|
|
2016-01-25 18:38:04 +01:00
|
|
|
// Occasionally AWS will error with a 'duplicate listener', without any
|
|
|
|
// other listeners on the ELB. Retry here to eliminate that.
|
2016-03-09 23:53:32 +01:00
|
|
|
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
|
2016-01-25 18:38:04 +01:00
|
|
|
log.Printf("[DEBUG] ELB Create Listeners opts: %s", createListenersOpts)
|
|
|
|
if _, err := elbconn.CreateLoadBalancerListeners(createListenersOpts); err != nil {
|
2016-02-17 23:32:54 +01:00
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
if awsErr.Code() == "DuplicateListener" {
|
|
|
|
log.Printf("[DEBUG] Duplicate listener found for ELB (%s), retrying", d.Id())
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(awsErr)
|
2016-02-17 23:32:54 +01:00
|
|
|
}
|
|
|
|
if awsErr.Code() == "CertificateNotFound" && strings.Contains(awsErr.Message(), "Server Certificate not found for the key: arn") {
|
|
|
|
log.Printf("[DEBUG] SSL Cert not found for given ARN, retrying")
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(awsErr)
|
2016-02-17 23:32:54 +01:00
|
|
|
}
|
2016-01-25 18:38:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't recognize the error, so shouldn't retry.
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2016-01-25 18:38:04 +01:00
|
|
|
}
|
|
|
|
// Successful creation
|
|
|
|
return nil
|
|
|
|
})
|
2015-04-22 00:17:17 +02:00
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure adding new or updated ELB listeners: %s", err)
|
2015-04-22 00:17:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("listener")
|
|
|
|
}
|
|
|
|
|
2014-07-16 23:02:47 +02:00
|
|
|
// If we currently have instances, or did have instances,
|
|
|
|
// we want to figure out what to add and remove from the load
|
|
|
|
// balancer
|
2014-10-10 08:58:48 +02:00
|
|
|
if d.HasChange("instances") {
|
|
|
|
o, n := d.GetChange("instances")
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
2015-04-16 22:28:18 +02:00
|
|
|
remove := expandInstanceString(os.Difference(ns).List())
|
|
|
|
add := expandInstanceString(ns.Difference(os).List())
|
2014-10-10 08:58:48 +02:00
|
|
|
|
|
|
|
if len(add) > 0 {
|
2015-04-14 23:41:36 +02:00
|
|
|
registerInstancesOpts := elb.RegisterInstancesWithLoadBalancerInput{
|
2015-03-02 16:44:06 +01:00
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
2014-10-10 08:58:48 +02:00
|
|
|
Instances: add,
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
2014-07-07 20:03:29 +02:00
|
|
|
|
2014-07-16 23:02:47 +02:00
|
|
|
_, err := elbconn.RegisterInstancesWithLoadBalancer(®isterInstancesOpts)
|
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure registering instances with ELB: %s", err)
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-10 08:58:48 +02:00
|
|
|
if len(remove) > 0 {
|
2015-04-14 23:41:36 +02:00
|
|
|
deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancerInput{
|
2015-03-02 16:44:06 +01:00
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
2014-10-10 08:58:48 +02:00
|
|
|
Instances: remove,
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := elbconn.DeregisterInstancesFromLoadBalancer(&deRegisterInstancesOpts)
|
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure deregistering instances from ELB: %s", err)
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
|
|
|
}
|
2014-12-16 13:13:59 +01:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
d.SetPartial("instances")
|
2014-07-16 23:02:47 +02:00
|
|
|
}
|
|
|
|
|
2015-11-03 23:30:18 +01:00
|
|
|
if d.HasChange("cross_zone_load_balancing") || d.HasChange("idle_timeout") || d.HasChange("access_logs") {
|
2015-03-02 16:44:06 +01:00
|
|
|
attrs := elb.ModifyLoadBalancerAttributesInput{
|
|
|
|
LoadBalancerName: aws.String(d.Get("name").(string)),
|
|
|
|
LoadBalancerAttributes: &elb.LoadBalancerAttributes{
|
|
|
|
CrossZoneLoadBalancing: &elb.CrossZoneLoadBalancing{
|
2015-07-28 22:29:46 +02:00
|
|
|
Enabled: aws.Bool(d.Get("cross_zone_load_balancing").(bool)),
|
2015-03-02 16:44:06 +01:00
|
|
|
},
|
2015-04-13 22:14:26 +02:00
|
|
|
ConnectionSettings: &elb.ConnectionSettings{
|
2015-07-28 22:29:46 +02:00
|
|
|
IdleTimeout: aws.Int64(int64(d.Get("idle_timeout").(int))),
|
2015-04-13 22:14:26 +02:00
|
|
|
},
|
2014-12-16 13:13:59 +01:00
|
|
|
},
|
2015-11-03 23:30:18 +01:00
|
|
|
}
|
|
|
|
|
2016-02-09 07:19:30 +01:00
|
|
|
logs := d.Get("access_logs").([]interface{})
|
2015-11-03 23:30:18 +01:00
|
|
|
if len(logs) > 1 {
|
|
|
|
return fmt.Errorf("Only one access logs config per ELB is supported")
|
|
|
|
} else if len(logs) == 1 {
|
|
|
|
log := logs[0].(map[string]interface{})
|
2015-11-10 23:58:24 +01:00
|
|
|
accessLog := &elb.AccessLog{
|
2016-08-24 11:07:47 +02:00
|
|
|
Enabled: aws.Bool(log["enabled"].(bool)),
|
2015-11-03 23:30:18 +01:00
|
|
|
EmitInterval: aws.Int64(int64(log["interval"].(int))),
|
|
|
|
S3BucketName: aws.String(log["bucket"].(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if log["bucket_prefix"] != "" {
|
2015-11-10 23:58:24 +01:00
|
|
|
accessLog.S3BucketPrefix = aws.String(log["bucket_prefix"].(string))
|
2015-11-03 23:30:18 +01:00
|
|
|
}
|
|
|
|
|
2015-11-10 23:58:24 +01:00
|
|
|
attrs.LoadBalancerAttributes.AccessLog = accessLog
|
2015-11-04 18:50:34 +01:00
|
|
|
} else if len(logs) == 0 {
|
|
|
|
// disable access logs
|
|
|
|
attrs.LoadBalancerAttributes.AccessLog = &elb.AccessLog{
|
|
|
|
Enabled: aws.Bool(false),
|
|
|
|
}
|
2015-11-03 23:30:18 +01:00
|
|
|
}
|
2015-08-23 20:58:25 +02:00
|
|
|
|
2015-11-04 18:50:34 +01:00
|
|
|
log.Printf("[DEBUG] ELB Modify Load Balancer Attributes Request: %#v", attrs)
|
2014-12-16 13:13:59 +01:00
|
|
|
_, err := elbconn.ModifyLoadBalancerAttributes(&attrs)
|
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure configuring ELB attributes: %s", err)
|
2014-12-10 07:07:08 +01:00
|
|
|
}
|
2015-05-06 20:42:59 +02:00
|
|
|
|
2014-12-16 13:13:59 +01:00
|
|
|
d.SetPartial("cross_zone_load_balancing")
|
2015-04-13 22:14:26 +02:00
|
|
|
d.SetPartial("idle_timeout")
|
|
|
|
d.SetPartial("connection_draining_timeout")
|
2014-12-16 13:13:59 +01:00
|
|
|
}
|
2014-12-10 07:07:08 +01:00
|
|
|
|
2015-05-06 20:46:51 +02:00
|
|
|
// We have to do these changes separately from everything else since
|
|
|
|
// they have some weird undocumented rules. You can't set the timeout
|
|
|
|
// without having connection draining to true, so we set that to true,
|
|
|
|
// set the timeout, then reset it to false if requested.
|
2015-05-06 20:42:59 +02:00
|
|
|
if d.HasChange("connection_draining") || d.HasChange("connection_draining_timeout") {
|
2015-05-06 20:46:51 +02:00
|
|
|
// We do timeout changes first since they require us to set draining
|
|
|
|
// to true for a hot second.
|
|
|
|
if d.HasChange("connection_draining_timeout") {
|
|
|
|
attrs := elb.ModifyLoadBalancerAttributesInput{
|
|
|
|
LoadBalancerName: aws.String(d.Get("name").(string)),
|
|
|
|
LoadBalancerAttributes: &elb.LoadBalancerAttributes{
|
|
|
|
ConnectionDraining: &elb.ConnectionDraining{
|
2015-07-28 22:29:46 +02:00
|
|
|
Enabled: aws.Bool(true),
|
|
|
|
Timeout: aws.Int64(int64(d.Get("connection_draining_timeout").(int))),
|
2015-05-06 20:46:51 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := elbconn.ModifyLoadBalancerAttributes(&attrs)
|
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure configuring ELB attributes: %s", err)
|
2015-05-06 20:46:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("connection_draining_timeout")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then we always set connection draining even if there is no change.
|
|
|
|
// This lets us reset to "false" if requested even with a timeout
|
|
|
|
// change.
|
2015-05-06 20:42:59 +02:00
|
|
|
attrs := elb.ModifyLoadBalancerAttributesInput{
|
|
|
|
LoadBalancerName: aws.String(d.Get("name").(string)),
|
|
|
|
LoadBalancerAttributes: &elb.LoadBalancerAttributes{
|
|
|
|
ConnectionDraining: &elb.ConnectionDraining{
|
2015-07-28 22:29:46 +02:00
|
|
|
Enabled: aws.Bool(d.Get("connection_draining").(bool)),
|
2015-05-06 20:42:59 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := elbconn.ModifyLoadBalancerAttributes(&attrs)
|
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure configuring ELB attributes: %s", err)
|
2015-05-06 20:42:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("connection_draining")
|
|
|
|
}
|
|
|
|
|
2015-02-03 04:25:54 +01:00
|
|
|
if d.HasChange("health_check") {
|
2016-02-09 08:08:35 +01:00
|
|
|
hc := d.Get("health_check").([]interface{})
|
2016-07-05 11:56:17 +02:00
|
|
|
if len(hc) > 0 {
|
2016-02-09 08:08:35 +01:00
|
|
|
check := hc[0].(map[string]interface{})
|
2015-03-02 16:44:06 +01:00
|
|
|
configureHealthCheckOpts := elb.ConfigureHealthCheckInput{
|
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
|
|
|
HealthCheck: &elb.HealthCheck{
|
2015-07-28 22:29:46 +02:00
|
|
|
HealthyThreshold: aws.Int64(int64(check["healthy_threshold"].(int))),
|
|
|
|
UnhealthyThreshold: aws.Int64(int64(check["unhealthy_threshold"].(int))),
|
|
|
|
Interval: aws.Int64(int64(check["interval"].(int))),
|
2015-03-02 16:44:06 +01:00
|
|
|
Target: aws.String(check["target"].(string)),
|
2015-07-28 22:29:46 +02:00
|
|
|
Timeout: aws.Int64(int64(check["timeout"].(int))),
|
2015-02-03 04:25:54 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err := elbconn.ConfigureHealthCheck(&configureHealthCheckOpts)
|
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure configuring health check for ELB: %s", err)
|
2015-02-03 04:25:54 +01:00
|
|
|
}
|
|
|
|
d.SetPartial("health_check")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 21:46:29 +02:00
|
|
|
if d.HasChange("security_groups") {
|
|
|
|
groups := d.Get("security_groups").(*schema.Set).List()
|
|
|
|
|
|
|
|
applySecurityGroupsOpts := elb.ApplySecurityGroupsToLoadBalancerInput{
|
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
|
|
|
SecurityGroups: expandStringList(groups),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := elbconn.ApplySecurityGroupsToLoadBalancer(&applySecurityGroupsOpts)
|
|
|
|
if err != nil {
|
2015-05-18 22:25:03 +02:00
|
|
|
return fmt.Errorf("Failure applying security groups to ELB: %s", err)
|
2015-04-23 21:46:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("security_groups")
|
|
|
|
}
|
|
|
|
|
2016-01-09 23:12:24 +01:00
|
|
|
if d.HasChange("availability_zones") {
|
|
|
|
o, n := d.GetChange("availability_zones")
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
|
|
|
|
removed := expandStringList(os.Difference(ns).List())
|
|
|
|
added := expandStringList(ns.Difference(os).List())
|
|
|
|
|
|
|
|
if len(added) > 0 {
|
|
|
|
enableOpts := &elb.EnableAvailabilityZonesForLoadBalancerInput{
|
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
|
|
|
AvailabilityZones: added,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] ELB enable availability zones opts: %s", enableOpts)
|
|
|
|
_, err := elbconn.EnableAvailabilityZonesForLoadBalancer(enableOpts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failure enabling ELB availability zones: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(removed) > 0 {
|
|
|
|
disableOpts := &elb.DisableAvailabilityZonesForLoadBalancerInput{
|
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
|
|
|
AvailabilityZones: removed,
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] ELB disable availability zones opts: %s", disableOpts)
|
|
|
|
_, err := elbconn.DisableAvailabilityZonesForLoadBalancer(disableOpts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failure disabling ELB availability zones: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("availability_zones")
|
|
|
|
}
|
|
|
|
|
2016-01-09 18:51:08 +01:00
|
|
|
if d.HasChange("subnets") {
|
|
|
|
o, n := d.GetChange("subnets")
|
|
|
|
os := o.(*schema.Set)
|
|
|
|
ns := n.(*schema.Set)
|
|
|
|
|
|
|
|
removed := expandStringList(os.Difference(ns).List())
|
|
|
|
added := expandStringList(ns.Difference(os).List())
|
|
|
|
|
2016-09-29 20:01:09 +02:00
|
|
|
if len(removed) > 0 {
|
|
|
|
detachOpts := &elb.DetachLoadBalancerFromSubnetsInput{
|
2016-01-09 18:51:08 +01:00
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
2016-09-29 20:01:09 +02:00
|
|
|
Subnets: removed,
|
2016-01-09 18:51:08 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 20:01:09 +02:00
|
|
|
log.Printf("[DEBUG] ELB detach subnets opts: %s", detachOpts)
|
|
|
|
_, err := elbconn.DetachLoadBalancerFromSubnets(detachOpts)
|
2016-01-09 18:51:08 +01:00
|
|
|
if err != nil {
|
2016-09-29 20:01:09 +02:00
|
|
|
return fmt.Errorf("Failure removing ELB subnets: %s", err)
|
2016-01-09 18:51:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 20:01:09 +02:00
|
|
|
if len(added) > 0 {
|
|
|
|
attachOpts := &elb.AttachLoadBalancerToSubnetsInput{
|
2016-01-09 18:51:08 +01:00
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
2016-09-29 20:01:09 +02:00
|
|
|
Subnets: added,
|
2016-01-09 18:51:08 +01:00
|
|
|
}
|
|
|
|
|
2016-09-29 20:01:09 +02:00
|
|
|
log.Printf("[DEBUG] ELB attach subnets opts: %s", attachOpts)
|
|
|
|
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
|
|
|
|
_, err := elbconn.AttachLoadBalancerToSubnets(attachOpts)
|
|
|
|
if err != nil {
|
|
|
|
if awsErr, ok := err.(awserr.Error); ok {
|
|
|
|
// eventually consistent issue with removing a subnet in AZ1 and
|
|
|
|
// immediately adding a new one in the same AZ
|
|
|
|
if awsErr.Code() == "InvalidConfigurationRequest" && strings.Contains(awsErr.Message(), "cannot be attached to multiple subnets in the same AZ") {
|
|
|
|
log.Printf("[DEBUG] retrying az association")
|
|
|
|
return resource.RetryableError(awsErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2016-01-09 18:51:08 +01:00
|
|
|
if err != nil {
|
2016-09-29 20:01:09 +02:00
|
|
|
return fmt.Errorf("Failure adding ELB subnets: %s", err)
|
2016-01-09 18:51:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetPartial("subnets")
|
|
|
|
}
|
|
|
|
|
2015-03-24 19:37:42 +01:00
|
|
|
if err := setTagsELB(elbconn, d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-04-13 22:14:26 +02:00
|
|
|
|
|
|
|
d.SetPartial("tags")
|
2014-10-10 08:58:48 +02:00
|
|
|
d.Partial(false)
|
2015-03-02 16:44:06 +01:00
|
|
|
|
2014-10-10 22:50:08 +02:00
|
|
|
return resourceAwsElbRead(d, meta)
|
2014-07-03 07:40:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
func resourceAwsElbDelete(d *schema.ResourceData, meta interface{}) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
elbconn := meta.(*AWSClient).elbconn
|
2014-07-07 20:03:29 +02:00
|
|
|
|
2014-10-10 08:58:48 +02:00
|
|
|
log.Printf("[INFO] Deleting ELB: %s", d.Id())
|
2014-07-07 20:03:29 +02:00
|
|
|
|
|
|
|
// Destroy the load balancer
|
2015-04-14 23:41:36 +02:00
|
|
|
deleteElbOpts := elb.DeleteLoadBalancerInput{
|
2015-03-02 16:44:06 +01:00
|
|
|
LoadBalancerName: aws.String(d.Id()),
|
2014-07-07 20:03:29 +02:00
|
|
|
}
|
2014-10-10 08:58:48 +02:00
|
|
|
if _, err := elbconn.DeleteLoadBalancer(&deleteElbOpts); err != nil {
|
2014-07-07 20:03:29 +02:00
|
|
|
return fmt.Errorf("Error deleting ELB: %s", err)
|
|
|
|
}
|
2014-06-27 18:47:19 +02:00
|
|
|
|
2014-07-01 20:56:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
func resourceAwsElbListenerHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["instance_port"].(int)))
|
2015-06-05 17:12:32 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-",
|
|
|
|
strings.ToLower(m["instance_protocol"].(string))))
|
2014-11-21 17:58:34 +01:00
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["lb_port"].(int)))
|
2015-06-05 17:12:32 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-",
|
|
|
|
strings.ToLower(m["lb_protocol"].(string))))
|
2014-08-08 02:14:48 +02:00
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
if v, ok := m["ssl_certificate_id"]; ok {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
|
2014-07-30 16:15:22 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
return hashcode.String(buf.String())
|
2014-07-15 18:18:36 +02:00
|
|
|
}
|
2015-04-29 18:31:23 +02:00
|
|
|
|
|
|
|
func isLoadBalancerNotFound(err error) bool {
|
2015-05-20 13:21:23 +02:00
|
|
|
elberr, ok := err.(awserr.Error)
|
|
|
|
return ok && elberr.Code() == "LoadBalancerNotFound"
|
2015-04-29 18:31:23 +02:00
|
|
|
}
|
2015-09-21 23:00:51 +02:00
|
|
|
|
2015-11-05 23:43:49 +01:00
|
|
|
func sourceSGIdByName(meta interface{}, sg, vpcId string) (string, error) {
|
|
|
|
conn := meta.(*AWSClient).ec2conn
|
|
|
|
var filters []*ec2.Filter
|
|
|
|
var sgFilterName, sgFilterVPCID *ec2.Filter
|
|
|
|
sgFilterName = &ec2.Filter{
|
|
|
|
Name: aws.String("group-name"),
|
|
|
|
Values: []*string{aws.String(sg)},
|
|
|
|
}
|
|
|
|
|
|
|
|
if vpcId != "" {
|
|
|
|
sgFilterVPCID = &ec2.Filter{
|
|
|
|
Name: aws.String("vpc-id"),
|
|
|
|
Values: []*string{aws.String(vpcId)},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
filters = append(filters, sgFilterName)
|
|
|
|
|
|
|
|
if sgFilterVPCID != nil {
|
|
|
|
filters = append(filters, sgFilterVPCID)
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &ec2.DescribeSecurityGroupsInput{
|
|
|
|
Filters: filters,
|
|
|
|
}
|
|
|
|
resp, err := conn.DescribeSecurityGroups(req)
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok {
|
|
|
|
if ec2err.Code() == "InvalidSecurityGroupID.NotFound" ||
|
|
|
|
ec2err.Code() == "InvalidGroup.NotFound" {
|
|
|
|
resp = nil
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error on ELB SG look up: %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp == nil || len(resp.SecurityGroups) == 0 {
|
|
|
|
return "", fmt.Errorf("No security groups found for name %s and vpc id %s", sg, vpcId)
|
|
|
|
}
|
|
|
|
|
|
|
|
group := resp.SecurityGroups[0]
|
|
|
|
return *group.GroupId, nil
|
|
|
|
}
|
2016-08-31 13:12:44 +02:00
|
|
|
|
|
|
|
func validateAccessLogsInterval(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(int)
|
|
|
|
|
|
|
|
// Check if the value is either 5 or 60 (minutes).
|
|
|
|
if value != 5 && value != 60 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q contains an invalid Access Logs interval \"%d\". "+
|
|
|
|
"Valid intervals are either 5 or 60 (minutes).",
|
|
|
|
k, value))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateHeathCheckTarget(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
|
|
|
|
// Parse the Health Check target value.
|
|
|
|
matches := regexp.MustCompile(`\A(\w+):(\d+)(.+)?\z`).FindStringSubmatch(value)
|
|
|
|
|
|
|
|
// Check if the value contains a valid target.
|
|
|
|
if matches == nil || len(matches) < 1 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q contains an invalid Health Check: %s",
|
|
|
|
k, value))
|
|
|
|
|
|
|
|
// Invalid target? Return immediately,
|
|
|
|
// there is no need to collect other
|
|
|
|
// errors.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the value contains a valid protocol.
|
|
|
|
if !isValidProtocol(matches[1]) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q contains an invalid Health Check protocol %q. "+
|
|
|
|
"Valid protocols are either %q, %q, %q, or %q.",
|
|
|
|
k, matches[1], "TCP", "SSL", "HTTP", "HTTPS"))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the value contains a valid port range.
|
|
|
|
port, _ := strconv.Atoi(matches[2])
|
|
|
|
if port < 1 || port > 65535 {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q contains an invalid Health Check target port \"%d\". "+
|
|
|
|
"Valid port is in the range from 1 to 65535 inclusive.",
|
|
|
|
k, port))
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToLower(matches[1]) {
|
|
|
|
case "tcp", "ssl":
|
|
|
|
// Check if value is in the form <PROTOCOL>:<PORT> for TCP and/or SSL.
|
|
|
|
if matches[3] != "" {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q cannot contain a path in the Health Check target: %s",
|
|
|
|
k, value))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case "http", "https":
|
|
|
|
// Check if value is in the form <PROTOCOL>:<PORT>/<PATH> for HTTP and/or HTTPS.
|
|
|
|
if matches[3] == "" {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q must contain a path in the Health Check target: %s",
|
|
|
|
k, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cannot be longer than 1024 multibyte characters.
|
|
|
|
if len([]rune(matches[3])) > 1024 {
|
|
|
|
errors = append(errors, fmt.Errorf("%q cannot contain a path longer "+
|
|
|
|
"than 1024 characters in the Health Check target: %s",
|
|
|
|
k, value))
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateListenerProtocol(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
|
|
|
|
if !isValidProtocol(value) {
|
|
|
|
errors = append(errors, fmt.Errorf(
|
|
|
|
"%q contains an invalid Listener protocol %q. "+
|
|
|
|
"Valid protocols are either %q, %q, %q, or %q.",
|
|
|
|
k, value, "TCP", "SSL", "HTTP", "HTTPS"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidProtocol(s string) bool {
|
|
|
|
if s == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
s = strings.ToLower(s)
|
|
|
|
|
|
|
|
validProtocols := map[string]bool{
|
|
|
|
"http": true,
|
|
|
|
"https": true,
|
|
|
|
"ssl": true,
|
|
|
|
"tcp": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := validProtocols[s]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|