github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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": map[string]interface{}{
    17  			"family":   "",
    18  			"revision": "",
    19  			"isValid":  false,
    20  		},
    21  		"invalidWithColon:": map[string]interface{}{
    22  			"family":   "",
    23  			"revision": "",
    24  			"isValid":  false,
    25  		},
    26  		"1234": map[string]interface{}{
    27  			"family":   "",
    28  			"revision": "",
    29  			"isValid":  false,
    30  		},
    31  		"invalid:aaa": map[string]interface{}{
    32  			"family":   "",
    33  			"revision": "",
    34  			"isValid":  false,
    35  		},
    36  		"invalid=family:1": map[string]interface{}{
    37  			"family":   "",
    38  			"revision": "",
    39  			"isValid":  false,
    40  		},
    41  		"invalid:name:1": map[string]interface{}{
    42  			"family":   "",
    43  			"revision": "",
    44  			"isValid":  false,
    45  		},
    46  		"valid:1": map[string]interface{}{
    47  			"family":   "valid",
    48  			"revision": "1",
    49  			"isValid":  true,
    50  		},
    51  		"abc12-def:54": map[string]interface{}{
    52  			"family":   "abc12-def",
    53  			"revision": "54",
    54  			"isValid":  true,
    55  		},
    56  		"lorem_ip-sum:123": map[string]interface{}{
    57  			"family":   "lorem_ip-sum",
    58  			"revision": "123",
    59  			"isValid":  true,
    60  		},
    61  		"lorem-ipsum:1": map[string]interface{}{
    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  			resource.TestStep{
    93  				Config: testAccAWSEcsService,
    94  				Check: resource.ComposeTestCheckFunc(
    95  					testAccCheckAWSEcsServiceExists("aws_ecs_service.mongo"),
    96  				),
    97  			},
    98  
    99  			resource.TestStep{
   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  			resource.TestStep{
   116  				Config: testAccAWSEcsServiceWithFamilyAndRevision,
   117  				Check: resource.ComposeTestCheckFunc(
   118  					testAccCheckAWSEcsServiceExists("aws_ecs_service.jenkins"),
   119  				),
   120  			},
   121  
   122  			resource.TestStep{
   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  			resource.TestStep{
   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  			resource.TestStep{
   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  			resource.TestStep{
   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  			resource.TestStep{
   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  			resource.TestStep{
   209  				Config: testAccAWSEcsService_withLbChanges,
   210  				Check: resource.ComposeTestCheckFunc(
   211  					testAccCheckAWSEcsServiceExists("aws_ecs_service.with_lb_changes"),
   212  				),
   213  			},
   214  			resource.TestStep{
   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  			resource.TestStep{
   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  			resource.TestStep{
   251  				Config: testAccAWSEcsServiceWithAlb,
   252  				Check: resource.ComposeTestCheckFunc(
   253  					testAccCheckAWSEcsServiceExists("aws_ecs_service.with_alb"),
   254  				),
   255  			},
   256  		},
   257  	})
   258  }
   259  
   260  func testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
   261  	conn := testAccProvider.Meta().(*AWSClient).ecsconn
   262  
   263  	for _, rs := range s.RootModule().Resources {
   264  		if rs.Type != "aws_ecs_service" {
   265  			continue
   266  		}
   267  
   268  		out, err := conn.DescribeServices(&ecs.DescribeServicesInput{
   269  			Services: []*string{aws.String(rs.Primary.ID)},
   270  			Cluster:  aws.String(rs.Primary.Attributes["cluster"]),
   271  		})
   272  
   273  		if err == nil {
   274  			if len(out.Services) > 0 {
   275  				var activeServices []*ecs.Service
   276  				for _, svc := range out.Services {
   277  					if *svc.Status != "INACTIVE" {
   278  						activeServices = append(activeServices, svc)
   279  					}
   280  				}
   281  				if len(activeServices) == 0 {
   282  					return nil
   283  				}
   284  
   285  				return fmt.Errorf("ECS service still exists:\n%#v", activeServices)
   286  			}
   287  			return nil
   288  		}
   289  
   290  		return err
   291  	}
   292  
   293  	return nil
   294  }
   295  
   296  func testAccCheckAWSEcsServiceExists(name string) resource.TestCheckFunc {
   297  	return func(s *terraform.State) error {
   298  		_, ok := s.RootModule().Resources[name]
   299  		if !ok {
   300  			return fmt.Errorf("Not found: %s", name)
   301  		}
   302  
   303  		return nil
   304  	}
   305  }
   306  
   307  var testAccAWSEcsService = `
   308  resource "aws_ecs_cluster" "default" {
   309  	name = "terraformecstest1"
   310  }
   311  
   312  resource "aws_ecs_task_definition" "mongo" {
   313    family = "mongodb"
   314    container_definitions = <<DEFINITION
   315  [
   316    {
   317      "cpu": 128,
   318      "essential": true,
   319      "image": "mongo:latest",
   320      "memory": 128,
   321      "name": "mongodb"
   322    }
   323  ]
   324  DEFINITION
   325  }
   326  
   327  resource "aws_ecs_service" "mongo" {
   328    name = "mongodb"
   329    cluster = "${aws_ecs_cluster.default.id}"
   330    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   331    desired_count = 1
   332  }
   333  `
   334  
   335  var testAccAWSEcsServiceModified = `
   336  resource "aws_ecs_cluster" "default" {
   337  	name = "terraformecstest1"
   338  }
   339  
   340  resource "aws_ecs_task_definition" "mongo" {
   341    family = "mongodb"
   342    container_definitions = <<DEFINITION
   343  [
   344    {
   345      "cpu": 128,
   346      "essential": true,
   347      "image": "mongo:latest",
   348      "memory": 128,
   349      "name": "mongodb"
   350    }
   351  ]
   352  DEFINITION
   353  }
   354  
   355  resource "aws_ecs_service" "mongo" {
   356    name = "mongodb"
   357    cluster = "${aws_ecs_cluster.default.id}"
   358    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   359    desired_count = 2
   360  }
   361  `
   362  
   363  var testAccAWSEcsService_withIamRole = `
   364  resource "aws_ecs_cluster" "main" {
   365  	name = "terraformecstest11"
   366  }
   367  
   368  resource "aws_ecs_task_definition" "ghost" {
   369    family = "ghost_service"
   370    container_definitions = <<DEFINITION
   371  [
   372    {
   373      "cpu": 128,
   374      "essential": true,
   375      "image": "ghost:latest",
   376      "memory": 128,
   377      "name": "ghost",
   378      "portMappings": [
   379        {
   380          "containerPort": 2368,
   381          "hostPort": 8080
   382        }
   383      ]
   384    }
   385  ]
   386  DEFINITION
   387  }
   388  
   389  resource "aws_iam_role" "ecs_service" {
   390      name = "EcsService"
   391      assume_role_policy = <<EOF
   392  {
   393      "Version": "2012-10-17",
   394      "Statement": [
   395          {
   396              "Action": "sts:AssumeRole",
   397              "Principal": {"AWS": "*"},
   398              "Effect": "Allow",
   399              "Sid": ""
   400          }
   401      ]
   402  }
   403  EOF
   404  }
   405  
   406  resource "aws_iam_role_policy" "ecs_service" {
   407      name = "EcsService"
   408      role = "${aws_iam_role.ecs_service.name}"
   409      policy = <<EOF
   410  {
   411    "Version": "2012-10-17",
   412    "Statement": [
   413      {
   414        "Effect": "Allow",
   415        "Action": [
   416          "elasticloadbalancing:*",
   417          "ec2:*",
   418          "ecs:*"
   419        ],
   420        "Resource": [
   421          "*"
   422        ]
   423      }
   424    ]
   425  }
   426  EOF
   427  }
   428  
   429  resource "aws_elb" "main" {
   430    availability_zones = ["us-west-2a"]
   431  
   432    listener {
   433      instance_port = 8080
   434      instance_protocol = "http"
   435      lb_port = 80
   436      lb_protocol = "http"
   437    }
   438  }
   439  
   440  resource "aws_ecs_service" "ghost" {
   441    name = "ghost"
   442    cluster = "${aws_ecs_cluster.main.id}"
   443    task_definition = "${aws_ecs_task_definition.ghost.arn}"
   444    desired_count = 1
   445    iam_role = "${aws_iam_role.ecs_service.name}"
   446  
   447    load_balancer {
   448      elb_name = "${aws_elb.main.id}"
   449      container_name = "ghost"
   450      container_port = "2368"
   451    }
   452  
   453    depends_on = ["aws_iam_role_policy.ecs_service"]
   454  }
   455  `
   456  
   457  var testAccAWSEcsServiceWithDeploymentValues = `
   458  resource "aws_ecs_cluster" "default" {
   459  	name = "terraformecstest1"
   460  }
   461  
   462  resource "aws_ecs_task_definition" "mongo" {
   463    family = "mongodb"
   464    container_definitions = <<DEFINITION
   465  [
   466    {
   467      "cpu": 128,
   468      "essential": true,
   469      "image": "mongo:latest",
   470      "memory": 128,
   471      "name": "mongodb"
   472    }
   473  ]
   474  DEFINITION
   475  }
   476  
   477  resource "aws_ecs_service" "mongo" {
   478    name = "mongodb"
   479    cluster = "${aws_ecs_cluster.default.id}"
   480    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   481    desired_count = 1
   482  }
   483  `
   484  
   485  var tpl_testAccAWSEcsService_withLbChanges = `
   486  resource "aws_ecs_cluster" "main" {
   487  	name = "terraformecstest12"
   488  }
   489  
   490  resource "aws_ecs_task_definition" "with_lb_changes" {
   491    family = "ghost_lbd"
   492    container_definitions = <<DEFINITION
   493  [
   494    {
   495      "cpu": 128,
   496      "essential": true,
   497      "image": "%s",
   498      "memory": 128,
   499      "name": "%s",
   500      "portMappings": [
   501        {
   502          "containerPort": %d,
   503          "hostPort": %d
   504        }
   505      ]
   506    }
   507  ]
   508  DEFINITION
   509  }
   510  
   511  resource "aws_iam_role" "ecs_service" {
   512      name = "EcsServiceLbd"
   513      assume_role_policy = <<EOF
   514  {
   515      "Version": "2012-10-17",
   516      "Statement": [
   517          {
   518              "Action": "sts:AssumeRole",
   519              "Principal": {"AWS": "*"},
   520              "Effect": "Allow",
   521              "Sid": ""
   522          }
   523      ]
   524  }
   525  EOF
   526  }
   527  
   528  resource "aws_iam_role_policy" "ecs_service" {
   529      name = "EcsServiceLbd"
   530      role = "${aws_iam_role.ecs_service.name}"
   531      policy = <<EOF
   532  {
   533    "Version": "2012-10-17",
   534    "Statement": [
   535      {
   536        "Effect": "Allow",
   537        "Action": [
   538          "elasticloadbalancing:*",
   539          "ec2:*",
   540          "ecs:*"
   541        ],
   542        "Resource": [
   543          "*"
   544        ]
   545      }
   546    ]
   547  }
   548  EOF
   549  }
   550  
   551  resource "aws_elb" "main" {
   552    availability_zones = ["us-west-2a"]
   553  
   554    listener {
   555      instance_port = %d
   556      instance_protocol = "http"
   557      lb_port = 80
   558      lb_protocol = "http"
   559    }
   560  }
   561  
   562  resource "aws_ecs_service" "with_lb_changes" {
   563    name = "ghost"
   564    cluster = "${aws_ecs_cluster.main.id}"
   565    task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
   566    desired_count = 1
   567    iam_role = "${aws_iam_role.ecs_service.name}"
   568  
   569    load_balancer {
   570      elb_name = "${aws_elb.main.id}"
   571      container_name = "%s"
   572      container_port = "%d"
   573    }
   574  
   575    depends_on = ["aws_iam_role_policy.ecs_service"]
   576  }
   577  `
   578  
   579  var testAccAWSEcsService_withLbChanges = fmt.Sprintf(
   580  	tpl_testAccAWSEcsService_withLbChanges,
   581  	"ghost:latest", "ghost", 2368, 8080, 8080, "ghost", 2368)
   582  var testAccAWSEcsService_withLbChanges_modified = fmt.Sprintf(
   583  	tpl_testAccAWSEcsService_withLbChanges,
   584  	"nginx:latest", "nginx", 80, 8080, 8080, "nginx", 80)
   585  
   586  var testAccAWSEcsServiceWithFamilyAndRevision = `
   587  resource "aws_ecs_cluster" "default" {
   588  	name = "terraformecstest2"
   589  }
   590  
   591  resource "aws_ecs_task_definition" "jenkins" {
   592    family = "jenkins"
   593    container_definitions = <<DEFINITION
   594  [
   595    {
   596      "cpu": 128,
   597      "essential": true,
   598      "image": "jenkins:latest",
   599      "memory": 128,
   600      "name": "jenkins"
   601    }
   602  ]
   603  DEFINITION
   604  }
   605  
   606  resource "aws_ecs_service" "jenkins" {
   607    name = "jenkins"
   608    cluster = "${aws_ecs_cluster.default.id}"
   609    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   610    desired_count = 1
   611  }
   612  `
   613  
   614  var testAccAWSEcsServiceWithFamilyAndRevisionModified = `
   615  resource "aws_ecs_cluster" "default" {
   616  	name = "terraformecstest2"
   617  }
   618  
   619  resource "aws_ecs_task_definition" "jenkins" {
   620    family = "jenkins"
   621    container_definitions = <<DEFINITION
   622  [
   623    {
   624      "cpu": 128,
   625      "essential": true,
   626      "image": "jenkins:latest",
   627      "memory": 128,
   628      "name": "jenkins"
   629    }
   630  ]
   631  DEFINITION
   632  }
   633  
   634  resource "aws_ecs_service" "jenkins" {
   635    name = "jenkins"
   636    cluster = "${aws_ecs_cluster.default.id}"
   637    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   638    desired_count = 1
   639  }
   640  `
   641  
   642  var testAccAWSEcsServiceWithRenamedCluster = `
   643  resource "aws_ecs_cluster" "default" {
   644  	name = "terraformecstest3"
   645  }
   646  resource "aws_ecs_task_definition" "ghost" {
   647    family = "ghost"
   648    container_definitions = <<DEFINITION
   649  [
   650    {
   651      "cpu": 128,
   652      "essential": true,
   653      "image": "ghost:latest",
   654      "memory": 128,
   655      "name": "ghost"
   656    }
   657  ]
   658  DEFINITION
   659  }
   660  resource "aws_ecs_service" "ghost" {
   661    name = "ghost"
   662    cluster = "${aws_ecs_cluster.default.id}"
   663    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   664    desired_count = 1
   665  }
   666  `
   667  
   668  var testAccAWSEcsServiceWithRenamedClusterModified = `
   669  resource "aws_ecs_cluster" "default" {
   670  	name = "terraformecstest3modified"
   671  }
   672  resource "aws_ecs_task_definition" "ghost" {
   673    family = "ghost"
   674    container_definitions = <<DEFINITION
   675  [
   676    {
   677      "cpu": 128,
   678      "essential": true,
   679      "image": "ghost:latest",
   680      "memory": 128,
   681      "name": "ghost"
   682    }
   683  ]
   684  DEFINITION
   685  }
   686  resource "aws_ecs_service" "ghost" {
   687    name = "ghost"
   688    cluster = "${aws_ecs_cluster.default.id}"
   689    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   690    desired_count = 1
   691  }
   692  `
   693  
   694  var testAccAWSEcsServiceWithEcsClusterName = `
   695  resource "aws_ecs_cluster" "default" {
   696  	name = "terraformecstestcluster"
   697  }
   698  
   699  resource "aws_ecs_task_definition" "jenkins" {
   700    family = "jenkins"
   701    container_definitions = <<DEFINITION
   702  [
   703    {
   704      "cpu": 128,
   705      "essential": true,
   706      "image": "jenkins:latest",
   707      "memory": 128,
   708      "name": "jenkins"
   709    }
   710  ]
   711  DEFINITION
   712  }
   713  
   714  resource "aws_ecs_service" "jenkins" {
   715    name = "jenkins"
   716    cluster = "${aws_ecs_cluster.default.name}"
   717    task_definition = "${aws_ecs_task_definition.jenkins.arn}"
   718    desired_count = 1
   719  }
   720  `
   721  
   722  var testAccAWSEcsServiceWithAlb = `
   723  data "aws_availability_zones" "available" {}
   724  
   725  resource "aws_vpc" "main" {
   726    cidr_block = "10.10.0.0/16"
   727  }
   728  
   729  resource "aws_subnet" "main" {
   730    count = 2
   731    cidr_block = "${cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)}"
   732    availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
   733    vpc_id = "${aws_vpc.main.id}"
   734  }
   735  
   736  resource "aws_ecs_cluster" "main" {
   737    name = "terraform_acc_test_ecs_15"
   738  }
   739  
   740  resource "aws_ecs_task_definition" "with_lb_changes" {
   741    family = "tf_acc_test_ghost_lbd"
   742    container_definitions = <<DEFINITION
   743  [
   744    {
   745      "cpu": 256,
   746      "essential": true,
   747      "image": "ghost:latest",
   748      "memory": 512,
   749      "name": "ghost",
   750      "portMappings": [
   751        {
   752          "containerPort": 2368,
   753          "hostPort": 8080
   754        }
   755      ]
   756    }
   757  ]
   758  DEFINITION
   759  }
   760  
   761  resource "aws_iam_role" "ecs_service" {
   762      name = "tf_acc_test_15_role"
   763      assume_role_policy = <<EOF
   764  {
   765    "Version": "2008-10-17",
   766    "Statement": [
   767      {
   768        "Sid": "",
   769        "Effect": "Allow",
   770        "Principal": {
   771          "Service": "ecs.amazonaws.com"
   772        },
   773        "Action": "sts:AssumeRole"
   774      }
   775    ]
   776  }
   777  EOF
   778  }
   779  
   780  resource "aws_iam_role_policy" "ecs_service" {
   781      name = "tf_acc_test_15_policy"
   782      role = "${aws_iam_role.ecs_service.name}"
   783      policy = <<EOF
   784  {
   785    "Version": "2012-10-17",
   786    "Statement": [
   787      {
   788        "Effect": "Allow",
   789        "Action": [
   790          "ec2:Describe*",
   791          "elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
   792          "elasticloadbalancing:DeregisterTargets",
   793          "elasticloadbalancing:Describe*",
   794          "elasticloadbalancing:RegisterInstancesWithLoadBalancer",
   795          "elasticloadbalancing:RegisterTargets"
   796        ],
   797        "Resource": "*"
   798      }
   799    ]
   800  }
   801  EOF
   802  }
   803  
   804  resource "aws_alb_target_group" "test" {
   805    name = "tf-acc-test-ecs-ghost"
   806    port = 80
   807    protocol = "HTTP"
   808    vpc_id = "${aws_vpc.main.id}"
   809  }
   810  
   811  resource "aws_alb" "main" {
   812    name            = "tf-acc-test-test-alb-ecs"
   813    subnets         = ["${aws_subnet.main.*.id}"]
   814  }
   815  
   816  resource "aws_alb_listener" "front_end" {
   817    load_balancer_arn = "${aws_alb.main.id}"
   818    port = "80"
   819    protocol = "HTTP"
   820  
   821    default_action {
   822      target_group_arn = "${aws_alb_target_group.test.id}"
   823      type = "forward"
   824    }
   825  }
   826  
   827  resource "aws_ecs_service" "with_alb" {
   828    name = "tf-acc-test-ecs-ghost"
   829    cluster = "${aws_ecs_cluster.main.id}"
   830    task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
   831    desired_count = 1
   832    iam_role = "${aws_iam_role.ecs_service.name}"
   833  
   834    load_balancer {
   835      target_group_arn = "${aws_alb_target_group.test.id}"
   836      container_name = "ghost"
   837      container_port = "2368"
   838    }
   839  
   840    depends_on = [
   841      "aws_iam_role_policy.ecs_service",
   842      "aws_alb_listener.front_end"
   843    ]
   844  }
   845  `