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