github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_ecs_service_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/service/ecs"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/terraform"
    12  )
    13  
    14  func TestParseTaskDefinition(t *testing.T) {
    15  	cases := map[string]map[string]interface{}{
    16  		"invalid": {
    17  			"family":   "",
    18  			"revision": "",
    19  			"isValid":  false,
    20  		},
    21  		"invalidWithColon:": {
    22  			"family":   "",
    23  			"revision": "",
    24  			"isValid":  false,
    25  		},
    26  		"1234": {
    27  			"family":   "",
    28  			"revision": "",
    29  			"isValid":  false,
    30  		},
    31  		"invalid:aaa": {
    32  			"family":   "",
    33  			"revision": "",
    34  			"isValid":  false,
    35  		},
    36  		"invalid=family:1": {
    37  			"family":   "",
    38  			"revision": "",
    39  			"isValid":  false,
    40  		},
    41  		"invalid:name:1": {
    42  			"family":   "",
    43  			"revision": "",
    44  			"isValid":  false,
    45  		},
    46  		"valid:1": {
    47  			"family":   "valid",
    48  			"revision": "1",
    49  			"isValid":  true,
    50  		},
    51  		"abc12-def:54": {
    52  			"family":   "abc12-def",
    53  			"revision": "54",
    54  			"isValid":  true,
    55  		},
    56  		"lorem_ip-sum:123": {
    57  			"family":   "lorem_ip-sum",
    58  			"revision": "123",
    59  			"isValid":  true,
    60  		},
    61  		"lorem-ipsum:1": {
    62  			"family":   "lorem-ipsum",
    63  			"revision": "1",
    64  			"isValid":  true,
    65  		},
    66  	}
    67  
    68  	for input, expectedOutput := range cases {
    69  		family, revision, err := parseTaskDefinition(input)
    70  		isValid := expectedOutput["isValid"].(bool)
    71  		if !isValid && err == nil {
    72  			t.Fatalf("Task definition %s should fail", input)
    73  		}
    74  
    75  		expectedFamily := expectedOutput["family"].(string)
    76  		if family != expectedFamily {
    77  			t.Fatalf("Unexpected family (%#v) for task definition %s\n%#v", family, input, err)
    78  		}
    79  		expectedRevision := expectedOutput["revision"].(string)
    80  		if revision != expectedRevision {
    81  			t.Fatalf("Unexpected revision (%#v) for task definition %s\n%#v", revision, input, err)
    82  		}
    83  	}
    84  }
    85  
    86  func TestAccAWSEcsServiceWithARN(t *testing.T) {
    87  	resource.Test(t, resource.TestCase{
    88  		PreCheck:     func() { testAccPreCheck(t) },
    89  		Providers:    testAccProviders,
    90  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
    91  		Steps: []resource.TestStep{
    92  			{
    93  				Config: testAccAWSEcsService,
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
    96  				),
    97  			},
    98  
    99  			{
   100  				Config: testAccAWSEcsServiceModified,
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
   103  				),
   104  			},
   105  		},
   106  	})
   107  }
   108  
   109  func TestAccAWSEcsServiceWithFamilyAndRevision(t *testing.T) {
   110  	resource.Test(t, resource.TestCase{
   111  		PreCheck:     func() { testAccPreCheck(t) },
   112  		Providers:    testAccProviders,
   113  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   114  		Steps: []resource.TestStep{
   115  			{
   116  				Config: testAccAWSEcsServiceWithFamilyAndRevision,
   117  				Check: resource.ComposeTestCheckFunc(
   118  					testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"),
   119  				),
   120  			},
   121  
   122  			{
   123  				Config: testAccAWSEcsServiceWithFamilyAndRevisionModified,
   124  				Check: resource.ComposeTestCheckFunc(
   125  					testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"),
   126  				),
   127  			},
   128  		},
   129  	})
   130  }
   131  
   132  // Regression for https://github.com/hashicorp/terraform/issues/2427
   133  func TestAccAWSEcsServiceWithRenamedCluster(t *testing.T) {
   134  	originalRegexp := regexp.MustCompile(
   135  		"^arn:aws:ecs:[^:]+:[0-9]+:cluster/terraformecstest3$")
   136  	modifiedRegexp := regexp.MustCompile(
   137  		"^arn:aws:ecs:[^:]+:[0-9]+:cluster/terraformecstest3modified$")
   138  
   139  	resource.Test(t, resource.TestCase{
   140  		PreCheck:     func() { testAccPreCheck(t) },
   141  		Providers:    testAccProviders,
   142  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   143  		Steps: []resource.TestStep{
   144  			{
   145  				Config: testAccAWSEcsServiceWithRenamedCluster,
   146  				Check: resource.ComposeTestCheckFunc(
   147  					testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
   148  					resource.TestMatchResourceAttr(
   149  						"aws_ecs_service.ghost", "cluster", originalRegexp),
   150  				),
   151  			},
   152  
   153  			{
   154  				Config: testAccAWSEcsServiceWithRenamedClusterModified,
   155  				Check: resource.ComposeTestCheckFunc(
   156  					testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
   157  					resource.TestMatchResourceAttr(
   158  						"aws_ecs_service.ghost", "cluster", modifiedRegexp),
   159  				),
   160  			},
   161  		},
   162  	})
   163  }
   164  
   165  func TestAccAWSEcsService_withIamRole(t *testing.T) {
   166  	resource.Test(t, resource.TestCase{
   167  		PreCheck:     func() { testAccPreCheck(t) },
   168  		Providers:    testAccProviders,
   169  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   170  		Steps: []resource.TestStep{
   171  			{
   172  				Config: testAccAWSEcsService_withIamRole,
   173  				Check: resource.ComposeTestCheckFunc(
   174  					testAccCheckAWSEcsServiceExists("aws_ecs_service.ghost"),
   175  				),
   176  			},
   177  		},
   178  	})
   179  }
   180  
   181  func TestAccAWSEcsService_withDeploymentValues(t *testing.T) {
   182  	resource.Test(t, resource.TestCase{
   183  		PreCheck:     func() { testAccPreCheck(t) },
   184  		Providers:    testAccProviders,
   185  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   186  		Steps: []resource.TestStep{
   187  			{
   188  				Config: testAccAWSEcsServiceWithDeploymentValues,
   189  				Check: resource.ComposeTestCheckFunc(
   190  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
   191  					resource.TestCheckResourceAttr(
   192  						"aws_ecs_service.mongo", "deployment_maximum_percent", "200"),
   193  					resource.TestCheckResourceAttr(
   194  						"aws_ecs_service.mongo", "deployment_minimum_healthy_percent", "100"),
   195  				),
   196  			},
   197  		},
   198  	})
   199  }
   200  
   201  // Regression for https://github.com/hashicorp/terraform/issues/3444
   202  func TestAccAWSEcsService_withLbChanges(t *testing.T) {
   203  	resource.Test(t, resource.TestCase{
   204  		PreCheck:     func() { testAccPreCheck(t) },
   205  		Providers:    testAccProviders,
   206  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   207  		Steps: []resource.TestStep{
   208  			{
   209  				Config: testAccAWSEcsService_withLbChanges,
   210  				Check: resource.ComposeTestCheckFunc(
   211  					testAccCheckAWSEcsServiceExists("aws_ecs_service.with_lb_changes"),
   212  				),
   213  			},
   214  			{
   215  				Config: testAccAWSEcsService_withLbChanges_modified,
   216  				Check: resource.ComposeTestCheckFunc(
   217  					testAccCheckAWSEcsServiceExists("aws_ecs_service.with_lb_changes"),
   218  				),
   219  			},
   220  		},
   221  	})
   222  }
   223  
   224  // Regression for https://github.com/hashicorp/terraform/issues/3361
   225  func TestAccAWSEcsService_withEcsClusterName(t *testing.T) {
   226  	clusterName := regexp.MustCompile("^terraformecstestcluster$")
   227  	resource.Test(t, resource.TestCase{
   228  		PreCheck:     func() { testAccPreCheck(t) },
   229  		Providers:    testAccProviders,
   230  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   231  		Steps: []resource.TestStep{
   232  			{
   233  				Config: testAccAWSEcsServiceWithEcsClusterName,
   234  				Check: resource.ComposeTestCheckFunc(
   235  					testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"),
   236  					resource.TestMatchResourceAttr(
   237  						"aws_ecs_service.jenkins", "cluster", clusterName),
   238  				),
   239  			},
   240  		},
   241  	})
   242  }
   243  
   244  func TestAccAWSEcsService_withAlb(t *testing.T) {
   245  	resource.Test(t, resource.TestCase{
   246  		PreCheck:     func() { testAccPreCheck(t) },
   247  		Providers:    testAccProviders,
   248  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   249  		Steps: []resource.TestStep{
   250  			{
   251  				Config: testAccAWSEcsServiceWithAlb,
   252  				Check: resource.ComposeTestCheckFunc(
   253  					testAccCheckAWSEcsServiceExists("aws_ecs_service.with_alb"),
   254  				),
   255  			},
   256  		},
   257  	})
   258  }
   259  
   260  func TestAccAWSEcsServiceWithPlacementStrategy(t *testing.T) {
   261  	resource.Test(t, resource.TestCase{
   262  		PreCheck:     func() { testAccPreCheck(t) },
   263  		Providers:    testAccProviders,
   264  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   265  		Steps: []resource.TestStep{
   266  			{
   267  				Config: testAccAWSEcsService,
   268  				Check: resource.ComposeTestCheckFunc(
   269  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
   270  					resource.TestCheckResourceAttr("aws_ecs_service.mongo", "placement_strategy.#", "0"),
   271  				),
   272  			},
   273  			{
   274  				Config: testAccAWSEcsServiceWithPlacementStrategy,
   275  				Check: resource.ComposeTestCheckFunc(
   276  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
   277  					resource.TestCheckResourceAttr("aws_ecs_service.mongo", "placement_strategy.#", "1"),
   278  				),
   279  			},
   280  		},
   281  	})
   282  }
   283  
   284  func TestAccAWSEcsServiceWithPlacementConstraints(t *testing.T) {
   285  	resource.Test(t, resource.TestCase{
   286  		PreCheck:     func() { testAccPreCheck(t) },
   287  		Providers:    testAccProviders,
   288  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   289  		Steps: []resource.TestStep{
   290  			{
   291  				Config: testAccAWSEcsServiceWithPlacementConstraint,
   292  				Check: resource.ComposeTestCheckFunc(
   293  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
   294  					resource.TestCheckResourceAttr("aws_ecs_service.mongo", "placement_constraints.#", "1"),
   295  				),
   296  			},
   297  		},
   298  	})
   299  }
   300  
   301  func TestAccAWSEcsServiceWithPlacementConstraints_emptyExpression(t *testing.T) {
   302  	resource.Test(t, resource.TestCase{
   303  		PreCheck:     func() { testAccPreCheck(t) },
   304  		Providers:    testAccProviders,
   305  		CheckDestroy: testAccCheckAWSEcsServiceDestroy,
   306  		Steps: []resource.TestStep{
   307  			{
   308  				Config: testAccAWSEcsServiceWithPlacementConstraintEmptyExpression,
   309  				Check: resource.ComposeTestCheckFunc(
   310  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
   311  					resource.TestCheckResourceAttr("aws_ecs_service.mongo", "placement_constraints.#", "1"),
   312  				),
   313  			},
   314  		},
   315  	})
   316  }
   317  
   318  func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
   319  	conn := testAccProvider.Meta().(*AWSClient).ecsconn
   320  
   321  	for _, rs := range s.RootModule().Resources {
   322  		if rs.Type != "aws_ecs_service" {
   323  			continue
   324  		}
   325  
   326  		out, err := conn.DescribeServices(&ecs.DescribeServicesInput{
   327  			Services: []*string{aws.String(rs.Primary.ID)},
   328  			Cluster:  aws.String(rs.Primary.Attributes["cluster"]),
   329  		})
   330  
   331  		if err == nil {
   332  			if len(out.Services) > 0 {
   333  				var activeServices []*ecs.Service
   334  				for _, svc := range out.Services {
   335  					if *svc.Status != "INACTIVE" {
   336  						activeServices = append(activeServices, svc)
   337  					}
   338  				}
   339  				if len(activeServices) == 0 {
   340  					return nil
   341  				}
   342  
   343  				return fmt.Errorf("ECS service still exists:\n%#v", activeServices)
   344  			}
   345  			return nil
   346  		}
   347  
   348  		return err
   349  	}
   350  
   351  	return nil
   352  }
   353  
   354  func testAccCheckAWSEcsServiceExists(name string) resource.TestCheckFunc {
   355  	return func(s *terraform.State) error {
   356  		_, ok := s.RootModule().Resources[name]
   357  		if !ok {
   358  			return fmt.Errorf("Not found: %s", name)
   359  		}
   360  
   361  		return nil
   362  	}
   363  }
   364  
   365  var testAccAWSEcsService = `
   366  resource "aws_ecs_cluster" "default" {
   367  	name = "terraformecstest1"
   368  }
   369  
   370  resource "aws_ecs_task_definition" "mongo" {
   371    family = "mongodb"
   372    container_definitions = <<DEFINITION
   373  [
   374    {
   375      "cpu": 128,
   376      "essential": true,
   377      "image": "mongo:latest",
   378      "memory": 128,
   379      "name": "mongodb"
   380    }
   381  ]
   382  DEFINITION
   383  }
   384  
   385  resource "aws_ecs_service" "mongo" {
   386    name = "mongodb"
   387    cluster = "${aws_ecs_cluster.default.id}"
   388    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   389    desired_count = 1
   390  }
   391  `
   392  
   393  var testAccAWSEcsServiceModified = `
   394  resource "aws_ecs_cluster" "default" {
   395  	name = "terraformecstest1"
   396  }
   397  
   398  resource "aws_ecs_task_definition" "mongo" {
   399    family = "mongodb"
   400    container_definitions = <<DEFINITION
   401  [
   402    {
   403      "cpu": 128,
   404      "essential": true,
   405      "image": "mongo:latest",
   406      "memory": 128,
   407      "name": "mongodb"
   408    }
   409  ]
   410  DEFINITION
   411  }
   412  
   413  resource "aws_ecs_service" "mongo" {
   414    name = "mongodb"
   415    cluster = "${aws_ecs_cluster.default.id}"
   416    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   417    desired_count = 2
   418  }
   419  `
   420  
   421  var testAccAWSEcsServiceWithPlacementStrategy = `
   422  resource "aws_ecs_cluster" "default" {
   423  	name = "terraformecstest1"
   424  }
   425  
   426  resource "aws_ecs_task_definition" "mongo" {
   427    family = "mongodb"
   428    container_definitions = <<DEFINITION
   429  [
   430    {
   431      "cpu": 128,
   432      "essential": true,
   433      "image": "mongo:latest",
   434      "memory": 128,
   435      "name": "mongodb"
   436    }
   437  ]
   438  DEFINITION
   439  }
   440  
   441  resource "aws_ecs_service" "mongo" {
   442    name = "mongodb"
   443    cluster = "${aws_ecs_cluster.default.id}"
   444    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   445    desired_count = 1
   446    placement_strategy {
   447  	type = "binpack"
   448  	field = "memory"
   449    }
   450  }
   451  `
   452  
   453  var testAccAWSEcsServiceWithPlacementConstraint = `
   454  resource "aws_ecs_cluster" "default" {
   455  	name = "terraformecstest21"
   456  }
   457  
   458  resource "aws_ecs_task_definition" "mongo" {
   459    family = "mongodb"
   460    container_definitions = <<DEFINITION
   461  [
   462    {
   463      "cpu": 128,
   464      "essential": true,
   465      "image": "mongo:latest",
   466      "memory": 128,
   467      "name": "mongodb"
   468    }
   469  ]
   470  DEFINITION
   471  }
   472  
   473  resource "aws_ecs_service" "mongo" {
   474    name = "mongodb"
   475    cluster = "${aws_ecs_cluster.default.id}"
   476    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   477    desired_count = 1
   478    placement_constraints {
   479  	type = "memberOf"
   480  	expression = "attribute:ecs.availability-zone in [us-west-2a, us-west-2b]"
   481    }
   482  }
   483  `
   484  
   485  var testAccAWSEcsServiceWithPlacementConstraintEmptyExpression = `
   486  resource "aws_ecs_cluster" "default" {
   487  	name = "terraformecstest212"
   488  }
   489  resource "aws_ecs_task_definition" "mongo" {
   490    family = "mongodb"
   491    container_definitions = <<DEFINITION
   492  [
   493    {
   494      "cpu": 128,
   495      "essential": true,
   496      "image": "mongo:latest",
   497      "memory": 128,
   498      "name": "mongodb"
   499    }
   500  ]
   501  DEFINITION
   502  }
   503  resource "aws_ecs_service" "mongo" {
   504    name = "mongodb"
   505    cluster = "${aws_ecs_cluster.default.id}"
   506    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   507    desired_count = 1
   508    placement_constraints {
   509  	  type = "distinctInstance"
   510    }
   511  }
   512  `
   513  
   514  var testAccAWSEcsService_withIamRole = `
   515  resource "aws_ecs_cluster" "main" {
   516  	name = "terraformecstest11"
   517  }
   518  
   519  resource "aws_ecs_task_definition" "ghost" {
   520    family = "ghost_service"
   521    container_definitions = <<DEFINITION
   522  [
   523    {
   524      "cpu": 128,
   525      "essential": true,
   526      "image": "ghost:latest",
   527      "memory": 128,
   528      "name": "ghost",
   529      "portMappings": [
   530        {
   531          "containerPort": 2368,
   532          "hostPort": 8080
   533        }
   534      ]
   535    }
   536  ]
   537  DEFINITION
   538  }
   539  
   540  resource "aws_iam_role" "ecs_service" {
   541      name = "EcsService"
   542      assume_role_policy = <<EOF
   543  {
   544      "Version": "2012-10-17",
   545      "Statement": [
   546          {
   547              "Action": "sts:AssumeRole",
   548              "Principal": {"AWS": "*"},
   549              "Effect": "Allow",
   550              "Sid": ""
   551          }
   552      ]
   553  }
   554  EOF
   555  }
   556  
   557  resource "aws_iam_role_policy" "ecs_service" {
   558      name = "EcsService"
   559      role = "${aws_iam_role.ecs_service.name}"
   560      policy = <<EOF
   561  {
   562    "Version": "2012-10-17",
   563    "Statement": [
   564      {
   565        "Effect": "Allow",
   566        "Action": [
   567          "elasticloadbalancing:*",
   568          "ec2:*",
   569          "ecs:*"
   570        ],
   571        "Resource": [
   572          "*"
   573        ]
   574      }
   575    ]
   576  }
   577  EOF
   578  }
   579  
   580  resource "aws_elb" "main" {
   581    availability_zones = ["us-west-2a"]
   582  
   583    listener {
   584      instance_port = 8080
   585      instance_protocol = "http"
   586      lb_port = 80
   587      lb_protocol = "http"
   588    }
   589  }
   590  
   591  resource "aws_ecs_service" "ghost" {
   592    name = "ghost"
   593    cluster = "${aws_ecs_cluster.main.id}"
   594    task_definition = "${aws_ecs_task_definition.ghost.arn}"
   595    desired_count = 1
   596    iam_role = "${aws_iam_role.ecs_service.name}"
   597  
   598    load_balancer {
   599      elb_name = "${aws_elb.main.id}"
   600      container_name = "ghost"
   601      container_port = "2368"
   602    }
   603  
   604    depends_on = ["aws_iam_role_policy.ecs_service"]
   605  }
   606  `
   607  
   608  var testAccAWSEcsServiceWithDeploymentValues = `
   609  resource "aws_ecs_cluster" "default" {
   610  	name = "terraformecstest1"
   611  }
   612  
   613  resource "aws_ecs_task_definition" "mongo" {
   614    family = "mongodb"
   615    container_definitions = <<DEFINITION
   616  [
   617    {
   618      "cpu": 128,
   619      "essential": true,
   620      "image": "mongo:latest",
   621      "memory": 128,
   622      "name": "mongodb"
   623    }
   624  ]
   625  DEFINITION
   626  }
   627  
   628  resource "aws_ecs_service" "mongo" {
   629    name = "mongodb"
   630    cluster = "${aws_ecs_cluster.default.id}"
   631    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   632    desired_count = 1
   633  }
   634  `
   635  
   636  var tpl_testAccAWSEcsService_withLbChanges = `
   637  resource "aws_ecs_cluster" "main" {
   638  	name = "terraformecstest12"
   639  }
   640  
   641  resource "aws_ecs_task_definition" "with_lb_changes" {
   642    family = "ghost_lbd"
   643    container_definitions = <<DEFINITION
   644  [
   645    {
   646      "cpu": 128,
   647      "essential": true,
   648      "image": "%s",
   649      "memory": 128,
   650      "name": "%s",
   651      "portMappings": [
   652        {
   653          "containerPort": %d,
   654          "hostPort": %d
   655        }
   656      ]
   657    }
   658  ]
   659  DEFINITION
   660  }
   661  
   662  resource "aws_iam_role" "ecs_service" {
   663      name = "EcsServiceLbd"
   664      assume_role_policy = <<EOF
   665  {
   666      "Version": "2012-10-17",
   667      "Statement": [
   668          {
   669              "Action": "sts:AssumeRole",
   670              "Principal": {"AWS": "*"},
   671              "Effect": "Allow",
   672              "Sid": ""
   673          }
   674      ]
   675  }
   676  EOF
   677  }
   678  
   679  resource "aws_iam_role_policy" "ecs_service" {
   680      name = "EcsServiceLbd"
   681      role = "${aws_iam_role.ecs_service.name}"
   682      policy = <<EOF
   683  {
   684    "Version": "2012-10-17",
   685    "Statement": [
   686      {
   687        "Effect": "Allow",
   688        "Action": [
   689          "elasticloadbalancing:*",
   690          "ec2:*",
   691          "ecs:*"
   692        ],
   693        "Resource": [
   694          "*"
   695        ]
   696      }
   697    ]
   698  }
   699  EOF
   700  }
   701  
   702  resource "aws_elb" "main" {
   703    availability_zones = ["us-west-2a"]
   704  
   705    listener {
   706      instance_port = %d
   707      instance_protocol = "http"
   708      lb_port = 80
   709      lb_protocol = "http"
   710    }
   711  }
   712  
   713  resource "aws_ecs_service" "with_lb_changes" {
   714    name = "ghost"
   715    cluster = "${aws_ecs_cluster.main.id}"
   716    task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
   717    desired_count = 1
   718    iam_role = "${aws_iam_role.ecs_service.name}"
   719  
   720    load_balancer {
   721      elb_name = "${aws_elb.main.id}"
   722      container_name = "%s"
   723      container_port = "%d"
   724    }
   725  
   726    depends_on = ["aws_iam_role_policy.ecs_service"]
   727  }
   728  `
   729  
   730  var testAccAWSEcsService_withLbChanges = fmt.Sprintf(
   731  	tpl_testAccAWSEcsService_withLbChanges,
   732  	"ghost:latest", "ghost", 2368, 8080, 8080, "ghost", 2368)
   733  var testAccAWSEcsService_withLbChanges_modified = fmt.Sprintf(
   734  	tpl_testAccAWSEcsService_withLbChanges,
   735  	"nginx:latest", "nginx", 80, 8080, 8080, "nginx", 80)
   736  
   737  var testAccAWSEcsServiceWithFamilyAndRevision = `
   738  resource "aws_ecs_cluster" "default" {
   739  	name = "terraformecstest2"
   740  }
   741  
   742  resource "aws_ecs_task_definition" "jenkins" {
   743    family = "jenkins"
   744    container_definitions = <<DEFINITION
   745  [
   746    {
   747      "cpu": 128,
   748      "essential": true,
   749      "image": "jenkins:latest",
   750      "memory": 128,
   751      "name": "jenkins"
   752    }
   753  ]
   754  DEFINITION
   755  }
   756  
   757  resource "aws_ecs_service" "jenkins" {
   758    name = "jenkins"
   759    cluster = "${aws_ecs_cluster.default.id}"
   760    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   761    desired_count = 1
   762  }
   763  `
   764  
   765  var testAccAWSEcsServiceWithFamilyAndRevisionModified = `
   766  resource "aws_ecs_cluster" "default" {
   767  	name = "terraformecstest2"
   768  }
   769  
   770  resource "aws_ecs_task_definition" "jenkins" {
   771    family = "jenkins"
   772    container_definitions = <<DEFINITION
   773  [
   774    {
   775      "cpu": 128,
   776      "essential": true,
   777      "image": "jenkins:latest",
   778      "memory": 128,
   779      "name": "jenkins"
   780    }
   781  ]
   782  DEFINITION
   783  }
   784  
   785  resource "aws_ecs_service" "jenkins" {
   786    name = "jenkins"
   787    cluster = "${aws_ecs_cluster.default.id}"
   788    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   789    desired_count = 1
   790  }
   791  `
   792  
   793  var testAccAWSEcsServiceWithRenamedCluster = `
   794  resource "aws_ecs_cluster" "default" {
   795  	name = "terraformecstest3"
   796  }
   797  resource "aws_ecs_task_definition" "ghost" {
   798    family = "ghost"
   799    container_definitions = <<DEFINITION
   800  [
   801    {
   802      "cpu": 128,
   803      "essential": true,
   804      "image": "ghost:latest",
   805      "memory": 128,
   806      "name": "ghost"
   807    }
   808  ]
   809  DEFINITION
   810  }
   811  resource "aws_ecs_service" "ghost" {
   812    name = "ghost"
   813    cluster = "${aws_ecs_cluster.default.id}"
   814    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   815    desired_count = 1
   816  }
   817  `
   818  
   819  var testAccAWSEcsServiceWithRenamedClusterModified = `
   820  resource "aws_ecs_cluster" "default" {
   821  	name = "terraformecstest3modified"
   822  }
   823  resource "aws_ecs_task_definition" "ghost" {
   824    family = "ghost"
   825    container_definitions = <<DEFINITION
   826  [
   827    {
   828      "cpu": 128,
   829      "essential": true,
   830      "image": "ghost:latest",
   831      "memory": 128,
   832      "name": "ghost"
   833    }
   834  ]
   835  DEFINITION
   836  }
   837  resource "aws_ecs_service" "ghost" {
   838    name = "ghost"
   839    cluster = "${aws_ecs_cluster.default.id}"
   840    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   841    desired_count = 1
   842  }
   843  `
   844  
   845  var testAccAWSEcsServiceWithEcsClusterName = `
   846  resource "aws_ecs_cluster" "default" {
   847  	name = "terraformecstestcluster"
   848  }
   849  
   850  resource "aws_ecs_task_definition" "jenkins" {
   851    family = "jenkins"
   852    container_definitions = <<DEFINITION
   853  [
   854    {
   855      "cpu": 128,
   856      "essential": true,
   857      "image": "jenkins:latest",
   858      "memory": 128,
   859      "name": "jenkins"
   860    }
   861  ]
   862  DEFINITION
   863  }
   864  
   865  resource "aws_ecs_service" "jenkins" {
   866    name = "jenkins"
   867    cluster = "${aws_ecs_cluster.default.name}"
   868    task_definition = "${aws_ecs_task_definition.jenkins.arn}"
   869    desired_count = 1
   870  }
   871  `
   872  
   873  var testAccAWSEcsServiceWithAlb = `
   874  data "aws_availability_zones" "available" {}
   875  
   876  resource "aws_vpc" "main" {
   877    cidr_block = "10.10.0.0/16"
   878  }
   879  
   880  resource "aws_subnet" "main" {
   881    count = 2
   882    cidr_block = "${cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)}"
   883    availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
   884    vpc_id = "${aws_vpc.main.id}"
   885  }
   886  
   887  resource "aws_ecs_cluster" "main" {
   888    name = "terraform_acc_test_ecs_15"
   889  }
   890  
   891  resource "aws_ecs_task_definition" "with_lb_changes" {
   892    family = "tf_acc_test_ghost_lbd"
   893    container_definitions = <<DEFINITION
   894  [
   895    {
   896      "cpu": 256,
   897      "essential": true,
   898      "image": "ghost:latest",
   899      "memory": 512,
   900      "name": "ghost",
   901      "portMappings": [
   902        {
   903          "containerPort": 2368,
   904          "hostPort": 8080
   905        }
   906      ]
   907    }
   908  ]
   909  DEFINITION
   910  }
   911  
   912  resource "aws_iam_role" "ecs_service" {
   913      name = "tf_acc_test_15_role"
   914      assume_role_policy = <<EOF
   915  {
   916    "Version": "2008-10-17",
   917    "Statement": [
   918      {
   919        "Sid": "",
   920        "Effect": "Allow",
   921        "Principal": {
   922          "Service": "ecs.amazonaws.com"
   923        },
   924        "Action": "sts:AssumeRole"
   925      }
   926    ]
   927  }
   928  EOF
   929  }
   930  
   931  resource "aws_iam_role_policy" "ecs_service" {
   932      name = "tf_acc_test_15_policy"
   933      role = "${aws_iam_role.ecs_service.name}"
   934      policy = <<EOF
   935  {
   936    "Version": "2012-10-17",
   937    "Statement": [
   938      {
   939        "Effect": "Allow",
   940        "Action": [
   941          "ec2:Describe*",
   942          "elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
   943          "elasticloadbalancing:DeregisterTargets",
   944          "elasticloadbalancing:Describe*",
   945          "elasticloadbalancing:RegisterInstancesWithLoadBalancer",
   946          "elasticloadbalancing:RegisterTargets"
   947        ],
   948        "Resource": "*"
   949      }
   950    ]
   951  }
   952  EOF
   953  }
   954  
   955  resource "aws_alb_target_group" "test" {
   956    name = "tf-acc-test-ecs-ghost"
   957    port = 80
   958    protocol = "HTTP"
   959    vpc_id = "${aws_vpc.main.id}"
   960  }
   961  
   962  resource "aws_alb" "main" {
   963    name            = "tf-acc-test-test-alb-ecs"
   964    internal        = true
   965    subnets         = ["${aws_subnet.main.*.id}"]
   966  }
   967  
   968  resource "aws_alb_listener" "front_end" {
   969    load_balancer_arn = "${aws_alb.main.id}"
   970    port = "80"
   971    protocol = "HTTP"
   972  
   973    default_action {
   974      target_group_arn = "${aws_alb_target_group.test.id}"
   975      type = "forward"
   976    }
   977  }
   978  
   979  resource "aws_ecs_service" "with_alb" {
   980    name = "tf-acc-test-ecs-ghost"
   981    cluster = "${aws_ecs_cluster.main.id}"
   982    task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
   983    desired_count = 1
   984    iam_role = "${aws_iam_role.ecs_service.name}"
   985  
   986    load_balancer {
   987      target_group_arn = "${aws_alb_target_group.test.id}"
   988      container_name = "ghost"
   989      container_port = "2368"
   990    }
   991  
   992    depends_on = [
   993      "aws_iam_role_policy.ecs_service",
   994      "aws_alb_listener.front_end"
   995    ]
   996  }
   997  `