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