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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"testing"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/emr"
    11  	"github.com/hashicorp/terraform/helper/acctest"
    12  	"github.com/hashicorp/terraform/helper/resource"
    13  	"github.com/hashicorp/terraform/terraform"
    14  )
    15  
    16  func TestAccAWSEMRInstanceGroup_basic(t *testing.T) {
    17  	var ig emr.InstanceGroup
    18  	rInt := acctest.RandInt()
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckAWSEmrInstanceGroupDestroy,
    23  		Steps: []resource.TestStep{
    24  			{
    25  				Config: testAccAWSEmrInstanceGroupConfig(rInt),
    26  				Check:  testAccCheckAWSEmrInstanceGroupExists("aws_emr_instance_group.task", &ig),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAWSEMRInstanceGroup_ebsBasic(t *testing.T) {
    33  	var ig emr.InstanceGroup
    34  	rInt := acctest.RandInt()
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckAWSEmrInstanceGroupDestroy,
    39  		Steps: []resource.TestStep{
    40  			{
    41  				Config: testAccAWSEmrInstanceGroupConfig_ebsBasic(rInt),
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckAWSEmrInstanceGroupExists("aws_emr_instance_group.task", &ig),
    44  					resource.TestCheckResourceAttr(
    45  						"aws_emr_instance_group.task", "ebs_config.#", "1"),
    46  					resource.TestCheckResourceAttr(
    47  						"aws_emr_instance_group.task", "ebs_optimized", "true"),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func testAccCheckAWSEmrInstanceGroupDestroy(s *terraform.State) error {
    55  	conn := testAccProvider.Meta().(*AWSClient).emrconn
    56  
    57  	for _, rs := range s.RootModule().Resources {
    58  		if rs.Type != "aws_emr_cluster" {
    59  			continue
    60  		}
    61  
    62  		params := &emr.DescribeClusterInput{
    63  			ClusterId: aws.String(rs.Primary.ID),
    64  		}
    65  
    66  		describe, err := conn.DescribeCluster(params)
    67  
    68  		if err == nil {
    69  			if describe.Cluster != nil &&
    70  				*describe.Cluster.Status.State == "WAITING" {
    71  				return fmt.Errorf("EMR Cluster still exists")
    72  			}
    73  		}
    74  
    75  		providerErr, ok := err.(awserr.Error)
    76  		if !ok {
    77  			return err
    78  		}
    79  
    80  		log.Printf("[ERROR] %v", providerErr)
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  func testAccCheckAWSEmrInstanceGroupExists(n string, v *emr.InstanceGroup) resource.TestCheckFunc {
    87  	return func(s *terraform.State) error {
    88  		rs, ok := s.RootModule().Resources[n]
    89  		if !ok {
    90  			return fmt.Errorf("Not found: %s", n)
    91  		}
    92  		if rs.Primary.ID == "" {
    93  			return fmt.Errorf("No task group id set")
    94  		}
    95  		meta := testAccProvider.Meta()
    96  		g, err := fetchEMRInstanceGroup(meta, rs.Primary.Attributes["cluster_id"], rs.Primary.ID)
    97  		if err != nil {
    98  			return fmt.Errorf("EMR error: %v", err)
    99  		}
   100  
   101  		if g == nil {
   102  			return fmt.Errorf("No match found for (%s)", n)
   103  		}
   104  
   105  		v = g
   106  		return nil
   107  	}
   108  }
   109  
   110  const testAccAWSEmrInstanceGroupBase = `
   111  provider "aws" {
   112    region = "us-west-2"
   113  }
   114  
   115  resource "aws_emr_cluster" "tf-test-cluster" {
   116    name          = "tf-test-emr-%d"
   117    release_label = "emr-4.6.0"
   118    applications  = ["Spark"]
   119  
   120    ec2_attributes {
   121      subnet_id                         = "${aws_subnet.main.id}"
   122      emr_managed_master_security_group = "${aws_security_group.allow_all.id}"
   123      emr_managed_slave_security_group  = "${aws_security_group.allow_all.id}"
   124      instance_profile                  = "${aws_iam_instance_profile.emr_profile.arn}"
   125    }
   126  
   127    master_instance_type = "m3.xlarge"
   128    core_instance_type   = "m3.xlarge"
   129    core_instance_count  = 2
   130  
   131    tags {
   132      role     = "rolename"
   133      dns_zone = "env_zone"
   134      env      = "env"
   135      name     = "name-env"
   136    }
   137  
   138    bootstrap_action {
   139      path = "s3://elasticmapreduce/bootstrap-actions/run-if"
   140      name = "runif"
   141      args = ["instance.isMaster=true", "echo running on master node"]
   142    }
   143  
   144    configurations = "test-fixtures/emr_configurations.json"
   145    service_role = "${aws_iam_role.iam_emr_default_role.arn}"
   146  
   147    depends_on = ["aws_internet_gateway.gw"]
   148  }
   149  
   150  resource "aws_security_group" "allow_all" {
   151    name        = "allow_all"
   152    description = "Allow all inbound traffic"
   153    vpc_id      = "${aws_vpc.main.id}"
   154  
   155    ingress {
   156      from_port   = 0
   157      to_port     = 0
   158      protocol    = "-1"
   159      cidr_blocks = ["0.0.0.0/0"]
   160    }
   161  
   162    egress {
   163      from_port   = 0
   164      to_port     = 0
   165      protocol    = "-1"
   166      cidr_blocks = ["0.0.0.0/0"]
   167    }
   168  
   169    depends_on = ["aws_subnet.main"]
   170  
   171    lifecycle {
   172      ignore_changes = ["ingress", "egress"]
   173    }
   174  }
   175  
   176  resource "aws_vpc" "main" {
   177    cidr_block           = "168.31.0.0/16"
   178    enable_dns_hostnames = true
   179  }
   180  
   181  resource "aws_subnet" "main" {
   182    vpc_id     = "${aws_vpc.main.id}"
   183    cidr_block = "168.31.0.0/20"
   184  
   185    #  map_public_ip_on_launch = true
   186  }
   187  
   188  resource "aws_internet_gateway" "gw" {
   189    vpc_id = "${aws_vpc.main.id}"
   190  }
   191  
   192  resource "aws_route_table" "r" {
   193    vpc_id = "${aws_vpc.main.id}"
   194  
   195    route {
   196      cidr_block = "0.0.0.0/0"
   197      gateway_id = "${aws_internet_gateway.gw.id}"
   198    }
   199  }
   200  
   201  resource "aws_main_route_table_association" "a" {
   202    vpc_id         = "${aws_vpc.main.id}"
   203    route_table_id = "${aws_route_table.r.id}"
   204  }
   205  
   206  ###
   207  
   208  # IAM role for EMR Service
   209  resource "aws_iam_role" "iam_emr_default_role" {
   210    name = "iam_emr_default_role_%d"
   211  
   212    assume_role_policy = <<EOT
   213  {
   214    "Version": "2008-10-17",
   215    "Statement": [
   216      {
   217        "Sid": "",
   218        "Effect": "Allow",
   219        "Principal": {
   220          "Service": "elasticmapreduce.amazonaws.com"
   221        },
   222        "Action": "sts:AssumeRole"
   223      }
   224    ]
   225  }
   226  EOT
   227  }
   228  
   229  resource "aws_iam_role_policy_attachment" "service-attach" {
   230    role       = "${aws_iam_role.iam_emr_default_role.id}"
   231    policy_arn = "${aws_iam_policy.iam_emr_default_policy.arn}"
   232  }
   233  
   234  resource "aws_iam_policy" "iam_emr_default_policy" {
   235    name = "iam_emr_default_policy_%d"
   236  
   237    policy = <<EOT
   238  {
   239      "Version": "2012-10-17",
   240      "Statement": [{
   241          "Effect": "Allow",
   242          "Resource": "*",
   243          "Action": [
   244              "ec2:AuthorizeSecurityGroupEgress",
   245              "ec2:AuthorizeSecurityGroupIngress",
   246              "ec2:CancelSpotInstanceRequests",
   247              "ec2:CreateNetworkInterface",
   248              "ec2:CreateSecurityGroup",
   249              "ec2:CreateTags",
   250              "ec2:DeleteNetworkInterface",
   251              "ec2:DeleteSecurityGroup",
   252              "ec2:DeleteTags",
   253              "ec2:DescribeAvailabilityZones",
   254              "ec2:DescribeAccountAttributes",
   255              "ec2:DescribeDhcpOptions",
   256              "ec2:DescribeInstanceStatus",
   257              "ec2:DescribeInstances",
   258              "ec2:DescribeKeyPairs",
   259              "ec2:DescribeNetworkAcls",
   260              "ec2:DescribeNetworkInterfaces",
   261              "ec2:DescribePrefixLists",
   262              "ec2:DescribeRouteTables",
   263              "ec2:DescribeSecurityGroups",
   264              "ec2:DescribeSpotInstanceRequests",
   265              "ec2:DescribeSpotPriceHistory",
   266              "ec2:DescribeSubnets",
   267              "ec2:DescribeVpcAttribute",
   268              "ec2:DescribeVpcEndpoints",
   269              "ec2:DescribeVpcEndpointServices",
   270              "ec2:DescribeVpcs",
   271              "ec2:DetachNetworkInterface",
   272              "ec2:ModifyImageAttribute",
   273              "ec2:ModifyInstanceAttribute",
   274              "ec2:RequestSpotInstances",
   275              "ec2:RevokeSecurityGroupEgress",
   276              "ec2:RunInstances",
   277              "ec2:TerminateInstances",
   278              "ec2:DeleteVolume",
   279              "ec2:DescribeVolumeStatus",
   280              "ec2:DescribeVolumes",
   281              "ec2:DetachVolume",
   282              "iam:GetRole",
   283              "iam:GetRolePolicy",
   284              "iam:ListInstanceProfiles",
   285              "iam:ListRolePolicies",
   286              "iam:PassRole",
   287              "s3:CreateBucket",
   288              "s3:Get*",
   289              "s3:List*",
   290              "sdb:BatchPutAttributes",
   291              "sdb:Select",
   292              "sqs:CreateQueue",
   293              "sqs:Delete*",
   294              "sqs:GetQueue*",
   295              "sqs:PurgeQueue",
   296              "sqs:ReceiveMessage"
   297          ]
   298      }]
   299  }
   300  EOT
   301  }
   302  
   303  # IAM Role for EC2 Instance Profile
   304  resource "aws_iam_role" "iam_emr_profile_role" {
   305    name = "iam_emr_profile_role_%d"
   306  
   307    assume_role_policy = <<EOT
   308  {
   309    "Version": "2008-10-17",
   310    "Statement": [
   311      {
   312        "Sid": "",
   313        "Effect": "Allow",
   314        "Principal": {
   315          "Service": "ec2.amazonaws.com"
   316        },
   317        "Action": "sts:AssumeRole"
   318      }
   319    ]
   320  }
   321  EOT
   322  }
   323  
   324  resource "aws_iam_instance_profile" "emr_profile" {
   325    name  = "emr_profile_%d"
   326    roles = ["${aws_iam_role.iam_emr_profile_role.name}"]
   327  }
   328  
   329  resource "aws_iam_role_policy_attachment" "profile-attach" {
   330    role       = "${aws_iam_role.iam_emr_profile_role.id}"
   331    policy_arn = "${aws_iam_policy.iam_emr_profile_policy.arn}"
   332  }
   333  
   334  resource "aws_iam_policy" "iam_emr_profile_policy" {
   335    name = "iam_emr_profile_policy_%d"
   336  
   337    policy = <<EOT
   338  {
   339      "Version": "2012-10-17",
   340      "Statement": [{
   341          "Effect": "Allow",
   342          "Resource": "*",
   343          "Action": [
   344              "cloudwatch:*",
   345              "dynamodb:*",
   346              "ec2:Describe*",
   347              "elasticmapreduce:Describe*",
   348              "elasticmapreduce:ListBootstrapActions",
   349              "elasticmapreduce:ListClusters",
   350              "elasticmapreduce:ListInstanceGroups",
   351              "elasticmapreduce:ListInstances",
   352              "elasticmapreduce:ListSteps",
   353              "kinesis:CreateStream",
   354              "kinesis:DeleteStream",
   355              "kinesis:DescribeStream",
   356              "kinesis:GetRecords",
   357              "kinesis:GetShardIterator",
   358              "kinesis:MergeShards",
   359              "kinesis:PutRecord",
   360              "kinesis:SplitShard",
   361              "rds:Describe*",
   362              "s3:*",
   363              "sdb:*",
   364              "sns:*",
   365              "sqs:*"
   366          ]
   367      }]
   368  }
   369  EOT
   370  }
   371  `
   372  
   373  func testAccAWSEmrInstanceGroupConfig(r int) string {
   374  	return fmt.Sprintf(testAccAWSEmrInstanceGroupBase+`
   375  	resource "aws_emr_instance_group" "task" {
   376      cluster_id     = "${aws_emr_cluster.tf-test-cluster.id}"
   377      instance_count = 1
   378      instance_type  = "m3.xlarge"
   379    }
   380  	`, r, r, r, r, r, r)
   381  }
   382  
   383  func testAccAWSEmrInstanceGroupConfig_ebsBasic(r int) string {
   384  	return fmt.Sprintf(testAccAWSEmrInstanceGroupBase+`
   385  		resource "aws_emr_instance_group" "task" {
   386      cluster_id     = "${aws_emr_cluster.tf-test-cluster.id}"
   387      instance_count = 1
   388      instance_type  = "m3.xlarge"
   389      ebs_optimized = true
   390      ebs_config {
   391        "size" = 10,
   392        "type" = "gp2",
   393      }
   394    }
   395  	`, r, r, r, r, r, r)
   396  }