github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/data_source_aws_instance_test.go (about) 1 package aws 2 3 import ( 4 "testing" 5 6 "fmt" 7 8 "github.com/hashicorp/terraform/helper/acctest" 9 "github.com/hashicorp/terraform/helper/resource" 10 ) 11 12 func TestAccAWSInstanceDataSource_basic(t *testing.T) { 13 resource.Test(t, resource.TestCase{ 14 PreCheck: func() { testAccPreCheck(t) }, 15 Providers: testAccProviders, 16 Steps: []resource.TestStep{ 17 { 18 Config: testAccInstanceDataSourceConfig, 19 Check: resource.ComposeTestCheckFunc( 20 resource.TestCheckResourceAttr( 21 "data.aws_instance.web-instance", "ami", "ami-4fccb37f"), 22 resource.TestCheckResourceAttr( 23 "data.aws_instance.web-instance", "tags.#", "1"), 24 resource.TestCheckResourceAttr( 25 "data.aws_instance.web-instance", "instance_type", "m1.small"), 26 ), 27 }, 28 }, 29 }) 30 } 31 32 func TestAccAWSInstanceDataSource_tags(t *testing.T) { 33 rInt := acctest.RandInt() 34 resource.Test(t, resource.TestCase{ 35 PreCheck: func() { testAccPreCheck(t) }, 36 Providers: testAccProviders, 37 Steps: []resource.TestStep{ 38 { 39 Config: testAccInstanceDataSourceConfig_Tags(rInt), 40 Check: resource.ComposeTestCheckFunc( 41 resource.TestCheckResourceAttr( 42 "data.aws_instance.web-instance", "ami", "ami-4fccb37f"), 43 resource.TestCheckResourceAttr( 44 "data.aws_instance.web-instance", "tags.#", "2"), 45 resource.TestCheckResourceAttr( 46 "data.aws_instance.web-instance", "instance_type", "m1.small"), 47 ), 48 }, 49 }, 50 }) 51 } 52 53 func TestAccAWSInstanceDataSource_AzUserData(t *testing.T) { 54 resource.Test(t, resource.TestCase{ 55 PreCheck: func() { testAccPreCheck(t) }, 56 Providers: testAccProviders, 57 Steps: []resource.TestStep{ 58 { 59 Config: testAccInstanceDataSourceConfig_AzUserData, 60 Check: resource.ComposeTestCheckFunc( 61 resource.TestCheckResourceAttr( 62 "data.aws_instance.foo", "ami", "ami-4fccb37f"), 63 resource.TestCheckResourceAttr( 64 "data.aws_instance.foo", "tags.#", "1"), 65 resource.TestCheckResourceAttr( 66 "data.aws_instance.foo", "instance_type", "m1.small"), 67 resource.TestCheckResourceAttr( 68 "data.aws_instance.foo", "availability_zone", "us-west-2a"), 69 resource.TestCheckResourceAttr( 70 "data.aws_instance.foo", "user_data", "3dc39dda39be1205215e776bad998da361a5955d"), 71 ), 72 }, 73 }, 74 }) 75 } 76 77 func TestAccAWSInstanceDataSource_gp2IopsDevice(t *testing.T) { 78 resource.Test(t, resource.TestCase{ 79 PreCheck: func() { testAccPreCheck(t) }, 80 Providers: testAccProviders, 81 Steps: []resource.TestStep{ 82 { 83 Config: testAccInstanceDataSourceConfig_gp2IopsDevice, 84 Check: resource.ComposeTestCheckFunc( 85 resource.TestCheckResourceAttr( 86 "data.aws_instance.foo", "ami", "ami-55a7ea65"), 87 resource.TestCheckResourceAttr( 88 "data.aws_instance.foo", "instance_type", "m3.medium"), 89 resource.TestCheckResourceAttr( 90 "aws_instance.foo", "root_block_device.#", "1"), 91 resource.TestCheckResourceAttr( 92 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 93 resource.TestCheckResourceAttr( 94 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 95 resource.TestCheckResourceAttr( 96 "aws_instance.foo", "root_block_device.0.iops", "100"), 97 ), 98 }, 99 }, 100 }) 101 } 102 103 func TestAccAWSInstanceDataSource_blockDevices(t *testing.T) { 104 resource.Test(t, resource.TestCase{ 105 PreCheck: func() { testAccPreCheck(t) }, 106 Providers: testAccProviders, 107 Steps: []resource.TestStep{ 108 { 109 Config: testAccInstanceDataSourceConfig_blockDevices, 110 Check: resource.ComposeTestCheckFunc( 111 resource.TestCheckResourceAttr( 112 "data.aws_instance.foo", "ami", "ami-55a7ea65"), 113 resource.TestCheckResourceAttr( 114 "data.aws_instance.foo", "instance_type", "m3.medium"), 115 resource.TestCheckResourceAttr( 116 "aws_instance.foo", "root_block_device.#", "1"), 117 resource.TestCheckResourceAttr( 118 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 119 resource.TestCheckResourceAttr( 120 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 121 resource.TestCheckResourceAttr( 122 "aws_instance.foo", "ebs_block_device.#", "3"), 123 resource.TestCheckResourceAttr( 124 "aws_instance.foo", "ephemeral_block_device.#", "1"), 125 ), 126 }, 127 }, 128 }) 129 } 130 131 func TestAccAWSInstanceDataSource_rootInstanceStore(t *testing.T) { 132 resource.Test(t, resource.TestCase{ 133 PreCheck: func() { testAccPreCheck(t) }, 134 Providers: testAccProviders, 135 Steps: []resource.TestStep{ 136 { 137 Config: testAccInstanceDataSourceConfig_rootInstanceStore, 138 Check: resource.ComposeTestCheckFunc( 139 resource.TestCheckResourceAttr( 140 "data.aws_instance.foo", "ami", "ami-44c36524"), 141 resource.TestCheckResourceAttr( 142 "data.aws_instance.foo", "instance_type", "m3.medium"), 143 resource.TestCheckResourceAttr( 144 "aws_instance.foo", "ebs_block_device.#", "0"), 145 resource.TestCheckResourceAttr( 146 "aws_instance.foo", "ebs_optimized", "false"), 147 resource.TestCheckResourceAttr( 148 "aws_instance.foo", "root_block_device.#", "0"), 149 ), 150 }, 151 }, 152 }) 153 } 154 155 func TestAccAWSInstanceDataSource_privateIP(t *testing.T) { 156 resource.Test(t, resource.TestCase{ 157 PreCheck: func() { testAccPreCheck(t) }, 158 Providers: testAccProviders, 159 Steps: []resource.TestStep{ 160 { 161 Config: testAccInstanceDataSourceConfig_privateIP, 162 Check: resource.ComposeTestCheckFunc( 163 resource.TestCheckResourceAttr( 164 "data.aws_instance.foo", "ami", "ami-c5eabbf5"), 165 resource.TestCheckResourceAttr( 166 "data.aws_instance.foo", "instance_type", "t2.micro"), 167 resource.TestCheckResourceAttr( 168 "data.aws_instance.foo", "private_ip", "10.1.1.42"), 169 ), 170 }, 171 }, 172 }) 173 } 174 175 func TestAccAWSInstanceDataSource_keyPair(t *testing.T) { 176 rName := fmt.Sprintf("tf-test-key-%d", acctest.RandInt()) 177 resource.Test(t, resource.TestCase{ 178 PreCheck: func() { testAccPreCheck(t) }, 179 Providers: testAccProviders, 180 Steps: []resource.TestStep{ 181 { 182 Config: testAccInstanceDataSourceConfig_keyPair(rName), 183 Check: resource.ComposeTestCheckFunc( 184 resource.TestCheckResourceAttr( 185 "data.aws_instance.foo", "ami", "ami-408c7f28"), 186 resource.TestCheckResourceAttr( 187 "data.aws_instance.foo", "instance_type", "t1.micro"), 188 resource.TestCheckResourceAttr( 189 "data.aws_instance.foo", "tags.#", "1"), 190 resource.TestCheckResourceAttr( 191 "data.aws_instance.foo", "key_name", rName), 192 ), 193 }, 194 }, 195 }) 196 } 197 198 func TestAccAWSInstanceDataSource_VPC(t *testing.T) { 199 resource.Test(t, resource.TestCase{ 200 PreCheck: func() { testAccPreCheck(t) }, 201 Providers: testAccProviders, 202 Steps: []resource.TestStep{ 203 { 204 Config: testAccInstanceDataSourceConfig_VPC, 205 Check: resource.ComposeTestCheckFunc( 206 resource.TestCheckResourceAttr( 207 "data.aws_instance.foo", "ami", "ami-4fccb37f"), 208 resource.TestCheckResourceAttr( 209 "data.aws_instance.foo", "instance_type", "m1.small"), 210 resource.TestCheckResourceAttr( 211 "data.aws_instance.foo", "user_data", "562a3e32810edf6ff09994f050f12e799452379d"), 212 resource.TestCheckResourceAttr( 213 "data.aws_instance.foo", "associate_public_ip_address", "true"), 214 resource.TestCheckResourceAttr( 215 "data.aws_instance.foo", "tenancy", "dedicated"), 216 ), 217 }, 218 }, 219 }) 220 } 221 222 func TestAccAWSInstanceDataSource_SecurityGroups(t *testing.T) { 223 rInt := acctest.RandInt() 224 resource.Test(t, resource.TestCase{ 225 PreCheck: func() { testAccPreCheck(t) }, 226 Providers: testAccProviders, 227 Steps: []resource.TestStep{ 228 { 229 Config: testAccInstanceDataSourceConfig_SecurityGroups(rInt), 230 Check: resource.ComposeTestCheckFunc( 231 resource.TestCheckResourceAttr( 232 "data.aws_instance.foo", "ami", "ami-408c7f28"), 233 resource.TestCheckResourceAttr( 234 "data.aws_instance.foo", "instance_type", "m1.small"), 235 resource.TestCheckResourceAttr( 236 "data.aws_instance.foo", "vpc_security_group_ids.#", "0"), 237 resource.TestCheckResourceAttr( 238 "data.aws_instance.foo", "security_groups.#", "1"), 239 resource.TestCheckResourceAttr( 240 "data.aws_instance.foo", "user_data", "3dc39dda39be1205215e776bad998da361a5955d"), 241 ), 242 }, 243 }, 244 }) 245 } 246 247 func TestAccAWSInstanceDataSource_VPCSecurityGroups(t *testing.T) { 248 resource.Test(t, resource.TestCase{ 249 PreCheck: func() { testAccPreCheck(t) }, 250 Providers: testAccProviders, 251 Steps: []resource.TestStep{ 252 { 253 Config: testAccInstanceDataSourceConfig_VPCSecurityGroups, 254 Check: resource.ComposeTestCheckFunc( 255 resource.TestCheckResourceAttr( 256 "data.aws_instance.foo", "ami", "ami-21f78e11"), 257 resource.TestCheckResourceAttr( 258 "data.aws_instance.foo", "instance_type", "t1.micro"), 259 resource.TestCheckResourceAttr( 260 "data.aws_instance.foo", "security_groups.#", "0"), 261 resource.TestCheckResourceAttr( 262 "data.aws_instance.foo", "vpc_security_group_ids.#", "1"), 263 ), 264 }, 265 }, 266 }) 267 } 268 269 // Lookup based on InstanceID 270 const testAccInstanceDataSourceConfig = ` 271 resource "aws_instance" "web" { 272 # us-west-2 273 ami = "ami-4fccb37f" 274 instance_type = "m1.small" 275 tags { 276 Name = "HelloWorld" 277 } 278 } 279 280 data "aws_instance" "web-instance" { 281 filter { 282 name = "instance-id" 283 values = ["${aws_instance.web.id}"] 284 } 285 } 286 ` 287 288 // Use the tags attribute to filter 289 func testAccInstanceDataSourceConfig_Tags(rInt int) string { 290 return fmt.Sprintf(` 291 resource "aws_instance" "web" { 292 # us-west-2 293 ami = "ami-4fccb37f" 294 instance_type = "m1.small" 295 tags { 296 Name = "HelloWorld" 297 TestSeed = "%d" 298 } 299 } 300 301 data "aws_instance" "web-instance" { 302 instance_tags { 303 Name = "${aws_instance.web.tags["Name"]}" 304 TestSeed = "%d" 305 } 306 } 307 `, rInt, rInt) 308 } 309 310 // filter on tag, populate more attributes 311 const testAccInstanceDataSourceConfig_AzUserData = ` 312 resource "aws_instance" "foo" { 313 # us-west-2 314 ami = "ami-4fccb37f" 315 availability_zone = "us-west-2a" 316 317 instance_type = "m1.small" 318 user_data = "foo:-with-character's" 319 tags { 320 TFAccTest = "YesThisIsATest" 321 } 322 } 323 324 data "aws_instance" "foo" { 325 instance_id = "${aws_instance.foo.id}" 326 } 327 ` 328 329 // GP2IopsDevice 330 const testAccInstanceDataSourceConfig_gp2IopsDevice = ` 331 resource "aws_instance" "foo" { 332 # us-west-2 333 ami = "ami-55a7ea65" 334 instance_type = "m3.medium" 335 root_block_device { 336 volume_type = "gp2" 337 volume_size = 11 338 iops = 330 339 } 340 } 341 342 data "aws_instance" "foo" { 343 instance_id = "${aws_instance.foo.id}" 344 } 345 ` 346 347 // Block Device 348 const testAccInstanceDataSourceConfig_blockDevices = ` 349 resource "aws_instance" "foo" { 350 # us-west-2 351 ami = "ami-55a7ea65" 352 instance_type = "m3.medium" 353 354 root_block_device { 355 volume_type = "gp2" 356 volume_size = 11 357 } 358 ebs_block_device { 359 device_name = "/dev/sdb" 360 volume_size = 9 361 } 362 ebs_block_device { 363 device_name = "/dev/sdc" 364 volume_size = 10 365 volume_type = "io1" 366 iops = 100 367 } 368 369 # Encrypted ebs block device 370 ebs_block_device { 371 device_name = "/dev/sdd" 372 volume_size = 12 373 encrypted = true 374 } 375 376 ephemeral_block_device { 377 device_name = "/dev/sde" 378 virtual_name = "ephemeral0" 379 } 380 } 381 382 data "aws_instance" "foo" { 383 instance_id = "${aws_instance.foo.id}" 384 } 385 ` 386 387 const testAccInstanceDataSourceConfig_rootInstanceStore = ` 388 resource "aws_instance" "foo" { 389 ami = "ami-44c36524" 390 instance_type = "m3.medium" 391 } 392 data "aws_instance" "foo" { 393 instance_id = "${aws_instance.foo.id}" 394 } 395 ` 396 397 const testAccInstanceDataSourceConfig_privateIP = ` 398 resource "aws_vpc" "foo" { 399 cidr_block = "10.1.0.0/16" 400 } 401 402 resource "aws_subnet" "foo" { 403 cidr_block = "10.1.1.0/24" 404 vpc_id = "${aws_vpc.foo.id}" 405 } 406 407 resource "aws_instance" "foo" { 408 ami = "ami-c5eabbf5" 409 instance_type = "t2.micro" 410 subnet_id = "${aws_subnet.foo.id}" 411 private_ip = "10.1.1.42" 412 } 413 414 data "aws_instance" "foo" { 415 instance_id = "${aws_instance.foo.id}" 416 } 417 ` 418 419 func testAccInstanceDataSourceConfig_keyPair(rName string) string { 420 return fmt.Sprintf(` 421 provider "aws" { 422 region = "us-east-1" 423 } 424 425 resource "aws_key_pair" "debugging" { 426 key_name = "%s" 427 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 428 } 429 430 resource "aws_instance" "foo" { 431 ami = "ami-408c7f28" 432 instance_type = "t1.micro" 433 key_name = "${aws_key_pair.debugging.key_name}" 434 tags { 435 Name = "testAccInstanceDataSourceConfigKeyPair_TestAMI" 436 } 437 } 438 439 data "aws_instance" "foo" { 440 filter { 441 name = "tag:Name" 442 values = ["testAccInstanceDataSourceConfigKeyPair_TestAMI"] 443 } 444 filter { 445 name = "key-name" 446 values = ["${aws_instance.foo.key_name}"] 447 } 448 }`, rName) 449 } 450 451 const testAccInstanceDataSourceConfig_VPC = ` 452 resource "aws_vpc" "foo" { 453 cidr_block = "10.1.0.0/16" 454 } 455 456 resource "aws_subnet" "foo" { 457 cidr_block = "10.1.1.0/24" 458 vpc_id = "${aws_vpc.foo.id}" 459 } 460 461 resource "aws_instance" "foo" { 462 # us-west-2 463 ami = "ami-4fccb37f" 464 instance_type = "m1.small" 465 subnet_id = "${aws_subnet.foo.id}" 466 associate_public_ip_address = true 467 tenancy = "dedicated" 468 # pre-encoded base64 data 469 user_data = "3dc39dda39be1205215e776bad998da361a5955d" 470 } 471 472 data "aws_instance" "foo" { 473 instance_id = "${aws_instance.foo.id}" 474 } 475 ` 476 477 func testAccInstanceDataSourceConfig_SecurityGroups(rInt int) string { 478 return fmt.Sprintf(` 479 provider "aws" { 480 region = "us-east-1" 481 } 482 483 resource "aws_security_group" "tf_test_foo" { 484 name = "tf_test_foo-%d" 485 description = "foo" 486 487 ingress { 488 protocol = "icmp" 489 from_port = -1 490 to_port = -1 491 cidr_blocks = ["0.0.0.0/0"] 492 } 493 } 494 495 resource "aws_instance" "foo" { 496 ami = "ami-408c7f28" 497 instance_type = "m1.small" 498 security_groups = ["${aws_security_group.tf_test_foo.name}"] 499 user_data = "foo:-with-character's" 500 } 501 502 data "aws_instance" "foo" { 503 instance_id = "${aws_instance.foo.id}" 504 } 505 `, rInt) 506 } 507 508 const testAccInstanceDataSourceConfig_VPCSecurityGroups = ` 509 resource "aws_internet_gateway" "gw" { 510 vpc_id = "${aws_vpc.foo.id}" 511 } 512 513 resource "aws_vpc" "foo" { 514 cidr_block = "10.1.0.0/16" 515 tags { 516 Name = "tf-network-test" 517 } 518 } 519 520 resource "aws_security_group" "tf_test_foo" { 521 name = "tf_test_foo" 522 description = "foo" 523 vpc_id="${aws_vpc.foo.id}" 524 525 ingress { 526 protocol = "icmp" 527 from_port = -1 528 to_port = -1 529 cidr_blocks = ["0.0.0.0/0"] 530 } 531 } 532 533 resource "aws_subnet" "foo" { 534 cidr_block = "10.1.1.0/24" 535 vpc_id = "${aws_vpc.foo.id}" 536 } 537 538 resource "aws_instance" "foo_instance" { 539 ami = "ami-21f78e11" 540 instance_type = "t1.micro" 541 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 542 subnet_id = "${aws_subnet.foo.id}" 543 depends_on = ["aws_internet_gateway.gw"] 544 } 545 546 data "aws_instance" "foo" { 547 instance_id = "${aws_instance.foo_instance.id}" 548 } 549 `