/usr/share/perl5/VM/EC2.pm is in libvm-ec2-perl 1.23-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 | package VM::EC2;
=head1 NAME
VM::EC2 - Control the Amazon EC2 and Eucalyptus Clouds
=head1 SYNOPSIS
# set environment variables EC2_ACCESS_KEY, EC2_SECRET_KEY and/or EC2_URL
# to fill in arguments automatically
## IMAGE AND INSTANCE MANAGEMENT
# get new EC2 object
my $ec2 = VM::EC2->new(-access_key => 'access key id',
-secret_key => 'aws_secret_key',
-endpoint => 'http://ec2.amazonaws.com');
# fetch an image by its ID
my $image = $ec2->describe_images('ami-12345');
# get some information about the image
my $architecture = $image->architecture;
my $description = $image->description;
my @devices = $image->blockDeviceMapping;
for my $d (@devices) {
print $d->deviceName,"\n";
print $d->snapshotId,"\n";
print $d->volumeSize,"\n";
}
# run two instances
my @instances = $image->run_instances(-key_name =>'My_key',
-security_group=>'default',
-min_count =>2,
-instance_type => 't1.micro')
or die $ec2->error_str;
# wait for both instances to reach "running" or other terminal state
$ec2->wait_for_instances(@instances);
# print out both instance's current state and DNS name
for my $i (@instances) {
my $status = $i->current_status;
my $dns = $i->dnsName;
print "$i: [$status] $dns\n";
}
# tag both instances with Role "server"
foreach (@instances) {$_->add_tag(Role=>'server');
# stop both instances
foreach (@instances) {$_->stop}
# find instances tagged with Role=Server that are
# stopped, change the user data and restart.
@instances = $ec2->describe_instances({'tag:Role' => 'Server',
'instance-state-name' => 'stopped'});
for my $i (@instances) {
$i->userData('Secure-mode: off');
$i->start or warn "Couldn't start $i: ",$i->error_str;
}
# create an image from both instance, tag them, and make
# them public
for my $i (@instances) {
my $img = $i->create_image("Autoimage from $i","Test image");
$img->add_tags(Name => "Autoimage from $i",
Role => 'Server',
Status=> 'Production');
$img->make_public(1);
}
## KEY MANAGEMENT
# retrieve the name and fingerprint of the first instance's
# key pair
my $kp = $instances[0]->keyPair;
print $instances[0], ": keypair $kp=",$kp->fingerprint,"\n";
# create a new key pair
$kp = $ec2->create_key_pair('My Key');
# get the private key from this key pair and write it to a disk file
# in ssh-compatible format
my $private_key = $kp->private_key;
open (my $f,'>MyKeypair.rsa') or die $!;
print $f $private_key;
close $f;
# Import a preexisting SSH key
my $public_key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8o...';
$key = $ec2->import_key_pair('NewKey',$public_key);
## SECURITY GROUPS AND FIREWALL RULES
# Create a new security group
my $group = $ec2->create_security_group(-name => 'NewGroup',
-description => 'example');
# Add a firewall rule
$group->authorize_incoming(-protocol => 'tcp',
-port => 80,
-source_ip => ['192.168.2.0/24','192.168.2.1/24'});
# Write rules back to Amazon
$group->update;
# Print current firewall rules
print join ("\n",$group->ipPermissions),"\n";
## VOLUME && SNAPSHOT MANAGEMENT
# find existing volumes that are available
my @volumes = $ec2->describe_volumes({status=>'available'});
# back 'em all up to snapshots
foreach (@volumes) {$_->snapshot('Backup on '.localtime)}
# find a stopped instance in first volume's availability zone and
# attach the volume to the instance using /dev/sdg
my $vol = $volumes[0];
my $zone = $vol->availabilityZone;
@instances = $ec2->describe_instances({'availability-zone'=> $zone,
'run-state-name' => $stopped);
$instances[0]->attach_volume($vol=>'/dev/sdg') if @instances;
# create a new 20 gig volume
$vol = $ec2->create_volume(-availability_zone=> 'us-east-1a',
-size => 20);
$ec2->wait_for_volumes($vol);
print "Volume $vol is ready!\n" if $vol->current_status eq 'available';
# create a new elastic address and associate it with an instance
my $address = $ec2->allocate_address();
$instances[0]->associate_address($address);
=head1 DESCRIPTION
This is an interface to the 2012-12-01 version of the Amazon AWS API
(http://aws.amazon.com/ec2). It was written provide access to the new
tag and metadata interface that is not currently supported by
Net::Amazon::EC2, as well as to provide developers with an extension
mechanism for the API. This library will also support the Eucalyptus
open source cloud (http://open.eucalyptus.com).
The main interface is the VM::EC2 object, which provides methods for
interrogating the Amazon EC2, launching instances, and managing
instance lifecycle. These methods return the following major object
classes which act as specialized interfaces to AWS:
VM::EC2::BlockDevice -- A block device
VM::EC2::BlockDevice::Attachment -- Attachment of a block device to an EC2 instance
VM::EC2::BlockDevice::EBS -- An elastic block device
VM::EC2::BlockDevice::Mapping -- Mapping of a virtual storage device to a block device
VM::EC2::BlockDevice::Mapping::EBS -- Mapping of a virtual storage device to an EBS block device
VM::EC2::Group -- Security groups
VM::EC2::Image -- Amazon Machine Images (AMIs)
VM::EC2::Instance -- Virtual machine instances
VM::EC2::Instance::Metadata -- Access to runtime metadata from running instances
VM::EC2::Region -- Availability regions
VM::EC2::Snapshot -- EBS snapshots
VM::EC2::Tag -- Metadata tags
In addition, there is a high level interface for interacting with EC2
servers and volumes, including file transfer and remote shell facilities:
VM::EC2::Staging::Manager -- Manage a set of servers and volumes.
VM::EC2::Staging::Server -- A staging server, with remote shell and file transfer
facilities.
VM::EC2::Staging::Volume -- A staging volume with the ability to copy itself between
availability zones and regions.
and a few specialty classes:
VM::EC2::Security::Token -- Temporary security tokens for granting EC2 access to
non-AWS account holders.
VM::EC2::Security::Credentials -- Credentials for use by temporary account holders.
VM::EC2::Security::Policy -- Policies that restrict what temporary account holders
can do with EC2 resources.
VM::EC2::Security::FederatedUser -- Account name information for temporary account holders.
Lastly, there are several utility classes:
VM::EC2::Generic -- Base class for all AWS objects
VM::EC2::Error -- Error messages
VM::EC2::Dispatch -- Maps AWS XML responses onto perl object classes
VM::EC2::ReservationSet -- Hidden class used for describe_instances() request;
The reservation Ids are copied into the Instance
object.
There is also a high-level API called "VM::EC2::Staging::Manager" for
managing groups of staging servers and volumes which greatly
simplifies the task of creating and updating instances that mount
multiple volumes. The API also provides a one-line command for
migrating EBS-backed AMIs from one zone to another. See
L<VM::EC2::Staging::Manager>.
The interface provided by these modules is based on that described at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/. The
following caveats apply:
1) Not all of the Amazon API is currently implemented. Specifically,
a handful calls dealing with cluster management and VM importing
are missing. See L</MISSING METHODS> for a list of all the
unimplemented API calls. Volunteers to fill in these gaps are
most welcome!
2) For consistency with common Perl coding practices, method calls
are lowercase and words in long method names are separated by
underscores. The Amazon API prefers mixed case. So in the Amazon
API the call to fetch instance information is "DescribeInstances",
while in VM::EC2, the method is "describe_instances". To avoid
annoyance, if you use the mixed case form for a method name, the
Perl autoloader will automatically translate it to underscores for
you, and vice-versa; this means you can call either
$ec2->describe_instances() or $ec2->DescribeInstances().
3) Named arguments passed to methods are all lowercase, use
underscores to separate words and start with hyphens.
In other words, if the AWS API calls for an argument named
"InstanceId" to be passed to the "DescribeInstances" call, then
the corresponding Perl function will look like:
$instance = $ec2->describe_instances(-instance_id=>'i-12345')
In most cases automatic case translation will be performed for you
on arguments. So in the previous example, you could use
-InstanceId as well as -instance_id. The exception
is when an absurdly long argument name was replaced with an
abbreviated one as described below. In this case, you must use
the documented argument name.
In a small number of cases, when the parameter name was absurdly
long, it has been abbreviated. For example, the
"Placement.AvailabilityZone" parameter has been represented as
-placement_zone and not -placement_availability_zone. See the
documentation for these cases.
4) For each of the describe_foo() methods (where "foo" is a type of
resource such as "instance"), you can fetch the resource by using
their IDs either with the long form:
$ec2->describe_foo(-foo_id=>['a','b','c']),
or a shortcut form:
$ec2->describe_foo('a','b','c');
Both forms are listed in the headings in the documentation.
5) When the API calls for a list of arguments named Arg.1, Arg.2,
then the Perl interface allows you to use an anonymous array for
the consecutive values. For example to call describe_instances()
with multiple instance IDs, use:
@i = $ec2->describe_instances(-instance_id=>['i-12345','i-87654'])
6) All Filter arguments are represented as a -filter argument whose value is
an anonymous hash:
@i = $ec2->describe_instances(-filter=>{architecture=>'i386',
'tag:Name' =>'WebServer'})
If there are no other arguments you wish to pass, you can omit the
-filter argument and just pass a hashref:
@i = $ec2->describe_instances({architecture=>'i386',
'tag:Name' =>'WebServer'})
For any filter, you may represent multiple OR arguments as an arrayref:
@i = $ec2->describe-instances({'instance-state-name'=>['stopped','terminated']})
When adding or removing tags, the -tag argument uses the same syntax.
7) The tagnames of each XML object returned from AWS are converted into methods
with the same name and typography. So the <privateIpAddress> tag in a
DescribeInstancesResponse, becomes:
$instance->privateIpAddress
You can also use the more Perlish form -- this is equivalent:
$instance->private_ip_address
Methods that correspond to complex objects in the XML hierarchy
return the appropriate Perl object. For example, an instance's
blockDeviceMapping() method returns an object of type
VM::EC2::BlockDevice::Mapping.
All objects have a fields() method that will return the XML
tagnames listed in the AWS specifications.
@fields = sort $instance->fields;
# 'amiLaunchIndex', 'architecture', 'blockDeviceMapping', ...
8) Whenever an object has a unique ID, string overloading is used so that
the object interpolates the ID into the string. For example, when you
print a VM::EC2::Volume object, or use it in another string context,
then it will appear as the string "vol-123456". Nevertheless, it will
continue to be usable for method calls.
($v) = $ec2->describe_volumes();
print $v,"\n"; # prints as "vol-123456"
$zone = $v->availabilityZone; # acts like an object
9) Many objects have convenience methods that invoke the AWS API on your
behalf. For example, instance objects have a current_status() method that returns
the run status of the object, as well as start(), stop() and terminate()
methods that control the instance's lifecycle.
if ($instance->current_status eq 'running') {
$instance->stop;
}
10) Calls to AWS that have failed for one reason or another (invalid
arguments, communications problems, service interruptions) will
return undef and set the VM::EC2->is_error() method to true. The
error message and its code can then be recovered by calling
VM::EC2->error.
$i = $ec2->describe_instance('i-123456');
unless ($i) {
warn 'Got no instance. Message was: ',$ec2->error;
}
You may also elect to raise an exception when an error occurs.
See the new() method for details.
=head1 EXAMPLE SCRIPT
The script sync_to_snapshot.pl, distributed with this module,
illustrates a relatively complex set of steps on EC2 that does
something useful. Given a list of directories or files on the local
filesystem it copies the files into an EBS snapshot with the desired
name by executing the following steps:
1. Provisions a new EBS volume on EC2 large enough to hold the data.
2. Spins up a staging instance to manage the network transfer of data
from the local machine to the staging volume.
3. Creates a temporary ssh keypair and a security group that allows an
rsync-over-ssh.
4. Formats and mounts the volume if necessary.
5. Initiates an rsync-over-ssh for the designated files and
directories.
6. Unmounts and snapshots the volume.
7. Cleans up.
If a snapshot of the same name already exists, then it is used to
create the staging volume, enabling network-efficient synchronization
of the files. A snapshot tag named "Version" is incremented each time
you synchronize.
=head1 CORE METHODS
This section describes the VM::EC2 constructor, accessor methods, and
methods relevant to error handling.
=cut
use strict;
use LWP::UserAgent;
use HTTP::Request::Common;
use MIME::Base64 qw(encode_base64 decode_base64);
use Digest::SHA qw(hmac_sha256 sha1_hex);
use POSIX 'strftime';
use URI;
use URI::Escape;
use VM::EC2::Dispatch;
use VM::EC2::Error;
use Carp 'croak','carp';
our $VERSION = '1.23';
our $AUTOLOAD;
our @CARP_NOT = qw(VM::EC2::Image VM::EC2::Volume
VM::EC2::Snapshot VM::EC2::Instance
VM::EC2::ReservedInstance);
# hard-coded timeout for several wait_for_terminal_state() calls.
use constant WAIT_FOR_TIMEOUT => 600;
sub AUTOLOAD {
my $self = shift;
my ($pack,$func_name) = $AUTOLOAD=~/(.+)::([^:]+)$/;
return if $func_name eq 'DESTROY';
my $proper = VM::EC2->canonicalize($func_name);
$proper =~ s/^-//;
if ($self->can($proper)) {
eval "sub $pack\:\:$func_name {shift->$proper(\@_)}";
$self->$func_name(@_);
} else {
croak "Can't locate object method \"$func_name\" via package \"$pack\"";
}
}
=head2 $ec2 = VM::EC2->new(-access_key=>$id,-secret_key=>$key,-endpoint=>$url)
Create a new Amazon access object. Required arguments are:
-access_key Access ID for an authorized user
-secret_key Secret key corresponding to the Access ID
-security_token Temporary security token obtained through a call to the
AWS Security Token Service
-endpoint The URL for making API requests
-region The region to receive the API requests
-raise_error If true, throw an exception.
-print_error If true, print errors to STDERR.
One or more of -access_key or -secret_key can be omitted if the
environment variables EC2_ACCESS_KEY and EC2_SECRET_KEY are
defined. If no endpoint is specified, then the environment variable
EC2_URL is consulted; otherwise the generic endpoint
http://ec2.amazonaws.com/ is used. You can also select the endpoint by
specifying one of the Amazon regions, such as "us-west-2", with the
-region argument. The endpoint specified by -region will override
-endpoint.
-security_token is used in conjunction with temporary security tokens
returned by $ec2->get_federation_token() and $ec2->get_session_token()
to grant restricted, time-limited access to some or all your EC2
resources to users who do not have access to your account. If you pass
either a VM::EC2::Security::Token object, or the
VM::EC2::Security::Credentials object contained within the token
object, then new() does not need the -access_key or -secret_key
arguments. You may also pass a session token string scalar to
-security_token, in which case you must also pass the access key ID
and secret keys generated at the same time the session token was
created. See
http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/UsingIAM.html
and L</AWS SECURITY TOKENS>.
To use a Eucalyptus cloud, please provide the appropriate endpoint
URL.
By default, when the Amazon API reports an error, such as attempting
to perform an invalid operation on an instance, the corresponding
method will return empty and the error message can be recovered from
$ec2->error(). However, if you pass -raise_error=>1 to new(), the module
will instead raise a fatal error, which you can trap with eval{} and
report with $@:
eval {
$ec2->some_dangerous_operation();
$ec2->another_dangerous_operation();
};
print STDERR "something bad happened: $@" if $@;
The error object can be retrieved with $ec2->error() as before.
=cut
sub new {
my $self = shift;
my %args = @_;
my ($id,$secret,$token);
if (ref $args{-security_token} && $args{-security_token}->can('access_key_id')) {
$id = $args{-security_token}->accessKeyId;
$secret = $args{-security_token}->secretAccessKey;
$token = $args{-security_token}->sessionToken;
}
$id ||= $args{-access_key} || $ENV{EC2_ACCESS_KEY}
or croak "Please provide -access_key parameter or define environment variable EC2_ACCESS_KEY";
$secret ||= $args{-secret_key} || $ENV{EC2_SECRET_KEY}
or croak "Please provide -secret_key or define environment variable EC2_SECRET_KEY";
$token ||= $args{-security_token};
my $endpoint_url = $args{-endpoint} || $ENV{EC2_URL} || 'http://ec2.amazonaws.com/';
$endpoint_url .= '/' unless $endpoint_url =~ m!/$!;
$endpoint_url = "http://".$endpoint_url unless $endpoint_url =~ m!https?://!;
my $raise_error = $args{-raise_error};
my $print_error = $args{-print_error};
my $obj = bless {
id => $id,
secret => $secret,
security_token => $token,
endpoint => $endpoint_url,
idempotent_seed => sha1_hex(rand()),
raise_error => $raise_error,
print_error => $print_error,
},ref $self || $self;
if ($args{-region}) {
my $region = eval{$obj->describe_regions($args{-region})};
my $endpoint = $region ? $region->regionEndpoint :"ec2.$args{-region}.amazonaws.com";
$obj->endpoint($endpoint);
}
return $obj;
}
=head2 $access_key = $ec2->access_key([$new_access_key])
Get or set the ACCESS KEY. In this and all similar get/set methods,
call the method with no arguments to get the current value, and with a
single argument to change the value:
$current_key = $ec2->access_key;
$ec2->access_key('XYZZY');
In the case of setting the value, these methods will return the old
value as their result:
$old_key = $ec2->access_key($new_key);
=cut
sub access_key {shift->id(@_)}
sub id {
my $self = shift;
my $d = $self->{id};
$self->{id} = shift if @_;
$d;
}
=head2 $secret = $ec2->secret([$new_secret])
Get or set the SECRET KEY
=cut
sub secret {
my $self = shift;
my $d = $self->{secret};
$self->{secret} = shift if @_;
$d;
}
=head2 $secret = $ec2->security_token([$new_token])
Get or set the temporary security token. See L</AWS SECURITY TOKENS>.
=cut
sub security_token {
my $self = shift;
my $d = $self->{security_token};
$self->{security_token} = shift if @_;
$d;
}
=head2 $endpoint = $ec2->endpoint([$new_endpoint])
Get or set the ENDPOINT URL.
=cut
sub endpoint {
my $self = shift;
my $d = $self->{endpoint};
if (@_) {
my $new_endpoint = shift;
$new_endpoint = 'http://'.$new_endpoint
unless $new_endpoint =~ /^https?:/;
$self->{endpoint} = $new_endpoint;
}
$d;
}
=head2 $region = $ec2->region([$new_region])
Get or set the EC2 region manipulated by this module. This has the side effect
of changing the endpoint.
=cut
sub region {
my $self = shift;
my $d = $self->{endpoint};
$d =~ s!^https?://!!;
$d =~ s!/$!!;
my @regions = $self->describe_regions;
my ($current_region) = grep {$_->regionEndpoint eq $d} @regions;
if (@_) {
my $new_region = shift;
my ($region) = grep {/$new_region/} @regions;
$region or croak "unknown region $new_region";
$self->endpoint($region->regionEndpoint);
}
return $current_region;
}
=head2 $ec2->raise_error($boolean)
Change the handling of error conditions. Pass a true value to cause
Amazon API errors to raise a fatal error. Pass false to make methods
return undef. In either case, you can detect the error condition
by calling is_error() and fetch the error message using error(). This
method will also return the current state of the raise error flag.
=cut
sub raise_error {
my $self = shift;
my $d = $self->{raise_error};
$self->{raise_error} = shift if @_;
$d;
}
=head2 $ec2->print_error($boolean)
Change the handling of error conditions. Pass a true value to cause
Amazon API errors to print error messages to STDERR. Pass false to
cancel this behavior.
=cut
sub print_error {
my $self = shift;
my $d = $self->{print_error};
$self->{print_error} = shift if @_;
$d;
}
=head2 $boolean = $ec2->is_error
If a method fails, it will return undef. However, some methods, such
as describe_images(), will also return undef if no resources matches
your search criteria. Call is_error() to distinguish the two
eventualities:
@images = $ec2->describe_images(-owner=>'29731912785');
unless (@images) {
die "Error: ",$ec2->error if $ec2->is_error;
print "No appropriate images found\n";
}
=cut
sub is_error {
defined shift->error();
}
=head2 $err = $ec2->error
If the most recently-executed method failed, $ec2->error() will return
the error code and other descriptive information. This method will
return undef if the most recently executed method was successful.
The returned object is actually an AWS::Error object, which
has two methods named code() and message(). If used in a string
context, its operator overloading returns the composite string
"$message [$code]".
=cut
sub error {
my $self = shift;
my $d = $self->{error};
$self->{error} = shift if @_;
$d;
}
=head2 $err = $ec2->error_str
Same as error() except it returns the string representation, not the
object. This works better in debuggers and exception handlers.
=cut
sub error_str {
my $e = shift->{error};
$e ||= '';
return "$e";
}
=head2 $account_id = $ec2->account_id
Looks up the account ID corresponding to the credentials provided when
the VM::EC2 instance was created. The way this is done is to fetch the
"default" security group, which is guaranteed to exist, and then
return its groupId field. The result is cached so that subsequent
accesses are fast.
=head2 $account_id = $ec2->userId
Same as above, for convenience.
=cut
sub account_id {
my $self = shift;
return $self->{account_id} if exists $self->{account_id};
my $sg = $self->describe_security_groups(-group_name=>'default') or return;
return $self->{account_id} ||= $sg->ownerId;
}
sub userId { shift->account_id }
=head2 $new_ec2 = $ec2->clone
This method creates an identical copy of the EC2 object. It is used
occasionally internally for creating an EC2 object in a different AWS
region:
$singapore = $ec2->clone;
$singapore->region('ap-souteast-1');
=cut
sub clone {
my $self = shift;
my %contents = %$self;
return bless \%contents,ref $self;
}
=head1 EC2 REGIONS AND AVAILABILITY ZONES
This section describes methods that allow you to fetch information on
EC2 regions and availability zones. These methods return objects of
type L<VM::EC2::Region> and L<VM::EC2::AvailabilityZone>.
=head2 @regions = $ec2->describe_regions(@list)
=head2 @regions = $ec2->describe_regions(-region_name=>\@list)
Describe regions and return a list of VM::EC2::Region objects. Call
with no arguments to return all regions. You may provide a list of
regions in either of the two forms shown above in order to restrict
the list returned. Glob-style wildcards, such as "*east") are allowed.
=cut
sub describe_regions {
my $self = shift;
my %args = $self->args('-region_name',@_);
my @params = $self->list_parm('RegionName',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeRegions',@params);
}
=head2 describe_availability_zones(@names)
@zones = $ec2->describe_availability_zones(@names)
=head2 describe_availability_zones(-zone_name=>\@names,-filter=>\%filters)
@zones = $ec2->describe_availability_zones(-zone_name=>\@names,-filter=>\%filters)
Describe availability zones and return a list of
VM::EC2::AvailabilityZone objects. Call with no arguments to return
all availability regions. You may provide a list of zones in either
of the two forms shown above in order to restrict the list
returned. Glob-style wildcards, such as "*east") are allowed.
If you provide a single argument consisting of a hashref, it is
treated as a -filter argument. In other words:
$ec2->describe_availability_zones({state=>'available'})
is equivalent to
$ec2->describe_availability_zones(-filter=>{state=>'available'})
Availability zone filters are described at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeAvailabilityZones.html
=cut
sub describe_availability_zones {
my $self = shift;
my %args = $self->args('-zone_name',@_);
my @params = $self->list_parm('ZoneName',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeAvailabilityZones',@params);
}
=head1 EC2 INSTANCES
The methods in this section allow you to retrieve information about
EC2 instances, launch new instances, control the instance lifecycle
(e.g. starting and stopping them), and fetching the console output
from instances.
The primary object manipulated by these methods is
L<VM::EC2::Instance>. Please see the L<VM::EC2::Instance> manual page
for additional methods that allow you to attach and detach volumes,
modify an instance's attributes, and convert instances into images.
=head2 @instances = $ec2->describe_instances(@instance_ids)
=head2 @instances = $ec2->describe_instances(\%filters)
=head2 @instances = $ec2->describe_instances(-instance_id=>\@ids,-filter=>\%filters)
Return a series of VM::EC2::Instance objects. Optional arguments are:
-instance_id ID of the instance(s) to return information on.
This can be a string scalar, or an arrayref.
-filter Tags and other filters to apply.
The filter argument is a hashreference in which the keys are the
filter names, and the values are the match strings. Some filters
accept wildcards.
A typical filter example:
$ec2->describe_instances(
-filter => {'block-device-mapping.device-name'=>'/dev/sdh',
'architecture' => 'i386',
'tag:Role' => 'Server'
});
You may omit the -filter argument name if there are no other arguments:
$ec2->describe_instances({'block-device-mapping.device-name'=>'/dev/sdh',
'architecture' => 'i386',
'tag:Role' => 'Server'});
There are a large number of filters, which are listed in full at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstances.html.
Here is a alpha-sorted list of filter names: architecture,
availability-zone, block-device-mapping.attach-time,
block-device-mapping.delete-on-termination,
block-device-mapping.device-name, block-device-mapping.status,
block-device-mapping.volume-id, client-token, dns-name, group-id,
group-name, hypervisor, image-id, instance-id, instance-lifecycle,
instance-state-code, instance-state-name, instance-type,
instance.group-id, instance.group-name, ip-address, kernel-id,
key-name, launch-index, launch-time, monitoring-state, owner-id,
placement-group-name, platform, private-dns-name, private-ip-address,
product-code, ramdisk-id, reason, requester-id, reservation-id,
root-device-name, root-device-type, source-dest-check,
spot-instance-request-id, state-reason-code, state-reason-message,
subnet-id, tag-key, tag-value, tag:key, virtualization-type, vpc-id.
Note that the objects returned from this method are the instances
themselves, and not a reservation set. The reservation ID can be
retrieved from each instance by calling its reservationId() method.
=cut
sub describe_instances {
my $self = shift;
my %args = $self->args('-instance_id',@_);
my @params = $self->list_parm('InstanceId',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeInstances',@params);
}
=head2 @i = $ec2->run_instances($ami_id)
=head2 @i = $ec2->run_instances(-image_id=>$id,%other_args)
This method will provision and launch one or more instances given an
AMI ID. If successful, the method returns a series of
VM::EC2::Instance objects.
If called with a single argument this will be interpreted as the AMI
to launch, and all other arguments will take their
defaults. Otherwise, the arguments will be taken as a
-parameter=>$argument list.
=over 4
=item Required arguments:
-image_id ID of an AMI to launch
=item Optional arguments:
-min_count Minimum number of instances to launch [1]
-max_count Maximum number of instances to launch [1]
-key_name Name of the keypair to use
-security_group_id Security group ID to use for this instance.
Use an arrayref for multiple group IDs
-security_group Security group name to use for this instance.
Use an arrayref for multiple values.
-user_data User data to pass to the instances. Do NOT base64
encode this. It will be done for you.
-instance_type Type of the instance to use. See below for a
list.
-availability_zone The availability zone you want to launch the
instance into. Call $ec2->regions for a list.
-zone Short version of -availability_aone.
-placement_zone Deprecated version of -availability_zone.
-placement_group An existing placement group to launch the
instance into. Applicable to cluster instances
only.
-placement_tenancy Specify 'dedicated' to launch the instance on a
dedicated server. Only applicable for VPC
instances.
-kernel_id ID of the kernel to use for the instances,
overriding the kernel specified in the image.
-ramdisk_id ID of the ramdisk to use for the instances,
overriding the ramdisk specified in the image.
-block_devices Specify block devices to map onto the instances,
overriding the values specified in the image.
See below for the syntax of this argument.
-block_device_mapping Alias for -block_devices.
-monitoring Pass a true value to enable detailed monitoring.
-subnet_id ID of the subnet to launch the instance
into. Only applicable for VPC instances.
-termination_protection Pass true to lock the instance so that it
cannot be terminated using the API. Use
modify_instance() to unset this if youu wish to
terminate the instance later.
-disable_api_termination -- Same as above.
-shutdown_behavior Pass "stop" (the default) to stop the instance
and save its disk state when "shutdown" is called
from within the instance. Stopped instances can
be restarted later. Pass "terminate" to
instead terminate the instance and discard its
state completely.
-instance_initiated_shutdown_behavior -- Same as above.
-private_ip_address Assign the instance to a specific IP address
from a VPC subnet (VPC only).
-client_token Unique identifier that you can provide to ensure
idempotency of the request. You can use
$ec2->token() to generate a suitable identifier.
See http://docs.amazonwebservices.com/AWSEC2/
latest/UserGuide/Run_Instance_Idempotency.html
-network_interfaces A single network interface specification string
or a list of them as an array reference (VPC only).
These are described in more detail below.
-iam_arn The Amazon resource name (ARN) of the IAM Instance Profile (IIP)
to associate with the instances.
-iam_name The name of the IAM instance profile (IIP) to associate with the
instances.
-ebs_optimized Boolean. If true, create an EBS-optimized instance
(valid only for certain instance types.
=item Instance types
The following is the list of instance types currently allowed by
Amazon:
m1.small c1.medium m2.xlarge cc1.4xlarge cg1.4xlarge t1.micro
m1.large c1.xlarge m2.2xlarge
m1.xlarge m2.4xlarge
=item Block device syntax
The syntax of -block_devices is identical to what is used by the
ec2-run-instances command-line tool. Borrowing from the manual page of
that tool:
The format is '<device>=<block-device>', where 'block-device' can be one of the
following:
- 'none': indicates that a block device that would be exposed at the
specified device should be suppressed. For example: '/dev/sdb=none'
- 'ephemeral[0-3]': indicates that the Amazon EC2 ephemeral store
(instance local storage) should be exposed at the specified device.
For example: '/dev/sdc=ephemeral0'.
- 'vol-12345678': A volume ID will attempt to attach the given volume to the
instance, contingent on volume state and availability zone.
- 'none': Suppress this block device, even if it is mapped in the AMI.
- '[<snapshot-id>][:<size>[:<delete-on-termination>[:<volume-type>[:<iops>]]]]':
indicates that an Amazon EBS volume, created from the specified Amazon EBS
snapshot, should be exposed at the specified device. The following
combinations are supported:
- '<snapshot-id>': the ID of an Amazon EBS snapshot, which must
be owned by or restorable by the caller. May be left out if a
<size> is specified, creating an empty Amazon EBS volume of
the specified size.
- '<size>': the size (GiBs) of the Amazon EBS volume to be
created. If a snapshot was specified, this may not be smaller
than the size of the snapshot itself.
- '<delete-on-termination>': indicates whether the Amazon EBS
volume should be deleted on instance termination. If not
specified, this will default to 'true' and the volume will be
deleted.
- '<volume-type>': The volume type. One of "standard" or "io1".
- '<iops>': The number of I/O operations per second (IOPS) that
the volume suports. A number between 100 to 2000. Only valid
for volumes of type "io1".
Examples: -block_devices => '/dev/sdb=snap-7eb96d16'
-block_devices => '/dev/sdc=snap-7eb96d16:80:false'
-block_devices => '/dev/sdd=:120'
-block_devices => '/dev/sdc=:120:true:io1:500'
To provide multiple mappings, use an array reference. In this example,
we launch two 'm1.small' instance in which /dev/sdb is mapped to
ephemeral storage and /dev/sdc is mapped to a new 100 G EBS volume:
@i=$ec2->run_instances(-image_id => 'ami-12345',
-min_count => 2,
-block_devices => ['/dev/sdb=ephemeral0',
'/dev/sdc=:100:true']
)
=item Network interface syntax
Each instance has a single primary network interface and private IP
address that is ordinarily automatically assigned by Amazon. When you
are running VPC instances, however, you can add additional elastic
network interfaces (ENIs) to the instance and add secondary private IP
addresses to one or more of these ENIs. ENIs can exist independently
of instances, and be detached and reattached in much the same way as
EBS volumes. This is explained in detail at
http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/using-instance-addressing.html.
The network configuration can be specified using the
-network_interface parameter:
-network_interfaces => ['eth0=10.10.0.12:subnet-1234567:sg-1234567:true:My Custom Eth0',
'eth1=10.10.1.12,10.10.1.13:subnet-999999:sg-1234567:true:My Custom Eth1',
The format is '<device>=<specification>'. The device is an ethernet
interface name such as eth0, eth1, eth2, etc. The specification has up
to five fields, each separated by the ":" character. All fields are
optional and can be left blank. If missing, AWS will choose a default.
10.10.1.12,10.10.1.13:subnet-999999:sg-1234567:true:My Custom Eth1
B<1. IP address(es)>: A single IP address in standard dot form, or a
list of IP addresses separated by commas. The first address in the
list will become the primary private IP address for the
interface. Subsequent addresses will become secondary private
addresses. You may specify "auto" or leave the field blank to have AWS
choose an address automatically from within the subnetwork. To
allocate several secondary IP addresses and have AWS pick the
addresses automatically, give the count of secondary addresses you
wish to allocate as an integer following the primary IP address. For
example, "auto,3" will allocate an automatic primary IP address and
three automatic secondary addresses, while "10.10.1.12,3" will force
the primary address to be 10.10.1.12 and create three automatic
secondary addresses.
B<2. Subnetwork ID>: The ID of the VPC subnetwork in which the ENI
resides. An instance may have several ENIs associated with it, and
each ENI may be attached to a different subnetwork.
B<3. Security group IDs>: A comma-delimited list of the security group
IDs to associate with this ENI.
B<4. DeleteOnTerminate>: True if this ENI should be automatically
deleted when the instance terminates.
B<5. Description>: A human-readable description of the ENI.
As an alternative syntax, you may specify the ID of an existing ENI in
lieu of the primary IP address and other fields. The ENI will be
attached to the instance if its permissions allow:
-network_interfaces => 'eth0=eni-123456'
=item Return value
On success, this method returns a list of VM::EC2::Instance
objects. If called in a scalar context AND only one instance was
requested, it will return a single instance object (rather than
returning a list of size one which is then converted into numeric "1",
as would be the usual Perl behavior).
Note that this behavior is different from the Amazon API, which
returns a ReservationSet. In this API, ask the instances for the
the reservation, owner, requester, and group information using
reservationId(), ownerId(), requesterId() and groups() methods.
=item Tips
1. If you have a VM::EC2::Image object returned from
Describe_images(), you may run it using run_instances():
my $image = $ec2->describe_images(-image_id => 'ami-12345');
$image->run_instances( -min_count => 10,
-block_devices => ['/dev/sdb=ephemeral0',
'/dev/sdc=:100:true']
)
2. It may take a short while for a newly-launched instance to be
returned by describe_instances(). You may need to sleep for 1-2 seconds
before current_status() returns the correct value.
3. Each instance object has a current_status() method which will
return the current run state of the instance. You may poll this
method to wait until the instance is running:
my $instance = $ec2->run_instances(...);
sleep 1;
while ($instance->current_status ne 'running') {
sleep 5;
}
4. The utility method wait_for_instances() will wait until all
passed instances are in the 'running' or other terminal state.
my @instances = $ec2->run_instances(...);
$ec2->wait_for_instances(@instances);
=back
=cut
sub run_instances {
my $self = shift;
my %args = $self->args('-image_id',@_);
$args{-image_id} or croak "run_instances(): -image_id argument missing";
$args{-min_count} ||= 1;
$args{-max_count} ||= $args{-min_count};
$args{-availability_zone} ||= $args{-zone};
$args{-availability_zone} ||= $args{-placement_zone};
my @p = map {$self->single_parm($_,\%args) }
qw(ImageId MinCount MaxCount KeyName KernelId RamdiskId PrivateIpAddress
InstanceInitiatedShutdownBehavior ClientToken SubnetId InstanceType);
push @p,map {$self->list_parm($_,\%args)} qw(SecurityGroup SecurityGroupId);
push @p,('UserData' =>encode_base64($args{-user_data},'')) if $args{-user_data};
push @p,('Placement.AvailabilityZone'=>$args{-availability_zone}) if $args{-availability_zone};
push @p,('Placement.GroupName'=>$args{-placement_group}) if $args{-placement_group};
push @p,('Placement.Tenancy'=>$args{-tenancy}) if $args{-placement_tenancy};
push @p,('Monitoring.Enabled' =>'true') if $args{-monitoring};
push @p,('DisableApiTermination'=>'true') if $args{-termination_protection};
push @p,('EbsOptimized'=>'true') if $args{-ebs_optimized};
push @p,('InstanceInitiatedShutdownBehavior'=>$args{-shutdown_behavior}) if $args{-shutdown_behavior};
push @p,$self->block_device_parm($args{-block_devices}||$args{-block_device_mapping});
push @p,$self->network_interface_parm(\%args);
push @p,$self->iam_parm(\%args);
return $self->call('RunInstances',@p);
}
=head2 @s = $ec2->start_instances(@instance_ids)
=head2 @s = $ec2->start_instances(-instance_id=>\@instance_ids)
Start the instances named by @instance_ids and return one or more
VM::EC2::Instance::State::Change objects.
To wait for the all the instance ids to reach their final state
("running" unless an error occurs), call wait_for_instances().
Example:
# find all stopped instances
@instances = $ec2->describe_instances(-filter=>{'instance-state-name'=>'stopped'});
# start them
$ec2->start_instances(@instances)
# pause till they are running (or crashed)
$ec2->wait_for_instances(@instances)
You can also start an instance by calling the object's start() method:
$instances[0]->start('wait'); # start instance and wait for it to
# be running
The objects returned by calling start_instances() indicate the current
and previous states of the instance. The previous state is typically
"stopped" and the current state is usually "pending." This information
is only current to the time that the start_instances() method was called.
To get the current run state of the instance, call its status()
method:
die "ouch!" unless $instances[0]->current_status eq 'running';
=cut
sub start_instances {
my $self = shift;
my @instance_ids = $self->instance_parm(@_)
or croak "usage: start_instances(\@instance_ids)";
my $c = 1;
my @params = map {'InstanceId.'.$c++,$_} @instance_ids;
return $self->call('StartInstances',@params);
}
=head2 @s = $ec2->stop_instances(@instance_ids)
=head2 @s = $ec2->stop_instances(-instance_id=>\@instance_ids,-force=>1)
Stop the instances named by @instance_ids and return one or more
VM::EC2::Instance::State::Change objects. In the named parameter
version of this method, you may optionally provide a -force argument,
which if true, forces the instance to halt without giving it a chance
to run its shutdown procedure (the equivalent of pulling a physical
machine's plug).
To wait for instances to reach their final state, call
wait_for_instances().
Example:
# find all running instances
@instances = $ec2->describe_instances(-filter=>{'instance-state-name'=>'running'});
# stop them immediately and wait for confirmation
$ec2->stop_instances(-instance_id=>\@instances,-force=>1);
$ec2->wait_for_instances(@instances);
You can also stop an instance by calling the object's start() method:
$instances[0]->stop('wait'); # stop first instance and wait for it to
# stop completely
=cut
sub stop_instances {
my $self = shift;
my (@instance_ids,$force);
if ($_[0] =~ /^-/) {
my %argv = @_;
@instance_ids = ref $argv{-instance_id} ?
@{$argv{-instance_id}} : $argv{-instance_id};
$force = $argv{-force};
} else {
@instance_ids = @_;
}
@instance_ids or croak "usage: stop_instances(\@instance_ids)";
my $c = 1;
my @params = map {'InstanceId.'.$c++,$_} @instance_ids;
push @params,Force=>1 if $force;
return $self->call('StopInstances',@params);
}
=head2 @s = $ec2->terminate_instances(@instance_ids)
=head2 @s = $ec2->terminate_instances(-instance_id=>\@instance_ids)
Terminate the instances named by @instance_ids and return one or more
VM::EC2::Instance::State::Change objects. This method will fail
for any instances whose termination protection field is set.
To wait for the all the instances to reach their final state, call
wait_for_instances().
Example:
# find all instances tagged as "Version 0.5"
@instances = $ec2->describe_instances({'tag:Version'=>'0.5'});
# terminate them
$ec2->terminate_instances(@instances);
You can also terminate an instance by calling its terminate() method:
$instances[0]->terminate;
=cut
sub terminate_instances {
my $self = shift;
my @instance_ids = $self->instance_parm(@_)
or croak "usage: start_instances(\@instance_ids)";
my $c = 1;
my @params = map {'InstanceId.'.$c++,$_} @instance_ids;
return $self->call('TerminateInstances',@params);
}
=head2 @s = $ec2->reboot_instances(@instance_ids)
=head2 @s = $ec2->reboot_instances(-instance_id=>\@instance_ids)
Reboot the instances named by @instance_ids and return one or more
VM::EC2::Instance::State::Change objects.
To wait for the all the instances to reach their final state, call
wait_for_instances().
You can also reboot an instance by calling its terminate() method:
$instances[0]->reboot;
=cut
sub reboot_instances {
my $self = shift;
my @instance_ids = $self->instance_parm(@_)
or croak "Usage: reboot_instances(\@instance_ids)";
my $c = 1;
my @params = map {'InstanceId.'.$c++,$_} @instance_ids;
return $self->call('RebootInstances',@params);
}
=head2 $boolean = $ec2->confirm_product_instance($instance_id,$product_code)
Return "true" if the instance indicated by $instance_id is associated
with the given product code.
=cut
sub confirm_product_instance {
my $self = shift;
@_ == 1 or croak "Usage: confirm_product_instance(\$instance_id,\$product_code)";
my ($instance_id,$product_code) = @_;
my @params = (InstanceId=>$instance_id,
ProductCode=>$product_code);
return $self->call('ConfirmProductInstance',@params);
}
=head2 $password_data = $ec2->get_password_data($instance_id);
=head2 $password_data = $ec2->get_password_data(-instance_id=>$id);
For Windows instances, get the administrator's password as a
L<VM::EC2::Instance::PasswordData> object.
=cut
sub get_password_data {
my $self = shift;
my %args = $self->args(-instance_id=>@_);
$args{-instance_id} or croak "Usage: get_password_data(-instance_id=>\$id)";
my @params = $self->single_parm('InstanceId',\%args);
return $self->call('GetPasswordData',@params);
}
=head2 $output = $ec2->get_console_output(-instance_id=>'i-12345')
=head2 $output = $ec2->get_console_output('i-12345');
Return the console output of the indicated instance. The output is
actually a VM::EC2::ConsoleOutput object, but it is
overloaded so that when treated as a string it will appear as a
large text string containing the console output. When treated like an
object it provides instanceId() and timestamp() methods.
=cut
sub get_console_output {
my $self = shift;
my %args = $self->args(-instance_id=>@_);
$args{-instance_id} or croak "Usage: get_console_output(-instance_id=>\$id)";
my @params = $self->single_parm('InstanceId',\%args);
return $self->call('GetConsoleOutput',@params);
}
=head2 @monitoring_state = $ec2->monitor_instances(@list_of_instanceIds)
=head2 @monitoring_state = $ec2->monitor_instances(-instance_id=>\@instanceIds)
This method enables monitoring for the listed instances and returns a
list of VM::EC2::Instance::MonitoringState objects. You can
later use these objects to activate and inactivate monitoring.
=cut
sub monitor_instances {
my $self = shift;
my %args = $self->args('-instance_id',@_);
my @params = $self->list_parm('InstanceId',\%args);
return $self->call('MonitorInstances',@params);
}
=head2 @monitoring_state = $ec2->unmonitor_instances(@list_of_instanceIds)
=head2 @monitoring_state = $ec2->unmonitor_instances(-instance_id=>\@instanceIds)
This method disables monitoring for the listed instances and returns a
list of VM::EC2::Instance::MonitoringState objects. You can
later use these objects to activate and inactivate monitoring.
=cut
sub unmonitor_instances {
my $self = shift;
my %args = $self->args('-instance_id',@_);
my @params = $self->list_parm('InstanceId',\%args);
return $self->call('UnmonitorInstances',@params);
}
=head2 $meta = VM::EC2->instance_metadata
=head2 $meta = $ec2->instance_metadata
B<For use on running EC2 instances only:> This method returns a
VM::EC2::Instance::Metadata object that will return information about
the currently running instance using the HTTP:// metadata fields
described at
http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?instancedata-data-categories.html. This
is usually fastest way to get runtime information on the current
instance.
Note that this method can be called as either an instance or a class
method.
=cut
sub instance_metadata {
VM::EC2::Dispatch::load_module('VM::EC2::Instance::Metadata');
return VM::EC2::Instance::Metadata->new();
}
=head2 @data = $ec2->describe_instance_attribute($instance_id,$attribute)
This method returns instance attributes. Only one attribute can be
retrieved at a time. The following is the list of attributes that can be
retrieved:
instanceType -- scalar
kernel -- scalar
ramdisk -- scalar
userData -- scalar
disableApiTermination -- scalar
instanceInitiatedShutdownBehavior -- scalar
rootDeviceName -- scalar
blockDeviceMapping -- list of hashref
sourceDestCheck -- scalar
groupSet -- list of scalar
All of these values can be retrieved more conveniently from the
L<VM::EC2::Instance> object returned from describe_instances(), so
there is no attempt to parse the results of this call into Perl
objects. Therefore, some of the attributes, in particular
'blockDeviceMapping' will be returned as raw hashrefs.
=cut
sub describe_instance_attribute {
my $self = shift;
@_ == 2 or croak "Usage: describe_instance_attribute(\$instance_id,\$attribute_name)";
my ($instance_id,$attribute) = @_;
my @param = (InstanceId=>$instance_id,Attribute=>$attribute);
my $result = $self->call('DescribeInstanceAttribute',@param);
return $result && $result->attribute($attribute);
}
=head2 $boolean = $ec2->modify_instance_attribute($instance_id,-$attribute_name=>$value)
This method changes instance attributes. It can only be applied to stopped instances.
The following is the list of attributes that can be set:
-instance_type -- type of instance, e.g. "m1.small"
-kernel -- kernel id
-ramdisk -- ramdisk id
-user_data -- user data
-termination_protection -- true to prevent termination from the console
-disable_api_termination -- same as the above
-shutdown_behavior -- "stop" or "terminate"
-instance_initiated_shutdown_behavior -- same as above
-root_device_name -- root device name
-source_dest_check -- enable NAT (VPC only)
-group_id -- VPC security group
-block_devices -- Specify block devices to change
deleteOnTermination flag
-block_device_mapping -- Alias for -block_devices
Only one attribute can be changed in a single request. For example:
$ec2->modify_instance_attribute('i-12345',-kernel=>'aki-f70657b2');
The result code is true if the attribute was successfully modified,
false otherwise. In the latter case, $ec2->error() will provide the
error message.
The ability to change the deleteOnTermination flag for attached block devices
is not documented in the official Amazon API documentation, but appears to work.
The syntax is:
# turn on deleteOnTermination
$ec2->modify_instance_attribute(-block_devices=>'/dev/sdf=v-12345')
# turn off deleteOnTermination
$ec2->modify_instance_attribute(-block_devices=>'/dev/sdf=v-12345')
The syntax is slightly different from what is used by -block_devices
in run_instances(), and is "device=volumeId:boolean". Multiple block
devices can be specified using an arrayref.
=cut
sub modify_instance_attribute {
my $self = shift;
my $instance_id = shift or croak "Usage: modify_instance_attribute(\$instanceId,%param)";
my %args = @_;
my @param = (InstanceId=>$instance_id);
push @param,$self->value_parm($_,\%args) foreach
qw(InstanceType Kernel Ramdisk UserData DisableApiTermination
InstanceInitiatedShutdownBehavior SourceDestCheck);
push @param,$self->list_parm('GroupId',\%args);
push @param,('DisableApiTermination.Value'=>'true') if $args{-termination_protection};
push @param,('InstanceInitiatedShutdownBehavior.Value'=>$args{-shutdown_behavior}) if $args{-shutdown_behavior};
my $block_devices = $args{-block_devices} || $args{-block_device_mapping};
push @param,$self->block_device_parm($block_devices);
return $self->call('ModifyInstanceAttribute',@param);
}
=head2 $boolean = $ec2->reset_instance_attribute($instance_id,$attribute)
This method resets an attribute of the given instance to its default
value. Valid attributes are "kernel", "ramdisk" and
"sourceDestCheck". The result code is true if the reset was
successful.
=cut
sub reset_instance_attribute {
my $self = shift;
@_ == 2 or croak "Usage: reset_instance_attribute(\$instanceId,\$attribute_name)";
my ($instance_id,$attribute) = @_;
my %valid = map {$_=>1} qw(kernel ramdisk sourceDestCheck);
$valid{$attribute} or croak "attribute to reset must be one of 'kernel', 'ramdisk', or 'sourceDestCheck'";
return $self->call('ResetInstanceAttribute',InstanceId=>$instance_id,Attribute=>$attribute);
}
=head2 @status_list = $ec2->describe_instance_status(@instance_ids);
=head2 @status_list = $ec2->describe_instance_status(-instance_id=>\@ids,-filter=>\%filters,%other_args);
=head2 @status_list = $ec2->describe_instance_status(\%filters);
This method returns a list of VM::EC2::Instance::Status objects
corresponding to status checks and scheduled maintenance events on the
instances of interest. You may provide a list of instances to return
information on, a set of filters, or both.
The filters are described at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInstanceStatus.html. The
brief list is:
availability-zone, event.code, event.description, event.not-after,
event.not-before, instance-state-name, instance-state-code,
system-status.status, system-status.reachability,
instance-status.status, instance-status.reachability.
Request arguments are:
-instance_id Scalar or array ref containing the instance ID(s) to return
information about (optional).
-filter Filters to apply (optional).
-include_all_instances If true, include all instances, including those that are
stopped, pending and shutting down. Otherwise, returns
the status of running instances only.
-max_results An integer corresponding to the number of instance items
per response (must be greater than 5).
If -max_results is specified, then the call will return at most the
number of instances you requested. You may see whether there are additional
results by calling more_instance_status(), and then retrieve the next set of
results with additional call(s) to describe_instance_status():
my @results = $ec2->describe_instance_status(-max_results => 10);
do_something(\@results);
while ($ec2->more_instance_status) {
@results = $ec2->describe_instance_status;
do_something(\@results);
}
NOTE: As of 29 July 2012, passing -include_all_instances causes an EC2
"unknown parameter" error, indicating some mismatch between the
documented API and the actual one.
=cut
sub more_instance_status {
my $self = shift;
return $self->{instance_status_token} &&
!$self->{instance_status_stop};
}
sub describe_instance_status {
my $self = shift;
my @parms;
if (!@_ && $self->{instance_status_token} && $self->{instance_status_args}) {
@parms = (@{$self->{instance_status_args}},NextToken=>$self->{instance_status_token});
}
else {
my %args = $self->args('-instance_id',@_);
push @parms,$self->list_parm('InstanceId',\%args);
push @parms,$self->filter_parm(\%args);
push @parms,$self->boolean_parm('IncludeAllInstances',\%args);
push @parms,$self->single_parm('MaxResults',\%args);
if ($args{-max_results}) {
$self->{instance_status_token} = 'xyzzy'; # dummy value
$self->{instance_status_args} = \@parms;
}
}
return $self->call('DescribeInstanceStatus',@parms);
}
=head2 $t = $ec2->token
Return a client token for use with start_instances().
=cut
sub token {
my $self = shift;
my $seed = $self->{idempotent_seed};
$self->{idempotent_seed} = sha1_hex($seed);
$seed =~ s/(.{6})/$1-/g;
return $seed;
}
=head1 Waiting for State Changes
The methods in this section allow your script to wait in an efficient
manner for desired state changes in instances, volumes and other
objects.
=head2 $ec2->wait_for_instances(@instances)
Wait for all members of the provided list of instances to reach some
terminal state ("running", "stopped" or "terminated"), and then return
a hash reference that maps each instance ID to its final state.
Typical usage:
my @instances = $image->run_instances(-key_name =>'My_key',
-security_group=>'default',
-min_count =>2,
-instance_type => 't1.micro')
or die $ec2->error_str;
my $status = $ec2->wait_for_instances(@instances);
my @failed = grep {$status->{$_} ne 'running'} @instances;
print "The following failed: @failed\n";
If no terminal state is reached within a set timeout, then this method
returns undef and sets $ec2->error_str() to a suitable message. The
timeout, which defaults to 10 minutes (600 seconds), can be get or set
with $ec2->wait_for_timeout().
=cut
sub wait_for_instances {
my $self = shift;
$self->wait_for_terminal_state(\@_,
['running','stopped','terminated'],
$self->wait_for_timeout);
}
=head2 $ec2->wait_for_snapshots(@snapshots)
Wait for all members of the provided list of snapshots to reach some
terminal state ("completed", "error"), and then return a hash
reference that maps each snapshot ID to its final state.
This method may potentially wait forever. It has no set timeout. Wrap
it in an eval{} and set alarm() if you wish to timeout.
=cut
sub wait_for_snapshots {
my $self = shift;
$self->wait_for_terminal_state(\@_,
['completed','error'],
0); # no timeout on snapshots -- they may take days
}
=head2 $ec2->wait_for_volumes(@volumes)
Wait for all members of the provided list of volumes to reach some
terminal state ("available", "in-use", "deleted" or "error"), and then
return a hash reference that maps each volume ID to its final state.
If no terminal state is reached within a set timeout, then this method
returns undef and sets $ec2->error_str() to a suitable message. The
timeout, which defaults to 10 minutes (600 seconds), can be get or set
with $ec2->wait_for_timeout().
=cut
sub wait_for_volumes {
my $self = shift;
$self->wait_for_terminal_state(\@_,
['available','in-use','deleted','error'],
$self->wait_for_timeout);
}
=head2 $ec2->wait_for_attachments(@attachment)
Wait for all members of the provided list of
VM::EC2::BlockDevice::Attachment objects to reach some terminal state
("attached" or "detached"), and then return a hash reference that maps
each attachment to its final state.
Typical usage:
my $i = 0;
my $instance = 'i-12345';
my @attach;
foreach (@volume) {
push @attach,$_->attach($instance,'/dev/sdf'.$i++;
}
my $s = $ec2->wait_for_attachments(@attach);
my @failed = grep($s->{$_} ne 'attached'} @attach;
warn "did not attach: ",join ', ',@failed;
If no terminal state is reached within a set timeout, then this method
returns undef and sets $ec2->error_str() to a suitable message. The
timeout, which defaults to 10 minutes (600 seconds), can be get or set
with $ec2->wait_for_timeout().
=cut
sub wait_for_attachments {
my $self = shift;
$self->wait_for_terminal_state(\@_,
['attached','detached'],
$self->wait_for_timeout);
}
=head2 $ec2->wait_for_terminal_state(\@objects,['list','of','states'] [,$timeout])
Generic version of the last four methods. Wait for all members of the provided list of Amazon objects
instances to reach some terminal state listed in the second argument, and then return
a hash reference that maps each object ID to its final state.
If a timeout is provided, in seconds, then the method will abort after
waiting the indicated time and return undef.
=cut
sub wait_for_terminal_state {
my $self = shift;
my ($objects,$terminal_states,$timeout) = @_;
my %terminal_state = map {$_=>1} @$terminal_states;
my %status = ();
my @pending = grep {defined $_} @$objects; # in case we're passed an undef
my $status = eval {
local $SIG{ALRM};
if ($timeout && $timeout > 0) {
$SIG{ALRM} = sub {die "timeout"};
alarm($timeout);
}
while (@pending) {
sleep 3;
$status{$_} = $_->current_status foreach @pending;
@pending = grep { !$terminal_state{$status{$_}} } @pending;
}
alarm(0);
\%status;
};
if ($@ =~ /timeout/) {
$self->error('timeout waiting for terminal state');
return;
}
return $status;
}
=head2 $timeout = $ec2->wait_for_timeout([$new_timeout]);
Get or change the timeout for wait_for_instances(), wait_for_attachments(),
and wait_for_volumes(). The timeout is given in seconds, and defaults to
600 (10 minutes). You can set this to 0 to wait forever.
=cut
sub wait_for_timeout {
my $self = shift;
$self->{wait_for_timeout} = WAIT_FOR_TIMEOUT
unless defined $self->{wait_for_timeout};
my $d = $self->{wait_for_timeout};
$self->{wait_for_timeout} = shift if @_;
return $d;
}
=head1 EC2 AMAZON MACHINE IMAGES
The methods in this section allow you to query and manipulate Amazon
machine images (AMIs). See L<VM::EC2::Image>.
=head2 @i = $ec2->describe_images(@image_ids)
=head2 @i = $ec2->describe_images(-image_id=>\@id,-executable_by=>$id,
-owner=>$id, -filter=>\%filters)
Return a series of VM::EC2::Image objects, each describing an
AMI. Optional arguments:
-image_id The id of the image, either a string scalar or an
arrayref.
-executable_by Filter by images executable by the indicated user account, or
one of the aliases "self" or "all".
-owner Filter by owner account number or one of the aliases "self",
"aws-marketplace", "amazon" or "all".
-filter Tags and other filters to apply
If there are no other arguments, you may omit the -filter argument
name and call describe_images() with a single hashref consisting of
the search filters you wish to apply.
The full list of image filters can be found at:
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeImages.html
=cut
sub describe_images {
my $self = shift;
my %args = $self->args(-image_id=>@_);
my @params;
push @params,$self->list_parm($_,\%args) foreach qw(ExecutableBy ImageId Owner);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeImages',@params);
}
=head2 $image = $ec2->create_image(-instance_id=>$id,-name=>$name,%other_args)
Create an image from an EBS-backed instance and return a
VM::EC2::Image object. The instance must be in the "stopped" or
"running" state. In the latter case, Amazon will stop the instance,
create the image, and then restart it unless the -no_reboot argument
is provided.
Arguments:
-instance_id ID of the instance to create an image from. (required)
-name Name for the image that will be created. (required)
-description Description of the new image.
-no_reboot If true, don't reboot the instance.
-block_device_mapping
Block device mapping as a scalar or array ref. See
run_instances() for the syntax.
-block_devices Alias of the above
=cut
sub create_image {
my $self = shift;
my %args = @_;
$args{-instance_id} && $args{-name}
or croak "Usage: create_image(-instance_id=>\$id,-name=>\$name)";
$args{-block_device_mapping} ||= $args{-block_devices};
my @param = $self->single_parm('InstanceId',\%args);
push @param,$self->single_parm('Name',\%args);
push @param,$self->single_parm('Description',\%args);
push @param,$self->boolean_parm('NoReboot',\%args);
push @param,$self->block_device_parm($args{-block_device_mapping});
return $self->call('CreateImage',@param);
}
=head2 $image = $ec2->register_image(-name=>$name,%other_args)
Register an image, creating an AMI. This can be used to create an AMI
from a S3-backed instance-store bundle, or to create an AMI from a
snapshot of an EBS-backed root volume.
Required arguments:
-name Name for the image that will be created.
Arguments required for an EBS-backed image:
-root_device_name The root device name, e.g. /dev/sda1
-block_device_mapping The block device mapping strings, including the
snapshot ID for the root volume. This can
be either a scalar string or an arrayref.
See run_instances() for a description of the
syntax.
-block_devices Alias of the above.
Arguments required for an instance-store image:
-image_location Full path to the AMI manifest in Amazon S3 storage.
Common optional arguments:
-description Description of the AMI
-architecture Architecture of the image ("i386" or "x86_64")
-kernel_id ID of the kernel to use
-ramdisk_id ID of the RAM disk to use
While you do not have to specify the kernel ID, it is strongly
recommended that you do so. Otherwise the kernel will have to be
specified for run_instances().
Note: Immediately after registering the image you can add tags to it
and use modify_image_attribute to change launch permissions, etc.
=cut
sub register_image {
my $self = shift;
my %args = @_;
$args{-name} or croak "register_image(): -name argument required";
$args{-block_device_mapping} ||= $args{-block_devices};
if (!$args{-image_location}) {
$args{-root_device_name} && $args{-block_device_mapping}
or croak "register_image(): either provide -image_location to create an instance-store AMI\nor both the -root_device_name && -block_device_mapping arguments to create an EBS-backed AMI.";
}
my @param;
for my $a (qw(Name RootDeviceName ImageLocation Description
Architecture KernelId RamdiskId)) {
push @param,$self->single_parm($a,\%args);
}
push @param,$self->block_device_parm($args{-block_devices} || $args{-block_device_mapping});
return $self->call('RegisterImage',@param);
}
=head2 $result = $ec2->deregister_image($image_id)
Deletes the registered image and returns true if successful.
=cut
sub deregister_image {
my $self = shift;
my %args = $self->args(-image_id => @_);
my @param = $self->single_parm(ImageId=>\%args);
return $self->call('DeregisterImage',@param);
}
=head2 @data = $ec2->describe_image_attribute($image_id,$attribute)
This method returns image attributes. Only one attribute can be
retrieved at a time. The following is the list of attributes that can be
retrieved:
description -- scalar
kernel -- scalar
ramdisk -- scalar
launchPermission -- list of scalar
productCodes -- array
blockDeviceMapping -- list of hashref
All of these values can be retrieved more conveniently from the
L<VM::EC2::Image> object returned from describe_images(), so there is
no attempt to parse the results of this call into Perl objects. In
particular, 'blockDeviceMapping' is returned as a raw hashrefs (there
also seems to be an AWS bug that causes fetching this attribute to return an
AuthFailure error).
Please see the VM::EC2::Image launchPermissions() and
blockDeviceMapping() methods for more convenient ways to get this
data.
=cut
sub describe_image_attribute {
my $self = shift;
@_ == 2 or croak "Usage: describe_image_attribute(\$instance_id,\$attribute_name)";
my ($instance_id,$attribute) = @_;
my @param = (ImageId=>$instance_id,Attribute=>$attribute);
my $result = $self->call('DescribeImageAttribute',@param);
return $result && $result->attribute($attribute);
}
=head2 $boolean = $ec2->modify_image_attribute($image_id,-$attribute_name=>$value)
This method changes image attributes. The first argument is the image
ID, and this is followed by the attribute name and the value to change
it to.
The following is the list of attributes that can be set:
-launch_add_user -- scalar or arrayref of UserIds to grant launch permissions to
-launch_add_group -- scalar or arrayref of Groups to remove launch permissions from
(only currently valid value is "all")
-launch_remove_user -- scalar or arrayref of UserIds to remove from launch permissions
-launch_remove_group -- scalar or arrayref of Groups to remove from launch permissions
-product_code -- scalar or array of product codes to add
-description -- scalar new description
You can abbreviate the launch permission arguments to -add_user,
-add_group, -remove_user, -remove_group, etc.
Only one attribute can be changed in a single request.
For example:
$ec2->modify_image_attribute('i-12345',-product_code=>['abcde','ghijk']);
The result code is true if the attribute was successfully modified,
false otherwise. In the latter case, $ec2->error() will provide the
error message.
To make an image public, specify -launch_add_group=>'all':
$ec2->modify_image_attribute('i-12345',-launch_add_group=>'all');
Also see L<VM::EC2::Image> for shortcut methods. For example:
$image->add_authorized_users(1234567,999991);
=cut
sub modify_image_attribute {
my $self = shift;
my $image_id = shift or croak "Usage: modify_image_attribute(\$imageId,%param)";
my %args = @_;
# shortcuts
foreach (qw(add_user remove_user add_group remove_group)) {
$args{"-launch_$_"} ||= $args{"-$_"};
}
my @param = (ImageId=>$image_id);
push @param,$self->value_parm('Description',\%args);
push @param,$self->list_parm('ProductCode',\%args);
push @param,$self->launch_perm_parm('Add','UserId', $args{-launch_add_user});
push @param,$self->launch_perm_parm('Remove','UserId',$args{-launch_remove_user});
push @param,$self->launch_perm_parm('Add','Group', $args{-launch_add_group});
push @param,$self->launch_perm_parm('Remove','Group', $args{-launch_remove_group});
return $self->call('ModifyImageAttribute',@param);
}
=head2 $boolean = $ec2->reset_image_attribute($image_id,$attribute_name)
This method resets an attribute of the given snapshot to its default
value. The valid attributes are:
launchPermission
=cut
sub reset_image_attribute {
my $self = shift;
@_ == 2 or
croak "Usage: reset_image_attribute(\$imageId,\$attribute_name)";
my ($image_id,$attribute) = @_;
my %valid = map {$_=>1} qw(launchPermission);
$valid{$attribute} or croak "attribute to reset must be one of ",join(' ',map{"'$_'"} keys %valid);
return $self->call('ResetImageAttribute',
ImageId => $image_id,
Attribute => $attribute);
}
=head1 EC2 VOLUMES AND SNAPSHOTS
The methods in this section allow you to query and manipulate EC2 EBS
volumes and snapshots. See L<VM::EC2::Volume> and L<VM::EC2::Snapshot>
for additional functionality provided through the object interface.
=head2 @v = $ec2->describe_volumes(-volume_id=>\@ids,-filter=>\%filters)
=head2 @v = $ec2->describe_volumes(@volume_ids)
Return a series of VM::EC2::Volume objects. Optional arguments:
-volume_id The id of the volume to fetch, either a string
scalar or an arrayref.
-filter One or more filters to apply to the search
The -filter argument name can be omitted if there are no other
arguments you wish to pass.
The full list of volume filters can be found at:
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVolumes.html
=cut
sub describe_volumes {
my $self = shift;
my %args = $self->args(-volume_id=>@_);
my @params;
push @params,$self->list_parm('VolumeId',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeVolumes',@params);
}
=head2 $v = $ec2->create_volume(-availability_zone=>$zone,-snapshot_id=>$snapshotId,-size=>$size)
Create a volume in the specified availability zone and return
information about it.
Arguments:
-availability_zone -- An availability zone from
describe_availability_zones (required)
-snapshot_id -- ID of a snapshot to use to build volume from.
-size -- Size of the volume, in GB (between 1 and 1024).
One or both of -snapshot_id or -size are required. For convenience,
you may abbreviate -availability_zone as -zone, and -snapshot_id as
-snapshot.
Optional Arguments:
-volume_type -- The volume type. standard or io1, default is
standard
-iops -- The number of I/O operations per second (IOPS) that
the volume supports. Range is 100 to 2000. Required
when volume type is io1.
The returned object is a VM::EC2::Volume object.
=cut
sub create_volume {
my $self = shift;
my %args = @_;
my $zone = $args{-availability_zone} || $args{-zone} or croak "-availability_zone argument is required";
my $snap = $args{-snapshot_id} || $args{-snapshot};
my $size = $args{-size};
$snap || $size or croak "One or both of -snapshot_id or -size are required";
if (exists $args{-volume_type} && $args{-volume_type} eq 'io1') {
$args{-iops} or croak "Argument -iops required when -volume_type is 'io1'";
}
elsif ($args{-iops}) {
croak "Argument -iops cannot be used when volume type is 'standard'";
}
my @params = (AvailabilityZone => $zone);
push @params,(SnapshotId => $snap) if $snap;
push @params,(Size => $size) if $size;
push @params,$self->single_parm('VolumeType',\%args);
push @params,$self->single_parm('Iops',\%args);
return $self->call('CreateVolume',@params);
}
=head2 $result = $ec2->delete_volume($volume_id);
Deletes the specified volume. Returns a boolean indicating success of
the delete operation. Note that a volume will remain in the "deleting"
state for some time after this call completes.
=cut
sub delete_volume {
my $self = shift;
my %args = $self->args(-volume_id => @_);
my @param = $self->single_parm(VolumeId=>\%args);
return $self->call('DeleteVolume',@param);
}
=head2 $attachment = $ec2->attach_volume($volume_id,$instance_id,$device);
=head2 $attachment = $ec2->attach_volume(-volume_id=>$volume_id,-instance_id=>$instance_id,-device=>$device);
Attaches the specified volume to the instance using the indicated
device. All arguments are required:
-volume_id -- ID of the volume to attach. The volume must be in
"available" state.
-instance_id -- ID of the instance to attach to. Both instance and
attachment must be in the same availability zone.
-device -- How the device is exposed to the instance, e.g.
'/dev/sdg'.
The result is a VM::EC2::BlockDevice::Attachment object which
you can monitor by calling current_status():
my $a = $ec2->attach_volume('vol-12345','i-12345','/dev/sdg');
while ($a->current_status ne 'attached') {
sleep 2;
}
print "volume is ready to go\n";
or more simply
my $a = $ec2->attach_volume('vol-12345','i-12345','/dev/sdg');
$ec2->wait_for_attachments($a);
=cut
sub attach_volume {
my $self = shift;
my %args;
if ($_[0] !~ /^-/ && @_ == 3) {
@args{qw(-volume_id -instance_id -device)} = @_;
} else {
%args = @_;
}
$args{-volume_id} && $args{-instance_id} && $args{-device} or
croak "-volume_id, -instance_id and -device arguments must all be specified";
my @param = $self->single_parm(VolumeId=>\%args);
push @param,$self->single_parm(InstanceId=>\%args);
push @param,$self->single_parm(Device=>\%args);
return $self->call('AttachVolume',@param);
}
=head2 $attachment = $ec2->detach_volume($volume_id)
=head2 $attachment = $ec2->detach_volume(-volume_id=>$volume_id,-instance_id=>$instance_id,
-device=>$device, -force=>$force);
Detaches the specified volume from an instance.
-volume_id -- ID of the volume to detach. (required)
-instance_id -- ID of the instance to detach from. (optional)
-device -- How the device is exposed to the instance. (optional)
-force -- Force detachment, even if previous attempts were
unsuccessful. (optional)
The result is a VM::EC2::BlockDevice::Attachment object which
you can monitor by calling current_status():
my $a = $ec2->detach_volume('vol-12345');
while ($a->current_status ne 'detached') {
sleep 2;
}
print "volume is ready to go\n";
Or more simply:
my $a = $ec2->detach_volume('vol-12345');
$ec2->wait_for_attachments($a);
print "volume is ready to go\n" if $a->current_status eq 'detached';
=cut
sub detach_volume {
my $self = shift;
my %args = $self->args(-volume_id => @_);
my @param = $self->single_parm(VolumeId=>\%args);
push @param,$self->single_parm(InstanceId=>\%args);
push @param,$self->single_parm(Device=>\%args);
push @param,$self->single_parm(Force=>\%args);
return $self->call('DetachVolume',@param);
}
=head2 @v = $ec2->describe_volume_status(@volume_ids)
=head2 @v = $ec2->describe_volume_status(\%filters)
=head2 @v = $ec2->describe_volume_status(-volume_id=>\@ids,-filter=>\%filters)
Return a series of VM::EC2::Volume::StatusItem objects. Optional arguments:
-volume_id The id of the volume to fetch, either a string
scalar or an arrayref.
-filter One or more filters to apply to the search
-max_results Maximum number of items to return (must be more than
5).
The -filter argument name can be omitted if there are no other
arguments you wish to pass.
The full list of volume filters can be found at:
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVolumeStatus.html
If -max_results is specified, then the call will return at most the
number of volume status items you requested. You may see whether there
are additional results by calling more_volume_status(), and then
retrieve the next set of results with additional call(s) to
describe_volume_status():
my @results = $ec2->describe_volume_status(-max_results => 10);
do_something(\@results);
while ($ec2->more_volume_status) {
@results = $ec2->describe_volume_status;
do_something(\@results);
}
=cut
sub more_volume_status {
my $self = shift;
return $self->{volume_status_token} &&
!$self->{volume_status_stop};
}
sub describe_volume_status {
my $self = shift;
my @parms;
if (!@_ && $self->{volume_status_token} && $self->{volume_status_args}) {
@parms = (@{$self->{volume_status_args}},NextToken=>$self->{volume_status_token});
}
else {
my %args = $self->args('-volume_id',@_);
push @parms,$self->list_parm('VolumeId',\%args);
push @parms,$self->filter_parm(\%args);
push @parms,$self->single_parm('MaxResults',\%args);
if ($args{-max_results}) {
$self->{volume_status_token} = 'xyzzy'; # dummy value
$self->{volume_status_args} = \@parms;
}
}
return $self->call('DescribeVolumeStatus',@parms);
}
=head2 @data = $ec2->describe_volume_attribute($volume_id,$attribute)
This method returns volume attributes. Only one attribute can be
retrieved at a time. The following is the list of attributes that can be
retrieved:
autoEnableIO -- boolean
productCodes -- list of scalar
These values can be retrieved more conveniently from the
L<VM::EC2::Volume> object returned from describe_volumes():
$volume->auto_enable_io(1);
@codes = $volume->product_codes;
=cut
sub describe_volume_attribute {
my $self = shift;
@_ == 2 or croak "Usage: describe_volume_attribute(\$instance_id,\$attribute_name)";
my ($instance_id,$attribute) = @_;
my @param = (VolumeId=>$instance_id,Attribute=>$attribute);
my $result = $self->call('DescribeVolumeAttribute',@param);
return $result && $result->attribute($attribute);
}
sub modify_volume_attribute {
my $self = shift;
my $volume_id = shift or croak "Usage: modify_volume_attribute(\$volumeId,%param)";
my %args = @_;
my @param = (VolumeId=>$volume_id);
push @param,('AutoEnableIO.Value'=>$args{-auto_enable_io} ? 'true':'false');
return $self->call('ModifyVolumeAttribute',@param);
}
=head2 $boolean = $ec2->enable_volume_io($volume_id)
=head2 $boolean = $ec2->enable_volume_io(-volume_id=>$volume_id)
Given the ID of a volume whose I/O has been disabled (e.g. due to
hardware degradation), this method will re-enable the I/O and return
true if successful.
=cut
sub enable_volume_io {
my $self = shift;
my %args = $self->args('-volume_id',@_);
$args{-volume_id} or croak "Usage: enable_volume_io(\$volume_id)";
my @param = $self->single_parm('VolumeId',\%args);
return $self->call('EnableVolumeIO',@param);
}
=head2 @snaps = $ec2->describe_snapshots(@snapshot_ids)
=head2 @snaps = $ec2->describe_snapshots(-snapshot_id=>\@ids,%other_args)
Returns a series of VM::EC2::Snapshot objects. All arguments
are optional:
-snapshot_id ID of the snapshot
-owner Filter by owner ID
-restorable_by Filter by IDs of a user who is allowed to restore
the snapshot
-filter Tags and other filters
The -filter argument name can be omitted if there are no other
arguments you wish to pass.
The full list of applicable filters can be found at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSnapshots.html
=cut
sub describe_snapshots {
my $self = shift;
my %args = $self->args('-snapshot_id',@_);
my @params;
push @params,$self->list_parm('SnapshotId',\%args);
push @params,$self->list_parm('Owner',\%args);
push @params,$self->list_parm('RestorableBy',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeSnapshots',@params);
}
=head2 @data = $ec2->describe_snapshot_attribute($snapshot_id,$attribute)
This method returns snapshot attributes. The first argument is the
snapshot ID, and the second is the name of the attribute to
fetch. Currently Amazon defines two attributes:
createVolumePermission -- return a list of user Ids who are
allowed to create volumes from this snapshot.
productCodes -- product codes for this snapshot
The result is a raw hash of attribute values. Please see
L<VM::EC2::Snapshot> for a more convenient way of accessing and
modifying snapshot attributes.
=cut
sub describe_snapshot_attribute {
my $self = shift;
@_ == 2 or croak "Usage: describe_snapshot_attribute(\$instance_id,\$attribute_name)";
my ($snapshot_id,$attribute) = @_;
my @param = (SnapshotId=>$snapshot_id,Attribute=>$attribute);
my $result = $self->call('DescribeSnapshotAttribute',@param);
return $result && $result->attribute($attribute);
}
=head2 $boolean = $ec2->modify_snapshot_attribute($snapshot_id,-$argument=>$value)
This method changes snapshot attributes. The first argument is the
snapshot ID, and this is followed by an attribute modification command
and the value to change it to.
Currently the only attribute that can be changed is the
createVolumeAttribute. This is done through the following arguments
-createvol_add_user -- scalar or arrayref of UserIds to grant create volume permissions to
-createvol_add_group -- scalar or arrayref of Groups to remove create volume permissions from
(only currently valid value is "all")
-createvol_remove_user -- scalar or arrayref of UserIds to remove from create volume permissions
-createvol_remove_group -- scalar or arrayref of Groups to remove from create volume permissions
You can abbreviate these to -add_user, -add_group, -remove_user, -remove_group, etc.
See L<VM::EC2::Snapshot> for more convenient methods for interrogating
and modifying the create volume permissions.
=cut
sub modify_snapshot_attribute {
my $self = shift;
my $snapshot_id = shift or croak "Usage: modify_snapshot_attribute(\$snapshotId,%param)";
my %args = @_;
# shortcuts
foreach (qw(add_user remove_user add_group remove_group)) {
$args{"-createvol_$_"} ||= $args{"-$_"};
}
my @param = (SnapshotId=>$snapshot_id);
push @param,$self->create_volume_perm_parm('Add','UserId', $args{-createvol_add_user});
push @param,$self->create_volume_perm_parm('Remove','UserId',$args{-createvol_remove_user});
push @param,$self->create_volume_perm_parm('Add','Group', $args{-createvol_add_group});
push @param,$self->create_volume_perm_parm('Remove','Group', $args{-createvol_remove_group});
return $self->call('ModifySnapshotAttribute',@param);
}
=head2 $boolean = $ec2->reset_snapshot_attribute($snapshot_id,$attribute)
This method resets an attribute of the given snapshot to its default
value. The only valid attribute at this time is
"createVolumePermission."
=cut
sub reset_snapshot_attribute {
my $self = shift;
@_ == 2 or
croak "Usage: reset_snapshot_attribute(\$snapshotId,\$attribute_name)";
my ($snapshot_id,$attribute) = @_;
my %valid = map {$_=>1} qw(createVolumePermission);
$valid{$attribute} or croak "attribute to reset must be 'createVolumePermission'";
return $self->call('ResetSnapshotAttribute',
SnapshotId => $snapshot_id,
Attribute => $attribute);
}
=head2 $snapshot = $ec2->create_snapshot($volume_id)
=head2 $snapshot = $ec2->create_snapshot(-volume_id=>$vol,-description=>$desc)
Snapshot the EBS volume and store it to S3 storage. To ensure a
consistent snapshot, the volume should be unmounted prior to
initiating this operation.
Arguments:
-volume_id -- ID of the volume to snapshot (required)
-description -- A description to add to the snapshot (optional)
The return value is a VM::EC2::Snapshot object that can be queried
through its current_status() interface to follow the progress of the
snapshot operation.
Another way to accomplish the same thing is through the
VM::EC2::Volume interface:
my $volume = $ec2->describe_volumes(-filter=>{'tag:Name'=>'AccountingData'});
$s = $volume->create_snapshot("Backed up at ".localtime);
while ($s->current_status eq 'pending') {
print "Progress: ",$s->progress,"% done\n";
}
print "Snapshot status: ",$s->current_status,"\n";
=cut
sub create_snapshot {
my $self = shift;
my %args = $self->args('-volume_id',@_);
my @params = $self->single_parm('VolumeId',\%args);
push @params,$self->single_parm('Description',\%args);
return $self->call('CreateSnapshot',@params);
}
=head2 $boolean = $ec2->delete_snapshot($snapshot_id)
Delete the indicated snapshot and return true if the request was
successful.
=cut
sub delete_snapshot {
my $self = shift;
my %args = $self->args('-snapshot_id',@_);
my @params = $self->single_parm('SnapshotId',\%args);
return $self->call('DeleteSnapshot',@params);
}
=head2 $snapshot = $ec2->copy_snapshot(-source_region=>$region,-source_snapshot_id=>$id,-description=>$desc)
Copies an existing snapshot within the same region or from one region to another.
Required arguments:
-region -- The region the existing snapshot to copy resides in
-snapshot_id -- The snapshot ID of the snapshot to copy
Optional arguments:
-description -- A description of the new snapshot
The return value is a VM::EC2::Snapshot object that can be queried
through its current_status() interface to follow the progress of the
snapshot operation.
=cut
sub copy_snapshot {
my $self = shift;
my %args = @_;
$args{-description} ||= $args{-desc};
$args{-source_region} ||= $args{-region};
$args{-source_snapshot_id} ||= $args{-snapshot_id};
$args{-source_region} or croak "copy_snapshot(): -source_region argument required";
$args{-source_snapshot_id} or croak "copy_snapshot(): -source_snapshot_id argument required";
# As of 2012-12-22, sourceRegion, sourceSnapshotId are not recognized even though API docs specify those as the parameters
# The initial 's' must be capitalized. This has been reported to AWS as an inconsistency in the docs and API.
my @params = $self->single_parm('SourceRegion',\%args);
push @params, $self->single_parm('SourceSnapshotId',\%args);
push @params, $self->single_parm('Description',\%args);
my $snap_id = $self->call('CopySnapshot',@params);
return $snap_id && $self->describe_snapshots($snap_id);
}
=head1 SECURITY GROUPS AND KEY PAIRS
The methods in this section allow you to query and manipulate security
groups (firewall rules) and SSH key pairs. See
L<VM::EC2::SecurityGroup> and L<VM::EC2::KeyPair> for functionality
that is available through these objects.
=head2 @sg = $ec2->describe_security_groups(@group_ids)
=head2 @sg = $ec2->describe_security_groups(%args);
=head2 @sg = $ec2->describe_security_groups(\%filters);
Searches for security groups (firewall rules) matching the provided
filters and return a series of VM::EC2::SecurityGroup objects.
In the named-argument form you can provide the following optional
arguments:
-group_name A single group name or an arrayref containing a list
of names
-name Shorter version of -group_name
-group_id A single group id (i.e. 'sg-12345') or an arrayref
containing a list of ids
-filter Filter on tags and other attributes.
The -filter argument name can be omitted if there are no other
arguments you wish to pass.
The full list of security group filters can be found at:
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSecurityGroups.html
=cut
sub describe_security_groups {
my $self = shift;
my %args = $self->args(-group_id=>@_);
$args{-group_name} ||= $args{-name};
my @params = map { $self->list_parm($_,\%args) } qw(GroupName GroupId);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeSecurityGroups',@params);
}
=head2 $group = $ec2->create_security_group(-group_name=>$name,
-group_description=>$description,
-vpc_id => $vpc_id
)
Create a security group. Arguments are:
-group_name Name of the security group (required)
-group_description Description of the security group (required)
-vpc_id Virtual private cloud security group ID
(required for VPC security groups)
For convenience, you may use -name and -description as aliases for
-group_name and -group_description respectively.
If succcessful, the method returns an object of type
L<VM::EC2::SecurityGroup>.
=cut
sub create_security_group {
my $self = shift;
my %args = @_;
$args{-group_name} ||= $args{-name};
$args{-group_description} ||= $args{-description};
$args{-group_name} && $args{-group_description}
or croak "create_security_group() requires -group_name and -group_description arguments";
my @param;
push @param,$self->single_parm($_=>\%args) foreach qw(GroupName GroupDescription VpcId);
my $g = $self->call('CreateSecurityGroup',@param) or return;
return $self->describe_security_groups($g);
}
=head2 $boolean = $ec2->delete_security_group($group_id)
=head2 $boolean = $ec2->delete_security_group(-group_id => $group_id,
-group_name => $name);
Delete a security group. Arguments are:
-group_name Name of the security group
-group_id ID of the security group
Either -group_name or -group_id is required. In the single-argument
form, the method deletes the security group given by its id.
If succcessful, the method returns true.
=cut
sub delete_security_group {
my $self = shift;
my %args = $self->args(-group_id=>@_);
$args{-group_name} ||= $args{-name};
my @param = $self->single_parm(GroupName=>\%args);
push @param,$self->single_parm(GroupId=>\%args);
return $self->call('DeleteSecurityGroup',@param);
}
=head2 $boolean = $ec2->update_security_group($security_group)
Add one or more incoming firewall rules to a security group. The rules
to add are stored in a L<VM::EC2::SecurityGroup> which is created
either by describe_security_groups() or create_security_group(). This method combines
the actions AuthorizeSecurityGroupIngress,
AuthorizeSecurityGroupEgress, RevokeSecurityGroupIngress, and
RevokeSecurityGroupEgress.
For details, see L<VM::EC2::SecurityGroup>. Here is a brief summary:
$sg = $ec2->create_security_group(-name=>'MyGroup',-description=>'Example group');
# TCP on port 80 for the indicated address ranges
$sg->authorize_incoming(-protocol => 'tcp',
-port => 80,
-source_ip => ['192.168.2.0/24','192.168.2.1/24'});
# TCP on ports 22 and 23 from anyone
$sg->authorize_incoming(-protocol => 'tcp',
-port => '22..23',
-source_ip => '0.0.0.0/0');
# ICMP on echo (ping) port from anyone
$sg->authorize_incoming(-protocol => 'icmp',
-port => 0,
-source_ip => '0.0.0.0/0');
# TCP to port 25 (mail) from instances belonging to
# the "Mail relay" group belonging to user 12345678.
$sg->authorize_incoming(-protocol => 'tcp',
-port => 25,
-group => '12345678/Mail relay');
$result = $ec2->update_security_group($sg);
or more simply:
$result = $sg->update();
=cut
sub update_security_group {
my $self = shift;
my $sg = shift;
my $group_id = $sg->groupId;
my $result = 1;
for my $action (qw(Authorize Revoke)) {
for my $direction (qw(Ingress Egress)) {
my @permissions = $sg->_uncommitted_permissions($action,$direction) or next;
my $call = "${action}SecurityGroup${direction}";
my @param = (GroupId=>$group_id);
push @param,$self->_security_group_parm(\@permissions);
my $r = $self->call($call=>@param);
$result &&= $r;
}
}
return $result;
}
sub _security_group_parm {
my $self = shift;
my $permissions = shift;
my @param;
for (my $i=0;$i<@$permissions;$i++) {
my $perm = $permissions->[$i];
my $n = $i+1;
push @param,("IpPermissions.$n.IpProtocol"=>$perm->ipProtocol);
push @param,("IpPermissions.$n.FromPort" => $perm->fromPort);
push @param,("IpPermissions.$n.ToPort" => $perm->toPort);
my @cidr = $perm->ipRanges;
for (my $i=0;$i<@cidr;$i++) {
my $m = $i+1;
push @param,("IpPermissions.$n.IpRanges.$m.CidrIp"=>$cidr[$i]);
}
my @groups = $perm->groups;
for (my $i=0;$i<@groups;$i++) {
my $m = $i+1;
my $group = $groups[$i];
if (defined $group->groupId) {
push @param,("IpPermissions.$n.Groups.$m.GroupId" => $group->groupId);
} else {
push @param,("IpPermissions.$n.Groups.$m.UserId" => $group->userId);
push @param,("IpPermissions.$n.Groups.$m.GroupName"=> $group->groupName);
}
}
}
return @param;
}
=head2 @keys = $ec2->describe_key_pairs(@names);
=head2 @keys = $ec2->describe_key_pairs(\%filters);
=head2 @keys = $ec2->describe_key_pairs(-key_name => \@names,
-filter => \%filters);
Searches for ssh key pairs matching the provided filters and return
a series of VM::EC2::KeyPair objects.
Optional arguments:
-key_name A single key name or an arrayref containing a list
of names
-filter Filter on tags and other attributes.
The full list of key filters can be found at:
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeKeyPairs.html
=cut
sub describe_key_pairs {
my $self = shift;
my %args = $self->args(-key_name=>@_);
my @params = $self->list_parm('KeyName',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeKeyPairs',@params);
}
=head2 $key = $ec2->create_key_pair($name)
Create a new key pair with the specified name (required). If the key
pair already exists, returns undef. The contents of the new keypair,
including the PEM-encoded private key, is contained in the returned
VM::EC2::KeyPair object:
my $key = $ec2->create_key_pair('My Keypair');
if ($key) {
print $key->fingerprint,"\n";
print $key->privateKey,"\n";
}
=cut
sub create_key_pair {
my $self = shift;
my $name = shift or croak "Usage: create_key_pair(\$name)";
$name =~ /^[\w _-]+$/
or croak "Invalid keypair name: must contain only alphanumerics, spaces, dashes and underscores";
my @params = (KeyName=>$name);
$self->call('CreateKeyPair',@params);
}
=head2 $key = $ec2->import_key_pair($name,$public_key)
=head2 $key = $ec2->import_key_pair(-key_name => $name,
-public_key_material => $public_key)
Imports a preexisting public key into AWS under the specified name.
If successful, returns a VM::EC2::KeyPair. The public key must be an
RSA key of length 1024, 2048 or 4096. The method can be called with
two unnamed arguments consisting of the key name and the public key
material, or in a named argument form with the following argument
names:
-key_name -- desired name for the imported key pair (required)
-name -- shorter version of -key_name
-public_key_material -- public key data (required)
-public_key -- shorter version of the above
This example uses Net::SSH::Perl::Key to generate a new keypair, and
then uploads the public key to Amazon.
use Net::SSH::Perl::Key;
my $newkey = Net::SSH::Perl::Key->keygen('RSA',1024);
$newkey->write_private('.ssh/MyKeypair.rsa'); # save private parts
my $key = $ec2->import_key_pair('My Keypair' => $newkey->dump_public)
or die $ec2->error;
print "My Keypair added with fingerprint ",$key->fingerprint,"\n";
Several different formats are accepted for the key, including SSH
"authorized_keys" format (generated by L<ssh-keygen> and
Net::SSH::Perl::Key), the SSH public keys format, and DER format. You
do not need to base64-encode the key or perform any other
pre-processing.
Note that the algorithm used by Amazon to calculate its key
fingerprints differs from the one used by the ssh library, so don't
try to compare the key fingerprints returned by Amazon to the ones
produced by ssh-keygen or Net::SSH::Perl::Key.
=cut
sub import_key_pair {
my $self = shift;
my %args;
if (@_ == 2 && $_[0] !~ /^-/) {
%args = (-key_name => shift,
-public_key_material => shift);
} else {
%args = @_;
}
my $name = $args{-key_name} || $args{-name} or croak "-key_name argument required";
my $pkm = $args{-public_key_material}|| $args{-public_key} or croak "-public_key_material argument required";
my @params = (KeyName => $name,PublicKeyMaterial=>encode_base64($pkm));
$self->call('ImportKeyPair',@params);
}
=head2 $result = $ec2->delete_key_pair($name)
Deletes the key pair with the specified name (required). Returns true
if successful.
=cut
sub delete_key_pair {
my $self = shift; my $name = shift or croak "Usage: delete_key_pair(\$name)";
$name =~ /^[\w _-]+$/
or croak "Invalid keypair name: must contain only alphanumerics, spaces, dashes and underscores";
my @params = (KeyName=>$name);
$self->call('DeleteKeyPair',@params);
}
=head1 TAGS
These methods allow you to create, delete and fetch resource tags. You
may find that you rarely need to use these methods directly because
every object produced by VM::EC2 supports a simple tag interface:
$object = $ec2->describe_volumes(-volume_id=>'vol-12345'); # e.g.
$tags = $object->tags();
$name = $tags->{Name};
$object->add_tags(Role => 'Web Server', Status=>'development);
$object->delete_tags(Name=>undef);
See L<VM::EC2::Generic> for a full description of the uniform object
tagging interface.
These methods are most useful when creating and deleting tags for
multiple resources simultaneously.
=head2 @t = $ec2->describe_tags(-filter=>\%filters);
Return a series of VM::EC2::Tag objects, each describing an
AMI. A single optional -filter argument is allowed.
Available filters are: key, resource-id, resource-type and value. See
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeTags.html
=cut
sub describe_tags {
my $self = shift;
my %args = @_;
my @params = $self->filter_parm(\%args);
return $self->call('DescribeTags',@params);
}
=head2 $bool = $ec2->create_tags(-resource_id=>\@ids,-tag=>{key1=>value1...})
Tags the resource indicated by -resource_id with the tag(s) in in the
hashref indicated by -tag. You may specify a single resource by
passing a scalar resourceId to -resource_id, or multiple resources
using an anonymous array. Returns a true value if tagging was
successful.
The method name "add_tags()" is an alias for create_tags().
You may find it more convenient to tag an object retrieved with any of
the describe() methods using the built-in add_tags() method:
@snap = $ec2->describe_snapshots(-filter=>{status=>'completed'});
foreach (@snap) {$_->add_tags(ReadyToUse => 'true')}
but if there are many snapshots to tag simultaneously, this will be faster:
@snap = $ec2->describe_snapshots(-filter=>{status=>'completed'});
$ec2->add_tags(-resource_id=>\@snap,-tag=>{ReadyToUse=>'true'});
Note that you can tag volumes, snapshots and images owned by other
people. Only you will be able to see these tags.
=cut
sub create_tags {
my $self = shift;
my %args = @_;
$args{-resource_id} or croak "create_tags() -resource_id argument required";
$args{-tag} or croak "create_tags() -tag argument required";
my @params = $self->list_parm('ResourceId',\%args);
push @params,$self->tagcreate_parm(\%args);
return $self->call('CreateTags',@params);
}
sub add_tags { shift->create_tags(@_) }
=head2 $bool = $ec2->delete_tags(-resource_id=>$id1,-tag=>{key1=>value1...})
Delete the indicated tags from the indicated resource. Pass an
arrayref to operate on several resources at once. The tag syntax is a
bit tricky. Use a value of undef to delete the tag unconditionally:
-tag => { Role => undef } # deletes any Role tag
Any scalar value will cause the tag to be deleted only if its value
exactly matches the specified value:
-tag => { Role => 'Server' } # only delete the Role tag
# if it currently has the value "Server"
An empty string value ('') will only delete the tag if its value is an
empty string, which is probably not what you want.
Pass an array reference of tag names to delete each of the tag names
unconditionally (same as passing a value of undef):
$ec2->delete_tags(['Name','Role','Description']);
You may find it more convenient to delete tags from objects using
their delete_tags() method:
@snap = $ec2->describe_snapshots(-filter=>{status=>'completed'});
foreach (@snap) {$_->delete_tags(Role => undef)}
=cut
sub delete_tags {
my $self = shift;
my %args = @_;
$args{-resource_id} or croak "create_tags() -resource_id argument required";
$args{-tag} or croak "create_tags() -tag argument required";
my @params = $self->list_parm('ResourceId',\%args);
push @params,$self->tagdelete_parm(\%args);
return $self->call('DeleteTags',@params);
}
=head1 ELASTIC IP ADDRESSES
The methods in this section allow you to allocate elastic IP
addresses, attach them to instances, and delete them. See
L<VM::EC2::ElasticAddress>.
=head2 @addr = $ec2->describe_addresses(@public_ips)
=head2 @addr = $ec2->describe_addresses(-public_ip=>\@addr,-allocation_id=>\@id,-filter->\%filters)
Queries AWS for a list of elastic IP addresses already allocated to
you. All arguments are optional:
-public_ip -- An IP address (in dotted format) or an arrayref of
addresses to return information about.
-allocation_id -- An allocation ID or arrayref of such IDs. Only
applicable to VPC addresses.
-filter -- A hashref of tag=>value pairs to filter the response
on.
The list of applicable filters can be found at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeAddresses.html.
This method returns a list of L<VM::EC2::ElasticAddress>.
=cut
sub describe_addresses {
my $self = shift;
my %args = $self->args(-public_ip=>@_);
my @param;
push @param,$self->list_parm('PublicIp',\%args);
push @param,$self->list_parm('AllocationId',\%args);
push @param,$self->filter_parm(\%args);
return $self->call('DescribeAddresses',@param);
}
=head2 $address_info = $ec2->allocate_address([-vpc=>1])
Request an elastic IP address. Pass -vpc=>1 to allocate a VPC elastic
address. The return object is a VM::EC2::ElasticAddress.
=cut
sub allocate_address {
my $self = shift;
my %args = @_;
my @param = $args{-vpc} ? (Domain=>'vpc') : ();
return $self->call('AllocateAddress',@param);
}
=head2 $boolean = $ec2->release_address($addr)
Release an elastic IP address. For non-VPC addresses, you may provide
either an IP address string, or a VM::EC2::ElasticAddress. For VPC
addresses, you must obtain a VM::EC2::ElasticAddress first
(e.g. with describe_addresses) and then pass that to the method.
=cut
sub release_address {
my $self = shift;
my $addr = shift or croak "Usage: release_address(\$addr)";
my @param = (PublicIp=>$addr);
if (my $allocationId = eval {$addr->allocationId}) {
push @param,(AllocatonId=>$allocationId);
}
return $self->call('ReleaseAddress',@param);
}
=head2 $result = $ec2->associate_address($elastic_addr => $instance_id)
Associate an elastic address with an instance id. Both arguments are
mandatory. If you are associating a VPC elastic IP address with the
instance, the result code will indicate the associationId. Otherwise
it will be a simple perl truth value ("1") if successful, undef if
false.
If this is an ordinary EC2 Elastic IP address, the first argument may
either be an ordinary string (xx.xx.xx.xx format) or a
VM::EC2::ElasticAddress object. However, if it is a VPC elastic
IP address, then the argument must be a VM::EC2::ElasticAddress
as returned by describe_addresses(). The reason for this is that the
allocationId must be retrieved from the object in order to use in the
call.
=cut
sub associate_address {
my $self = shift;
@_ == 2 or croak "Usage: associate_address(\$elastic_addr => \$instance_id)";
my ($addr,$instance) = @_;
my @param = (InstanceId=>$instance);
push @param,eval {$addr->domain eq 'vpc'} ? (AllocationId => $addr->allocationId)
: (PublicIp => $addr);
return $self->call('AssociateAddress',@param);
}
=head2 $bool = $ec2->disassociate_address($elastic_addr)
Disassociate an elastic address from whatever instance it is currently
associated with, if any. The result will be true if disassociation was
successful.
If this is an ordinary EC2 Elastic IP address, the argument may
either be an ordinary string (xx.xx.xx.xx format) or a
VM::EC2::ElasticAddress object. However, if it is a VPC elastic
IP address, then the argument must be a VM::EC2::ElasticAddress
as returned by describe_addresses(). The reason for this is that the
allocationId must be retrieved from the object in order to use in the
call.
=cut
sub disassociate_address {
my $self = shift;
@_ == 1 or croak "Usage: associate_address(\$elastic_addr)";
my $addr = shift;
my @param = eval {$addr->domain eq 'vpc'} ? (AssociationId => $addr->associationId)
: (PublicIp => $addr);
return $self->call('DisassociateAddress',@param);
}
=head1 RESERVED INSTANCES
These methods apply to describing, purchasing and using Reserved Instances.
=head2 @offerings = $ec2->describe_reserved_instances_offerings(@offering_ids)
=head2 @offerings = $ec2->describe_reserved_instances_offerings(%args)
This method returns a list of the reserved instance offerings
currently available for purchase. The arguments allow you to filter
the offerings according to a variety of filters.
All arguments are optional. If no named arguments are used, then the
arguments are treated as Reserved Instance Offering IDs.
-reserved_instances_offering_id A scalar or arrayref of reserved
instance offering IDs
-instance_type The instance type on which the
reserved instance can be used,
e.g. "c1.medium"
-availability_zone, -zone The availability zone in which the
reserved instance can be used.
-product_description The reserved instance description.
Valid values are "Linux/UNIX",
"Linux/UNIX (Amazon VPC)",
"Windows", and "Windows (Amazon
VPC)"
-instance_tenancy The tenancy of the reserved instance
offering, either "default" or
"dedicated". (VPC instances only)
-offering_type The reserved instance offering type, one of
"Heavy Utilization", "Medium Utilization",
or "Light Utilization".
-filter A set of filters to apply.
For available filters, see http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeReservedInstancesOfferings.html.
The returned objects are of type L<VM::EC2::ReservedInstance::Offering>
This can be combined with the Offering purchase() method as shown here:
@offerings = $ec2->describe_reserved_instances_offerings(
{'availability-zone' => 'us-east-1a',
'instance-type' => 'c1.medium',
'product-description' =>'Linux/UNIX',
'duration' => 31536000, # this is 1 year
});
$offerings[0]->purchase(5) and print "Five reserved instances purchased\n";
=cut
sub describe_reserved_instances_offerings {
my $self = shift;
my %args = $self->args('-reserved_instances_offering_id',@_);
$args{-availability_zone} ||= $args{-zone};
my @param = $self->list_parm('ReservedInstancesOfferingId',\%args);
push @param,$self->single_parm('ProductDescription',\%args);
push @param,$self->single_parm('InstanceType',\%args);
push @param,$self->single_parm('AvailabilityZone',\%args);
push @param,$self->single_parm('instanceTenancy',\%args); # should initial "i" be upcase?
push @param,$self->single_parm('offeringType',\%args); # should initial "o" be upcase?
push @param,$self->filter_parm(\%args);
return $self->call('DescribeReservedInstancesOfferings',@param);
}
=head2 $id = $ec2->purchase_reserved_instances_offering($offering_id)
=head2 $id = $ec2->purchase_reserved_instances_offering(%args)
Purchase one or more reserved instances based on an offering.
Arguments:
-reserved_instances_offering_id, -id -- The reserved instance offering ID
to purchase (required).
-instance_count, -count -- Number of instances to reserve
under this offer (optional, defaults
to 1).
Returns a Reserved Instances Id on success, undef on failure. Also see the purchase() method of
L<VM::EC2::ReservedInstance::Offering>.
=cut
sub purchase_reserved_instances_offering {
my $self = shift;
my %args = $self->args('-reserved_instances_offering_id'=>@_);
$args{-reserved_instances_offering_id} ||= $args{-id};
$args{-reserved_instances_offering_id} or
croak "purchase_reserved_instances_offering(): the -reserved_instances_offering_id argument is required";
$args{-instance_count} ||= $args{-count};
my @param = $self->single_parm('ReservedInstancesOfferingId',\%args);
push @param,$self->single_parm('InstanceCount',\%args);
return $self->call('PurchaseReservedInstancesOffering',@param);
}
=head2 @res_instances = $ec2->describe_reserved_instances(@res_instance_ids)
=head2 @res_instances = $ec2->describe_reserved_instances(%args)
This method returns a list of the reserved instances that you
currently own. The information returned includes the type of
instances that the reservation allows you to launch, the availability
zone, and the cost per hour to run those reserved instances.
All arguments are optional. If no named arguments are used, then the
arguments are treated as Reserved Instance IDs.
-reserved_instances_id -- A scalar or arrayref of reserved
instance IDs
-filter -- A set of filters to apply.
For available filters, see http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeReservedInstances.html.
The returned objects are of type L<VM::EC2::ReservedInstance>
=cut
sub describe_reserved_instances {
my $self = shift;
my %args = $self->args('-reserved_instances_id',@_);
my @param = $self->list_parm('ReservedInstancesId',\%args);
push @param,$self->filter_parm(\%args);
return $self->call('DescribeReservedInstances',@param);
}
=head1 SPOT INSTANCES
These methods allow you to request spot instances and manipulte spot
data feed subscriptoins.
=cut
=head2 $subscription = $ec2->create_spot_datafeed_subscription($bucket,$prefix)
This method creates a spot datafeed subscription. Provide the method with the
name of an S3 bucket associated with your account, and a prefix to be appended
to the files written by the datafeed. Spot instance usage logs will be written
into the requested bucket, and prefixed with the desired prefix.
If no prefix is specified, it defaults to "SPOT_DATAFEED_";
On success, a VM::EC2::Spot::DatafeedSubscription object is returned;
Only one datafeed is allowed per account;
=cut
sub create_spot_datafeed_subscription {
my $self = shift;
my ($bucket,$prefix) = @_;
$bucket or croak "Usage: create_spot_datafeed_subscription(\$bucket,\$prefix)";
$prefix ||= 'SPOT_DATAFEED_';
my @param = (Bucket => $bucket,
Prefix => $prefix);
return $self->call('CreateSpotDatafeedSubscription',@param);
}
=head2 $boolean = $ec2->delete_spot_datafeed_subscription()
This method delete's the current account's spot datafeed
subscription, if any. It takes no arguments.
On success, it returns true.
=cut
sub delete_spot_datafeed_subscription {
my $self = shift;
return $self->call('DeleteSpotDatafeedSubscription');
}
=head2 $subscription = $ec2->describe_spot_datafeed_subscription()
This method describes the current account's spot datafeed
subscription, if any. It takes no arguments.
On success, a VM::EC2::Spot::DatafeedSubscription object is returned;
=cut
sub describe_spot_datafeed_subscription {
my $self = shift;
return $self->call('DescribeSpotDatafeedSubscription');
}
=head2 @spot_price_history = $ec2->describe_spot_price_history(@filters)
This method applies the specified filters to spot instances and
returns a list of instances, timestamps and their price at the
indicated time. Each spot price history point is represented as a
VM::EC2::Spot::PriceHistory object.
Option arguments are:
-start_time Start date and time of the desired history
data, in the form yyyy-mm-ddThh:mm:ss (GMT).
The Perl DateTime module provides a convenient
way to create times in this format.
-end_time End date and time of the desired history
data.
-instance_type The instance type, e.g. "m1.small", can be
a scalar value or an arrayref.
-product_description The product description. One of "Linux/UNIX",
"SUSE Linux" or "Windows". Can be a scalar value
or an arrayref.
-availability_zone A single availability zone, such as "us-east-1a".
-max_results Maximum number of rows to return in a single
call.
-next_token Specifies the next set of results to return; used
internally.
-filter Hashref containing additional filters to apply,
The following filters are recognized: "instance-type",
"product-description", "spot-price", "timestamp",
"availability-zone". The '*' and '?' wildcards can be used in filter
values, but numeric comparison operations are not supported by the
Amazon API. Note that wildcards are not generally allowed in the
standard options. Hence if you wish to get spot price history in all
availability zones in us-east, this will work:
$ec2->describe_spot_price_history(-filter=>{'availability-zone'=>'us-east*'})
but this will return an invalid parameter error:
$ec2->describe_spot_price_history(-availability_zone=>'us-east*')
If you specify -max_results, then the list of history objects returned
may not represent the complete result set. In this case, the method
more_spot_prices() will return true. You can then call
describe_spot_price_history() repeatedly with no arguments in order to
retrieve the remainder of the results. When there are no more results,
more_spot_prices() will return false.
my @results = $ec2->describe_spot_price_history(-max_results => 20,
-instance_type => 'm1.small',
-availability_zone => 'us-east*',
-product_description=>'Linux/UNIX');
print_history(\@results);
while ($ec2->more_spot_prices) {
@results = $ec2->describe_spot_price_history
print_history(\@results);
}
=cut
sub more_spot_prices {
my $self = shift;
return $self->{spot_price_history_token} &&
!$self->{spot_price_history_stop};
}
sub describe_spot_price_history {
my $self = shift;
my @parms;
if (!@_ && $self->{spot_price_history_token} && $self->{price_history_args}) {
@parms = (@{$self->{price_history_args}},NextToken=>$self->{spot_price_history_token});
}
else {
my %args = $self->args('-filter',@_);
push @parms,$self->single_parm($_,\%args)
foreach qw(StartTime EndTime MaxResults AvailabilityZone);
push @parms,$self->list_parm($_,\%args)
foreach qw(InstanceType ProductDescription);
push @parms,$self->filter_parm(\%args);
if ($args{-max_results}) {
$self->{spot_price_history_token} = 'xyzzy'; # dummy value
$self->{price_history_args} = \@parms;
}
}
return $self->call('DescribeSpotPriceHistory',@parms);
}
=head2 @requests = $ec2->request_spot_instances(%args)
This method will request one or more spot instances to be launched
when the current spot instance run-hour price drops below a preset
value and terminated when the spot instance run-hour price exceeds the
value.
On success, will return a series of VM::EC2::Spot::InstanceRequest
objects, one for each instance specified in -instance_count.
=over 4
=item Required arguments:
-spot_price The desired spot price, in USD.
-image_id ID of an AMI to launch
-instance_type Type of the instance(s) to launch, such as "m1.small"
=item Optional arguments:
-instance_count Maximum number of instances to launch (default 1)
-type Spot instance request type; one of "one-time" or "persistent"
-valid_from Date/time the request becomes effective, in format
yyyy-mm-ddThh:mm:ss. Default is immediately.
-valid_until Date/time the request expires, in format
yyyy-mm-ddThh:mm:ss. Default is to remain in
effect indefinitely.
-launch_group Name of the launch group. Instances in the same
launch group are started and terminated together.
Default is to launch instances independently.
-availability_zone_group If specified, all instances that are given
the same zone group name will be launched into the
same availability zone. This is independent of
the -availability_zone argument, which specifies
a particular availability zone.
-key_name Name of the keypair to use
-security_group_id Security group ID to use for this instance.
Use an arrayref for multiple group IDs
-security_group Security group name to use for this instance.
Use an arrayref for multiple values.
-user_data User data to pass to the instances. Do NOT base64
encode this. It will be done for you.
-availability_zone The availability zone you want to launch the
instance into. Call $ec2->regions for a list.
-zone Short version of -availability_aone.
-placement_group An existing placement group to launch the
instance into. Applicable to cluster instances
only.
-placement_tenancy Specify 'dedicated' to launch the instance on a
dedicated server. Only applicable for VPC
instances.
-kernel_id ID of the kernel to use for the instances,
overriding the kernel specified in the image.
-ramdisk_id ID of the ramdisk to use for the instances,
overriding the ramdisk specified in the image.
-block_devices Specify block devices to map onto the instances,
overriding the values specified in the image.
See run_instances() for the syntax of this argument.
-block_device_mapping Alias for -block_devices.
-network_interfaces Same as the -network_interfaces option in run_instances().
-monitoring Pass a true value to enable detailed monitoring.
-subnet The ID of the Amazon VPC subnet in which to launch the
spot instance (VPC only).
-subnet_id deprecated
-addressing_type Deprecated and undocumented, but present in the
current EC2 API documentation.
-iam_arn The Amazon resource name (ARN) of the IAM Instance Profile (IIP)
to associate with the instances.
-iam_name The name of the IAM instance profile (IIP) to associate with the
instances.
-ebs_optimized If true, request an EBS-optimized instance (certain
instance types only).
=cut
sub request_spot_instances {
my $self = shift;
my %args = @_;
$args{-spot_price} or croak "-spot_price argument missing";
$args{-image_id} or croak "-image_id argument missing";
$args{-instance_type} or croak "-instance_type argument missing";
$args{-availability_zone} ||= $args{-zone};
$args{-availability_zone} ||= $args{-placement_zone};
my @p = map {$self->single_parm($_,\%args)}
qw(SpotPrice InstanceCount Type ValidFrom ValidUntil LaunchGroup AvailabilityZoneGroup Subnet);
# oddly enough, the following args need to be prefixed with "LaunchSpecification."
my @launch_spec = map {$self->single_parm($_,\%args)}
qw(ImageId KeyName UserData AddressingType InstanceType KernelId RamdiskId SubnetId);
push @launch_spec, map {$self->list_parm($_,\%args)} qw(SecurityGroup SecurityGroupId);
push @launch_spec, ('EbsOptimized'=>'true') if $args{-ebs_optimized};
push @launch_spec, $self->block_device_parm($args{-block_devices}||$args{-block_device_mapping});
push @launch_spec, $self->iam_parm(\%args);
push @launch_spec, $self->network_interface_parm(\%args);
while (my ($key,$value) = splice(@launch_spec,0,2)) {
push @p,("LaunchSpecification.$key" => $value);
}
# a few more oddballs
push @p,('LaunchSpecification.Placement.AvailabilityZone'=> $args{-availability_zone})
if $args{-availability_zone};
push @p,('Placement.GroupName' =>$args{-placement_group}) if $args{-placement_group};
push @p,('LaunchSpecification.Monitoring.Enabled' => 'true') if $args{-monitoring};
push @p,('LaunchSpecification.UserData' =>
encode_base64($args{-user_data},'')) if $args{-user_data};
return $self->call('RequestSpotInstances',@p);
}
=back
=head2 @requests = $ec2->cancel_spot_instance_requests(@request_ids)
This method cancels the pending requests. It does not terminate any
instances that are already running as a result of the requests. It
returns a list of VM::EC2::Spot::InstanceRequest objects, whose fields
will be unpopulated except for spotInstanceRequestId and state.
=cut
sub cancel_spot_instance_requests {
my $self = shift;
my %args = $self->args('-spot_instance_request_id',@_);
my @parm = $self->list_parm('SpotInstanceRequestId',\%args);
return $self->call('CancelSpotInstanceRequests',@parm);
}
=head2 @requests = $ec2->describe_spot_instance_requests(@spot_instance_request_ids)
=head2 @requests = $ec2->describe_spot_instance_requests(\%filters)
=head2 @requests = $ec2->describe_spot_instance_requests(-spot_instance_request_id=>\@ids,-filter=>\%filters)
This method will return information about current spot instance
requests as a list of VM::EC2::Spot::InstanceRequest objects.
Optional arguments:
-spot_instance_request_id -- Scalar or arrayref of request Ids.
-filter -- Tags and other filters to apply.
There are many filters available, described fully at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/index.html?ApiReference-ItemType-SpotInstanceRequestSetItemType.html:
availability-zone-group
create-time
fault-code
fault-message
instance-id
launch-group
launch.block-device-mapping.delete-on-termination
launch.block-device-mapping.device-name
launch.block-device-mapping.snapshot-id
launch.block-device-mapping.volume-size
launch.block-device-mapping.volume-type
launch.group-id
launch.image-id
launch.instance-type
launch.kernel-id
launch.key-name
launch.monitoring-enabled
launch.ramdisk-id
launch.network-interface.network-interface-id
launch.network-interface.device-index
launch.network-interface.subnet-id
launch.network-interface.description
launch.network-interface.private-ip-address
launch.network-interface.delete-on-termination
launch.network-interface.group-id
launch.network-interface.group-name
launch.network-interface.addresses.primary
product-description
spot-instance-request-id
spot-price
state
status-code
status-message
tag-key
tag-value
tag:<key>
type
launched-availability-zone
valid-from
valid-until
=cut
sub describe_spot_instance_requests {
my $self = shift;
my %args = $self->args('-spot_instance_request_id',@_);
my @params = $self->list_parm('SpotInstanceRequestId',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeSpotInstanceRequests',@params);
}
=head1 PLACEMENT GROUPS
Placement groups Provide low latency and high-bandwidth connectivity
between cluster instances within a single Availability Zone. Create
a placement group and then launch cluster instances into it. Instances
launched within a placement group participate in a full-bisection
bandwidth cluster appropriate for HPC applications.
=head2 @groups = $ec2->describe_placement_groups(@group_names)
=head2 @groups = $ec2->describe_placement_groups(\%filters)
=head2 @groups = $ec2->describe_placement_groups(-group_name=>\@ids,-filter=>\%filters)
This method will return information about cluster placement groups
as a list of VM::EC2::PlacementGroup objects.
Optional arguments:
-group_name -- Scalar or arrayref of placement group names.
-filter -- Tags and other filters to apply.
The filters available are described fully at:
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribePlacementGroups.html
group-name
state
strategy
=cut
sub describe_placement_groups {
my $self = shift;
my %args = $self->args('-group_name',@_);
my @params = $self->list_parm('GroupName',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribePlacementGroups',@params);
}
=head2 $success = $ec2->create_placement_group($group_name)
=head2 $success = $ec2->create_placement_group(-group_name=>$name,-strategy=>$strategy)
Creates a placement group that cluster instances are launched into.
Required arguments:
-group_name -- The name of the placement group to create
Optional:
-strategy -- As of 2012-12-23, the only available option is 'cluster'
so the parameter defaults to that.
Returns true on success.
=cut
sub create_placement_group {
my $self = shift;
my %args = $self->args('-group_name',@_);
$args{-strategy} ||= 'cluster';
my @params = $self->single_parm('GroupName',\%args);
push @params, $self->single_parm('Strategy',\%args);
return $self->call('CreatePlacementGroup',@params);
}
=head2 $success = $ec2->delete_placement_group($group_name)
=head2 $success = $ec2->delete_placement_group(-group_name=>$group_name)
Deletes a placement group from the account.
Required arguments:
-group_name -- The name of the placement group to delete
Returns true on success.
=cut
sub delete_placement_group {
my $self = shift;
my %args = $self->args('-group_name',@_);
my @params = $self->single_parm('GroupName',\%args);
return $self->call('DeletePlacementGroup',@params);
}
=head1 VIRTUAL PRIVATE CLOUDS
EC2 virtual private clouds (VPCs) provide facilities for creating
tiered applications combining public and private subnetworks, and for
extending your home/corporate network into the cloud.
=cut
=head2 $vpc = $ec2->create_vpc(-cidr_block=>$cidr,-instance_tenancy=>$tenancy)
Create a new VPC. This can be called with a single argument, in which
case it is interpreted as the desired CIDR block, or
$vpc = $ec2->$ec2->create_vpc('10.0.0.0/16') or die $ec2->error_str;
Or it can be called with named arguments.
Required arguments:
-cidr_block The Classless Internet Domain Routing address, in the
form xx.xx.xx.xx/xx. One or more subnets will be allocated
from within this block.
Optional arguments:
-instance_tenancy "default" or "dedicated". The latter requests AWS to
launch all your instances in the VPC on single-tenant
hardware (at additional cost).
See
http://docs.amazonwebservices.com/AmazonVPC/latest/UserGuide/VPC_Subnets.html
for a description of the valid CIDRs that can be used with EC2.
On success, this method will return a new VM::EC2::VPC object. You can
then use this object to create new subnets within the VPC:
$vpc = $ec2->create_vpc('10.0.0.0/16') or die $ec2->error_str;
$subnet1 = $vpc->create_subnet('10.0.0.0/24') or die $vpc->error_str;
$subnet2 = $vpc->create_subnet('10.0.1.0/24') or die $vpc->error_str;
$subnet3 = $vpc->create_subnet('10.0.2.0/24') or die $vpc->error_str;
=cut
sub create_vpc {
my $self = shift;
my %args = $self->args('-cidr_block',@_);
$args{-cidr_block} or croak "create_vpc(): must provide a -cidr_block parameter";
my @parm = $self->list_parm('CidrBlock',\%args);
push @parm, $self->single_parm('instanceTenancy',\%args);
return $self->call('CreateVpc',@parm);
}
=head2 @vpc = $ec2->describe_vpcs(@vpc_ids)
=head2 @vpc = $ec2->describe_vpcs(\%filter)
=head2 @vpc = $ec2->describe_vpcs(-vpc_id=>\@list,-filter=>\%filter)
Describe VPCs that you own and return a list of VM::EC2::VPC
objects. Call with no arguments to return all VPCs, or provide a list
of VPC IDs to return information on those only. You may also provide a
filter list, or named argument forms.
Optional arguments:
-vpc_id A scalar or array ref containing the VPC IDs you want
information on.
-filter A hashref of filters to apply to the query.
The filters you can use are described at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVpcs.html
=cut
sub describe_vpcs {
my $self = shift;
my %args = $self->args('-vpc_id',@_);
my @parm = $self->list_parm('VpcId',\%args);
push @parm, $self->filter_parm(\%args);
return $self->call('DescribeVpcs',@parm);
}
=head2 $success = $ec2->delete_vpc($vpc_id)
=head2 $success = $ec2->delete_vpc(-vpc_id=>$vpc_id)
Delete the indicated VPC, returning true if successful.
=cut
sub delete_vpc {
my $self = shift;
my %args = $self->args(-vpc_id => @_);
my @param = $self->single_parm(VpcId=>\%args);
return $self->call('DeleteVpc',@param);
}
=head1 VPC Subnets and Routing
These methods manage subnet objects and the routing among them. A VPC
may have a single subnet or many, and routing rules determine whether
the subnet has access to the internet ("public"), is entirely private,
or is connected to a customer gateway device to form a Virtual Private
Network (VPN) in which your home network's address space is extended
into the Amazon VPC.
All instances in a VPC are located in one subnet or another. Subnets
may be public or private, and are organized in a star topology with a
logical router in the middle.
Although all these methods can be called from VM::EC2 objects, many
are more conveniently called from the VM::EC2::VPC object family. This
allows for steps that typically follow each other, such as creating a
route table and associating it with a subnet, happen
automatically. For example, this series of calls creates a VPC with a
single subnet, creates an Internet gateway attached to the VPC,
associates a new route table with the subnet and then creates a
default route from the subnet to the Internet gateway.
$vpc = $ec2->create_vpc('10.0.0.0/16') or die $ec2->error_str;
$subnet1 = $vpc->create_subnet('10.0.0.0/24') or die $vpc->error_str;
$gateway = $vpc->create_internet_gateway or die $vpc->error_str;
$routeTbl = $subnet->create_route_table or die $vpc->error_str;
$routeTbl->create_route('0.0.0.0/0' => $gateway) or die $vpc->error_str;
=head2 $subnet = $ec2->create_subnet(-vpc_id=>$id,-cidr_block=>$block)
This method creates a new subnet within the given VPC. Pass a VPC
object or VPC ID, and a CIDR block string. If successful, the method
will return a VM::EC2::VPC::Subnet object.
Required arguments:
-vpc_id A VPC ID or previously-created VM::EC2::VPC object.
-cidr_block A CIDR block string in the form "xx.xx.xx.xx/xx". The
CIDR address must be within the CIDR block previously
assigned to the VPC, and must not overlap other subnets
in the VPC.
Optional arguments:
-availability_zone The availability zone for the instances launched
within this instance, either an availability zone
name, or a VM::EC2::AvailabilityZone object. If
this is not specified, then AWS chooses a zone for
you automatically.
=cut
sub create_subnet {
my $self = shift;
my %args = @_;
$args{-vpc_id} && $args{-cidr_block}
or croak "Usage: create_subnet(-vpc_id=>\$id,-cidr_block=>\$block)";
my @parm = map {$self->single_parm($_ => \%args)} qw(VpcId CidrBlock AvailabilityZone);
return $self->call('CreateSubnet',@parm);
}
=head2 $success = $ec2->delete_subnet($subnet_id)
=head2 $success = $ec2->delete_subnet(-subnet_id=>$id)
This method deletes the indicated subnet. It may be called with a
single argument consisting of the subnet ID, or a named argument form
with the argument -subnet_id.
=cut
sub delete_subnet {
my $self = shift;
my %args = $self->args(-subnet_id=>@_);
my @parm = $self->single_parm(SubnetId=>\%args);
return $self->call('DeleteSubnet',@parm);
}
=head2 @subnets = $ec2->describe_subnets(@subnet_ids)
=head2 @subnets = $ec2->describe_subnets(\%filters)
=head2 @subnets = $ec2->describe_subnets(-subnet_id=>$id,
-filter => \%filters)
This method returns a list of VM::EC2::VPC::Subnet objects. Called
with no arguments, it returns all Subnets (not filtered by VPC
Id). Pass a list of subnet IDs or a filter hashref in order to
restrict the search.
Optional arguments:
-subnet_id Scalar or arrayref of subnet IDs.
-filter Hashref of filters.
Available filters are described at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeSubnets.html
=cut
sub describe_subnets {
my $self = shift;
my %args = $self->args(-subnet_id => @_);
my @parm = $self->list_parm('SubnetId',\%args);
push @parm, $self->filter_parm(\%args);
return $self->call('DescribeSubnets',@parm);
}
=head2 $table = $ec2->create_route_table($vpc_id)
=head2 $table = $ec2->create_route_table(-vpc_id=>$id)
This method creates a new route table within the given VPC and returns
a VM::EC2::VPC::RouteTable object. By default, every route table
includes a local route that enables traffic to flow within the
VPC. You may add additional routes using create_route().
This method can be called using a single argument corresponding to VPC
ID for the new route table, or with the named argument form.
Required arguments:
-vpc_id A VPC ID or previously-created VM::EC2::VPC object.
=cut
sub create_route_table {
my $self = shift;
my %args = $self->args(-vpc_id => @_);
$args{-vpc_id}
or croak "Usage: create_subnet(-vpc_id=>\$id)";
my @parm = $self->single_parm(VpcId => \%args);
return $self->call('CreateRouteTable',@parm);
}
=head2 $success = $ec2->delete_route_table($route_table_id)
=head2 $success = $ec2->delete_route_table(-route_table_id=>$id)
This method deletes the indicated route table and all the route
entries within it. It may not be called on the main route table, or if
the route table is currently associated with a subnet.
The method can be called with a single argument corresponding to the
route table's ID, or using the named form with argument -route_table_id.
=cut
sub delete_route_table {
my $self = shift;
my %args = $self->args(-route_table_id=>@_);
my @parm = $self->single_parm(RouteTableId=>\%args);
return $self->call('DeleteRouteTable',@parm);
}
=head2 @tables = $ec2->describe_route_tables(@route_table_ids)
=head2 @tables = $ec2->describe_route_tables(\%filters)
=head2 @tables = $ec2->describe_route_tables(-route_table_id=> \@ids,
-filter => \%filters);
This method describes all or some of the route tables available to
you. You may use the filter to restrict the search to a particular
type of route table using one of the filters described at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeRouteTables.html.
Some of the commonly used filters are:
vpc-id ID of the VPC the route table is in.
association.subnet-id ID of the subnet the route table is
associated with.
route.state State of the route, either 'active' or 'blackhole'
tag:<key> Value of a tag
=cut
sub describe_route_tables {
my $self = shift;
my %args = $self->args(-route_table_id => @_);
my @parm = $self->list_parm('RouteTableId',\%args);
push @parm, $self->filter_parm(\%args);
return $self->call('DescribeRouteTables',@parm);
}
=head2 $associationId = $ec2->associate_route_table($subnet_id => $route_table_id)
=head2 $associationId = $ec2->associate_route_table(-subnet_id => $id,
-route_table_id => $id)
This method associates a route table with a subnet. Both objects must
be in the same VPC. You may use either string IDs, or
VM::EC2::VPC::RouteTable and VM::EC2::VPC::Subnet objects.
On success, an associationID is returned, which you may use to
disassociate the route table from the subnet later. The association ID
can also be found by searching through the VM::EC2::VPC::RouteTable
object.
Required arguments:
-subnet_id The subnet ID or a VM::EC2::VPC::Subnet object.
-route_table_id The route table ID or a M::EC2::VPC::RouteTable object.
It may be more convenient to call the
VM::EC2::VPC::Subnet->associate_route_table() or
VM::EC2::VPC::RouteTable->associate_subnet() methods, which are front
ends to this method.
=cut
sub associate_route_table {
my $self = shift;
my %args;
if ($_[0] !~ /^-/ && @_ == 2) {
@args{qw(-subnet_id -route_table_id)} = @_;
} else {
%args = @_;
}
$args{-subnet_id} && $args{-route_table_id}
or croak "-subnet_id, and -route_table_id arguments required";
my @param = $self->single_parm(SubnetId=>\%args),
$self->single_parm(RouteTableId=>\%args);
return $self->call('AssociateRouteTable',@param);
}
=head2 $success = $ec2->dissociate_route_table($association_id)
=head2 $success = $ec2->dissociate_route_table(-association_id => $id)
This method disassociates a route table from a subnet. You must
provide the association ID (either returned from
associate_route_table() or found among the associations() of a
RouteTable object). You may use the short single-argument form, or the
longer named argument form with the required argument -association_id.
The method returns true on success.
=cut
sub disassociate_route_table {
my $self = shift;
my %args = $self->args('-association_id',@_);
my @params = $self->single_parm('AssociationId',\%args);
return $self->call('DisassociateRouteTable',@params);
}
=head2 $new_association = $ec2->replace_route_table_association($association_id=>$route_table_id)
=head2 $new_association = $ec2->replace_route_table_association(-association_id => $id,
-route_table_id => $id)
This method changes the route table associated with a given
subnet. You must pass the replacement route table ID and the
association ID. To replace the main route table, use its association
ID and the ID of the route table you wish to replace it with.
On success, a new associationID is returned.
Required arguments:
-association_id The association ID
-route_table_id The route table ID or a M::EC2::VPC::RouteTable object.
=cut
sub replace_route_table_association {
my $self = shift;
my %args;
if ($_[0] !~ /^-/ && @_ == 2) {
@args{qw(-association_id -route_table_id)} = @_;
} else {
%args = @_;
}
$args{-association_id} && $args{-route_table_id}
or croak "-association_id, and -route_table_id arguments required";
my @param = $self->single_parm(AssociationId => \%args),
$self->single_parm(RouteTableId => \%args);
return $self->call('ReplaceRouteTableAssociation',@param);
}
=head2 $success = $ec2->create_route($route_table_id,$destination,$target)
=head2 $success = $ec2->create_route(-route_table_id => $id,
-destination_cidr_block => $block,
-target=>$target)
This method creates a routing rule in a route table within a VPC. It
takes three mandatory arguments consisting of the route table, the
CIDR address block to match packet destinations against, and a target
to route matching packets to. The target may be an internet gateway, a
NAT instance, or a network interface ID.
Network packets are routed by matching their destination addresses
against a CIDR block. For example, 0.0.0.0/0 matches all addresses,
while 10.0.1.0/24 matches 10.0.1.* addresses. When a packet matches
more than one rule, the most specific matching routing rule is chosen.
In the named argument form, the following arguments are recognized:
-route_table_id The ID of a route table, or a VM::EC2::VPC::RouteTable
object.
-destination_cidr_block
The CIDR address block to match against packet destinations.
-destination A shorthand version of -destination_cidr_block.
-target The destination of matching packets. See below for valid
targets.
The -target value can be any one of the following:
1. A VM::EC2::VPC::InternetGateway object, or an internet gateway ID matching
the regex /^igw-[0-9a-f]{8}$/
2. A VM::EC2::Instance object, or an instance ID matching the regex
/^i-[0-9a-f]{8}$/.
3. A VM::EC2::NetworkInterface object, or a network interface ID
matching the regex /^eni-[0-9a-f]{8}$/.
On success, this method returns true.
=cut
sub create_route {
my $self = shift;
return $self->_manipulate_route('CreateRoute',@_);
}
=head2 $success = $ec2->delete_route($route_table_id,$destination_block)
This method deletes a route in the specified routing table. The
destination CIDR block is used to indicate which route to delete. On
success, the method returns true.
=cut
sub delete_route {
my $self = shift;
@_ == 2 or croak "Usage: delete_route(\$route_table_id,\$destination_block)";
my %args;
@args{qw(-route_table_id -destination_cidr_block)} = @_;
my @parm = map {$self->single_parm($_,\%args)} qw(RouteTableId DestinationCidrBlock);
return $self->call('DeleteRoute',@parm);
}
=head2 $success = $ec2->replace_route($route_table_id,$destination,$target)
=head2 $success = $ec2->replace_route(-route_table_id => $id,
-destination_cidr_block => $block,
-target=>$target)
This method replaces an existing routing rule in a route table within
a VPC. It takes three mandatory arguments consisting of the route
table, the CIDR address block to match packet destinations against,
and a target to route matching packets to. The target may be an
internet gateway, a NAT instance, or a network interface ID.
Network packets are routed by matching their destination addresses
against a CIDR block. For example, 0.0.0.0/0 matches all addresses,
while 10.0.1.0/24 matches 10.0.1.* addresses. When a packet matches
more than one rule, the most specific matching routing rule is chosen.
In the named argument form, the following arguments are recognized:
-route_table_id The ID of a route table, or a VM::EC2::VPC::RouteTable
object.
-destination_cidr_block
The CIDR address block to match against packet destinations.
-destination A shorthand version of -destination_cidr_block.
-target The destination of matching packets. See below for valid
targets.
The -target value can be any one of the following:
1. A VM::EC2::VPC::InternetGateway object, or an internet gateway ID matching
the regex /^igw-[0-9a-f]{8}$/
2. A VM::EC2::Instance object, or an instance ID matching the regex
/^i-[0-9a-f]{8}$/.
3. A VM::EC2::NetworkInterface object, or a network interface ID
matching the regex /^eni-[0-9a-f]{8}$/.
On success, this method returns true.
=cut
sub replace_route {
my $self = shift;
return $self->_manipulate_route('ReplaceRoute',@_);
}
sub _manipulate_route {
my $self = shift;
my $api_call = shift;
my %args;
if ($_[0] !~ /^-/ && @_ == 3) {
@args{qw(-route_table_id -destination -target)} = @_;
} else {
%args = @_;
}
$args{-destination_cidr_block} ||= $args{-destination};
$args{-destination_cidr_block} && $args{-route_table_id} && $args{-target}
or croak "-route_table_id, -destination_cidr_block, and -target arguments required";
# figure out what the target is.
$args{-gateway_id} = $args{-target} if eval{$args{-target}->isa('VM::EC2::VPC::InternetGateway')}
|| $args{-target} =~ /^igw-[0-9a-f]{8}$/;
$args{-instance_id} = $args{-target} if eval{$args{-target}->isa('VM::EC2::Instance')}
|| $args{-target} =~ /^i-[0-9a-f]{8}$/;
$args{-network_interface_id} = $args{-target} if eval{$args{-target}->isa('VM::EC2::NetworkInterface')}
|| $args{-target} =~ /^eni-[0-9a-f]{8}$/;
my @parm = map {$self->single_parm($_,\%args)}
qw(RouteTableId DestinationCidrBlock GatewayId InstanceId NetworkInterfaceId);
return $self->call($api_call,@parm);
}
=head2 $gateway = $ec2->create_internet_gateway()
This method creates a new Internet gateway. It takes no arguments and
returns a VM::EC2::VPC::InternetGateway object. Gateways are initially
independent of any VPC, but later can be attached to one or more VPCs
using attach_internet_gateway().
=cut
sub create_internet_gateway {
my $self = shift;
return $self->call('CreateInternetGateway');
}
=head2 $success = $ec2->delete_internet_gateway($internet_gateway_id)
=head2 $success = $ec2->delete_internet_gateway(-internet_gateway_id=>$id)
This method deletes the indicated internet gateway. It may be called
with a single argument corresponding to the route table's ID, or using
the named form with argument -internet_gateway_id.
=cut
sub delete_internet_gateway {
my $self = shift;
my %args = $self->args(-internet_gateway_id=>@_);
my @parm = $self->single_parm(InternetGatewayId=>\%args);
return $self->call('DeleteInternetGateway',@parm);
}
=head2 @gateways = $ec2->describe_internet_gateways(@gateway_ids)
=head2 @gateways = $ec2->describe_internet_gateways(\%filters)
=head2 @gateways = $ec2->describe_internet_gateways(-internet_gateway_id=>\@ids,
-filter =>\$filters)
This method describes all or some of the internet gateways available
to you. You may use the filter to restrict the search to a particular
type of internet gateway using one or more of the filters described at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeInternetGateways.html.
Some of the commonly used filters are:
attachment.vpc-id ID of one of the VPCs the gateway is attached to
attachment.state State of the gateway, always "available"
tag:<key> Value of a tag
On success this method returns a list of VM::EC2::VPC::InternetGateway
objects.
=cut
sub describe_internet_gateways {
my $self = shift;
my %args = $self->args(-internet_gateway_id => @_);
my @parm = $self->list_parm('InternetGatewayId',\%args);
push @parm, $self->filter_parm(\%args);
return $self->call('DescribeInternetGateways',@parm);
}
=head2 $boolean = $ec2->attach_internet_gateway($internet_gateway_id,$vpc_id)
=head2 $boolean = $ec2->attach_internet_gateway(-internet_gateway_id => $id,
-vpc_id => $id)
This method attaches an internet gateway to a VPC. You can use
internet gateway and VPC IDs, or their corresponding
VM::EC2::VPC::InternetGateway and VM::EC2::VPC objects.
Required arguments:
-internet_gateway_id ID of the network interface to attach.
-vpc_id ID of the instance to attach the interface to.
On success, this method a true value.
Note that it may be more convenient to attach and detach gateways via
methods in the VM::EC2::VPC and VM::EC2::VPC::Gateway objects.
$vpc->attach_internet_gateway($gateway);
$gateway->attach($vpc);
=cut
sub attach_internet_gateway {
my $self = shift;
my %args;
if ($_[0] !~ /^-/ && @_ == 2) {
@args{qw(-internet_gateway_id -vpc_id)} = @_;
} else {
%args = @_;
}
$args{-internet_gateway_id} && $args{-vpc_id}
or croak "-internet_gateway_id and-vpc_id arguments must be specified";
$args{-device_index} =~ s/^eth//;
my @param = $self->single_parm(InternetGatewayId=>\%args);
push @param,$self->single_parm(VpcId=>\%args);
return $self->call('AttachInternetGateway',@param);
}
=head2 $boolean = $ec2->detach_internet_gateway($internet_gateway_id,$vpc_id)
=head2 $boolean = $ec2->detach_internet_gateway(-internet_gateway_id => $id,
-vpc_id => $id)
This method detaches an internet gateway to a VPC. You can use
internet gateway and VPC IDs, or their corresponding
VM::EC2::VPC::InternetGateway and VM::EC2::VPC objects.
Required arguments:
-internet_gateway_id ID of the network interface to detach.
-vpc_id ID of the VPC to detach the gateway from.
On success, this method a true value.
Note that it may be more convenient to detach and detach gateways via
methods in the VM::EC2::VPC and VM::EC2::VPC::Gateway objects.
$vpc->detach_internet_gateway($gateway);
$gateway->detach($vpc);
=cut
sub detach_internet_gateway {
my $self = shift;
my %args;
if ($_[0] !~ /^-/ && @_ == 2) {
@args{qw(-internet_gateway_id -vpc_id)} = @_;
} else {
%args = @_;
}
$args{-internet_gateway_id} && $args{-vpc_id}
or croak "-internet_gateway_id and-vpc_id arguments must be specified";
$args{-device_index} =~ s/^eth//;
my @param = $self->single_parm(InternetGatewayId=>\%args);
push @param,$self->single_parm(VpcId=>\%args);
return $self->call('DetachInternetGateway',@param);
}
=head2 @acls = $ec2->describe_network_acls(-network_acl_id=>\@ids, -filter=>\%filters)
=head2 @acls = $ec2->describe_network_acls(\@network_acl_ids)
=head2 @acls = $ec2->describe_network_acls(%filters)
Provides information about network ACLs.
Returns a series of L<VM::EC2::VPC::NetworkAcl> objects.
Optional parameters are:
-network_acl_id -- ID of the network ACL(s) to return information on.
This can be a string scalar, or an arrayref.
-filter -- Tags and other filters to apply.
The filter argument is a hashreference in which the keys are the
filter names, and the values are the match strings. Some filters
accept wildcards.
There are a number of filters, which are listed in full at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeNetworkAcls.html
Here is a alpha-sorted list of filter names:
association.association-id, association.network-acl-id,
association.subnet-id, default, entry.cidr, entry.egress,
entry.icmp.code, entry.icmp.type, entry.port-range.from,
entry.port-range.to, entry.protocol, entry.rule-action,
entry.rule-number, network-acl-id, tag-key, tag-value,
tag:key, vpc-id
=cut
sub describe_network_acls {
my $self = shift;
my %args = $self->args('-network_acl_id',@_);
my @params = $self->list_parm('NetworkAclId',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeNetworkAcls',@params);
}
=head2 $acl = $ec2->create_network_acl(-vpc_id=>$vpc_id)
=head2 $acl = $ec2->create_network_acl($vpc_id)
Creates a new network ACL in a VPC. Network ACLs provide an optional layer of
security (on top of security groups) for the instances in a VPC.
Arguments:
-vpc_id -- The VPC ID to create the ACL in
Retuns a VM::EC2::VPC::NetworkAcl object on success.
=cut
sub create_network_acl {
my $self = shift;
my %args = $self->args('-vpc_id',@_);
$args{-vpc_id} or
croak "create_network_acl(): -vpc_id argument missing";
my @params = $self->single_parm('VpcId',\%args);
return $self->call('CreateNetworkAcl',@params);
}
=head2 $boolean = $ec2->delete_network_acl(-network_acl_id=>$id)
=head2 $boolean = $ec2->delete_network_acl($id)
Deletes a network ACL from a VPC. The ACL must not have any subnets associated
with it. The default network ACL cannot be deleted.
Arguments:
-network_acl_id -- The ID of the network ACL to delete
Returns true on successful deletion.
=cut
sub delete_network_acl {
my $self = shift;
my %args = $self->args('-network_acl_id',@_);
my @params = $self->single_parm('NetworkAclId',\%args);
return $self->call('DeleteNetworkAcl',@params);
}
=head2 $boolean = $ec2->create_network_acl_entry(%args)
Creates an entry (i.e., rule) in a network ACL with the rule number you
specified. Each network ACL has a set of numbered ingress rules and a
separate set of numbered egress rules. When determining whether a packet
should be allowed in or out of a subnet associated with the ACL, Amazon
VPC processes the entries in the ACL according to the rule numbers, in
ascending order.
Arguments:
-network_acl_id -- ID of the ACL where the entry will be created
(Required)
-rule_number -- Rule number to assign to the entry (e.g., 100).
ACL entries are processed in ascending order by
rule number. Positive integer from 1 to 32766.
(Required)
-protocol -- The IP protocol the rule applies to. You can use
-1 to mean all protocols. See
http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
for a list of protocol numbers. (Required)
-rule_action -- Indicates whether to allow or deny traffic that
matches the rule. allow | deny (Required)
-egress -- Indicates whether this rule applies to egress
traffic from the subnet (true) or ingress traffic
to the subnet (false). Default is false.
-cidr_block -- The CIDR range to allow or deny, in CIDR notation
(e.g., 172.16.0.0/24). (Required)
-icmp_code -- For the ICMP protocol, the ICMP code. You can use
-1 to specify all ICMP codes for the given ICMP
type. Required if specifying 1 (ICMP) for protocol.
-icmp_type -- For the ICMP protocol, the ICMP type. You can use
-1 to specify all ICMP types. Required if
specifying 1 (ICMP) for the protocol
-port_from -- The first port in the range. Required if specifying
6 (TCP) or 17 (UDP) for the protocol.
-port_to -- The last port in the range. Required if specifying
6 (TCP) or 17 (UDP) for the protocol.
Returns true on successful creation.
=cut
sub create_network_acl_entry {
my $self = shift;
my %args = @_;
$args{-network_acl_id} or
croak "create_network_acl_entry(): -network_acl_id argument missing";
$args{-rule_number} or
croak "create_network_acl_entry(): -rule_number argument missing";
defined $args{-protocol} or
croak "create_network_acl_entry(): -protocol argument missing";
$args{-rule_action} or
croak "create_network_acl_entry(): -rule_action argument missing";
$args{-cidr_block} or
croak "create_network_acl_entry(): -cidr_block argument missing";
if ($args{-protocol} == 1) {
defined $args{-icmp_type} && defined $args{-icmp_code} or
croak "create_network_acl_entry(): -icmp_type or -icmp_code argument missing";
}
elsif ($args{-protocol} == 6 || $args{-protocol} == 17) {
defined $args{-port_from} or
croak "create_network_acl_entry(): -port_from argument missing";
$args{-port_to} = $args{-port_from} if (! defined $args{-port_to});
}
$args{'-Icmp.Type'} = $args{-icmp_type};
$args{'-Icmp.Code'} = $args{-icmp_code};
$args{'-PortRange.From'} = $args{-port_from};
$args{'-PortRange.To'} = $args{-port_to};
my @params;
push @params,$self->single_parm($_,\%args) foreach
qw(NetworkAclId RuleNumber Protocol RuleAction Egress CidrBlock
Icmp.Code Icmp.Type PortRange.From PortRange.To);
return $self->call('CreateNetworkAclEntry',@params);
}
=head2 $success = $ec2->delete_network_acl_entry(-network_acl_id=>$id,
-rule_number =>$int,
-egress =>$bool)
Deletes an ingress or egress entry (i.e., rule) from a network ACL.
Arguments:
-network_acl_id -- ID of the ACL where the entry will be created
-rule_number -- Rule number of the entry (e.g., 100).
Optional arguments:
-egress -- Whether the rule to delete is an egress rule (true) or ingress
rule (false). Default is false.
Returns true on successful deletion.
=cut
sub delete_network_acl_entry {
my $self = shift;
my %args = @_;
$args{-network_acl_id} or
croak "delete_network_acl_entry(): -network_acl_id argument missing";
$args{-rule_number} or
croak "delete_network_acl_entry(): -rule_number argument missing";
my @params;
push @params,$self->single_parm($_,\%args) foreach
qw(NetworkAclId RuleNumber Egress);
return $self->call('DeleteNetworkAclEntry',@params);
}
=head2 $assoc_id = $ec2->replace_network_acl_association(-association_id=>$assoc_id,
-network_acl_id=>$id)
Changes which network ACL a subnet is associated with. By default when you
create a subnet, it's automatically associated with the default network ACL.
Arguments:
-association_id -- The ID of the association to replace
-network_acl_id -- The ID of the network ACL to associated the subnet with
Returns the new association ID.
=cut
sub replace_network_acl_association {
my $self = shift;
my %args = @_;
$args{-association_id} or
croak "replace_network_acl_association(): -association_id argument missing";
$args{-network_acl_id} or
croak "replace_network_acl_association(): -network_acl_id argument missing";
my @params;
push @params,$self->single_parm($_,\%args) foreach
qw(AssociationId NetworkAclId);
return $self->call('ReplaceNetworkAclAssociation',@params);
}
=head2 $success = $ec2->replace_network_acl_entry(%args)
Replaces an entry (i.e., rule) in a network ACL.
Arguments:
-network_acl_id -- ID of the ACL where the entry will be created
(Required)
-rule_number -- Rule number of the entry to replace. (Required)
-protocol -- The IP protocol the rule applies to. You can use
-1 to mean all protocols. See
http://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml
for a list of protocol numbers. (Required)
-rule_action -- Indicates whether to allow or deny traffic that
matches the rule. allow | deny (Required)
-egress -- Indicates whether this rule applies to egress
traffic from the subnet (true) or ingress traffic
to the subnet (false). Default is false.
-cidr_block -- The CIDR range to allow or deny, in CIDR notation
(e.g., 172.16.0.0/24). (Required)
-icmp_code -- For the ICMP protocol, the ICMP code. You can use
-1 to specify all ICMP codes for the given ICMP
type. Required if specifying 1 (ICMP) for protocol.
-icmp_type -- For the ICMP protocol, the ICMP type. You can use
-1 to specify all ICMP types. Required if
specifying 1 (ICMP) for the protocol
-port_from -- The first port in the range. Required if specifying
6 (TCP) or 17 (UDP) for the protocol.
-port_to -- The last port in the range. Only required if
specifying 6 (TCP) or 17 (UDP) for the protocol and
is a different port than -port_from.
Returns true on successful replacement.
=cut
sub replace_network_acl_entry {
my $self = shift;
my %args = @_;
$args{-network_acl_id} or
croak "replace_network_acl_entry(): -network_acl_id argument missing";
$args{-rule_number} or
croak "replace_network_acl_entry(): -rule_number argument missing";
$args{-protocol} or
croak "replace_network_acl_entry(): -protocol argument missing";
$args{-rule_action} or
croak "replace_network_acl_entry(): -rule_action argument missing";
if ($args{-protocol} == 1) {
defined $args{-icmp_type} && defined $args{-icmp_code} or
croak "replace_network_acl_entry(): -icmp_type or -icmp_code argument missing";
}
elsif ($args{-protocol} == 6 || $args{-protocol} == 17) {
defined $args{-port_from} or
croak "create_network_acl_entry(): -port_from argument missing";
$args{-port_to} = $args{-port_from} if (! defined $args{-port_to});
}
$args{'-Icmp.Type'} = $args{-icmp_type};
$args{'-Icmp.Code'} = $args{-icmp_code};
$args{'-PortRange.From'} = $args{-port_from};
$args{'-PortRange.To'} = $args{-port_to};
my @params;
push @params,$self->single_parm($_,\%args) foreach
qw(NetworkAclId RuleNumber Protocol RuleAction Egress CidrBlock
Icmp.Code Icmp.Type PortRange.From PortRange.To);
return $self->call('ReplaceNetworkAclEntry',@params);
}
=head1 DHCP Options
These methods manage DHCP Option objects, which can then be applied to
a VPC to configure the DHCP options applied to running instances.
=head2 $options = $ec2->create_dhcp_options(\%configuration_list)
This method creates a DhcpOption object, The single required argument is a
configuration list hash (which can be passed either as a hashref or a
flattened hash) with one or more of the following keys:
-domain_name Domain name for instances running in this VPC.
-domain_name_servers Scalar or arrayref containing up to 4 IP addresses of
domain name servers for this VPC.
-ntp_servers Scalar or arrayref containing up to 4 IP addresses
of network time protocol servers
-netbios_name_servers Scalar or arrayref containing up to 4 IP addresses for
NetBIOS name servers.
-netbios_node_type The NetBios node type (1,2,4 or 8). Amazon recommends
using "2" at this time.
On successful completion, a VM::EC2::VPC::DhcpOptions object will be
returned. This can be associated with a VPC using the VPC object's
set_dhcp_options() method:
$vpc = $ec2->create_vpc(...);
$options = $ec2->create_dhcp_options(-domain_name=>'test.com',
-domain_name_servers=>['204.16.255.55','216.239.34.10']);
$vpc->set_dhcp_options($options);
=cut
# { 'domain-name-servers' => ['192.168.2.1','192.168.2.2'],'domain-name'=>'example.com'}
sub create_dhcp_options {
my $self = shift;
my %args;
if (@_ == 1 && ref $_[0] eq 'HASH') {
%args = %{$_[0]};
} else {
%args = @_;
}
my @parm;
my $count = 1;
for my $key (sort keys %args) {
my $value = $args{$key};
my @values = ref $value && ref $value eq 'ARRAY' ? @$value : $value;
$key =~ s/^-//;
$key =~ s/_/-/g;
my $item = 1;
push @parm,("DhcpConfiguration.$count.Key" => $key);
push @parm,("DhcpConfiguration.$count.Value.".$item++ => $_) foreach @values;
$count++;
}
return $self->call('CreateDhcpOptions',@parm);
}
=head2 $success = $ec2->delete_dhcp_options($dhcp_id)
Delete the indicated DHCPOptions, returning true if successful. You
may also use the named argument -dhcp_options_id..
=cut
sub delete_dhcp_options {
my $self = shift;
my %args = $self->args(-dhcp_options_id => @_);
my @param = $self->single_parm(DhcpOptionsId=>\%args);
return $self->call('DeleteDhcpOptions',@param);
}
=head2 @options = $ec2->describe_dhcp_options(@option_ids)
=head2 @options = $ec2->describe_dhcp_options(\%filters)
=head2 @options = $ec2->describe_dhcp_options(-dhcp_options_id=>$id,
-filter => \%filters)
This method returns a list of VM::EC2::VPC::DhcpOptions objects, which
describe a set of DHCP options that can be assigned to a VPC. Called
with no arguments, it returns all DhcpOptions. Pass a list of option
IDs or a filter hashref in order to restrict the search.
Optional arguments:
-dhcp_options_id Scalar or arrayref of DhcpOption IDs.
-filter Hashref of filters.
Available filters are described at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeDhcpOptions.html.
=cut
sub describe_dhcp_options {
my $self = shift;
my %args = $self->args(-dhcp_options_id => @_);
my @parm = $self->list_parm('DhcpOptionsId',\%args);
push @parm, $self->filter_parm(\%args);
return $self->call('DescribeDhcpOptions',@parm);
}
=head2 $success = $ec2->associate_dhcp_options($vpc_id => $dhcp_id)
=head2 $success = $ec2->associate_dhcp_options(-vpc_id => $vpc_id,-dhcp_options_id => $dhcp_id)
Associate a VPC ID with a DHCP option set. Pass an ID of 'default' to
restore the default DHCP options for the VPC.
=cut
sub associate_dhcp_options {
my $self = shift;
my %args;
if ($_[0] !~ /^-/ && @_ == 2) {
@args{qw(-vpc_id -dhcp_options_id)} = @_;
} else {
%args = @_;
}
$args{-vpc_id} && $args{-dhcp_options_id}
or croak "-vpc_id and -dhcp_options_id must be specified";
my @param = $self->single_parm(DhcpOptionsId=> \%args);
push @param, $self->single_parm(VpcId => \%args);
return $self->call('AssociateDhcpOptions',@param);
}
=head1 Virtual Private Networks
These methods create and manage Virtual Private Network (VPN) connections
to an Amazon Virtual Private Cloud (VPC).
=head2 @gtwys = $ec2->describe_vpn_gateways(-vpn_gateway_id=>\@ids,
-filter =>\%filters)
=head2 @gtwys = $ec2->describe_vpn_gateways(@vpn_gateway_ids)
=head2 @gtwys = $ec2->describe_vpn_gateways(%filters)
Provides information on VPN gateways.
Return a series of VM::EC2::VPC::VpnGateway objects. When called with no
arguments, returns all VPN gateways. Pass a list of VPN gateway IDs or
use the assorted filters to restrict the search.
Optional parameters are:
-vpn_gateway_id ID of the gateway(s) to return information on.
This can be a string scalar, or an arrayref.
-filter Tags and other filters to apply.
The filter argument is a hashreference in which the keys are the
filter names, and the values are the match strings. Some filters
accept wildcards.
There are a number of filters, which are listed in full at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVpnGateways.html
Here is a alpha-sorted list of filter names: attachment.state,
attachment.vpc-id, availability-zone, state, tag-key, tag-value, tag:key, type,
vpn-gateway-id
=cut
sub describe_vpn_gateways {
my $self = shift;
my %args = $self->args('-vpn_gateway_id',@_);
my @params = $self->list_parm('VpnGatewayId',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeVpnGateways',@params);
}
=head2 $vpn_gateway = $ec2->create_vpn_gateway(-type=>$type)
=head2 $vpn_gateway = $ec2->create_vpn_gateway($type)
=head2 $vpn_gateway = $ec2->create_vpn_gateway
Creates a new virtual private gateway. A virtual private gateway is the
VPC-side endpoint for a VPN connection. You can create a virtual private
gateway before creating the VPC itself.
-type switch is optional as there is only one type as of API 2012-06-15
Returns a VM::EC2::VPC::VpnGateway object on success
=cut
sub create_vpn_gateway {
my $self = shift;
my %args = $self->args('-type',@_);
$args{-type} ||= 'ipsec.1';
my @params = $self->list_parm('Type',\%args);
return $self->call('CreateVpnGateway',@params);
}
=head2 $success = $ec2->delete_vpn_gateway(-vpn_gateway_id=>$id);
=head2 $success = $ec2->delete_vpn_gateway($id);
Deletes a virtual private gateway. Use this when a VPC and all its associated
components are no longer needed. It is recommended that before deleting a
virtual private gateway, detach it from the VPC and delete the VPN connection.
Note that it is not necessary to delete the virtual private gateway if the VPN
connection between the VPC and data center needs to be recreated.
Arguments:
-vpn_gateway_id -- The ID of the VPN gateway to delete.
Returns true on successful deletion
=cut
sub delete_vpn_gateway {
my $self = shift;
my %args = $self->args('-vpn_gateway_id',@_);
$args{-vpn_gateway_id} or
croak "delete_vpn_gateway(): -vpn_gateway_id argument missing";
my @params = $self->single_parm('VpnGatewayId',\%args);
return $self->call('DeleteVpnGateway',@params);
}
=head2 $state = $ec2->attach_vpn_gateway(-vpn_gateway_id=>$vpn_gtwy_id,
-vpc_id =>$vpc_id)
Attaches a virtual private gateway to a VPC.
Arguments:
-vpc_id -- The ID of the VPC to attach the VPN gateway to
-vpn_gateway_id -- The ID of the VPN gateway to attach
Returns the state of the attachment, one of:
attaching | attached | detaching | detached
=cut
sub attach_vpn_gateway {
my $self = shift;
my %args = @_;
$args{-vpn_gateway_id} or
croak "attach_vpn_gateway(): -vpn_gateway_id argument missing";
$args{-vpc_id} or
croak "attach_vpn_gateway(): -vpc_id argument missing";
my @params = $self->single_parm('VpnGatewayId',\%args);
push @params, $self->single_parm('VpcId',\%args);
return $self->call('AttachVpnGateway',@params);
}
=head2 $success = $ec2->detach_vpn_gateway(-vpn_gateway_id=>$vpn_gtwy_id,
-vpc_id =>$vpc_id)
Detaches a virtual private gateway from a VPC. You do this if you're
planning to turn off the VPC and not use it anymore. You can confirm
a virtual private gateway has been completely detached from a VPC by
describing the virtual private gateway (any attachments to the
virtual private gateway are also described).
You must wait for the attachment's state to switch to detached
before you can delete the VPC or attach a different VPC to the
virtual private gateway.
Arguments:
-vpc_id -- The ID of the VPC to detach the VPN gateway from
-vpn_gateway_id -- The ID of the VPN gateway to detach
Returns true on successful detachment.
=cut
sub detach_vpn_gateway {
my $self = shift;
my %args = @_;
$args{-vpn_gateway_id} or
croak "detach_vpn_gateway(): -vpn_gateway_id argument missing";
$args{-vpc_id} or
croak "detach_vpn_gateway(): -vpc_id argument missing";
my @params = $self->single_parm('VpnGatewayId',\%args);
push @params, $self->single_parm('VpcId',\%args);
return $self->call('DetachVpnGateway',@params);
}
=head2 @vpn_connections = $ec2->describe_vpn_connections(-vpn_connection_id=>\@ids,
-filter=>\%filters);
=head2 @vpn_connections = $ec2->describe_vpn_connections(@vpn_connection_ids)
=head2 @vpn_connections = $ec2->describe_vpn_connections(%filters);
Gives information about VPN connections
Returns a series of VM::EC2::VPC::VpnConnection objects.
Optional parameters are:
-vpn_connection_id ID of the connection(s) to return information on.
This can be a string scalar, or an arrayref.
-filter Tags and other filters to apply.
The filter argument is a hashreference in which the keys are the
filter names, and the values are the match strings. Some filters
accept wildcards.
There are a number of filters, which are listed in full at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeVpnConnections.html
Here is a alpha-sorted list of filter names:
customer-gateway-configuration, customer-gateway-id, state,
tag-key, tag-value, tag:key, type, vpn-connection-id,
vpn-gateway-id
=cut
sub describe_vpn_connections {
my $self = shift;
my %args = $self->args('-vpn_connection_id',@_);
my @params = $self->list_parm('VpnConnectionId',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeVpnConnections',@params);
}
=head2 $vpn_connection = $ec2->create_vpn_connection(-type =>$type,
-customer_gateway_id=>$gtwy_id,
-vpn_gateway_id =>$vpn_id)
Creates a new VPN connection between an existing virtual private
gateway and a VPN customer gateway. The only supported connection
type is ipsec.1.
Required Arguments:
-customer_gateway_id -- The ID of the customer gateway
-vpn_gateway_id -- The ID of the VPN gateway
Optional arguments:
-type -- Default is the only currently available option:
ipsec.1 (API 2012-06-15)
-static_routes_only -- Indicates whether or not the VPN connection
requires static routes. If you are creating a VPN
connection for a device that does not support
BGP, you must specify this value as true.
Returns a L<VM::EC2::VPC::VpnConnection> object.
=cut
sub create_vpn_connection {
my $self = shift;
my %args = @_;
$args{-type} ||= 'ipsec.1';
$args{-vpn_gateway_id} or
croak "create_vpn_connection(): -vpn_gateway_id argument missing";
$args{-customer_gateway_id} or
croak "create_vpn_connection(): -customer_gateway_id argument missing";
$args{'Options.StaticRoutesOnly'} = $args{-static_routes_only};
my @params;
push @params,$self->single_parm($_,\%args) foreach
qw(VpnGatewayId CustomerGatewayId Type);
push @params,$self->boolean_parm('Options.StaticRoutesOnly',\%args);
return $self->call('CreateVpnConnection',@params);
}
=head2 $success = $ec2->delete_vpn_connection(-vpn_connection_id=>$vpn_id)
=head2 $success = $ec2->delete_vpn_connection($vpn_id)
Deletes a VPN connection. Use this if you want to delete a VPC and
all its associated components. Another reason to use this operation
is if you believe the tunnel credentials for your VPN connection
have been compromised. In that situation, you can delete the VPN
connection and create a new one that has new keys, without needing
to delete the VPC or virtual private gateway. If you create a new
VPN connection, you must reconfigure the customer gateway using the
new configuration information returned with the new VPN connection ID.
Arguments:
-vpn_connection_id -- The ID of the VPN connection to delete
Returns true on successful deletion.
=cut
sub delete_vpn_connection {
my $self = shift;
my %args = $self->args('-vpn_connection_id',@_);
$args{-vpn_connection_id} or
croak "delete_vpn_connection(): -vpn_connection_id argument missing";
my @params = $self->single_parm('VpnConnectionId',\%args);
return $self->call('DeleteVpnConnection',@params);
}
=head2 @gtwys = $ec2->describe_customer_gateways(-customer_gateway_id=>\@ids,
-filter =>\%filters)
=head2 @gtwys = $ec2->describe_customer_gateways(\@customer_gateway_ids)
=head2 @gtwys = $ec2->describe_customer_gateways(%filters)
Provides information on VPN customer gateways.
Returns a series of VM::EC2::VPC::CustomerGateway objects.
Optional parameters are:
-customer_gateway_id ID of the gateway(s) to return information on.
This can be a string scalar, or an arrayref.
-filter Tags and other filters to apply.
The filter argument is a hashreference in which the keys are the filter names,
and the values are the match strings. Some filters accept wildcards.
There are a number of filters, which are listed in full at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeCustomerGateways.html
Here is a alpha-sorted list of filter names: bgp-asn, customer-gateway-id,
ip-address, state, type, tag-key, tag-value, tag:key
=cut
sub describe_customer_gateways {
my $self = shift;
my %args = $self->args('-customer_gateway_id',@_);
my @params = $self->list_parm('CustomerGatewayId',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeCustomerGateways',@params);
}
=head2 $cust_gtwy = $ec2->create_customer_gateway(-type =>$type,
-ip_address=>$ip,
-bgp_asn =>$asn)
Provides information to AWS about a VPN customer gateway device. The customer
gateway is the appliance at the customer end of the VPN connection (compared
to the virtual private gateway, which is the device at the AWS side of the
VPN connection).
Arguments:
-ip_address -- The IP address of the customer gateway appliance
-bgp_asn -- The Border Gateway Protocol (BGP) Autonomous System Number
(ASN) of the customer gateway
-type -- Optional as there is only currently (2012-06-15 API) only
one type (ipsec.1)
-ip -- Alias for -ip_address
Returns a L<VM::EC2::VPC::CustomerGateway> object on success.
=cut
sub create_customer_gateway {
my $self = shift;
my %args = @_;
$args{-type} ||= 'ipsec.1';
$args{-ip_address} ||= $args{-ip};
$args{-ip_address} or
croak "create_customer_gateway(): -ip_address argument missing";
$args{-bgp_asn} or
croak "create_customer_gateway(): -bgp_asn argument missing";
my @params = $self->single_parm('Type',\%args);
push @params, $self->single_parm('IpAddress',\%args);
push @params, $self->single_parm('BgpAsn',\%args);
return $self->call('CreateCustomerGateway',@params);
}
=head2 $success = $ec2->delete_customer_gateway(-customer_gateway_id=>$id)
=head2 $success = $ec2->delete_customer_gateway($id)
Deletes a VPN customer gateway. You must delete the VPN connection before
deleting the customer gateway.
Arguments:
-customer_gateway_id -- The ID of the customer gateway to delete
Returns true on successful deletion.
=cut
sub delete_customer_gateway {
my $self = shift;
my %args = $self->args('-customer_gateway_id',@_);
$args{-customer_gateway_id} or
croak "delete_customer_gateway(): -customer_gateway_id argument missing";
my @params = $self->single_parm('CustomerGatewayId',\%args);
return $self->call('DeleteCustomerGateway',@params);
}
=head2 $success = $ec2->create_vpn_connection_route(-destination_cidr_block=>$cidr,
-vpn_connection_id =>$id)
Creates a new static route associated with a VPN connection between an existing
virtual private gateway and a VPN customer gateway. The static route allows
traffic to be routed from the virtual private gateway to the VPN customer
gateway.
Arguments:
-destination_cidr_block -- The CIDR block associated with the local subnet
of the customer data center.
-vpn_connection_id -- The ID of the VPN connection.
Returns true on successsful creation.
=cut
sub create_vpn_connection_route {
my $self = shift;
my %args = @_;
$args{-destination_cidr_block} or
croak "create_vpn_connection_route(): -destination_cidr_block argument missing";
$args{-vpn_connection_id} or
croak "create_vpn_connection_route(): -vpn_connection_id argument missing";
my @params = $self->single_parm($_,\%args)
foreach qw(DestinationCidrBlock VpnConnectionId);
return $self->call('CreateVpnConnectionRoute',@params);
}
=head2 $success = $ec2->delete_vpn_connection_route(-destination_cidr_block=>$cidr,
-vpn_connection_id =>$id)
Deletes a static route associated with a VPN connection between an existing
virtual private gateway and a VPN customer gateway. The static route allows
traffic to be routed from the virtual private gateway to the VPN customer
gateway.
Arguments:
-destination_cidr_block -- The CIDR block associated with the local subnet
of the customer data center.
-vpn_connection_id -- The ID of the VPN connection.
Returns true on successsful deletion.
=cut
sub delete_vpn_connection_route {
my $self = shift;
my %args = @_;
$args{-destination_cidr_block} or
croak "delete_vpn_connection_route(): -destination_cidr_block argument missing";
$args{-vpn_connection_id} or
croak "delete_vpn_connection_route(): -vpn_connection_id argument missing";
my @params = $self->single_parm($_,\%args)
foreach qw(DestinationCidrBlock VpnConnectionId);
return $self->call('DeleteVpnConnectionRoute',@params);
}
=head2 $success = $ec2->disable_vgw_route_propogation(-route_table_id=>$rt_id,
-gateway_id =>$gtwy_id)
Disables a virtual private gateway (VGW) from propagating routes to the routing
tables of an Amazon VPC.
Arguments:
-route_table_id -- The ID of the routing table.
-gateway_id -- The ID of the virtual private gateway.
Returns true on successful disablement.
=cut
sub disable_vgw_route_propogation {
my $self = shift;
my %args = @_;
$args{-route_table_id} or
croak "disable_vgw_route_propogation(): -route_table_id argument missing";
$args{-gateway_id} or
croak "disable_vgw_route_propogation(): -gateway_id argument missing";
my @params = $self->single_parm($_,\%args)
foreach qw(RouteTableId GatewayId);
return $self->call('DisableVgwRoutePropagation',@params);
}
=head2 $success = $ec2->enable_vgw_route_propogation(-route_table_id=>$rt_id,
-gateway_id =>$gtwy_id)
Enables a virtual private gateway (VGW) to propagate routes to the routing
tables of an Amazon VPC.
Arguments:
-route_table_id -- The ID of the routing table.
-gateway_id -- The ID of the virtual private gateway.
Returns true on successful enablement.
=cut
sub enable_vgw_route_propogation {
my $self = shift;
my %args = @_;
$args{-route_table_id} or
croak "enable_vgw_route_propogation(): -route_table_id argument missing";
$args{-gateway_id} or
croak "enable_vgw_route_propogation(): -gateway_id argument missing";
my @params = $self->single_parm($_,\%args)
foreach qw(RouteTableId GatewayId);
return $self->call('EnableVgwRoutePropagation',@params);
}
=head1 Elastic Network Interfaces
These methods create and manage Elastic Network Interfaces (ENI). Once
created, an ENI can be attached to instances and/or be associated with
a public IP address. ENIs can only be used in conjunction with VPC
instances.
=head2 $interface = $ec2->create_network_interface($subnet_id)
=head2 $interface = $ec2->create_network_interface(%args)
This method creates an elastic network interface (ENI). If only a
single argument is provided, it is treated as the ID of the VPC subnet
to associate with the ENI. If multiple arguments are provided, they
are treated as -arg=>value parameter pairs.
Arguments:
The -subnet_id argument is mandatory. Others are optional.
-subnet_id -- ID of the VPC subnet to associate with the network
interface (mandatory)
-private_ip_address -- The primary private IP address of the network interface,
or a reference to an array of private IP addresses. In the
latter case, the first element of the array becomes the
primary address, and the subsequent ones become secondary
addresses. If no private IP address is specified, one will
be chosen for you. See below for more information on this
parameter.
-private_ip_addresses -- Same as -private_ip_address, for readability.
-secondary_ip_address_count -- An integer requesting this number of secondary IP
addresses to be allocated automatically. If present,
cannot provide any secondary addresses explicitly.
-description -- Description of this ENI.
-security_group_id -- Array reference or scalar containing IDs of the security
group(s) to assign to this interface.
You can assign multiple IP addresses to the interface explicitly, or
by allowing EC2 to choose addresses within the designated subnet
automatically. The following examples demonstrate the syntax:
# one primary address, chosen explicitly
-private_ip_address => '192.168.0.12'
# one primary address and two secondary addresses, chosen explicitly
-private_ip_address => ['192.168.0.12','192.168.0.200','192.168.0.201']
# one primary address chosen explicitly, and two secondaries chosen automatically
-private_ip_address => ['192.168.0.12','auto','auto']
# one primary address chosen explicitly, and two secondaries chosen automatically (another syntax)
-private_ip_address => ['192.168.0.12',2]
# one primary address chosen automatically, and two secondaries chosen automatically
-private_ip_address => [auto,2]
You cannot assign some secondary addresses explicitly and others
automatically on the same ENI. If you provide no -private_ip_address
parameter at all, then a single private IP address will be chosen for
you (the same as -private_ip_address=>'auto').
The return value is a VM::EC2::NetworkInterface object
=cut
# NOTE: there is code overlap with network_interface_parm()
sub create_network_interface {
my $self = shift;
my %args = $self->args(-subnet_id=>@_);
$args{-subnet_id} or croak "Usage: create_network_interface(-subnet_id=>\$id,\@more_args)";
my @parm = $self->single_parm('SubnetId',\%args);
push @parm, $self->single_parm('Description',\%args);
push @parm, $self->list_parm('SecurityGroupId',\%args);
my $address = $args{-private_ip_address} || $args{-private_ip_addresses};
my $auto_count;
if ($address) {
my $c = 0;
my @addresses = ref $address && ref $address eq 'ARRAY' ? @$address : ($address);
my $primary = shift @addresses;
unless ($primary eq 'auto') {
push @parm, ("PrivateIpAddresses.$c.PrivateIpAddress" => $primary);
push @parm, ("PrivateIpAddresses.$c.Primary" => 'true');
}
# deal with automatic secondary addresses .. this seems needlessly complex
if (my @auto = grep {/auto/i} @addresses) {
@auto == @addresses or croak "cannot request both explicit and automatic secondary IP addresses";
$auto_count = @auto;
}
$auto_count = $addresses[0] if @addresses == 1 && $addresses[0] =~ /^\d+$/;
$auto_count ||= $args{-secondary_ip_address_count};
unless ($auto_count) {
foreach (@addresses) {
$c++;
push @parm,("PrivateIpAddresses.$c.PrivateIpAddress" => $_ );
push @parm,("PrivateIpAddresses.$c.Primary" => 'false');
}
}
}
push @parm,('SecondaryPrivateIpAddressCount'=>$auto_count) if $auto_count ||= $args{-secondary_ip_address_count};
$self->call('CreateNetworkInterface',@parm);
}
=head2 $result = $ec2->assign_private_ip_addresses(%args)
Assign one or more secondary private IP addresses to a network
interface. You can either set the addresses explicitly, or provide a
count of secondary addresses, and let Amazon select them for you.
Required arguments:
-network_interface_id The network interface to which the IP address(es)
will be assigned.
-private_ip_address One or more secondary IP addresses, as a scalar string
-private_ip_addresses or array reference. (The two arguments are equivalent).
Optional arguments:
-allow_reassignment If true, allow assignment of an IP address is already in
use by another network interface or instance.
The following are valid arguments to -private_ip_address:
-private_ip_address => '192.168.0.12' # single address
-private_ip_address => ['192.168.0.12','192.168.0.13] # multiple addresses
-private_ip_address => 3 # autoselect three addresses
The mixed form of address, such as ['192.168.0.12','auto'] is not allowed in this call.
On success, this method returns true.
=cut
sub assign_private_ip_addresses {
my $self = shift;
my %args = $self->args(-network_interface_id => @_);
$args{-private_ip_address} ||= $args{-private_ip_addresses};
$args{-network_interface_id} && $args{-private_ip_address}
or croak "usage: assign_private_ip_addresses(-network_interface_id=>\$id,-private_ip_address=>\\\@addresses)";
my @parms = $self->single_parm('NetworkInterfaceId',\%args);
if (!ref($args{-private_ip_address}) && $args{-private_ip_address} =~ /^\d+$/) {
push @parms,('SecondaryPrivateIpAddressCount' => $args{-private_ip_address});
} else {
push @parms,$self->list_parm('PrivateIpAddress',\%args);
}
push @parms,('AllowReassignment' => $args{-allow_reassignment} ? 'true' : 'false')
if exists $args{-allow_reassignment};
$self->call('AssignPrivateIpAddresses',@parms);
}
=head2 $result = $ec2->unassign_private_ip_addresses(%args)
Unassign one or more secondary private IP addresses from a network
interface.
Required arguments:
-network_interface_id The network interface to which the IP address(es)
will be assigned.
-private_ip_address One or more secondary IP addresses, as a scalar string
-private_ip_addresses or array reference. (The two arguments are equivalent).
The following are valid arguments to -private_ip_address:
-private_ip_address => '192.168.0.12' # single address
-private_ip_address => ['192.168.0.12','192.168.0.13] # multiple addresses
On success, this method returns true.
=cut
sub unassign_private_ip_addresses {
my $self = shift;
my %args = $self->args(-network_interface_id => @_);
$args{-private_ip_address} ||= $args{-private_ip_addresses};
$args{-network_interface_id} && $args{-private_ip_address}
or croak "usage: assign_private_ip_addresses(-network_interface_id=>\$id,-private_ip_address=>\\\@addresses)";
my @parms = $self->single_parm('NetworkInterfaceId',\%args);
push @parms,$self->list_parm('PrivateIpAddress',\%args);
$self->call('UnassignPrivateIpAddresses',@parms);
}
=head2 $result = $ec2->delete_network_interface($network_interface_id);
=head2 $result = $ec2->delete_network_interface(-network_interface_id => $id);
Deletes the specified network interface. Returns a boolean indicating
success of the delete operation.
=cut
sub delete_network_interface {
my $self = shift;
my %args = $self->args(-network_interface_id => @_);
my @param = $self->single_parm(NetworkInterfaceId=>\%args);
return $self->call('DeleteNetworkInterface',@param);
}
=head2 @ifs = $ec2->describe_network_interfaces(@interface_ids)
=head2 @ifs = $ec2->describe_network_interfaces(\%filters)
=head2 @ifs = $ec2->describe_network_interfaces(-network_interface_id=>\@interface_ids,-filter=>\%filters)
Return a list of elastic network interfaces as
VM::EC2::VPC::NetworkInterface objects. You may restrict the list by
passing a list of network interface IDs, a hashref of filters or by
using the full named-parameter form.
Optional arguments:
-network_interface_id A single network interface ID or an arrayref to
a list of IDs.
-filter A hashref for filtering on tags and other attributes.
The list of valid filters can be found at
http://docs.amazonwebservices.com/AWSEC2/latest/APIReference/ApiReference-query-DescribeNetworkInterfaces.html.
=cut
sub describe_network_interfaces {
my $self = shift;
my %args = $self->args(-network_interface_id=>@_);
my @params = $self->list_parm('NetworkInterfaceId',\%args);
push @params,$self->filter_parm(\%args);
return $self->call('DescribeNetworkInterfaces',@params);
}
=head2 @data = $ec2->describe_network_interface_attribute($network_id,$attribute)
This method returns network interface attributes. Only one attribute
can be retrieved at a time. The following is the list of attributes
that can be retrieved:
description -- hashref
groupSet -- hashref
sourceDestCheck -- hashref
attachment -- hashref
These values can be retrieved more conveniently from the
L<VM::EC2::NetworkInterface> object, so there is no attempt to parse
the results of this call into Perl objects.
=cut
sub describe_network_interface_attribute {
my $self = shift;
@_ == 2 or croak "Usage: describe_network_interface_attribute(\$interface_id,\$attribute_name)";
my ($interface_id,$attribute) = @_;
my @param = (NetworkInterfaceId=>$interface_id,Attribute=>$attribute);
my $result = $self->call('DescribeNetworkInterfaceAttribute',@param);
return $result && $result->attribute($attribute);
}
=head2 $boolean = $ec2->modify_network_interface_attribute($interface_id,-$attribute_name=>$value)
This method changes network interface attributes. Only one attribute can be set per call
The following is the list of attributes that can be set:
-description -- interface description
-security_group_id -- single security group ID or arrayref to a list of group ids
-source_dest_check -- boolean; if false enables packets to be forwarded, and is necessary
for NAT and other router tasks
-delete_on_termination -- [$attachment_id=>$delete_on_termination]; Pass this a two-element
array reference consisting of the attachment ID and a boolean
indicating whether deleteOnTermination should be enabled for
this attachment.
=cut
sub modify_network_interface_attribute {
my $self = shift;
my $interface_id = shift or croak "Usage: modify_network_interface_attribute(\$interfaceId,%param)";
my %args = @_;
my @param = (NetworkInterfaceId=>$interface_id);
push @param,$self->value_parm($_,\%args) foreach qw(Description SourceDestCheck);
push @param,$self->list_parm('SecurityGroupId',\%args);
if (my $dot = $args{-delete_on_termination}) {
my ($attachment_id,$delete_on_termination) = @$dot;
push @param,'Attachment.AttachmentId'=>$attachment_id;
push @param,'Attachment.DeleteOnTermination'=>$delete_on_termination ? 'true' : 'false';
}
return $self->call('ModifyNetworkInterfaceAttribute',@param);
}
=head2 $boolean = $ec2->reset_network_interface_attribute($interface_id => $attribute_name)
This method resets the named network interface attribute to its
default value. Only one attribute can be reset per call. The AWS
documentation is not completely clear on this point, but it appears
that the only attribute that can be reset using this method is:
source_dest_check -- Turns on source destination checking
For consistency with modify_network_interface_attribute, you may
specify attribute names with or without a leading dash, and using
either under_score or mixedCase naming:
$ec2->reset_network_interface_atribute('eni-12345678' => 'source_dest_check');
$ec2->reset_network_interface_atribute('eni-12345678' => '-source_dest_check');
$ec2->reset_network_interface_atribute('eni-12345678' => sourceDestCheck);
=cut
sub reset_network_interface_attribute {
my $self = shift;
@_ == 2 or croak "Usage: reset_network_interface_attribute(\$interfaceId,\$attribute)";
my ($interface_id,$attribute) = @_;
$attribute = s/^-//;
$attribute = $self->uncanonicalize($attribute);
my @param = (NetworkInterfaceId=> $interface_id,
Attribute => $attribute
);
return $self->call('ResetNetworkInterfaceAttribute',@param);
}
=head2 $attachmentId = $ec2->attach_network_interface($network_interface_id,$instance_id,$device_index)
=head2 $attachmentId = $ec2->attach_network_interface(-network_interface_id => $id,
-instance_id => $id,
-device_index => $index)
This method attaches a network interface to an instance using the
indicated device index. You can use instance and network interface
IDs, or VM::EC2::Instance and VM::EC2::NetworkInterface objects. You
may use an integer for -device_index, or use the strings "eth0",
"eth1" etc.
Required arguments:
-network_interface_id ID of the network interface to attach.
-instance_id ID of the instance to attach the interface to.
-device_index Network device number to use (e.g. 0 for eth0).
On success, this method returns the attachmentId of the new attachment
(not a VM::EC2::NetworkInterface::Attachment object, due to an AWS API
inconsistency).
Note that it may be more convenient to attach and detach network
interfaces via methods in the VM::EC2::Instance and
VM::EC2::NetworkInterface objects:
$instance->attach_network_interface($interface=>'eth0');
$interface->attach($instance=>'eth0');
=cut
sub attach_network_interface {
my $self = shift;
my %args;
if ($_[0] !~ /^-/ && @_ == 3) {
@args{qw(-network_interface_id -instance_id -device_index)} = @_;
} else {
%args = @_;
}
$args{-network_interface_id} && $args{-instance_id} && defined $args{-device_index} or
croak "-network_interface_id, -instance_id and -device_index arguments must all be specified";
$args{-device_index} =~ s/^eth//;
my @param = $self->single_parm(NetworkInterfaceId=>\%args);
push @param,$self->single_parm(InstanceId=>\%args);
push @param,$self->single_parm(DeviceIndex=>\%args);
return $self->call('AttachNetworkInterface',@param);
}
=head2 $boolean = $ec2->detach_network_interface($attachment_id [,$force])
This method detaches a network interface from an instance. Both the
network interface and instance are specified using their
attachmentId. If the $force flag is present, and true, then the
detachment will be forced even if the interface is in use.
Note that it may be more convenient to attach and detach network
interfaces via methods in the VM::EC2::Instance and
VM::EC2::NetworkInterface objects:
$instance->detach_network_interface($interface);
$interface->detach();
=cut
sub detach_network_interface {
my $self = shift;
my ($attachment_id,$force) = @_;
$attachment_id or croak "Usage: detach_network_interface(\$attachment_id [,\$force])";
my @param = (AttachmentId => $attachment_id);
push @param,(Force => 'true') if defined $force && $force;
return $self->call('DetachNetworkInterface',@param);
}
=head1 Elastic Load Balancers (ELB)
The methods in this section allow you to retrieve information about
Elastic Load Balancers, create new ELBs, and change the properties
of the ELBs.
The primary object manipulated by these methods is
L<VM::EC2::ELB>. Please see the L<VM::EC2::ELB> manual page
=head2 @lbs = $ec2->describe_load_balancers(-load_balancer_name=>\@names)
=head2 @lbs = $ec2->describe_load_balancers(@names)
Provides detailed configuration information for the specified ELB(s).
Optional parameters are:
-load_balancer_names Name of the ELB to return information on.
This can be a string scalar, or an arrayref.
-lb_name,-lb_names,
-load_balancer_name Aliases for -load_balancer_names
Returns a series of L<VM::EC2::ELB> objects.
=cut
sub describe_load_balancers {
my $self = shift;
my %args = $self->args('-load_balancer_names',@_);
$args{'-load_balancer_names'} ||= $args{-lb_name};
$args{'-load_balancer_names'} ||= $args{-lb_names};
$args{'-load_balancer_names'} ||= $args{-load_balancer_name};
my @params = $self->member_list_parm('LoadBalancerNames',\%args);
push @params,$self->filter_parm(\%args);
return $self->elb_call('DescribeLoadBalancers',@params);
}
=head2 $success = $ec2->delete_load_balancer(-load_balancer_name=>$name)
=head2 $success = $ec2->delete_load_balancer($name)
Deletes the specified ELB.
Arguments:
-load_balancer_name -- The name of the ELB to delete
-lb_name -- Alias for -load_balancer_name
Returns true on successful deletion. NOTE: This API call will return
success regardless of existence of the ELB.
=cut
sub delete_load_balancer {
my $self = shift;
my %args = $self->args('-load_balancer_name',@_);
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "delete_load_balancer(): -load_balancer_name argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
return $self->elb_call('DeleteLoadBalancer',@params);
}
=head2 $healthcheck = $ec2->configure_health_check(-load_balancer_name => $name,
-healthy_threshold => $cnt,
-interval => $secs,
-target => $target,
-timeout => $secs,
-unhealthy_threshold => $cnt)
Define an application healthcheck for the instances.
All Parameters are required.
-load_balancer_name Name of the ELB.
-healthy_threashold Specifies the number of consecutive health probe successes
required before moving the instance to the Healthy state.
-interval Specifies the approximate interval, in seconds, between
health checks of an individual instance.
-target Must be a string in the form: Protocol:Port[/PathToPing]
- Valid Protocol types are: HTTP, HTTPS, TCP, SSL
- Port must be in range 1-65535
- PathToPing is only applicable to HTTP or HTTPS protocol
types and must be 1024 characters long or fewer.
-timeout Specifies the amount of time, in seconds, during which no
response means a failed health probe.
-unhealthy_threashold Specifies the number of consecutive health probe failures
required before moving the instance to the Unhealthy state.
-lb_name Alias for -load_balancer_name
Returns a L<VM::EC2::ELB::HealthCheck> object.
=cut
sub configure_health_check {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "configure_health_check(): -load_balancer_name argument missing";
$args{-healthy_threshold} && $args{-interval} &&
$args{-target} && $args{-timeout} && $args{-unhealthy_threshold} or
croak "configure_health_check(): healthcheck argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, map {$self->prefix_parm('HealthCheck',$_,\%args)}
qw(HealthyThreshold Interval Target Timeout UnhealthyThreshold);
return $self->elb_call('ConfigureHealthCheck',@params);
}
=head2 $success = $ec2->create_app_cookie_stickiness_policy(-load_balancer_name => $name,
-cookie_name => $cookie,
-policy_name => $policy)
Generates a stickiness policy with sticky session lifetimes that follow that of
an application-generated cookie. This policy can be associated only with
HTTP/HTTPS listeners.
Required arguments:
-load_balancer_name Name of the ELB.
-cookie_name Name of the application cookie used for stickiness.
-policy_name The name of the policy being created. The name must
be unique within the set of policies for this ELB.
-lb_name Alias for -load_balancer_name
Returns true on successful execution.
=cut
sub create_app_cookie_stickiness_policy {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "create_app_cookie_stickiness_policy(): -load_balancer_name argument missing";
$args{-cookie_name} && $args{-policy_name} or
croak "create_app_cookie_stickiness_policy(): -cookie_name or -policy_name option missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, map {$self->single_parm($_,\%args)} qw(CookieName PolicyName);
return $self->elb_call('CreateAppCookieStickinessPolicy',@params);
}
=head2 $success = $ec2->create_lb_cookie_stickiness_policy(-load_balancer_name => $name,
-cookie_expiration_period => $secs,
-policy_name => $policy)
Generates a stickiness policy with sticky session lifetimes controlled by the
lifetime of the browser (user-agent) or a specified expiration period. This
policy can be associated only with HTTP/HTTPS listeners.
Required arguments:
-load_balancer_name Name of the ELB.
-cookie_expiration_period The time period in seconds after which the
cookie should be considered stale. Not
specifying this parameter indicates that the
sticky session will last for the duration of
the browser session. OPTIONAL
-policy_name The name of the policy being created. The name
must be unique within the set of policies for
this ELB.
-lb_name Alias for -load_balancer_name
Returns true on successful execution.
=cut
sub create_lb_cookie_stickiness_policy {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "create_lb_cookie_stickiness_policy(): -load_balancer_name argument missing";
$args{-cookie_expiration_period} && $args{-policy_name} or
croak "create_lb_cookie_stickiness_policy(): -cookie_expiration_period or -policy_name option missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, map {$self->single_parm($_,\%args)} qw(CookieExpirationPeriod PolicyName);
return $self->elb_call('CreateLBCookieStickinessPolicy',@params);
}
=head2 $lb = $ec2->create_load_balancer(-load_balancer_name => $name,
-listeners => \@listeners,
-availability_zones => \@zones,
-scheme => $scheme,
)
Creates a new ELB.
Required arguments:
-load_balancer_name Name of the ELB.
-listeners Must either be a L<VM::EC2::ELB:Listener> object
(or arrayref of objects) or a hashref (or arrayref
of hashrefs) containing the following keys:
Protocol -- Value as one of: HTTP, HTTPS, TCP, or SSL
LoadBalancerPort -- Value in range 1-65535
InstancePort -- Value in range 1-65535
and optionally:
InstanceProtocol -- Value as one of: HTTP, HTTPS, TCP, or SSL
SSLCertificateId -- Certificate ID from AWS IAM certificate list
-availability_zones Literal string or array of strings containing valid
availability zones. Optional if subnets are
specified in a VPC usage scenario.
Optional arguments:
-scheme The type of ELB. By default, Elastic Load Balancing
creates an Internet-facing LoadBalancer with a
publicly resolvable DNS name, which resolves to
public IP addresses. Specify the value 'internal'
for this option to create an internal LoadBalancer
with a DNS name that resolves to private IP addresses.
This option is only available in a VPC.
-security_groups The security groups assigned to your ELB within your
VPC. String or arrayref.
-subnets A list of subnet IDs in your VPC to attach to your
ELB. String or arrayref. REQUIRED if availability
zones are not specified above.
Argument aliases:
-zones Alias for -availability_zones
-lb_name Alias for -load_balancer_name
Returns a L<VM::EC2::ELB> object if successful.
=cut
sub create_load_balancer {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-availability_zones } ||= $args{-zones};
$args{-load_balancer_name} or
croak "create_load_balancer(): -load_balancer_name argument missing";
$args{-listeners} or
croak "create_load_balancer(): -listeners option missing";
$args{-availability_zones} || $args{-subnets} or
croak "create_load_balancer(): -availability_zones option is required if subnets are not specified";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->_listener_parm($args{-listeners});
push @params, $self->member_list_parm('AvailabilityZones',\%args);
push @params, $self->single_parm('Scheme',\%args);
push @params, $self->member_list_parm('SecurityGroups',\%args);
push @params, $self->member_list_parm('Subnets',\%args);
return if (! defined $self->elb_call('CreateLoadBalancer',@params));
return $self->describe_load_balancers($args{-load_balancer_name});
}
# Internal method for building ELB listener parameters
sub _listener_parm {
my $self = shift;
my $l = shift;
my @param;
my $i = 1;
for my $lsnr (ref $l && ref $l eq 'ARRAY' ? @$l : $l) {
if (ref $lsnr && ref $lsnr eq 'HASH') {
push @param,("Listeners.member.$i.Protocol"=> $lsnr->{Protocol});
push @param,("Listeners.member.$i.LoadBalancerPort"=> $lsnr->{LoadBalancerPort});
push @param,("Listeners.member.$i.InstancePort"=> $lsnr->{InstancePort});
push @param,("Listeners.member.$i.InstanceProtocol"=> $lsnr->{InstanceProtocol})
if $lsnr->{InstanceProtocol};
push @param,("Listeners.member.$i.SSLCertificateId"=> $lsnr->{SSLCertificateId})
if $lsnr->{SSLCertificateId};
$i++;
} elsif (ref $lsnr && ref $lsnr eq 'VM::EC2::ELB::Listener') {
push @param,("Listeners.member.$i.Protocol"=> $lsnr->Protocol);
push @param,("Listeners.member.$i.LoadBalancerPort"=> $lsnr->LoadBalancerPort);
push @param,("Listeners.member.$i.InstancePort"=> $lsnr->InstancePort);
if (my $InstanceProtocol = $lsnr->InstanceProtocol) {
push @param,("Listeners.member.$i.InstanceProtocol"=> $InstanceProtocol)
}
if (my $SSLCertificateId = $lsnr->SSLCertificateId) {
push @param,("Listeners.member.$i.SSLCertificateId"=> $SSLCertificateId)
}
$i++;
}
}
return @param;
}
=head2 $success = $ec2->create_load_balancer_listeners(-load_balancer_name => $name,
-listeners => \@listeners)
Creates one or more listeners on a ELB for the specified port. If a listener
with the given port does not already exist, it will be created; otherwise, the
properties of the new listener must match the properties of the existing
listener.
-listeners Must either be a L<VM::EC2::ELB:Listener> object (or arrayref of
objects) or a hash (or arrayref of hashes) containing the
following keys:
Protocol -- Value as one of: HTTP, HTTPS, TCP, or SSL
LoadBalancerPort -- Value in range 1-65535
InstancePort -- Value in range 1-65535
and optionally:
InstanceProtocol -- Value as one of: HTTP, HTTPS, TCP, or SSL
SSLCertificateId -- Certificate ID from AWS IAM certificate list
-lb_name Alias for -load_balancer_name
Returns true on successful execution.
=cut
sub create_load_balancer_listeners {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "create_load_balancer_listeners(): -load_balancer_name argument missing";
$args{-listeners} or
croak "create_load_balancer_listeners(): -listeners option missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->_listener_parm($args{-listeners});
return $self->elb_call('CreateLoadBalancerListeners',@params);
}
=head2 $success = $ec2->delete_load_balancer_listeners(-load_balancer_name => $name,
-load_balancer_ports => \@ports)
Deletes listeners from the ELB for the specified port.
Arguments:
-load_balancer_name The name of the ELB
-load_balancer_ports An arrayref of strings or literal string containing
the port numbers.
-ports Alias for -load_balancer_ports
-lb_name Alias for -load_balancer_name
Returns true on successful execution.
=cut
sub delete_load_balancer_listeners {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_ports} ||= $args{-ports};
$args{-load_balancer_name} or
croak "delete_load_balancer_listeners(): -load_balancer_name argument missing";
$args{-load_balancer_ports} or
croak "delete_load_balancer_listeners(): -load_balancer_ports argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->member_list_parm('LoadBalancerPorts',\%args);
return $self->elb_call('DeleteLoadBalancerListeners',@params);
}
=head2 @z = $ec2->disable_availability_zones_for_load_balancer(-load_balancer_name => $name,
-availability_zones => \@zones)
Removes the specified EC2 Availability Zones from the set of configured
Availability Zones for the ELB. There must be at least one Availability Zone
registered with a LoadBalancer at all times. Instances registered with the ELB
that are in the removed Availability Zone go into the OutOfService state.
Arguments:
-load_balancer_name The name of the ELB
-availability_zones Arrayref or literal string of availability zone names
(ie. us-east-1a)
-zones Alias for -availability_zones
-lb_name Alias for -load_balancer_name
Returns an array of L<VM::EC2::AvailabilityZone> objects now associated with the ELB.
=cut
sub disable_availability_zones_for_load_balancer {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-availability_zones} ||= $args{-zones};
$args{-load_balancer_name} or
croak "disable_availability_zones_for_load_balancer(): -load_balancer_name argument missing";
$args{-availability_zones} or
croak "disable_availability_zones_for_load_balancer(): -availability_zones argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->member_list_parm('AvailabilityZones',\%args);
my @zones = $self->elb_call('DisableAvailabilityZonesForLoadBalancer',@params) or return;
return $self->describe_availability_zones(@zones);
}
=head2 @z = $ec2->enable_availability_zones_for_load_balancer(-load_balancer_name => $name,
-availability_zones => \@zones)
Adds one or more EC2 Availability Zones to the ELB. The ELB evenly distributes
requests across all its registered Availability Zones that contain instances.
Arguments:
-load_balancer_name The name of the ELB
-availability_zones Array or literal string of availability zone names
(ie. us-east-1a)
-zones Alias for -availability_zones
-lb_name Alias for -load_balancer_name
Returns an array of L<VM::EC2::AvailabilityZone> objects now associated with the ELB.
=cut
sub enable_availability_zones_for_load_balancer {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-availability_zones} ||= $args{-zones};
$args{-load_balancer_name} or
croak "enable_availability_zones_for_load_balancer(): -load_balancer_name argument missing";
$args{-availability_zones} or
croak "enable_availability_zones_for_load_balancer(): -availability_zones argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->member_list_parm('AvailabilityZones',\%args);
my @zones = $self->elb_call('EnableAvailabilityZonesForLoadBalancer',@params) or return;
return $self->describe_availability_zones(@zones);
}
=head2 @i = $ec2->register_instances_with_load_balancer(-load_balancer_name => $name,
-instances => \@instance_ids)
Adds new instances to the ELB. If the instance is in an availability zone that
is not registered with the ELB will be in the OutOfService state. Once the zone
is added to the ELB the instance will go into the InService state.
Arguments:
-load_balancer_name The name of the ELB
-instances An arrayref or literal string of Instance IDs.
-lb_name Alias for -load_balancer_name
Returns an array of instances now associated with the ELB in the form of
L<VM::EC2::Instance> objects.
=cut
sub register_instances_with_load_balancer {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-instances} ||= $args{-instance_id};
$args{-load_balancer_name} or
croak "register_instances_with_load_balancer(): -load_balancer_name argument missing";
$args{-instances} or
croak "register_instances_with_load_balancer(): -instances argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->_perm_parm('Instances','member','InstanceId',$args{-instances});
my @i = $self->elb_call('RegisterInstancesWithLoadBalancer',@params) or return;
return $self->describe_instances(@i);
}
=head2 @i = $ec2->deregister_instances_from_load_balancer(-load_balancer_name => $name,
-instances => \@instance_ids)
Deregisters instances from the ELB. Once the instance is deregistered, it will
stop receiving traffic from the ELB.
Arguments:
-load_balancer_name The name of the ELB
-instances An arrayref or literal string of Instance IDs.
-lb_name Alias for -load_balancer_name
Returns an array of instances now associated with the ELB in the form of
L<VM::EC2::Instance> objects.
=cut
sub deregister_instances_from_load_balancer {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-instances} ||= $args{-instance_id};
$args{-load_balancer_name} or
croak "deregister_instances_from_load_balancer(): -load_balancer_name argument missing";
$args{-instances} or
croak "deregister_instances_from_load_balancer(): -instances argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->_perm_parm('Instances','member','InstanceId',$args{-instances});
my @i = $self->elb_call('DeregisterInstancesFromLoadBalancer',@params) or return;
return $self->describe_instances(@i);
}
=head2 $success = $ec2->set_load_balancer_listener_ssl_certificate(-load_balancer_name => $name,
-load_balancer_port => $port,
-ssl_certificate_id => $cert_id)
Sets the certificate that terminates the specified listener's SSL connections.
The specified certificate replaces any prior certificate that was used on the
same ELB and port.
Required arguments:
-load_balancer_name The name of the the ELB.
-load_balancer_port The port that uses the specified SSL certificate.
-ssl_certificate_id The ID of the SSL certificate chain to use. See the
AWS Identity and Access Management documentation under
Managing Server Certificates for more information.
Alias arguments:
-lb_name Alias for -load_balancer_name
-port Alias for -load_balancer_port
-cert_id Alias for -ssl_certificate_id
Returns true on successful execution.
=cut
sub set_load_balancer_listener_ssl_certificate {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_port} ||= $args{-port};
$args{-ssl_certificate_id} ||= $args{-cert_id};
$args{-load_balancer_name} or
croak "set_load_balancer_listener_ssl_certificate(): -load_balancer_name argument missing";
$args{-load_balancer_port} or
croak "set_load_balancer_listener_ssl_certificate(): -load_balancer_port argument missing";
$args{-ssl_certificate_id} or
croak "set_load_balancer_listener_ssl_certificate(): -ssl_certificate_id argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->single_parm('LoadBalancerPort',\%args);
push @params,('SSLCertificateId'=>$args{-ssl_certificate_id}) if $args{-ssl_certificate_id};
return $self->elb_call('SetLoadBalancerListenerSSLCertificate',@params);
}
=head2 @states = $ec2->describe_instance_health(-load_balancer_name => $name,
-instances => \@instance_ids)
Returns the current state of the instances of the specified LoadBalancer. If no
instances are specified, the state of all the instances for the ELB is returned.
Required arguments:
-load_balancer_name The name of the ELB
Optional parameters:
-instances Literal string or arrayref of Instance IDs
-lb_name Alias for -load_balancer_name
-instance_id Alias for -instances
Returns an array of L<VM::EC2::ELB::InstanceState> objects.
=cut
sub describe_instance_health {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-instances} ||= $args{-instance_id};
$args{-load_balancer_name} or
croak "describe_instance_health(): -load_balancer_name argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->_perm_parm('Instances','member','InstanceId',$args{-instances});
return $self->elb_call('DescribeInstanceHealth',@params);
}
=head2 $success = $ec2->create_load_balancer_policy(-load_balancer_name => $name,
-policy_name => $policy,
-policy_type_name => $type_name,
-policy_attributes => \@attrs)
Creates a new policy that contains the necessary attributes depending on the
policy type. Policies are settings that are saved for your ELB and that can be
applied to the front-end listener, or the back-end application server,
depending on your policy type.
Required Arguments:
-load_balancer_name The name associated with the LoadBalancer for which the
policy is being created. This name must be unique within
the client AWS account.
-policy_name The name of the ELB policy being created. The name must
be unique within the set of policies for this ELB.
-policy_type_name The name of the base policy type being used to create
this policy. To get the list of policy types, use the
describe_load_balancer_policy_types function.
Optional Arguments:
-policy_attributes Arrayref of hashes containing AttributeName and AttributeValue
-lb_name Alias for -load_balancer_name
Returns true if successful.
=cut
sub create_load_balancer_policy {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "create_load_balancer_policy(): -load_balancer_name argument missing";
$args{-policy_name} or
croak "create_load_balancer_policy(): -policy_name argument missing";
$args{-policy_type_name} or
croak "create_load_balancer_policy(): -policy_type_name argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->single_parm('PolicyName',\%args);
push @params, $self->single_parm('PolicyTypeName',\%args);
push @params, $self->_policy_attr_parm($args{-policy_attributes});
return $self->elb_call('CreateLoadBalancerPolicy',@params);
}
# internal method for building policy attribute parameters
sub _policy_attr_parm {
my $self = shift;
my $p = shift;
my @param;
my $i = 1;
for my $policy (ref $p && ref $p eq 'ARRAY' ? @$p : $p) {
if (ref $policy && ref $policy eq 'HASH') {
push @param,("PolicyAttributes.member.$i.AttributeName"=> $policy->{AttributeName});
push @param,("PolicyAttributes.member.$i.AttributeValue"=> $policy->{AttributeValue});
$i++;
} elsif (ref $policy && ref $policy eq 'VM::EC2::ELB::PolicyAttribute') {
push @param,("PolicyAttributes.member.$i.AttributeName"=> $policy->AttributeName);
push @param,("PolicyAttributes.member.$i.AttributeValue"=> $policy->AttributeValue);
$i++;
}
}
return @param;
}
=head2 $success = $ec2->delete_load_balancer_policy(-load_balancer_name => $name,
-policy_name => $policy)
Deletes a policy from the ELB. The specified policy must not be enabled for any
listeners.
Arguments:
-load_balancer_name The name of the ELB
-policy_name The name of the ELB policy
-lb_name Alias for -load_balancer_name
Returns true if successful.
=cut
sub delete_load_balancer_policy {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "delete_load_balancer_policy(): -load_balancer_name argument missing";
$args{-policy_name} or
croak "delete_load_balancer_policy(): -policy_name argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->single_parm('PolicyName',\%args);
return $self->elb_call('DeleteLoadBalancerPolicy',@params);
}
=head2 @policy_descs = $ec2->describe_load_balancer_policies(-load_balancer_name => $name,
-policy_names => \@names)
Returns detailed descriptions of ELB policies. If you specify an ELB name, the
operation returns either the descriptions of the specified policies, or
descriptions of all the policies created for the ELB. If you don't specify a ELB
name, the operation returns descriptions of the specified sample policies, or
descriptions of all the sample policies. The names of the sample policies have
the ELBSample- prefix.
Optional Arguments:
-load_balancer_name The name of the ELB.
-policy_names The names of ELB policies created or ELB sample policy names.
-lb_name Alias for -load_balancer_name
Returns an array of L<VM::EC2::ELB::PolicyDescription> objects if successful.
=cut
sub describe_load_balancer_policies {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-policy_names} ||= $args{-policy_name};
$args{-load_balancer_name} or
croak "describe_load_balancer_policies(): -load_balancer_name argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->member_list_parm('PolicyNames',\%args);
return $self->elb_call('DescribeLoadBalancerPolicies',@params);
}
=head2 @policy_types = $ec2->describe_load_balancer_policy_types(-policy_type_names => \@names)
Returns meta-information on the specified ELB policies defined by the Elastic
Load Balancing service. The policy types that are returned from this action can
be used in a create_load_balander_policy call to instantiate specific policy
configurations that will be applied to an ELB.
Required arguemnts:
-load_balancer_name The name of the ELB.
Optional arguments:
-policy_type_names Literal string or arrayref of policy type names
-names Alias for -policy_type_names
Returns an array of L<VM::EC2::ELB::PolicyTypeDescription> objects if successful.
=cut
sub describe_load_balancer_policy_types {
my $self = shift;
my %args = @_;
$args{-policy_type_names} ||= $args{-names};
my @params = $self->member_list_parm('PolicyTypeNames',\%args);
return $self->elb_call('DescribeLoadBalancerPolicyTypes',@params);
}
=head2 $success = $ec2->set_load_balancer_policies_of_listener(-load_balancer_name => $name,
-load_balancer_port => $port,
-policy_names => \@names)
Associates, updates, or disables a policy with a listener on the ELB. Multiple
policies may be associated with a listener.
Required arguments:
-load_balancer_name The name associated with the ELB.
-load_balancer_port The external port of the LoadBalancer with which this
policy applies to.
-policy_names List of policies to be associated with the listener.
Currently this list can have at most one policy. If the
list is empty, the current policy is removed from the
listener. String or arrayref.
Returns true if successful.
=cut
sub set_load_balancer_policies_of_listener {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_port} ||= $args{-port};
$args{-load_balancer_name} or
croak "set_load_balancer_policies_of_listener(): -load_balancer_name argument missing";
$args{-load_balancer_port} or
croak "set_load_balancer_policies_of_listener(): -load_balancer_port argument missing";
$args{-policy_names} or
croak "set_load_balancer_policies_of_listener(): -policy_names argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->single_parm('LoadBalancerPort',\%args);
push @params, $self->member_list_parm('PolicyNames',\%args);
return $self->elb_call('SetLoadBalancerPoliciesOfListener',@params);
}
=head2 @sgs = $ec2->apply_security_groups_to_load_balancer(-load_balancer_name => $name,
-security_groups => \@groups)
Associates one or more security groups with your ELB in VPC. The provided
security group IDs will override any currently applied security groups.
Required arguments:
-load_balancer_name The name associated with the ELB.
-security_groups A list of security group IDs to associate with your ELB in
VPC. The security group IDs must be provided as the ID and
not the security group name (For example, sg-123456).
String or arrayref.
Returns a series of L<VM::EC2::SecurityGroup> objects.
=cut
sub apply_security_groups_to_load_balancer {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "apply_security_groups_to_load_balancer(): -load_balancer_name argument missing";
$args{-security_groups} or
croak "apply_security_groups_to_load_balancer(): -security_groups argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->member_list_parm('SecurityGroups',\%args);
my @g = $self->elb_call('ApplySecurityGroupsToLoadBalancer',@params) or return;
return $self->describe_security_groups(@g);
}
=head2 @subnets = $ec2->attach_load_balancer_to_subnets(-load_balancer_name => $name,
-subnets => \@subnets)
Adds one or more subnets to the set of configured subnets for the ELB.
Required arguments:
-load_balancer_name The name associated with the ELB.
-subnets A list of subnet IDs to add for the ELB. String or
arrayref.
Returns a series of L<VM::EC2::VPC::Subnet> objects corresponding to the
subnets the ELB is now attached to.
=cut
sub attach_load_balancer_to_subnets {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "attach_load_balancer_to_subnets(): -load_balancer_name argument missing";
$args{-subnets} or
croak "attach_load_balancer_to_subnets(): -subnets argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->member_list_parm('Subnets',\%args);
my @sn = $self->elb_call('AttachLoadBalancerToSubnets',@params) or return;
return $self->describe_subnets(@sn);
}
=head2 @subnets = $ec2->detach_load_balancer_from_subnets(-load_balancer_name => $name,
-subnets => \@subnets)
Removes subnets from the set of configured subnets in the VPC for the ELB.
Required arguments:
-load_balancer_name The name associated with the ELB.
-subnets A list of subnet IDs to add for the ELB. String or
arrayref.
Returns a series of L<VM::EC2::VPC::Subnet> objects corresponding to the
subnets the ELB is now attached to.
=cut
sub detach_load_balancer_from_subnets {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-load_balancer_name} or
croak "detach_load_balancer_from_subnets(): -load_balancer_name argument missing";
$args{-subnets} or
croak "detach_load_balancer_from_subnets(): -subnets argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->member_list_parm('Subnets',\%args);
my @sn = $self->elb_call('DetachLoadBalancerFromSubnets',@params) or return;
return $self->describe_subnets(@sn);
}
=head2 $success = $ec2->set_load_balancer_policies_for_backend_server(-instance_port => $port,
-load_balancer_name => $name,
-policy_names => \@policies)
Replaces the current set of policies associated with a port on which the back-
end server is listening with a new set of policies. After the policies have
been created, they can be applied here as a list. At this time, only the back-
end server authentication policy type can be applied to the back-end ports;
this policy type is composed of multiple public key policies.
Required arguments:
-load_balancer_name The name associated with the ELB.
-instance_port The port number associated with the back-end server.
-policy_names List of policy names to be set. If the list is empty,
then all current polices are removed from the back-end
server.
Aliases:
-port Alias for -instance_port
-lb_name Alias for -load_balancer_name
Returns true if successful.
=cut
sub set_load_balancer_policies_for_backend_server {
my $self = shift;
my %args = @_;
$args{-load_balancer_name} ||= $args{-lb_name};
$args{-instance_port} ||= $args{-port};
$args{-load_balancer_name} or
croak "set_load_balancer_policies_for_backend_server(): -load_balancer_name argument missing";
$args{-instance_port} or
croak "set_load_balancer_policies_for_backend_server(): -instance_port argument missing";
$args{-policy_names} or
croak "set_load_balancer_policies_for_backend_server(): -policy_names argument missing";
my @params = $self->single_parm('LoadBalancerName',\%args);
push @params, $self->single_parm('InstancePort',\%args);
push @params, $self->member_list_parm('PolicyNames',\%args);
return $self->elb_call('SetLoadBalancerPoliciesForBackendServer',@params);
}
=head1 AWS SECURITY TOKENS
AWS security tokens provide a way to grant temporary access to
resources in your EC2 space without giving them permanent
accounts. They also provide the foundation for mobile services and
multifactor authentication devices (MFA).
Used in conjunction with VM::EC2::Security::Policy and
VM::EC2::Security::Credentials, you can create a temporary user who is
authenticated for a limited length of time and pass the credentials to
him or her via a secure channel. He or she can then create a
credentials object to access your AWS resources.
Here is an example:
# on your side of the connection
$ec2 = VM::EC2->new(...); # as usual
my $policy = VM::EC2::Security::Policy->new;
$policy->allow('DescribeImages','RunInstances');
my $token = $ec2->get_federation_token(-name => 'TemporaryUser',
-duration => 60*60*3, # 3 hrs, as seconds
-policy => $policy);
my $serialized = $token->credentials->serialize;
send_data_to_user_somehow($serialized);
# on the temporary user's side of the connection
my $serialized = get_data_somehow();
my $token = VM::EC2::Security::Credentials->new_from_serialized($serialized);
my $ec2 = VM::EC2->new(-security_token => $token);
print $ec2->describe_images(-owner=>'self');
For temporary users who are not using the Perl VM::EC2 API, you can
transmit the required fields individually:
my $credentials = $token->credentials;
my $access_key_id = $credentials->accessKeyId;
my $secret_key = $credentials->secretKey;
my $session_token = $credentials->sessionToken;
send_data_to_user_somehow($session_token,
$access_key_id,
$secret_key);
Calls to get_federation_token() return a VM::EC2::Security::Token
object. This object contains two sub-objects, a
VM::EC2::Security::Credentials object, and a
VM::EC2::Security::FederatedUser object. The Credentials object
contains a temporary access key ID, secret access key, and session
token which together can be used to authenticate to the EC2 API. The
FederatedUser object contains the temporary user account name and ID.
See L<VM::EC2::Security::Token>, L<VM::EC2::Security::FederatedUser>,
L<VM::EC2::Security::Credentials>, and L<VM::EC2::Security::Policy>.
=cut
=head2 $token = $ec2->get_federation_token($username)
=head2 $token = $ec2->get_federation_token(-name=>$username,@args)
This method creates a new temporary user under the provided username
and returns a VM::EC2::Security::Token object that contains temporary
credentials for the user, as well as information about the user's
account. Other options allow you to control the duration for which the
credentials will be valid, and the policy the controls what resources
the user is allowed to access.
=over 4
=item Required arguments:
-name The username
The username must comply with the guidelines described in
http://docs.amazonwebservices.com/IAM/latest/UserGuide/LimitationsOnEntities.html:
essentially all alphanumeric plus the characters [+=,.@-].
=item Optional arguments:
-duration_seconds Length of time the session token will be valid for,
expressed in seconds.
-duration Same thing, faster to type.
-policy A VM::EC2::Security::Policy object, or a JSON string
complying with the IAM policy syntax.
The duration must be no shorter than 1 hour (3600 seconds) and no
longer than 36 hours (129600 seconds). If no duration is specified,
Amazon will default to 12 hours. If no policy is provided, then the
user will not be able to execute B<any> actions.
Note that if the temporary user wishes to create a VM::EC2 object and
specify a region name at create time
(e.g. VM::EC2->new(-region=>'us-west-1'), then the user must have
access to the DescribeRegions action:
$policy->allow('DescribeRegions')
Otherwise the call to new() will fail.
=back
=cut
sub get_federation_token {
my $self = shift;
my %args = $self->args('-name',@_);
$args{-name} or croak "Usage: get_federation_token(-name=>\$name,\@more_args)";
$args{-duration_seconds} ||= $args{-duration};
my @p = map {$self->single_parm($_,\%args)} qw(Name DurationSeconds Policy);
return $self->sts_call('GetFederationToken',@p);
}
=head2 $token = $ec2->get_session_token(%args)
This method creates a temporary VM::EC2::Security::Token object for an
anonymous user. The token has no policy associated with it, and can be
used to run any of the EC2 actions available to the user who created
the token. Optional arguments allow the session token to be used in
conjunction with MFA devices.
=over 4
=item Required arguments:
none
=item Optional arguments:
-duration_seconds Length of time the session token will be valid for,
expressed in seconds.
-duration Same thing, faster to type.
-serial_number The identification number of the user's MFA device,
if any.
-token_code The code provided by the MFA device, if any.
If no duration is specified, Amazon will default to 12 hours.
See
http://docs.amazonwebservices.com/IAM/latest/UserGuide/Using_ManagingMFA.html
for information on using AWS in conjunction with MFA devices.
=back
=cut
sub get_session_token {
my $self = shift;
my %args = @_;
my @p = map {$self->single_parm($_,\%args)} qw(SerialNumber DurationSeconds TokenCode);
return $self->sts_call('GetSessionToken',@p);
}
=head1 LAUNCH CONFIGURATIONS
=head2 @lc = $ec2->describe_launch_configurations(-names => \@names);
=head2 @lc = $ec->describe_launch_configurations(@names);
Provides detailed information for the specified launch configuration(s).
Optional parameters are:
-launch_configuration_names Name of the Launch config.
This can be a string scalar or an arrayref.
-name Alias for -launch_configuration_names
Returns a series of L<VM::EC2::LaunchConfiguration> objects.
=cut
sub describe_launch_configurations {
my $self = shift;
my %args = $self->args('-launch_configuration_names',@_);
$args{-launch_configuration_names} ||= $args{-names};
my @params = $self->list_parm('LaunchConfigurationNames',\%args);
return $self->asg_call('DescribeLaunchConfigurations', @params);
}
=head2 $success = $ec2->create_launch_configuration(%args);
Creates a new launch configuration.
Required arguments:
-name -- scalar, name for the Launch config.
-image_id -- scalar, AMI id which this launch config will use
-instance_type -- scalar, instance type of the Amazon EC2 instance.
Optional arguments:
-block_device_mappings -- list of hashref
-ebs_optimized -- scalar (boolean). false by default
-iam_instance_profile -- scalar
-instance_monitoring -- scalar (boolean). true by default
-kernel_id -- scalar
-key_name -- scalar
-ramdisk -- scalar
-security_groups -- list of scalars
-spot_price -- scalar
-user_data -- scalar
Returns true on successful execution.
=cut
sub create_launch_configuration {
my $self = shift;
my %args = @_;
my $name = $args{-name} or croak "-name argument is required";
my $imageid = $args{-image_id} or croak "-image_id argument is required";
my $itype = $args{-instance_type} or croak "-instance_type argument is required";
my @params = (ImageId => $imageid, InstanceType => $itype, LaunchConfigurationName => $name);
push @params, $self->member_list_parm('BlockDeviceMappings',\%args);
push @params, $self->member_list_parm('SecurityGroups',\%args);
push @params, $self->boolean_parm('EbsOptimized', \%args);
push @params, ('UserData' =>encode_base64($args{-user_data},'')) if $args{-user_data};
push @params, ('InstanceMonitoring.Enabled' => 'false')
if (exists $args{-instance_monitoring} and not $args{-instance_monitoring});
my @p = map {$self->single_parm($_,\%args) }
qw(IamInstanceProfile KernelId KeyName RamdiskId SpotPrice);
push @params, @p;
return $self->asg_call('CreateLaunchConfiguration',@params);
}
=head2 $success = $ec2->delete_launch_configuration(-name => $name);
Deletes a launch config.
-name Required. Name of the launch config to delete
Returns true on success.
=cut
sub delete_launch_configuration {
my $self = shift;
my %args = @_;
my $name = $args{-name} or croak "-name argument is required";
my @params = (LaunchConfigurationName => $name);
return $self->asg_call('DeleteLaunchConfiguration', @params);
}
=head1 AUTOSCALING GROUPS
=head2 @asg = $ec2->describe_autoscaling_groups(-auto_scaling_group_names => \@names);
Returns information about autoscaling groups
-auto_scaling_group_names List of auto scaling groups to describe
-names Alias of -auto_scaling_group_names
Returns a list of L<VM::EC2::ASG>.
=cut
sub describe_autoscaling_groups {
my ($self, %args) = @_;
$args{-auto_scaling_group_names} ||= $args{-names};
my @params = $self->member_list_parm('AutoScalingGroupNames',\%args);
return $self->asg_call('DescribeAutoScalingGroups', @params);
}
=head2 $success = $ec2->create_autoscaling_group(-name => $name,
-launch_config => $lc,
-max_size => $max_size,
-min_size => $min_size);
Creates a new autoscaling group.
Required arguments:
-name Name for the autoscaling group
-launch_config Name of the launch configuration to be used
-max_size Max number of instances to be run at once
-min_size Min number of instances
Optional arguments:
-availability_zones List of availability zone names
-load_balancer_names List of ELB names
-tags List of tags to apply to the instances run
-termination_policies List of policy names
-default_cooldown Time in seconds between autoscaling activities
-desired_capacity Number of instances to be run after creation
-health_check_type One of "ELB" or "EC2"
-health_check_grace_period Mandatory for health check type ELB. Number of
seconds between an instance is started and the
autoscaling group starts checking its health
-placement_group Physical location of your cluster placement group
-vpc_zone_identifier Strinc containing a comma-separated list of subnet
identifiers
Returns true on success.
=cut
sub create_autoscaling_group {
my $self = shift;
my %args = @_;
my $name = $args{-name} or croak "-name argument is required";
my $lconfig = $args{-launch_config} or croak "-launch_config argument is required";
my $max = $args{-max_size};
croak "-max_size argument is required" if (not defined $max);
my $min = $args{-min_size};
croak "-min_size argument is required" if (not defined $min);
my @params = (AutoScalingGroupName => $name, LaunchConfigurationName => $lconfig, MaxSize => $max,
MinSize => $max);
push @params, $self->member_list_parm('AvailabilityZones',\%args);
push @params, $self->member_list_parm('LoadBalancerNames',\%args);
push @params, $self->member_list_parm('TerminationPolicies',\%args);
push @params, $self->autoscaling_tags('Tags', \%args);
my @p = map {$self->single_parm($_,\%args) }
qw( DefaultCooldown DesiredCapacity HealthCheckGracePeriod HealthCheckType PlacementGroup
VPCZoneIdentifier);
push @params, @p;
return $self->asg_call('CreateAutoScalingGroup',@params);
}
=head2 $success = $ec2->delete_autoscaling_group(-name => $name)
Deletes an autoscaling group.
-name Name of the autoscaling group to delete
Returns true on success.
=cut
sub delete_autoscaling_group {
my $self = shift;
my %args = @_;
my $name = $args{-name} or croak "-name argument is required";
my @params = (AutoScalingGroupName => $name);
push @params, $self->single_parm('ForceDelete',\%args);
return $self->asg_call('DeleteAutoScalingGroup', @params);
}
=head2 $success = $ec2->update_autoscaling_group(-name => $name);
Updates an autoscaling group. Only required parameter is C<-name>
Optional arguments:
-availability_zones List of AZ's
-termination_policies List of policy names
-default_cooldown
-desired_capacity
-health_check_type
-health_check_grace_period
-placement_group
-vpc_zone_identifier
-max_size
-min_size
Returns true on success;
=cut
sub update_autoscaling_group {
my $self = shift;
my %args = @_;
my $name = $args{-name} or croak "-name argument is required";
my @params = (AutoScalingGroupName => $name);
push @params, $self->member_list_parm('AvailabilityZones',\%args);
push @params, $self->member_list_parm('TerminationPolicies',\%args);
my @p = map {$self->single_parm($_,\%args) }
qw( DefaultCooldown DesiredCapacity HealthCheckGracePeriod
HealthCheckType PlacementGroup VPCZoneIdentifier MaxSize MinSize );
push @params, @p;
return $self->asg_call('UpdateAutoScalingGroup',@params);
}
=head2 $success = $ec2->suspend_processes(-name => $asg_name,
-scaling_processes => \@procs);
Suspend the requested autoscaling processes.
-name Name of the autoscaling group
-scaling_processes List of process names to suspend. Valid processes are:
Launch
Terminate
HealthCheck
ReplaceUnhealty
AZRebalance
AlarmNotification
ScheduledActions
AddToLoadBalancer
Returns true on success.
=cut
sub suspend_processes {
my ($self, %args) = @_;
my $name = $args{-name} or croak "-name argument is required";
my @params = (AutoScalingGroupName => $name);
push @params, $self->member_list_parm('ScalingProcesses', \%args);
return $self->asg_call('SuspendProcesses', @params);
}
=head2 $success = $ec2->resume_processes(-name => $asg_name,
-scaling_processes => \@procs);
Resumes the requested autoscaling processes. It accepts the same arguments than
C<suspend_processes>.
Returns true on success.
=cut
sub resume_processes {
my ($self, %args) = @_;
my $name = $args{-name} or croak "-name argument is required";
my @params = (AutoScalingGroupName => $name);
push @params, $self->member_list_parm('ScalingProcesses', \%args);
return $self->asg_call('ResumeProcesses', @params);
}
# ------------------------------------------------------------------------------------------
=head1 INTERNAL METHODS
These methods are used internally and are listed here without
documentation (yet).
=head2 $underscore_name = $ec2->canonicalize($mixedCaseName)
=cut
sub canonicalize {
my $self = shift;
my $name = shift;
while ($name =~ /\w[A-Z]/) {
$name =~ s/([a-zA-Z])([A-Z])/\L$1_$2/g or last;
}
return '-'.lc $name;
}
sub uncanonicalize {
my $self = shift;
my $name = shift;
$name =~ s/_([a-z])/\U$1/g;
return $name;
}
=head2 $instance_id = $ec2->instance_parm(@args)
=cut
sub instance_parm {
my $self = shift;
my %args;
if ($_[0] =~ /^-/) {
%args = @_;
} elsif (@_ > 1) {
%args = (-instance_id => [@_]);
} else {
%args = (-instance_id => shift);
}
my $id = $args{-instance_id};
return ref $id && ref $id eq 'ARRAY' ? @$id : $id;
}
=head2 @arguments = $ec2->value_parm(ParameterName => \%args)
=cut
sub value_parm {
my $self = shift;
my ($argname,$args) = @_;
my $name = $self->canonicalize($argname);
return unless exists $args->{$name} || exists $args->{"-$argname"};
my $val = $args->{$name} || $args->{"-$argname"};
return ("$argname.Value"=>$val);
}
=head2 @arguments = $ec2->single_parm(ParameterName => \%args)
=cut
sub single_parm {
my $self = shift;
my ($argname,$args) = @_;
my $name = $self->canonicalize($argname);
my $val = $args->{$name} || $args->{"-$argname"};
defined $val or return;
my $v = ref $val && ref $val eq 'ARRAY' ? $val->[0] : $val;
return ($argname=>$v);
}
=head2 @parameters = $ec2->prefix_parm($prefix, ParameterName => \%args)
=cut
sub prefix_parm {
my $self = shift;
my ($prefix,$argname,$args) = @_;
my $name = $self->canonicalize($argname);
my $val = $args->{$name} || $args->{"-$argname"};
defined $val or return;
my $v = ref $val && ref $val eq 'ARRAY' ? $val->[0] : $val;
return ("$prefix.$argname"=>$v);
}
=head2 @parameters = $ec2->member_list_parm(ParameterName => \%args)
=cut
sub member_list_parm {
my $self = shift;
my ($argname,$args) = @_;
my $name = $self->canonicalize($argname);
my @params;
if (my $a = $args->{$name}||$args->{"-$argname"}) {
my $c = 1;
for (ref $a && ref $a eq 'ARRAY' ? @$a : $a) {
push @params,("$argname.member.".$c++ => $_);
}
}
return @params;
}
=head2 @arguments = $ec2->list_parm(ParameterName => \%args)
=cut
sub list_parm {
my $self = shift;
my ($argname,$args) = @_;
my $name = $self->canonicalize($argname);
my @params;
if (my $a = $args->{$name}||$args->{"-$argname"}) {
my $c = 1;
for (ref $a && ref $a eq 'ARRAY' ? @$a : $a) {
push @params,("$argname.".$c++ => $_);
}
}
return @params;
}
=head2 @arguments = $ec2->autoscaling_tags($argname, \%args)
=cut
sub autoscaling_tags {
my $self = shift;
my ($argname, $args) = @_;
my $name = $self->canonicalize($argname);
my @params;
if (my $a = $args->{$name}||$args->{"-$argname"}) {
my $c = 1;
for my $tag (ref $a && ref $a eq 'ARRAY' ? @$a : $a) {
my $prefix = "$argname.member." . $c++;
while (my ($k, $v) = each %$tag) {
push @params, ("$prefix.$k" => $v);
}
}
}
return @params;
}
=head2 @arguments = $ec2->filter_parm(\%args)
=cut
sub filter_parm {
my $self = shift;
my $args = shift;
return $self->key_value_parameters('Filter','Name','Value',$args);
}
=head2 @arguments = $ec2->tagcreate_parm(\%args)
=cut
sub tagcreate_parm {
my $self = shift;
my $args = shift;
return $self->key_value_parameters('Tag','Key','Value',$args);
}
=head2 @arguments = $ec2->tagdelete_parm(\%args)
=cut
sub tagdelete_parm {
my $self = shift;
my $args = shift;
return $self->key_value_parameters('Tag','Key','Value',$args,1);
}
=head2 @arguments = $ec2->key_value_parameters($param_name,$keyname,$valuename,\%args,$skip_undef_values)
=cut
sub key_value_parameters {
my $self = shift;
# e.g. 'Filter', 'Name','Value',{-filter=>{a=>b}}
my ($parameter_name,$keyname,$valuename,$args,$skip_undef_values) = @_;
my $arg_name = $self->canonicalize($parameter_name);
my @params;
if (my $a = $args->{$arg_name}||$args->{"-$parameter_name"}) {
my $c = 1;
if (ref $a && ref $a eq 'HASH') {
while (my ($name,$value) = each %$a) {
push @params,("$parameter_name.$c.$keyname" => $name);
if (ref $value && ref $value eq 'ARRAY') {
for (my $m=1;$m<=@$value;$m++) {
push @params,("$parameter_name.$c.$valuename.$m" => $value->[$m-1])
}
} else {
push @params,("$parameter_name.$c.$valuename" => $value)
unless !defined $value && $skip_undef_values;
}
$c++;
}
} else {
for (ref $a ? @$a : $a) {
my ($name,$value) = /([^=]+)\s*=\s*(.+)/;
push @params,("$parameter_name.$c.$keyname" => $name);
push @params,("$parameter_name.$c.$valuename" => $value)
unless !defined $value && $skip_undef_values;
$c++;
}
}
}
return @params;
}
=head2 @arguments = $ec2->launch_perm_parm($prefix,$suffix,$value)
=cut
sub launch_perm_parm {
my $self = shift;
my ($prefix,$suffix,$value) = @_;
return unless defined $value;
$self->_perm_parm('LaunchPermission',$prefix,$suffix,$value);
}
sub create_volume_perm_parm {
my $self = shift;
my ($prefix,$suffix,$value) = @_;
return unless defined $value;
$self->_perm_parm('CreateVolumePermission',$prefix,$suffix,$value);
}
sub _perm_parm {
my $self = shift;
my ($base,$prefix,$suffix,$value) = @_;
return unless defined $value;
my @list = ref $value && ref $value eq 'ARRAY' ? @$value : $value;
my $c = 1;
my @param;
for my $v (@list) {
push @param,("$base.$prefix.$c.$suffix" => $v);
$c++;
}
return @param;
}
=head2 @arguments = $ec2->iam_parm($args)
=cut
sub iam_parm {
my $self = shift;
my $args = shift;
my @p;
push @p,('IamInstanceProfile.Arn' => $args->{-iam_arn}) if $args->{-iam_arn};
push @p,('IamInstanceProfile.Name' => $args->{-iam_name}) if $args->{-iam_name};
return @p;
}
=head2 @arguments = $ec2->block_device_parm($block_device_mapping_string)
=cut
sub block_device_parm {
my $self = shift;
my $devlist = shift or return;
my @dev = ref $devlist && ref $devlist eq 'ARRAY' ? @$devlist : $devlist;
my @p;
my $c = 1;
for my $d (@dev) {
$d =~ /^([^=]+)=([^=]+)$/ or croak "block device mapping must be in format /dev/sdXX=device-name";
my ($devicename,$blockdevice) = ($1,$2);
push @p,("BlockDeviceMapping.$c.DeviceName"=>$devicename);
if ($blockdevice =~ /^vol-/) { # this is a volume, and not a snapshot
my ($volume,$delete_on_term) = split ':',$blockdevice;
push @p,("BlockDeviceMapping.$c.Ebs.VolumeId" => $volume);
push @p,("BlockDeviceMapping.$c.Ebs.DeleteOnTermination"=>$delete_on_term)
if defined $delete_on_term && $delete_on_term=~/^(true|false|1|0)$/
}
elsif ($blockdevice eq 'none') {
push @p,("BlockDeviceMapping.$c.NoDevice" => '');
} elsif ($blockdevice =~ /^ephemeral\d$/) {
push @p,("BlockDeviceMapping.$c.VirtualName"=>$blockdevice);
} else {
my ($snapshot,$size,$delete_on_term,$vtype,$iops) = split ':',$blockdevice;
# Workaround for apparent bug in 2012-12-01 API; instances will crash without volume size
# even if a snapshot ID is provided
if ($snapshot) {
$size ||= eval{$self->describe_snapshots($snapshot)->volumeSize};
push @p,("BlockDeviceMapping.$c.Ebs.SnapshotId" =>$snapshot);
}
push @p,("BlockDeviceMapping.$c.Ebs.VolumeSize" =>$size) if $size;
push @p,("BlockDeviceMapping.$c.Ebs.DeleteOnTermination"=>$delete_on_term)
if defined $delete_on_term && $delete_on_term=~/^(true|false|1|0)$/;
push @p,("BlockDeviceMapping.$c.Ebs.VolumeType"=>$vtype) if $vtype;
push @p,("BlockDeviceMapping.$c.Ebs.Iops"=>$iops) if $iops;
}
$c++;
}
return @p;
}
# ['eth0=eni-123456','eth1=192.168.2.1,192.168.3.1,192.168.4.1:subnet-12345:sg-12345:true:My Weird Network']
# form 1: ethX=network device id
# form 2: ethX=primary_address,secondary_address1,secondary_address2...:subnetId:securityGroupId:deleteOnTermination:description
# form 3: ethX=primary_address,secondary_address_count:subnetId:securityGroupId:deleteOnTermination:description
sub network_interface_parm {
my $self = shift;
my $args = shift;
my $devlist = $args->{-network_interfaces} or return;
my @dev = ref $devlist && ref $devlist eq 'ARRAY' ? @$devlist : $devlist;
my @p;
my $c = 0;
for my $d (@dev) {
$d =~ /^eth(\d+)\s*=\s*([^=]+)$/ or croak "network device mapping must be in format ethX=option-string";
my ($device_index,$device_options) = ($1,$2);
push @p,("NetworkInterface.$c.DeviceIndex" => $device_index);
my @options = split ':',$device_options;
if (@options == 1) {
push @p,("NetworkInterface.$c.NetworkInterfaceId" => $options[0]);
}
else {
my ($ip_addresses,$subnet_id,$security_group_id,$delete_on_termination,$description) = @options;
my @addresses = split /\s*,\s*/,$ip_addresses;
for (my $a = 0; $a < @addresses; $a++) {
if ($addresses[$a] =~ /^\d+\.\d+\.\d+\.\d+$/ ) {
push @p,("NetworkInterface.$c.PrivateIpAddresses.$a.PrivateIpAddress" => $addresses[$a]);
push @p,("NetworkInterface.$c.PrivateIpAddresses.$a.Primary" => $a == 0 ? 'true' : 'false');
}
elsif ($addresses[$a] =~ /^\d+$/ && $a > 0) {
push @p,("NetworkInterface.$c.SecondaryPrivateIpAddressCount" => $addresses[$a]);
}
}
my @sgs = split ',',$security_group_id;
for (my $i=0;$i<@sgs;$i++) {
push @p,("NetworkInterface.$c.SecurityGroupId.$i" => $sgs[$i]);
}
push @p,("NetworkInterface.$c.SubnetId" => $subnet_id) if length $subnet_id;
push @p,("NetworkInterface.$c.DeleteOnTermination" => $delete_on_termination) if length $delete_on_termination;
push @p,("NetworkInterface.$c.Description" => $description) if length $description;
}
$c++;
}
return @p;
}
sub boolean_parm {
my $self = shift;
my ($argname,$args) = @_;
my $name = $self->canonicalize($argname);
return unless exists $args->{$name} || exists $args->{$argname};
my $val = $args->{$name} || $args->{$argname};
return ($argname => $val ? 'true' : 'false');
}
=head2 $version = $ec2->version()
API version.
=cut
sub version {
my $self = shift;
return $self->{version} ||= '2012-12-01';
}
=head2 $ts = $ec2->timestamp
=cut
sub timestamp {
return strftime("%Y-%m-%dT%H:%M:%SZ",gmtime);
}
=head2 $ua = $ec2->ua
LWP::UserAgent object.
=cut
sub ua {
my $self = shift;
return $self->{ua} ||= LWP::UserAgent->new;
}
=head2 @obj = $ec2->call($action,@param);
Make a call to Amazon using $action and the passed arguments, and
return a list of objects.
=cut
sub call {
my $self = shift;
my $response = $self->make_request(@_);
unless ($response->is_success) {
my $content = $response->decoded_content;
my $error;
if ($content =~ /<Response>/) {
$error = VM::EC2::Dispatch->create_error_object($response->decoded_content,$self,$_[0]);
} else {
my $code = $response->status_line;
my $msg = $response->decoded_content;
$error = VM::EC2::Error->new({Code=>$code,Message=>"$msg from API call '$_[0]')"},$self);
}
$self->error($error);
carp "$error" if $self->print_error;
croak "$error" if $self->raise_error;
return;
}
$self->error(undef);
my @obj = VM::EC2::Dispatch->response2objects($response,$self);
# slight trick here so that we return one object in response to
# describe_images(-image_id=>'foo'), rather than the number "1"
if (!wantarray) { # scalar context
return $obj[0] if @obj == 1;
return if @obj == 0;
return @obj;
} else {
return @obj;
}
}
sub sts_call {
my $self = shift;
local $self->{endpoint} = 'https://sts.amazonaws.com';
local $self->{version} = '2011-06-15';
$self->call(@_);
}
sub elb_call {
my $self = shift;
(my $endpoint = $self->{endpoint}) =~ s/ec2/elasticloadbalancing/;
local $self->{endpoint} = $endpoint;
local $self->{version} = '2012-06-01';
$self->call(@_);
}
sub asg_call {
my $self = shift;
(my $endpoint = $self->{endpoint}) =~ s/ec2/autoscaling/;
local $self->{endpoint} = $endpoint;
local $self->{version} = '2011-01-01';
$self->call(@_);
}
=head2 $request = $ec2->make_request($action,@param);
Set up the signed HTTP::Request object.
=cut
sub make_request {
my $self = shift;
my ($action,@args) = @_;
my $request = $self->_sign(Action=>$action,@args);
return $self->ua->request($request);
}
=head2 $request = $ec2->_sign(@args)
Create and sign an HTTP::Request.
=cut
# adapted from Jeff Kim's Net::Amazon::EC2 module
sub _sign {
my $self = shift;
my @args = @_;
my $action = 'POST';
my $host = lc URI->new($self->endpoint)->host;
my $path = '/';
my %sign_hash = @args;
$sign_hash{AWSAccessKeyId} = $self->id;
$sign_hash{Timestamp} = $self->timestamp;
$sign_hash{Version} = $self->version;
$sign_hash{SignatureVersion} = 2;
$sign_hash{SignatureMethod} = 'HmacSHA256';
$sign_hash{SecurityToken} = $self->security_token if $self->security_token;
my @param;
my @parameter_keys = sort keys %sign_hash;
for my $p (@parameter_keys) {
push @param,join '=',map {uri_escape($_,"^A-Za-z0-9\-_.~")} ($p,$sign_hash{$p});
}
my $to_sign = join("\n",
$action,$host,$path,join('&',@param));
my $signature = encode_base64(hmac_sha256($to_sign,$self->secret),'');
$sign_hash{Signature} = $signature;
my $uri = URI->new($self->endpoint);
$uri->query_form(\%sign_hash);
return POST $self->endpoint,[%sign_hash];
}
=head2 @param = $ec2->args(ParamName=>@_)
Set up calls that take either method(-resource_id=>'foo') or method('foo').
=cut
sub args {
my $self = shift;
my $default_param_name = shift;
return unless @_;
return @_ if $_[0] =~ /^-/;
return (-filter=>shift) if @_==1 && ref $_[0] && ref $_[0] eq 'HASH';
return ($default_param_name => \@_);
}
=head1 MISSING METHODS
As of 24 Dec 2012, the following Amazon API calls were NOT
implemented. Volunteers to implement these calls are most welcome.
BundleInstance
CancelBundleTask
CancelConversionTask
CancelReservedInstancesListing
CreateReservedInstancesListing
DescribeBundleTasks
DescribeConversionTasks
DescribeReservedInstancesListings
ImportInstance
ImportVolume
=head1 OTHER INFORMATION
This section contains technical information that may be of interest to developers.
=head2 Signing and authentication protocol
This module uses Amazon AWS signing protocol version 2, as described at
http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/index.html?using-query-api.html.
It uses the HmacSHA256 signature method, which is the most secure
method currently available. For additional security, use "https" for
the communications endpoint:
$ec2 = VM::EC2->new(-endpoint=>'https://ec2.amazonaws.com');
=head2 Subclassing VM::EC2 objects
To subclass VM::EC2 objects (or implement your own from scratch) you
will need to override the object dispatch mechanism. Fortunately this
is very easy. After "use VM::EC2" call
VM::EC2::Dispatch->add_override() one or more times:
VM::EC2::Dispatch->add_override($call_name => $dispatch).
The first argument, $call_name, is name of the Amazon API call, such as "DescribeImages".
The second argument, $dispatch, instructs VM::EC2::Dispatch how to
create objects from the parsed XML. There are three possible syntaxes:
1) A CODE references, such as an anonymous subroutine.
In this case the code reference will be invoked to handle the
parsed XML returned from the request. The code will receive
two arguments consisting of the parsed
content of the response, and the VM::EC2 object used to generate the
request.
2) A VM::EC2::Dispatch method name, optionally followed by its arguments
delimited by commas. Example:
"fetch_items,securityGroupInfo,VM::EC2::SecurityGroup"
This tells Dispatch to invoke its fetch_items() method with
the following arguments:
$dispatch->fetch_items($parsed_xml,$ec2,'securityGroupInfo','VM::EC2::SecurityGroup')
The fetch_items() method is used for responses in which a
list of objects is embedded within a series of <item> tags.
See L<VM::EC2::Dispatch> for more information.
Other commonly-used methods are "fetch_one", and "boolean".
3) A class name, such as 'MyVolume'
In this case, class MyVolume is loaded and then its new() method
is called with the four arguments ($parsed_xml,$ec2,$xmlns,$requestid),
where $parsed_xml is the parsed XML response, $ec2 is the VM::EC2
object that generated the request, $xmlns is the XML namespace
of the XML response, and $requestid is the AWS-generated ID for the
request. Only the first two arguments are really useful.
I suggest you inherit from VM::EC2::Generic and use the inherited new()
method to store the parsed XML object and other arguments.
Dispatch tries each of (1), (2) and (3), in order. This means that
class names cannot collide with method names.
The parsed content is the result of passing the raw XML through a
XML::Simple object created with:
XML::Simple->new(ForceArray => ['item'],
KeyAttr => ['key'],
SuppressEmpty => undef);
In general, this will give you a hash of hashes. Any tag named 'item'
will be forced to point to an array reference, and any tag named "key"
will be flattened as described in the XML::Simple documentation.
A simple way to examine the raw parsed XML is to invoke any
VM::EC2::Generic's as_string() method:
my ($i) = $ec2->describe_instances;
print $i->as_string;
This will give you a Data::Dumper representation of the XML after it
has been parsed.
The suggested way to override the dispatch table is from within a
subclass of VM::EC2:
package 'VM::EC2New';
use base 'VM::EC2';
sub new {
my $self=shift;
VM::EC2::Dispatch->add_override('call_name_1'=>\&subroutine1).
VM::EC2::Dispatch->add_override('call_name_2'=>\&subroutine2).
$self->SUPER::new(@_);
}
See L<VM::EC2::Dispatch> for a working example of subclassing VM::EC2
and one of its object classes.
=head1 DEVELOPING
The git source for this library can be found at https://github.com/lstein/LibVM-EC2-Perl,
To contribute to development, please obtain a github account and then either:
1) Fork a copy of the repository, make your changes against this repository,
and send a pull request to me to incorporate your changes.
2) Contact me by email and ask for push privileges on the repository.
See http://help.github.com/ for help getting started.
=head1 SEE ALSO
L<Net::Amazon::EC2>
L<VM::EC2::Dispatch>
L<VM::EC2::Generic>
L<VM::EC2::BlockDevice>
L<VM::EC2::BlockDevice::Attachment>
L<VM::EC2::BlockDevice::EBS>
L<VM::EC2::BlockDevice::Mapping>
L<VM::EC2::BlockDevice::Mapping::EBS>
L<VM::EC2::Error>
L<VM::EC2::Generic>
L<VM::EC2::Group>
L<VM::EC2::Image>
L<VM::EC2::Instance>
L<VM::EC2::Instance::ConsoleOutput>
L<VM::EC2::Instance::Metadata>
L<VM::EC2::Instance::MonitoringState>
L<VM::EC2::Instance::PasswordData>
L<VM::EC2::Instance::Set>
L<VM::EC2::Instance::State>
L<VM::EC2::Instance::State::Change>
L<VM::EC2::Instance::State::Reason>
L<VM::EC2::KeyPair>
L<VM::EC2::Region>
L<VM::EC2::ReservationSet>
L<VM::EC2::ReservedInstance>
L<VM::EC2::ReservedInstance::Offering>
L<VM::EC2::SecurityGroup>
L<VM::EC2::Snapshot>
L<VM::EC2::Staging::Manager>
L<VM::EC2::Tag>
L<VM::EC2::Volume>
=head1 AUTHOR
Lincoln Stein E<lt>lincoln.stein@gmail.comE<gt>.
Copyright (c) 2011 Ontario Institute for Cancer Research
This package and its accompanying libraries is free software; you can
redistribute it and/or modify it under the terms of the GPL (either
version 1, or at your option, any later version) or the Artistic
License 2.0. Refer to LICENSE for the full license text. In addition,
please see DISCLAIMER.txt for disclaimers of warranty.
=cut
1;
|