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