Save 37% off PRO during our Black Friday Sale! »

ECSのサービスをslack botでデプロイする // ecs-goploy

Ba0b2e7a2783ae54f12665086315ed83?s=47 h3poteto
July 22, 2017

ECSのサービスをslack botでデプロイする // ecs-goploy

「JAWS-UG コンテナ支部 #9」

Ba0b2e7a2783ae54f12665086315ed83?s=128

h3poteto

July 22, 2017
Tweet

Transcript

  1. ECSͷαʔϏεΛslack botͰσϓϩΠ͢Δ 2017/7/21 JAWS-UG ίϯςφࢧ෦ #9 @h3_poteto

  2. ࣗݾ঺հ • Akira Fukushimaʢ෱ౡ ໌ʣ • github: h3poteto • twitter:

    h3_poteto • ࢓ࣄɿCrowdWorksʢ࠷ۙ͸ΠϯϑϥपΓ
  3. झຯɿσϓϩΠ

  4. ࠓ೔͸σϓϩΠͷ࿩

  5. ECSͰαʔϏε࡞Γ͍ͨΜͩ ͚ͲσϓϩΠͲ͏͠Α͏

  6. ͪΐͬͱ༨ஊ

  7. ઌ೔googleͷਓ͕དྷͯ

  8. ʮECSͬͯσϓϩΠ͕……Ͱ ͏ͪʹདྷΔ͓٬͞Μ΋ ଟ͍Ͱ͢ʯ

  9. ͍΍͍΍ɼ͏ͪͷECSσϓϩ Πָ͔ͩΒʂʂʂ

  10. ECSͰαʔϏε࡞Γ͍ͨΜͩ ͚ͲσϓϩΠͲ͏͠Α͏

  11. CrowdWorksͷͭΒΈ • ڊେͳRailsΞϓϦέʔγϣϯ • Πϯϑϥߏங͸chefͰ • CrowdWorksຊମ͸Dockerʹͨͯ͘͠΋·ͩ·ͩԕ͍ • ґଘ͢Δ෺͕ଟ͘Docker Image͕ංେԽ

    • ιʔείʔυ΋ଟͯ͘Docker Image͕ංେԽ • ࣗલͰ࡞ͬͯΔϑΝΠϧΩϟογϡ
  12. CrowdWorksͷECS׆༻ࣄ৘ • ຊମ͔Β੾Γग़ͨ͠ϚΠΫϩαʔϏε • ͱ͸͍͑·ͩ·ͩશવϚΠΫϩαʔϏεͬΆ ͘͸ͳ͍

  13. ͦΕͰ΋σϓϩΠ͸ඞཁ

  14. ECSͷσϓϩΠͲ͏͠Α͏ ໰୊

  15. aws-cliͷσϓϩΠ͸ෆศ

  16. DockerͷσϓϩΠखॱ • CI౳Ͱ৽͍͠Docker ImageΛ࡞੒ • ϗετ্Ͱ৽͍͠DockerίϯςφΛىಈ • ݹ͍ίϯςφΛఀࢭ

  17. aws ecs update-service

  18. ͱࢥ͍͖΍ ᶃͦͷલʹTaskDefinitionΛ ߋ৽͢Δඞཁ͕͋Δ

  19. ᶄservice-update͸Task͕ ׬શʹ੾ΓସΘΔ·Ͱ଴ͬͯ ͘Εͳ͍

  20. ݱࡏಈ͍͍ͯΔλεΫ

  21. ݱࡏಈ͍͍ͯΔλεΫ ৽͘͠ੜΈग़͞ΕͨλεΫ

  22. ৽͘͠ੜΈग़͞ΕͨλεΫ

  23. ͦΜͳ͜ΜͳͰσϓϩΠπʔ ϧ͕͍͔ͭ͋͘Δ

  24. ECSͷσϓϩΠπʔϧ • amazon-ecs-cli • ecs-formation • ecs-deploy • hako

  25. ͱΓ͋͑ͣecs-deployΛ ࢖ͬͯͨ

  26. None
  27. த਎͸γΣϧεΫϦϓτ

  28. $ ./ecs-deploy \ -c crowdworks-staging \ -n lp-crowdworks-staging \ -i

    $AWS_ECR_REPOSITORY:$CIRCLE_SHA1
  29. ͜ΕΛCircleCI಺͔Βୟ͘

  30. ecs-deployͷ͍͍ͱ͜Ζ • 1ίϚϯυͰTaskDefinitionͷߋ৽ɼupdate- service·Ͱߦ͑Δ • Task͕੾ΓସΘͬͯσϓϩΠ͕׬ྃ͢Δ·Ͱ ଴ͬͯ͘ΕΔ

  31. ecs-deployͷ൵͍͠ͱ͜Ζ • jq, aws-cli౳ґଘ͍ͯ͠Δ΋ͷ͕͍͔ͭ͘…… • γΣϧεΫϦϓτͳͷͰɼslack botͷϓϩά ϥϜ͔Βݺͼग़͢ͱ͖͸֎෦ίϚϯυͱͯ͠ ࣮ߦʢΤϥʔϋϯυϦϯάΊΜͲ͍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Sࣄ৘ • cluster, service࡞ͬͨΓॳظͷTaskDefinition ͷ࡞੒͸શͯterraformͰߦ͍ͬͯΔ • طʹग़དྷ্͕ͬͨcluster, serviceʹɼొ࿥ࡁΈ ͷTaskDefinitionͰσϓϩΠͰ͖Ε͹े෼ •

    ͨͩ͠Image͚ͩ͸ߋ৽͢Δ
  42. < \ OBNFMQDSPXEXPSLTTUBHJOH  JNBHFELSFDSBQOPSUIFBTUBNB[POBXTDPNDSPXEXPSLTDSPXEXPSLTMQEFWFMPQ  FTTFOUJBMUSVF  NFNPSZ3FTFSWBUJPO 

    QPSU.BQQJOHT< \ DPOUBJOFS1PSU ^ >  FOWJSPONFOU< \OBNF3"*-4@&/7 WBMVFTUBHJOH^  \OBNF3"*-4@-0(@50@45%065 WBMVFUSVF^ >  MPH$POpHVSBUJPO\ MPH%SJWFSqVFOUE  PQUJPOT\ UBHDSPXEXPSLTTUBHJOHFDTMQ ^ ^ ^ > ͚ͩ͜͜ม͍͑ͨ
  43. ecs-goploy https://github.com/ crowdworks/ecs-goploy

  44. None
  45. $ ./ecs-goploy --help Deploy commands for ECS Usage: ecs-goploy [command]

    Available Commands: help Help about any command service Service deploy to ECS task Run task on ECS version Print the version number Flags: -h, --help help for ecs-goploy Use "ecs-goploy [command] --help" for more information about a command. αʔϏεσϓϩΠ ୯ൃλεΫ࣮ߦ
  46. FDTHPQMPZTFSWJDFŠIFMQ 4FSWJDFEFQMPZUP&$4 6TBHF FDTHPQMPZTFSWJDF<qBHT> 'MBHT D DMVTUFSTUSJOH/BNFPG&$4DMVTUFS FOBCMFSPMMCBDL3PMMCBDLUBTLEFpOJUJPOJGOFXWFSTJPOJTOPU SVOOJOHCFGPSF5*.&065 I

    IFMQIFMQGPSTFSWJDF J JNBHFTUSJOH/BNFPG%PDLFSJNBHFUPSVO FYSFQPJNBHFMBUFTU Q QSPpMFTUSJOH"841SPpMFUPVTF S SFHJPOTUSJOH"843FHJPO/BNF O TFSWJDFOBNFTUSJOH/BNFPGTFSWJDFUPEFQMPZ E UBTLEFpOJUJPOTUSJOH/BNFPGCBTFUBTLEFpOJUJPOUPEFQMPZ'BNJMZ BOESFWJTJPO GBNJMZSFWJTJPO PSGVMM"3/ U UJNFPVUJOU5JNFPVUTFDPOET4DSJQUNPOJUPST&$44FSWJDFGPS OFXUBTLEFpOJUJPOUPCFSVOOJOH EFGBVMU
  47. Φϓγϣϯ͕ଟ͍……

  48. ecs-deployͷΦϓγϣϯ΋ ଟ͔ͬͨͷͰڐͯ͠……

  49. ͦ΋ͦ΋ECSؔ࿈ͷύϥϝʔ λ͸ଟ͍……

  50. ecs-goploy service

  51. $ ./ecs-goploy service \ -c crowdworks-staging \ -n lp-crowdworks-staging \

    -i $AWS_ECR_REPOSITORY:$CIRCLE_SHA1
  52. ecs-goploy service • ಈ͍͍ͯΔECS Serviceͷ৘ใऔಘ • ্هͷ৘ใ͔ΒTask DefinitionΛׂΓग़͢ • Docker

    Imageͷ߲໨͚ͩ৽͍͠ImageͰ্ॻ͖ͯ͠ొ࿥ • ৽͍͠Task DefinitionΛࢦఆͯ͠ update-service૬౰ͷAPIΛୟ͘ • ৽͍͠λεΫ͕ಈ͖ग़͢ͷΛ଴ͭ • ΋͠৽͍͠λεΫ͕ಈ͖ग़͞ͳ͍৔߹ɼݩʑಈ͍͍ͯͨTask Definition Ͱ࠶౓ update-service͠ϩʔϧόοΫ͢Δ
  53. ݱࡏಈ͍͍ͯΔλεΫͷ TaskDefinitionΛऔಘ͢Δ

  54. TaskDefinitionͷऔಘ • ϥετͰϩʔϧόοΫ͢Δࡍʹઈରʹඞཁ • Ҿ਺ͰTask Definition͕༩͑ΒΕͳ͔ͬͨ৔߹ ʹɼϕʔεͷTask Definitionͱͯ͠࢖͏ʢຆͲ ͷύλʔϯ͸͜Εʣ •

    Ҿ਺ͰϕʔεͱͳΔTask DefinitionΛ༩͑Δ͜ ͱ΋Մೳ
  55. Ҿ਺ͰTaskDeinifitionΛ༩͑Δ • ImageҎ֎ͷ߲໨ߋ৽ʹ͸ରԠ͍ͯ͠ͳ͍ͨΊ • ؀ڥม਺౳Λߋ৽͢ΔͨΊɼterraformͰ৽͠ ͍Task DefinitionΛ࡞Δ • ͜ͷ৽͍͠Task DefinitionͰσϓϩΠ͍ͨ͠৔

    ߹
  56. Imageͷ߲໨͚ͩߋ৽ͨ͠ TaskDefinitionΛొ࿥

  57. update-service૬౰ͷAPIΛ ୟ͘

  58. ৽͍͠λεΫ͕ಈ͖ग़͢ͷΛ ଴ͭ

  59. λεΫ੾ΓସΘΓͷ൑ఆ • λεΫ͕ऩଋ͢ΔͷΛ଴ͭ • ऩଋͨ͠λεΫ͕σϓϩΠ࣌ʹࢦఆͨ͠Task DefinitionͰ͋Δ͜ͱ • ʮ଴ͪʯ͕͋ΔͷͰλΠϜΞ΢τΛઃఆͯ͠ ͍Δ

  60. ੾ΓସΘΒͳ͍৔߹͸ϩʔϧ όοΫσϓϩΠΛߦ͏

  61. ৽͍͠λεΫ͕ਖ਼ৗʹىಈ͠ͳ͍ͱ͖ ݱࡏಈ͍͍ͯΔઃఆͰ࠶౓σϓϩΠ͠௚͢

  62. ecs-goploy task

  63. FDTHPQMPZUBTLa DDSPXEXPSLTTUBHJOHa OMQDSPXEXPSLTTUBHJOHa J"84@&$3@3&104*503:$*3$-&@4)"a E"84@5"4,@%&'*/*5*0/a ŠDPNNBOElFDIPIPHFz

  64. ecs-goploy task • Ҿ਺Ͱ༩͑ΒΕͨTask Definition͕ଘࡏ͢Δ͔֬ೝ • Ҿ਺Ͱ༩͑ΒΕͨTask DefinitionͷDocker Image߲໨͚ͩ৽͠ ͍ImageͰ্ॻ͖ͯ͠ొ࿥

    • ৽͍͠Task DefinitionΛࢦఆͯ͠ run-task ૬౰ͷAPIΛୟ͘ • ৽͍͠λεΫͷ࣮ߦ׬ྃΛ଴ͭ • λεΫͷ࣮ߦ͕ऴྃͨ͠ΒɼͦͷExitCodeʹԠͨ͡ग़ྗΛฦ͢
  65. TaskDefinitionͷ֬ೝ

  66. TaskDefinition͸ඞਢ • serviceͱҧͬͯݱࡏಈ͍͍ͯΔ΋ͷ͕ͳ͍ͷ ͰɼϕʔεͱͳΔTaskDefinitionΛࢦఆ͠ͳ͍ ͱ͍͚ͳ͍ • serviceͱҧ͏TaskDefinition΋ࢦఆͰ͖Δ

  67. Imageͷ߲໨͚ͩΛߋ৽ͨ͠ TaskDefinitionΛొ࿥

  68. run-task૬౰ͷAPIΛୟ͘

  69. ৽͍͠λεΫͷ࣮ߦऴྃΛ଴ ͭ

  70. ࣮ߦऴྃ

  71. ExitCodeΛ֬ೝͯͦ͠ΕʹԠ ͨ͡ग़ྗΛฦ͢

  72. ExitCode

  73. ExitCodeʹԠͨ͡ग़ྗ • ExitCode͕0ͳΒecs-goploy΋ਖ਼ৗऴྃ • ExitCode͕0Ҏ֎ͩͱecs-goploy΋ྫ֎Λు ͘

  74. ecs-deployͱͷࠩ෼ • taskͱ͍͏ίϚϯυͰrun-task͕Ͱ͖Δ • ϕʔεͱͳΔTaskDefinitionΛࢦఆͰ͖Δ
 ʢ࣮͸ecs-deployͰ΋Ͱ͖Δʁ

  75. ্ॻ͖Ͱ͖ΔTaskDefinition ߲໨͸ImageͷΈ

  76. ;ͩΜͷσϓϩΠͰ͸Image ͔͠৽͍ͯ͘͠͠ͳ͍

  77. ؀ڥม਺౳Λมߋ͍ͨ͠৔߹ ʹ͸terraformͰϕʔεͱͳΔ TaskDefinitionΛมߋ͢Δ

  78. packageͱͯ͠΋ར༻Ͱ͖Δ

  79. JNQPSUlHJUIVCDPNDSPXEXPSLTFDTHPQMPZEFQMPZ GVODNBJO \ T FSSEFQMPZ/FX4FSWJDF DMVTUFS TFSWJDFOBNF  OHJOYTUBCMF OJM

     UJNF.JOVUF USVF    JGFSSOJM\ MPH'BUBMG <&3303>W FSS  ^ EFQMPZOFXJNBHF JGFSST%FQMPZ FSSOJM\ MPH'BUBMG <&3303>W FSS  ^ ^
  80. packageͱͯ͠import • σϓϩΠͷॱংΛมߋ͍ͨ͠ • σϓϩΠͷ߹ؒʹผͷॲཧΛڬΈ͍ͨ

  81. ͋ͱ͸͜ΕΛslack botͷιʔ εͰimport͢Δ

  82. JNQPSUlHJUIVCDPNDSPXEXPSLTFDTHPQMPZEFQMPZ GVODNBJO \ NJHSBUJPOλεΫ࣮ߦ UBTL FSSEFQMPZ/FX5BTL DMVTUFS DPOUBJOFSOBNF OHJOYTUBCMF FDIPIPHF

    TBNQMFUBTL EFpOJUJPO   UJNF.JOVUF    JGFSSOJM\ MPH'BUBM FSS  ^ JG@ FSSUBTL3VO FSSOJM\ MPH'BUBM FSS  ^ MPH1SJOUMO <*/'0>5BTLTVDDFTT  αʔϏεσϓϩΠ T FSSEFQMPZ/FX4FSWJDF DMVTUFS TFSWJDFOBNF OHJOYTUBCMF OJM  UJNF.JOVUF USVF    JGFSSOJM\ MPH'BUBMG <&3303>W FSS  ^ EFQMPZOFXJNBHF JGFSST%FQMPZ FSSOJM\ MPH'BUBMG <&3303>W FSS  ^ ^
  83. None
  84. ͳΜͰslack botΛgoͰʁ

  85. None
  86. Interactive Message ࢖͍͍ͨ

  87. ϚΠΫϩαʔϏεʹ͍͖ͯ͠ ͍ͨͷͰɼαʔϏε͸ͲΜͲ Μ૿͑Δ༧ఆ

  88. ECSʹαʔϏε࡞Δͨͼʹbot Λ࡞Δͷ͸ආ͚͍ͨ

  89. ͲͷαʔϏεΛσϓϩΠ͢Δ ͔બ୒Ͱ͖Δbotʹ͍ͨ͠

  90. None
  91. DMVTUFSDSPXEXPSLTTUBHJOH JNBHFDSPXEXPSLTCBOLJOHQMBZ TFSWJDF OBNFCBOLJOHDSPXEXPSLTTUBHJOH UJNFPVU UBTL DPOUBJOFS@OBNFCBOLJOHBEIPDDSPXEXPSLTTUBHJOH UBTL@EFpOJUJPOCBOLJOHBEIPDDSPXEXPSLTTUBHJOH DPNNBOEQMBZ@SVONBJOUBTLTNJHSBUJPO'MZXBZ.JHSBUF5BTL UJNFPVU

    SFQPTJUPSZ PXOFSDSPXEXPSLTKQ OBNFCBOLJOH CSBODIEFWFMPQ
  92. slack bot • ECSͷαʔϏε৘ใΛઃఆϑΝΠϧʹ௥ه͢ Δ͚ͩͰσϓϩΠͰ͖Δ • ୭͕σϓϩΠͯ͠Δ͔ʢ͔ͨ͠ʣ໌֬

  93. ॴײ

  94. • ͠͹Β͘ecs-goployͰ଍Γͦ͏ • goָ͍͠ • σϓϩΠεΫϦϓτͷςετ͕ॻ͚Δɼ࠷ߴ • slackͷInteractive MessageΛड͚෇͚Δͷ ͸ɼී௨ʹhttpαʔόͳͷͰෳࡶʹͳΓ͕ͪ

    • ࠓͷͱ͜Ζࣄނͳ͠
  95. ࠓޙདྷͦ͏ͳͱ͜Ζ

  96. • DesiredCountมߋΦϓγϣϯʢecs-deployʹ ͸͋Δʣ • ෆཁʹͳͬͨTaskDefinitionͷ࡟আʢecs- deployʹ͸͋Δʣ

  97. ecs-goploy ͨͿΜ·ͩόά͕຾͍ͬͯΔ ؾ͕͢Δ

  98. godoc΋ҰԠॻ͍ͯ͋ΔͷͰ ࢖ͬͯΈ͍ͯͩ͘͞

  99. ΋͠Կ͔ෆศ͕͋Ε͹ ͝࿈བྷ͍ͩ͘͞

  100. ࢀߟʹ͍͖ͤͯͨͩ͞·ͨ͠ • https://speakerdeck.com/yukiyan/ ecsfalsedepuroituruwoshi-siteiruhua