2015-05-05 00:10:54 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-08-23 16:13:02 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-05-05 00:10:54 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/ecs"
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
var taskDefinitionRE = regexp.MustCompile("^([a-zA-Z0-9_-]+):([0-9]+)$")
|
|
|
|
|
|
|
|
func resourceAwsEcsService() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsEcsServiceCreate,
|
|
|
|
Read: resourceAwsEcsServiceRead,
|
|
|
|
Update: resourceAwsEcsServiceUpdate,
|
|
|
|
Delete: resourceAwsEcsServiceDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-17 19:43:34 +01:00
|
|
|
"name": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"cluster": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2015-07-12 13:20:57 +02:00
|
|
|
ForceNew: true,
|
2015-05-05 00:10:54 +02:00
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"task_definition": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"desired_count": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"iam_role": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeString,
|
2015-12-22 21:10:23 +01:00
|
|
|
ForceNew: true,
|
2015-05-05 00:10:54 +02:00
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"deployment_maximum_percent": {
|
2016-02-19 21:32:39 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 200,
|
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"deployment_minimum_healthy_percent": {
|
2016-02-19 21:32:39 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 100,
|
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"load_balancer": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
2015-12-22 21:10:23 +01:00
|
|
|
ForceNew: true,
|
2016-03-13 08:58:14 +01:00
|
|
|
MaxItems: 1,
|
2015-05-05 00:10:54 +02:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-17 19:43:34 +01:00
|
|
|
"elb_name": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeString,
|
2016-08-23 18:19:43 +02:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"target_group_arn": {
|
2016-08-23 18:19:43 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
2015-12-22 21:10:23 +01:00
|
|
|
ForceNew: true,
|
2015-05-05 00:10:54 +02:00
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"container_name": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2015-12-22 21:10:23 +01:00
|
|
|
ForceNew: true,
|
2015-05-05 00:10:54 +02:00
|
|
|
},
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"container_port": {
|
2015-05-05 00:10:54 +02:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
2015-12-22 21:10:23 +01:00
|
|
|
ForceNew: true,
|
2015-05-05 00:10:54 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAwsEcsLoadBalancerHash,
|
|
|
|
},
|
2017-01-17 19:10:45 +01:00
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"placement_strategy": {
|
2017-01-17 19:10:45 +01:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
MaxItems: 5,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-17 19:43:34 +01:00
|
|
|
"type": {
|
2017-01-17 19:10:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Required: true,
|
|
|
|
},
|
2017-01-17 19:43:34 +01:00
|
|
|
"field": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
2017-01-19 23:54:52 +01:00
|
|
|
Optional: true,
|
2017-03-30 22:42:16 +02:00
|
|
|
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
|
|
|
|
if strings.ToLower(old) == strings.ToLower(new) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2017-01-17 19:43:34 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-01-19 23:54:52 +01:00
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
"placement_constraints": {
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
MaxItems: 10,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-19 23:54:52 +01:00
|
|
|
"type": {
|
2017-01-17 19:43:34 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"expression": {
|
2017-01-17 19:10:45 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
2017-01-19 23:54:52 +01:00
|
|
|
Optional: true,
|
2017-01-17 19:10:45 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-05-05 00:10:54 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecsconn
|
|
|
|
|
|
|
|
input := ecs.CreateServiceInput{
|
|
|
|
ServiceName: aws.String(d.Get("name").(string)),
|
|
|
|
TaskDefinition: aws.String(d.Get("task_definition").(string)),
|
2015-07-28 22:29:46 +02:00
|
|
|
DesiredCount: aws.Int64(int64(d.Get("desired_count").(int))),
|
2015-07-12 15:30:49 +02:00
|
|
|
ClientToken: aws.String(resource.UniqueId()),
|
2016-02-19 21:32:39 +01:00
|
|
|
DeploymentConfiguration: &ecs.DeploymentConfiguration{
|
|
|
|
MaximumPercent: aws.Int64(int64(d.Get("deployment_maximum_percent").(int))),
|
|
|
|
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
|
|
|
|
},
|
2015-05-05 00:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("cluster"); ok {
|
|
|
|
input.Cluster = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
loadBalancers := expandEcsLoadBalancers(d.Get("load_balancer").(*schema.Set).List())
|
|
|
|
if len(loadBalancers) > 0 {
|
2015-07-28 22:29:46 +02:00
|
|
|
log.Printf("[DEBUG] Adding ECS load balancers: %s", loadBalancers)
|
2015-05-05 00:10:54 +02:00
|
|
|
input.LoadBalancers = loadBalancers
|
|
|
|
}
|
|
|
|
if v, ok := d.GetOk("iam_role"); ok {
|
|
|
|
input.Role = aws.String(v.(string))
|
|
|
|
}
|
|
|
|
|
2017-01-17 19:10:45 +01:00
|
|
|
strategies := d.Get("placement_strategy").(*schema.Set).List()
|
|
|
|
if len(strategies) > 0 {
|
|
|
|
var ps []*ecs.PlacementStrategy
|
|
|
|
for _, raw := range strategies {
|
|
|
|
p := raw.(map[string]interface{})
|
2017-01-19 23:54:52 +01:00
|
|
|
t := p["type"].(string)
|
|
|
|
f := p["field"].(string)
|
|
|
|
if err := validateAwsEcsPlacementStrategy(t, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-17 19:10:45 +01:00
|
|
|
ps = append(ps, &ecs.PlacementStrategy{
|
|
|
|
Type: aws.String(p["type"].(string)),
|
|
|
|
Field: aws.String(p["field"].(string)),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
input.PlacementStrategy = ps
|
|
|
|
}
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
constraints := d.Get("placement_constraints").(*schema.Set).List()
|
|
|
|
if len(constraints) > 0 {
|
|
|
|
var pc []*ecs.PlacementConstraint
|
|
|
|
for _, raw := range constraints {
|
|
|
|
p := raw.(map[string]interface{})
|
2017-01-19 23:54:52 +01:00
|
|
|
t := p["type"].(string)
|
|
|
|
e := p["expression"].(string)
|
|
|
|
if err := validateAwsEcsPlacementConstraint(t, e); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-29 17:42:50 +01:00
|
|
|
constraint := &ecs.PlacementConstraint{
|
|
|
|
Type: aws.String(t),
|
|
|
|
}
|
|
|
|
if e != "" {
|
|
|
|
constraint.Expression = aws.String(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
pc = append(pc, constraint)
|
2017-01-17 19:43:34 +01:00
|
|
|
}
|
|
|
|
input.PlacementConstraints = pc
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:29:46 +02:00
|
|
|
log.Printf("[DEBUG] Creating ECS service: %s", input)
|
2015-08-23 16:13:02 +02:00
|
|
|
|
2017-06-05 10:50:23 +02:00
|
|
|
// Retry due to AWS IAM & ECS eventual consistency
|
2015-08-23 16:13:02 +02:00
|
|
|
var out *ecs.CreateServiceOutput
|
|
|
|
var err error
|
2016-03-09 23:53:32 +01:00
|
|
|
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
|
2015-08-23 16:13:02 +02:00
|
|
|
out, err = conn.CreateService(&input)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-05-21 21:19:41 +02:00
|
|
|
awsErr, ok := err.(awserr.Error)
|
2015-08-23 16:13:02 +02:00
|
|
|
if !ok {
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2015-08-23 16:13:02 +02:00
|
|
|
}
|
2017-05-21 21:19:41 +02:00
|
|
|
if awsErr.Code() == "InvalidParameterException" {
|
2015-08-23 16:13:02 +02:00
|
|
|
log.Printf("[DEBUG] Trying to create ECS service again: %q",
|
2017-06-05 10:50:23 +02:00
|
|
|
awsErr.Message())
|
|
|
|
return resource.RetryableError(err)
|
|
|
|
}
|
|
|
|
if awsErr.Code() == "ClusterNotFoundException" {
|
|
|
|
log.Printf("[DEBUG] Trying to create ECS service again: %q",
|
2017-05-21 21:19:41 +02:00
|
|
|
awsErr.Message())
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(err)
|
2015-08-23 16:13:02 +02:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2015-08-23 16:13:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2015-05-05 00:10:54 +02:00
|
|
|
if err != nil {
|
2017-02-18 14:59:49 +01:00
|
|
|
return fmt.Errorf("%s %q", err, d.Get("name").(string))
|
2015-05-05 00:10:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
service := *out.Service
|
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
log.Printf("[DEBUG] ECS service created: %s", *service.ServiceArn)
|
|
|
|
d.SetId(*service.ServiceArn)
|
2015-05-05 00:10:54 +02:00
|
|
|
|
|
|
|
return resourceAwsEcsServiceUpdate(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecsconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Reading ECS service %s", d.Id())
|
|
|
|
input := ecs.DescribeServicesInput{
|
|
|
|
Services: []*string{aws.String(d.Id())},
|
|
|
|
Cluster: aws.String(d.Get("cluster").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := conn.DescribeServices(&input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-07-12 13:20:57 +02:00
|
|
|
|
|
|
|
if len(out.Services) < 1 {
|
2015-11-16 23:29:52 +01:00
|
|
|
log.Printf("[DEBUG] Removing ECS service %s (%s) because it's gone", d.Get("name").(string), d.Id())
|
2015-11-13 22:44:17 +01:00
|
|
|
d.SetId("")
|
2015-07-12 13:20:57 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-05-05 00:10:54 +02:00
|
|
|
|
|
|
|
service := out.Services[0]
|
2015-11-09 15:10:40 +01:00
|
|
|
|
|
|
|
// Status==INACTIVE means deleted service
|
|
|
|
if *service.Status == "INACTIVE" {
|
2015-11-17 00:12:26 +01:00
|
|
|
log.Printf("[DEBUG] Removing ECS service %q because it's INACTIVE", *service.ServiceArn)
|
2015-11-09 15:10:40 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-07-28 22:29:46 +02:00
|
|
|
log.Printf("[DEBUG] Received ECS service %s", service)
|
2015-05-05 00:10:54 +02:00
|
|
|
|
2015-08-17 20:27:16 +02:00
|
|
|
d.SetId(*service.ServiceArn)
|
2016-08-28 18:15:45 +02:00
|
|
|
d.Set("name", service.ServiceName)
|
2015-05-05 00:10:54 +02:00
|
|
|
|
|
|
|
// Save task definition in the same format
|
2017-01-23 18:02:53 +01:00
|
|
|
if strings.HasPrefix(d.Get("task_definition").(string), "arn:"+meta.(*AWSClient).partition+":ecs:") {
|
2016-08-28 18:15:45 +02:00
|
|
|
d.Set("task_definition", service.TaskDefinition)
|
2015-05-05 00:10:54 +02:00
|
|
|
} else {
|
|
|
|
taskDefinition := buildFamilyAndRevisionFromARN(*service.TaskDefinition)
|
|
|
|
d.Set("task_definition", taskDefinition)
|
|
|
|
}
|
|
|
|
|
2016-08-28 18:15:45 +02:00
|
|
|
d.Set("desired_count", service.DesiredCount)
|
2015-10-28 15:54:53 +01:00
|
|
|
|
|
|
|
// Save cluster in the same format
|
2017-01-23 18:02:53 +01:00
|
|
|
if strings.HasPrefix(d.Get("cluster").(string), "arn:"+meta.(*AWSClient).partition+":ecs:") {
|
2016-08-28 18:15:45 +02:00
|
|
|
d.Set("cluster", service.ClusterArn)
|
2015-10-28 15:54:53 +01:00
|
|
|
} else {
|
|
|
|
clusterARN := getNameFromARN(*service.ClusterArn)
|
|
|
|
d.Set("cluster", clusterARN)
|
|
|
|
}
|
2015-05-05 00:10:54 +02:00
|
|
|
|
2015-08-23 14:48:16 +02:00
|
|
|
// Save IAM role in the same format
|
2015-08-17 20:27:16 +02:00
|
|
|
if service.RoleArn != nil {
|
2017-01-23 18:02:53 +01:00
|
|
|
if strings.HasPrefix(d.Get("iam_role").(string), "arn:"+meta.(*AWSClient).partition+":iam:") {
|
2016-08-28 18:15:45 +02:00
|
|
|
d.Set("iam_role", service.RoleArn)
|
2015-08-23 14:48:16 +02:00
|
|
|
} else {
|
2015-10-28 15:54:53 +01:00
|
|
|
roleARN := getNameFromARN(*service.RoleArn)
|
2015-08-23 14:48:16 +02:00
|
|
|
d.Set("iam_role", roleARN)
|
|
|
|
}
|
2015-05-05 00:10:54 +02:00
|
|
|
}
|
|
|
|
|
2016-02-19 21:32:39 +01:00
|
|
|
if service.DeploymentConfiguration != nil {
|
2016-08-28 18:15:45 +02:00
|
|
|
d.Set("deployment_maximum_percent", service.DeploymentConfiguration.MaximumPercent)
|
|
|
|
d.Set("deployment_minimum_healthy_percent", service.DeploymentConfiguration.MinimumHealthyPercent)
|
2016-02-19 21:32:39 +01:00
|
|
|
}
|
|
|
|
|
2015-05-05 00:10:54 +02:00
|
|
|
if service.LoadBalancers != nil {
|
|
|
|
d.Set("load_balancers", flattenEcsLoadBalancers(service.LoadBalancers))
|
|
|
|
}
|
|
|
|
|
2017-01-17 19:10:45 +01:00
|
|
|
if err := d.Set("placement_strategy", flattenPlacementStrategy(service.PlacementStrategy)); err != nil {
|
|
|
|
log.Printf("[ERR] Error setting placement_strategy for (%s): %s", d.Id(), err)
|
|
|
|
}
|
2017-01-17 19:43:34 +01:00
|
|
|
if err := d.Set("placement_constraints", flattenServicePlacementConstraints(service.PlacementConstraints)); err != nil {
|
|
|
|
log.Printf("[ERR] Error setting placement_constraints for (%s): %s", d.Id(), err)
|
|
|
|
}
|
2017-01-17 19:10:45 +01:00
|
|
|
|
2015-05-05 00:10:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
func flattenServicePlacementConstraints(pcs []*ecs.PlacementConstraint) []map[string]interface{} {
|
|
|
|
if len(pcs) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
results := make([]map[string]interface{}, 0)
|
|
|
|
for _, pc := range pcs {
|
|
|
|
c := make(map[string]interface{})
|
|
|
|
c["type"] = *pc.Type
|
2017-01-29 17:42:50 +01:00
|
|
|
if pc.Expression != nil {
|
|
|
|
c["expression"] = *pc.Expression
|
|
|
|
}
|
|
|
|
|
2017-01-17 19:43:34 +01:00
|
|
|
results = append(results, c)
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2017-01-17 19:10:45 +01:00
|
|
|
func flattenPlacementStrategy(pss []*ecs.PlacementStrategy) []map[string]interface{} {
|
|
|
|
if len(pss) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
results := make([]map[string]interface{}, 0)
|
|
|
|
for _, ps := range pss {
|
|
|
|
c := make(map[string]interface{})
|
|
|
|
c["type"] = *ps.Type
|
2017-03-23 15:10:50 +01:00
|
|
|
c["field"] = *ps.Field
|
|
|
|
|
|
|
|
// for some fields the API requires lowercase for creation but will return uppercase on query
|
|
|
|
if *ps.Field == "MEMORY" || *ps.Field == "CPU" {
|
|
|
|
c["field"] = strings.ToLower(*ps.Field)
|
|
|
|
}
|
|
|
|
|
2017-01-17 19:10:45 +01:00
|
|
|
results = append(results, c)
|
|
|
|
}
|
|
|
|
return results
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:10:54 +02:00
|
|
|
func resourceAwsEcsServiceUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecsconn
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updating ECS service %s", d.Id())
|
|
|
|
input := ecs.UpdateServiceInput{
|
|
|
|
Service: aws.String(d.Id()),
|
|
|
|
Cluster: aws.String(d.Get("cluster").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if d.HasChange("desired_count") {
|
|
|
|
_, n := d.GetChange("desired_count")
|
2015-07-28 22:29:46 +02:00
|
|
|
input.DesiredCount = aws.Int64(int64(n.(int)))
|
2015-05-05 00:10:54 +02:00
|
|
|
}
|
|
|
|
if d.HasChange("task_definition") {
|
|
|
|
_, n := d.GetChange("task_definition")
|
|
|
|
input.TaskDefinition = aws.String(n.(string))
|
|
|
|
}
|
|
|
|
|
2016-02-19 21:32:39 +01:00
|
|
|
if d.HasChange("deployment_maximum_percent") || d.HasChange("deployment_minimum_healthy_percent") {
|
|
|
|
input.DeploymentConfiguration = &ecs.DeploymentConfiguration{
|
|
|
|
MaximumPercent: aws.Int64(int64(d.Get("deployment_maximum_percent").(int))),
|
|
|
|
MinimumHealthyPercent: aws.Int64(int64(d.Get("deployment_minimum_healthy_percent").(int))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 14:20:19 +02:00
|
|
|
// Retry due to IAM & ECS eventual consistency
|
2017-05-21 21:19:41 +02:00
|
|
|
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
|
|
|
|
out, err := conn.UpdateService(&input)
|
|
|
|
if err != nil {
|
|
|
|
awsErr, ok := err.(awserr.Error)
|
|
|
|
if ok && awsErr.Code() == "InvalidParameterException" {
|
|
|
|
log.Printf("[DEBUG] Trying to update ECS service again: %#v", err)
|
|
|
|
return resource.RetryableError(err)
|
|
|
|
}
|
2017-06-05 14:20:19 +02:00
|
|
|
if ok && awsErr.Code() == "ServiceNotFoundException" {
|
|
|
|
log.Printf("[DEBUG] Trying to update ECS service again: %#v", err)
|
|
|
|
return resource.RetryableError(err)
|
|
|
|
}
|
2017-05-21 21:19:41 +02:00
|
|
|
|
|
|
|
return resource.NonRetryableError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[DEBUG] Updated ECS service %s", out.Service)
|
|
|
|
return nil
|
|
|
|
})
|
2015-05-05 00:10:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceAwsEcsServiceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcsServiceDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
conn := meta.(*AWSClient).ecsconn
|
|
|
|
|
|
|
|
// Check if it's not already gone
|
|
|
|
resp, err := conn.DescribeServices(&ecs.DescribeServicesInput{
|
|
|
|
Services: []*string{aws.String(d.Id())},
|
|
|
|
Cluster: aws.String(d.Get("cluster").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-13 22:41:22 +01:00
|
|
|
|
|
|
|
if len(resp.Services) == 0 {
|
|
|
|
log.Printf("[DEBUG] ECS Service %q is already gone", d.Id())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:10:54 +02:00
|
|
|
log.Printf("[DEBUG] ECS service %s is currently %s", d.Id(), *resp.Services[0].Status)
|
|
|
|
|
|
|
|
if *resp.Services[0].Status == "INACTIVE" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drain the ECS service
|
|
|
|
if *resp.Services[0].Status != "DRAINING" {
|
|
|
|
log.Printf("[DEBUG] Draining ECS service %s", d.Id())
|
|
|
|
_, err = conn.UpdateService(&ecs.UpdateServiceInput{
|
|
|
|
Service: aws.String(d.Id()),
|
|
|
|
Cluster: aws.String(d.Get("cluster").(string)),
|
2015-07-28 22:29:46 +02:00
|
|
|
DesiredCount: aws.Int64(int64(0)),
|
2015-05-05 00:10:54 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 15:21:05 +01:00
|
|
|
// Wait until the ECS service is drained
|
2016-03-09 23:53:32 +01:00
|
|
|
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
|
2015-12-17 15:21:05 +01:00
|
|
|
input := ecs.DeleteServiceInput{
|
|
|
|
Service: aws.String(d.Id()),
|
|
|
|
Cluster: aws.String(d.Get("cluster").(string)),
|
|
|
|
}
|
2015-05-05 00:10:54 +02:00
|
|
|
|
2015-12-17 15:21:05 +01:00
|
|
|
log.Printf("[DEBUG] Trying to delete ECS service %s", input)
|
|
|
|
_, err := conn.DeleteService(&input)
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ec2err, ok := err.(awserr.Error)
|
|
|
|
if !ok {
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2015-12-17 15:21:05 +01:00
|
|
|
}
|
|
|
|
if ec2err.Code() == "InvalidParameterException" {
|
|
|
|
// Prevent "The service cannot be stopped while deployments are active."
|
|
|
|
log.Printf("[DEBUG] Trying to delete ECS service again: %q",
|
|
|
|
ec2err.Message())
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.RetryableError(err)
|
2015-12-17 15:21:05 +01:00
|
|
|
}
|
|
|
|
|
2016-03-09 23:53:32 +01:00
|
|
|
return resource.NonRetryableError(err)
|
2015-12-17 15:21:05 +01:00
|
|
|
|
|
|
|
})
|
2015-05-05 00:10:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait until it's deleted
|
|
|
|
wait := resource.StateChangeConf{
|
2017-03-23 10:29:15 +01:00
|
|
|
Pending: []string{"ACTIVE", "DRAINING"},
|
2016-01-21 02:20:41 +01:00
|
|
|
Target: []string{"INACTIVE"},
|
2016-10-22 15:16:59 +02:00
|
|
|
Timeout: 10 * time.Minute,
|
2015-05-05 00:10:54 +02:00
|
|
|
MinTimeout: 1 * time.Second,
|
|
|
|
Refresh: func() (interface{}, string, error) {
|
|
|
|
log.Printf("[DEBUG] Checking if ECS service %s is INACTIVE", d.Id())
|
|
|
|
resp, err := conn.DescribeServices(&ecs.DescribeServicesInput{
|
|
|
|
Services: []*string{aws.String(d.Id())},
|
|
|
|
Cluster: aws.String(d.Get("cluster").(string)),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return resp, "FAILED", err
|
|
|
|
}
|
|
|
|
|
2016-01-12 23:07:26 +01:00
|
|
|
log.Printf("[DEBUG] ECS service (%s) is currently %q", d.Id(), *resp.Services[0].Status)
|
2015-05-05 00:10:54 +02:00
|
|
|
return resp, *resp.Services[0].Status, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = wait.WaitForState()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-17 15:21:05 +01:00
|
|
|
log.Printf("[DEBUG] ECS service %s deleted.", d.Id())
|
2015-05-05 00:10:54 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsEcsLoadBalancerHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
2017-04-04 05:57:42 +02:00
|
|
|
|
2017-04-10 15:00:18 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["elb_name"].(string)))
|
2015-05-05 00:10:54 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["container_name"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["container_port"].(int)))
|
|
|
|
|
2017-04-10 15:00:18 +02:00
|
|
|
if s := m["target_group_arn"].(string); s != "" {
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", s))
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:10:54 +02:00
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildFamilyAndRevisionFromARN(arn string) string {
|
|
|
|
return strings.Split(arn, "/")[1]
|
|
|
|
}
|
|
|
|
|
2015-10-28 15:54:53 +01:00
|
|
|
// Expects the following ARNs:
|
|
|
|
// arn:aws:iam::0123456789:role/EcsService
|
|
|
|
// arn:aws:ecs:us-west-2:0123456789:cluster/radek-cluster
|
|
|
|
func getNameFromARN(arn string) string {
|
2015-08-23 14:48:16 +02:00
|
|
|
return strings.Split(arn, "/")[1]
|
|
|
|
}
|
|
|
|
|
2015-05-05 00:10:54 +02:00
|
|
|
func parseTaskDefinition(taskDefinition string) (string, string, error) {
|
|
|
|
matches := taskDefinitionRE.FindAllStringSubmatch(taskDefinition, 2)
|
|
|
|
|
|
|
|
if len(matches) == 0 || len(matches[0]) != 3 {
|
|
|
|
return "", "", fmt.Errorf(
|
|
|
|
"Invalid task definition format, family:rev or ARN expected (%#v)",
|
|
|
|
taskDefinition)
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches[0][1], matches[0][2], nil
|
|
|
|
}
|