github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 testAccCheckAWSEcsServiceDestroy(s *terraform.State) error {
   302  	conn := testAccProvider.Meta().(*AWSClient).ecsconn
   303  
   304  	for _, rs := range s.RootModule().Resources {
   305  		if rs.Type != "aws_ecs_service" {
   306  			continue
   307  		}
   308  
   309  		out, err := conn.DescribeServices(&ecs.DescribeServicesInput{
   310  			Services: []*string{aws.String(rs.Primary.ID)},
   311  			Cluster:  aws.String(rs.Primary.Attributes["cluster"]),
   312  		})
   313  
   314  		if err == nil {
   315  			if len(out.Services) > 0 {
   316  				var activeServices []*ecs.Service
   317  				for _, svc := range out.Services {
   318  					if *svc.Status != "INACTIVE" {
   319  						activeServices = append(activeServices, svc)
   320  					}
   321  				}
   322  				if len(activeServices) == 0 {
   323  					return nil
   324  				}
   325  
   326  				return fmt.Errorf("ECS service still exists:\n%#v", activeServices)
   327  			}
   328  			return nil
   329  		}
   330  
   331  		return err
   332  	}
   333  
   334  	return nil
   335  }
   336  
   337  func testAccCheckAWSEcsServiceExists(name string) resource.TestCheckFunc {
   338  	return func(s *terraform.State) error {
   339  		_, ok := s.RootModule().Resources[name]
   340  		if !ok {
   341  			return fmt.Errorf("Not found: %s", name)
   342  		}
   343  
   344  		return nil
   345  	}
   346  }
   347  
   348  var testAccAWSEcsService = `
   349  resource "aws_ecs_cluster" "default" {
   350  	name = "terraformecstest1"
   351  }
   352  
   353  resource "aws_ecs_task_definition" "mongo" {
   354    family = "mongodb"
   355    container_definitions = <<DEFINITION
   356  [
   357    {
   358      "cpu": 128,
   359      "essential": true,
   360      "image": "mongo:latest",
   361      "memory": 128,
   362      "name": "mongodb"
   363    }
   364  ]
   365  DEFINITION
   366  }
   367  
   368  resource "aws_ecs_service" "mongo" {
   369    name = "mongodb"
   370    cluster = "${aws_ecs_cluster.default.id}"
   371    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   372    desired_count = 1
   373  }
   374  `
   375  
   376  var testAccAWSEcsServiceModified = `
   377  resource "aws_ecs_cluster" "default" {
   378  	name = "terraformecstest1"
   379  }
   380  
   381  resource "aws_ecs_task_definition" "mongo" {
   382    family = "mongodb"
   383    container_definitions = <<DEFINITION
   384  [
   385    {
   386      "cpu": 128,
   387      "essential": true,
   388      "image": "mongo:latest",
   389      "memory": 128,
   390      "name": "mongodb"
   391    }
   392  ]
   393  DEFINITION
   394  }
   395  
   396  resource "aws_ecs_service" "mongo" {
   397    name = "mongodb"
   398    cluster = "${aws_ecs_cluster.default.id}"
   399    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   400    desired_count = 2
   401  }
   402  `
   403  
   404  var testAccAWSEcsServiceWithPlacementStrategy = `
   405  resource "aws_ecs_cluster" "default" {
   406  	name = "terraformecstest1"
   407  }
   408  
   409  resource "aws_ecs_task_definition" "mongo" {
   410    family = "mongodb"
   411    container_definitions = <<DEFINITION
   412  [
   413    {
   414      "cpu": 128,
   415      "essential": true,
   416      "image": "mongo:latest",
   417      "memory": 128,
   418      "name": "mongodb"
   419    }
   420  ]
   421  DEFINITION
   422  }
   423  
   424  resource "aws_ecs_service" "mongo" {
   425    name = "mongodb"
   426    cluster = "${aws_ecs_cluster.default.id}"
   427    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   428    desired_count = 1
   429    placement_strategy {
   430  	type = "binpack"
   431  	field = "MEMORY"
   432    }
   433  }
   434  `
   435  
   436  var testAccAWSEcsServiceWithPlacementConstraint = `
   437  resource "aws_ecs_cluster" "default" {
   438  	name = "terraformecstest21"
   439  }
   440  
   441  resource "aws_ecs_task_definition" "mongo" {
   442    family = "mongodb"
   443    container_definitions = <<DEFINITION
   444  [
   445    {
   446      "cpu": 128,
   447      "essential": true,
   448      "image": "mongo:latest",
   449      "memory": 128,
   450      "name": "mongodb"
   451    }
   452  ]
   453  DEFINITION
   454  }
   455  
   456  resource "aws_ecs_service" "mongo" {
   457    name = "mongodb"
   458    cluster = "${aws_ecs_cluster.default.id}"
   459    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   460    desired_count = 1
   461    placement_constraints {
   462  	type = "memberOf"
   463  	expression = "attribute:ecs.availability-zone in [us-west-2a, us-west-2b]"
   464    }
   465  }
   466  `
   467  
   468  var testAccAWSEcsService_withIamRole = `
   469  resource "aws_ecs_cluster" "main" {
   470  	name = "terraformecstest11"
   471  }
   472  
   473  resource "aws_ecs_task_definition" "ghost" {
   474    family = "ghost_service"
   475    container_definitions = <<DEFINITION
   476  [
   477    {
   478      "cpu": 128,
   479      "essential": true,
   480      "image": "ghost:latest",
   481      "memory": 128,
   482      "name": "ghost",
   483      "portMappings": [
   484        {
   485          "containerPort": 2368,
   486          "hostPort": 8080
   487        }
   488      ]
   489    }
   490  ]
   491  DEFINITION
   492  }
   493  
   494  resource "aws_iam_role" "ecs_service" {
   495      name = "EcsService"
   496      assume_role_policy = <<EOF
   497  {
   498      "Version": "2012-10-17",
   499      "Statement": [
   500          {
   501              "Action": "sts:AssumeRole",
   502              "Principal": {"AWS": "*"},
   503              "Effect": "Allow",
   504              "Sid": ""
   505          }
   506      ]
   507  }
   508  EOF
   509  }
   510  
   511  resource "aws_iam_role_policy" "ecs_service" {
   512      name = "EcsService"
   513      role = "${aws_iam_role.ecs_service.name}"
   514      policy = <<EOF
   515  {
   516    "Version": "2012-10-17",
   517    "Statement": [
   518      {
   519        "Effect": "Allow",
   520        "Action": [
   521          "elasticloadbalancing:*",
   522          "ec2:*",
   523          "ecs:*"
   524        ],
   525        "Resource": [
   526          "*"
   527        ]
   528      }
   529    ]
   530  }
   531  EOF
   532  }
   533  
   534  resource "aws_elb" "main" {
   535    availability_zones = ["us-west-2a"]
   536  
   537    listener {
   538      instance_port = 8080
   539      instance_protocol = "http"
   540      lb_port = 80
   541      lb_protocol = "http"
   542    }
   543  }
   544  
   545  resource "aws_ecs_service" "ghost" {
   546    name = "ghost"
   547    cluster = "${aws_ecs_cluster.main.id}"
   548    task_definition = "${aws_ecs_task_definition.ghost.arn}"
   549    desired_count = 1
   550    iam_role = "${aws_iam_role.ecs_service.name}"
   551  
   552    load_balancer {
   553      elb_name = "${aws_elb.main.id}"
   554      container_name = "ghost"
   555      container_port = "2368"
   556    }
   557  
   558    depends_on = ["aws_iam_role_policy.ecs_service"]
   559  }
   560  `
   561  
   562  var testAccAWSEcsServiceWithDeploymentValues = `
   563  resource "aws_ecs_cluster" "default" {
   564  	name = "terraformecstest1"
   565  }
   566  
   567  resource "aws_ecs_task_definition" "mongo" {
   568    family = "mongodb"
   569    container_definitions = <<DEFINITION
   570  [
   571    {
   572      "cpu": 128,
   573      "essential": true,
   574      "image": "mongo:latest",
   575      "memory": 128,
   576      "name": "mongodb"
   577    }
   578  ]
   579  DEFINITION
   580  }
   581  
   582  resource "aws_ecs_service" "mongo" {
   583    name = "mongodb"
   584    cluster = "${aws_ecs_cluster.default.id}"
   585    task_definition = "${aws_ecs_task_definition.mongo.arn}"
   586    desired_count = 1
   587  }
   588  `
   589  
   590  var tpl_testAccAWSEcsService_withLbChanges = `
   591  resource "aws_ecs_cluster" "main" {
   592  	name = "terraformecstest12"
   593  }
   594  
   595  resource "aws_ecs_task_definition" "with_lb_changes" {
   596    family = "ghost_lbd"
   597    container_definitions = <<DEFINITION
   598  [
   599    {
   600      "cpu": 128,
   601      "essential": true,
   602      "image": "%s",
   603      "memory": 128,
   604      "name": "%s",
   605      "portMappings": [
   606        {
   607          "containerPort": %d,
   608          "hostPort": %d
   609        }
   610      ]
   611    }
   612  ]
   613  DEFINITION
   614  }
   615  
   616  resource "aws_iam_role" "ecs_service" {
   617      name = "EcsServiceLbd"
   618      assume_role_policy = <<EOF
   619  {
   620      "Version": "2012-10-17",
   621      "Statement": [
   622          {
   623              "Action": "sts:AssumeRole",
   624              "Principal": {"AWS": "*"},
   625              "Effect": "Allow",
   626              "Sid": ""
   627          }
   628      ]
   629  }
   630  EOF
   631  }
   632  
   633  resource "aws_iam_role_policy" "ecs_service" {
   634      name = "EcsServiceLbd"
   635      role = "${aws_iam_role.ecs_service.name}"
   636      policy = <<EOF
   637  {
   638    "Version": "2012-10-17",
   639    "Statement": [
   640      {
   641        "Effect": "Allow",
   642        "Action": [
   643          "elasticloadbalancing:*",
   644          "ec2:*",
   645          "ecs:*"
   646        ],
   647        "Resource": [
   648          "*"
   649        ]
   650      }
   651    ]
   652  }
   653  EOF
   654  }
   655  
   656  resource "aws_elb" "main" {
   657    availability_zones = ["us-west-2a"]
   658  
   659    listener {
   660      instance_port = %d
   661      instance_protocol = "http"
   662      lb_port = 80
   663      lb_protocol = "http"
   664    }
   665  }
   666  
   667  resource "aws_ecs_service" "with_lb_changes" {
   668    name = "ghost"
   669    cluster = "${aws_ecs_cluster.main.id}"
   670    task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
   671    desired_count = 1
   672    iam_role = "${aws_iam_role.ecs_service.name}"
   673  
   674    load_balancer {
   675      elb_name = "${aws_elb.main.id}"
   676      container_name = "%s"
   677      container_port = "%d"
   678    }
   679  
   680    depends_on = ["aws_iam_role_policy.ecs_service"]
   681  }
   682  `
   683  
   684  var testAccAWSEcsService_withLbChanges = fmt.Sprintf(
   685  	tpl_testAccAWSEcsService_withLbChanges,
   686  	"ghost:latest", "ghost", 2368, 8080, 8080, "ghost", 2368)
   687  var testAccAWSEcsService_withLbChanges_modified = fmt.Sprintf(
   688  	tpl_testAccAWSEcsService_withLbChanges,
   689  	"nginx:latest", "nginx", 80, 8080, 8080, "nginx", 80)
   690  
   691  var testAccAWSEcsServiceWithFamilyAndRevision = `
   692  resource "aws_ecs_cluster" "default" {
   693  	name = "terraformecstest2"
   694  }
   695  
   696  resource "aws_ecs_task_definition" "jenkins" {
   697    family = "jenkins"
   698    container_definitions = <<DEFINITION
   699  [
   700    {
   701      "cpu": 128,
   702      "essential": true,
   703      "image": "jenkins:latest",
   704      "memory": 128,
   705      "name": "jenkins"
   706    }
   707  ]
   708  DEFINITION
   709  }
   710  
   711  resource "aws_ecs_service" "jenkins" {
   712    name = "jenkins"
   713    cluster = "${aws_ecs_cluster.default.id}"
   714    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   715    desired_count = 1
   716  }
   717  `
   718  
   719  var testAccAWSEcsServiceWithFamilyAndRevisionModified = `
   720  resource "aws_ecs_cluster" "default" {
   721  	name = "terraformecstest2"
   722  }
   723  
   724  resource "aws_ecs_task_definition" "jenkins" {
   725    family = "jenkins"
   726    container_definitions = <<DEFINITION
   727  [
   728    {
   729      "cpu": 128,
   730      "essential": true,
   731      "image": "jenkins:latest",
   732      "memory": 128,
   733      "name": "jenkins"
   734    }
   735  ]
   736  DEFINITION
   737  }
   738  
   739  resource "aws_ecs_service" "jenkins" {
   740    name = "jenkins"
   741    cluster = "${aws_ecs_cluster.default.id}"
   742    task_definition = "${aws_ecs_task_definition.jenkins.family}:${aws_ecs_task_definition.jenkins.revision}"
   743    desired_count = 1
   744  }
   745  `
   746  
   747  var testAccAWSEcsServiceWithRenamedCluster = `
   748  resource "aws_ecs_cluster" "default" {
   749  	name = "terraformecstest3"
   750  }
   751  resource "aws_ecs_task_definition" "ghost" {
   752    family = "ghost"
   753    container_definitions = <<DEFINITION
   754  [
   755    {
   756      "cpu": 128,
   757      "essential": true,
   758      "image": "ghost:latest",
   759      "memory": 128,
   760      "name": "ghost"
   761    }
   762  ]
   763  DEFINITION
   764  }
   765  resource "aws_ecs_service" "ghost" {
   766    name = "ghost"
   767    cluster = "${aws_ecs_cluster.default.id}"
   768    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   769    desired_count = 1
   770  }
   771  `
   772  
   773  var testAccAWSEcsServiceWithRenamedClusterModified = `
   774  resource "aws_ecs_cluster" "default" {
   775  	name = "terraformecstest3modified"
   776  }
   777  resource "aws_ecs_task_definition" "ghost" {
   778    family = "ghost"
   779    container_definitions = <<DEFINITION
   780  [
   781    {
   782      "cpu": 128,
   783      "essential": true,
   784      "image": "ghost:latest",
   785      "memory": 128,
   786      "name": "ghost"
   787    }
   788  ]
   789  DEFINITION
   790  }
   791  resource "aws_ecs_service" "ghost" {
   792    name = "ghost"
   793    cluster = "${aws_ecs_cluster.default.id}"
   794    task_definition = "${aws_ecs_task_definition.ghost.family}:${aws_ecs_task_definition.ghost.revision}"
   795    desired_count = 1
   796  }
   797  `
   798  
   799  var testAccAWSEcsServiceWithEcsClusterName = `
   800  resource "aws_ecs_cluster" "default" {
   801  	name = "terraformecstestcluster"
   802  }
   803  
   804  resource "aws_ecs_task_definition" "jenkins" {
   805    family = "jenkins"
   806    container_definitions = <<DEFINITION
   807  [
   808    {
   809      "cpu": 128,
   810      "essential": true,
   811      "image": "jenkins:latest",
   812      "memory": 128,
   813      "name": "jenkins"
   814    }
   815  ]
   816  DEFINITION
   817  }
   818  
   819  resource "aws_ecs_service" "jenkins" {
   820    name = "jenkins"
   821    cluster = "${aws_ecs_cluster.default.name}"
   822    task_definition = "${aws_ecs_task_definition.jenkins.arn}"
   823    desired_count = 1
   824  }
   825  `
   826  
   827  var testAccAWSEcsServiceWithAlb = `
   828  data "aws_availability_zones" "available" {}
   829  
   830  resource "aws_vpc" "main" {
   831    cidr_block = "10.10.0.0/16"
   832  }
   833  
   834  resource "aws_subnet" "main" {
   835    count = 2
   836    cidr_block = "${cidrsubnet(aws_vpc.main.cidr_block, 8, count.index)}"
   837    availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
   838    vpc_id = "${aws_vpc.main.id}"
   839  }
   840  
   841  resource "aws_ecs_cluster" "main" {
   842    name = "terraform_acc_test_ecs_15"
   843  }
   844  
   845  resource "aws_ecs_task_definition" "with_lb_changes" {
   846    family = "tf_acc_test_ghost_lbd"
   847    container_definitions = <<DEFINITION
   848  [
   849    {
   850      "cpu": 256,
   851      "essential": true,
   852      "image": "ghost:latest",
   853      "memory": 512,
   854      "name": "ghost",
   855      "portMappings": [
   856        {
   857          "containerPort": 2368,
   858          "hostPort": 8080
   859        }
   860      ]
   861    }
   862  ]
   863  DEFINITION
   864  }
   865  
   866  resource "aws_iam_role" "ecs_service" {
   867      name = "tf_acc_test_15_role"
   868      assume_role_policy = <<EOF
   869  {
   870    "Version": "2008-10-17",
   871    "Statement": [
   872      {
   873        "Sid": "",
   874        "Effect": "Allow",
   875        "Principal": {
   876          "Service": "ecs.amazonaws.com"
   877        },
   878        "Action": "sts:AssumeRole"
   879      }
   880    ]
   881  }
   882  EOF
   883  }
   884  
   885  resource "aws_iam_role_policy" "ecs_service" {
   886      name = "tf_acc_test_15_policy"
   887      role = "${aws_iam_role.ecs_service.name}"
   888      policy = <<EOF
   889  {
   890    "Version": "2012-10-17",
   891    "Statement": [
   892      {
   893        "Effect": "Allow",
   894        "Action": [
   895          "ec2:Describe*",
   896          "elasticloadbalancing:DeregisterInstancesFromLoadBalancer",
   897          "elasticloadbalancing:DeregisterTargets",
   898          "elasticloadbalancing:Describe*",
   899          "elasticloadbalancing:RegisterInstancesWithLoadBalancer",
   900          "elasticloadbalancing:RegisterTargets"
   901        ],
   902        "Resource": "*"
   903      }
   904    ]
   905  }
   906  EOF
   907  }
   908  
   909  resource "aws_alb_target_group" "test" {
   910    name = "tf-acc-test-ecs-ghost"
   911    port = 80
   912    protocol = "HTTP"
   913    vpc_id = "${aws_vpc.main.id}"
   914  }
   915  
   916  resource "aws_alb" "main" {
   917    name            = "tf-acc-test-test-alb-ecs"
   918    subnets         = ["${aws_subnet.main.*.id}"]
   919  }
   920  
   921  resource "aws_alb_listener" "front_end" {
   922    load_balancer_arn = "${aws_alb.main.id}"
   923    port = "80"
   924    protocol = "HTTP"
   925  
   926    default_action {
   927      target_group_arn = "${aws_alb_target_group.test.id}"
   928      type = "forward"
   929    }
   930  }
   931  
   932  resource "aws_ecs_service" "with_alb" {
   933    name = "tf-acc-test-ecs-ghost"
   934    cluster = "${aws_ecs_cluster.main.id}"
   935    task_definition = "${aws_ecs_task_definition.with_lb_changes.arn}"
   936    desired_count = 1
   937    iam_role = "${aws_iam_role.ecs_service.name}"
   938  
   939    load_balancer {
   940      target_group_arn = "${aws_alb_target_group.test.id}"
   941      container_name = "ghost"
   942      container_port = "2368"
   943    }
   944  
   945    depends_on = [
   946      "aws_iam_role_policy.ecs_service",
   947      "aws_alb_listener.front_end"
   948    ]
   949  }
   950  `