CloudFormationとOpsWorksによる継続的なインフラ更新について / Infrastructure with CloudFormation and OpsWorks

CloudFormationとOpsWorksによる継続的なインフラ更新について / Infrastructure with CloudFormation and OpsWorks

Minami Aoyama Night #3
https://minami-aoyama-night.connpass.com/event/58102/

CloudFormationとOpsWorksによる継続的なインフラ更新について

231dbe4867c139c325b61e808b757750?s=128

Tatsuro Mitsuno

June 16, 2017
Tweet

Transcript

  1. © 2017 VASILY,Inc. CloudFormationͱOpsWorksʹΑΔ ܧଓతͳΠϯϑϥߋ৽ʹ͍ͭͯ Minami Aoyama Night#3
 2017/06/16 Fri.

    ޫ໺ୡ࿕
  2. © 2017 VASILY,Inc. ࣗݾ঺հ ▸ 5BUTVSP.JUTVOPޫ໺ୡ࿕ ▸ Ϡϑʔגࣜձࣾ ▸ גࣜձࣾ7"4*-:

    ▸ ΠϯϑϥΤϯδχΞ ▸ 5XJUUFS(JU)VC2JJUB!LPUBUTV *DPOJMMVTUSBUFECZ:04)*
 IUUQTKBKQGBDFCPPLDPNZPTIJZPOF ฐٕࣾज़ސ໰·ͭ΋ͱΏ͖ͻΖࢯ
  3. © 2017 VASILY,Inc. ຊ೔ͷओ໾ $MPVE'PSNBUJPO 0QT8PSLT

  4. © 2017 VASILY,Inc. ໨࣍ ▸ ࣗݾ঺հ ▸ ੜ࢈ੑ޲্΁ͷϞνϕʔγϣϯ ▸ *20/ͱͦΕΛࢧ͑Δٕज़

    ▸ ࣾ֎΁ͷల։ ▸ ՝୊ ▸ Πϯϑϥߏஙͷੜ࢈ੑ޲্ ▸ $MPVE'PSNBUJPO0QT8PSLT ▸ ز͔ͭͷ޻෉
  5. © 2017 VASILY,Inc. ੜ࢈ੑ޲্΁ͷϞνϕʔγϣϯ

  6. © 2017 VASILY,Inc. Ҏ্ͷϑΝογϣϯ&$αΠτ͔Βͷ΂ສ఺Λ௒͑Δ঎඼Λܝࡌ ݄ؒສਓҎ্͕ར༻͢Δ೔ຊ࠷େڃͷϑΝογϣϯαΠτ

  7. © 2017 VASILY,Inc. Google Play ϕετΞϓϦ J04ΞϓϦ ɾ"QQ4UPSF#&45 ɾ&TTFOUJBMೝఆ "OESPJEΞϓϦ

    ɾ೥(PPHMF1MBZϕετΞϓϦ ɾ೥(PPHMF1MBZϕετΞϓϦ ɾ೥(PPHMF1MBZϕετΞϓϦ
 ɹɹϕετΠϊϕʔςΟϒΞϓϦେ৆ ɾ(PPHMF͔Βτοϓσϕϩούʔೝఆ ೥࿈ଓϕετΞϓϦड৆͸ ੈքͰ7"4*-:͚ͩ
  8. © 2017 VASILY,Inc. IQONΛࢧ͑Δٕज़ ▸ ߴ౓ͳΞϓϦ։ൃٕज़ ▸ ֦ுੑͷߴ͍Ϋϩʔϥʔٕज़ ▸ ๲େͳϑΝογϣϯσʔλΛݩʹͨ͠ػցֶशɾը૾ղੳ

    ΫΤϦը૾ ݕग़ ݕࡧ
  9. © 2017 VASILY,Inc. ଞࣾͱͷ࿈ܞࣄྫɿWowma! ▸ ,%%*ίϚʔεϑΥϫʔυגࣜձࣾ ▸ &$Ϟʔϧʮ8PXNBʯ΁
 ը૾ղੳٕज़Λఏڙ ▸

    εφοϓࣸਅ͔Βண༻ΞΠςϜͱ
 ͦͷྨࣅΞΠςϜΛਪન ▸ ΞϓϦ։ൃͰ΋ڠྗ
  10. © 2017 VASILY,Inc. ՝୊ɿ؅ཧωοτϫʔΫͷ૿Ճ αʔϏε"
 ʢӡ༻தʣ αʔϏε# ʢӡ༻தʣ αʔϏε$ ʢӡ༻தʣ

    *DPOTNBEFCZ'SFFQJLGSPNXXXqBUJDPODPNJTMJDFOTFECZ$$#: αʔϏε% ʢ৽ن։ൃʣ αʔϏε&
 ʢ৽ن։ൃʣ ɾɾɾ ॳظߏங ηΩϡϦςΟϧʔϧߋ৽ɺΦϓγϣϯมߋ ؅ཧऀʢޫ໺ʣ ӡ༻ෛՙ
  11. © 2017 VASILY,Inc. ՝୊ɿ؅ཧωοτϫʔΫͷ૿Ճ αʔϏε"
 ʢӡ༻தʣ αʔϏε# ʢӡ༻தʣ αʔϏε$ ʢӡ༻தʣ

    αʔϏε% ʢ৽ن։ൃʣ αʔϏε&
 ʢ৽ن։ൃʣ ɾɾɾ ؅ཧऀʢޫ໺ʣ ॳظߏங
 ܾ·ͬͨखॱ͕ଘࡏ ηΩϡϦςΟϧʔϧߋ৽ɺΦϓγϣϯมߋ
 มߋཧ༝͸ʁมߋཤྺ͸ʁ ܦݧ஋ͷ஝ੵɾڞ༗ʹ΋՝୊ *DPOTNBEFCZ'SFFQJLGSPNXXXqBUJDPODPNJTMJDFOTFECZ$$#:
  12. © 2017 VASILY,Inc. ՝୊ɿ؅ཧωοτϫʔΫͷ૿Ճ αʔϏε਺ ӡ༻ෛՙ ໨ࢦ͢΂͖ੈք ਏ͍ੈք

  13. © 2017 VASILY,Inc. ՝୊ɿ؅ཧωοτϫʔΫͷ૿Ճ লྗԽɾޮ཰ԽɾจॻԽʹΑΔ
 ୯Ґ࣌ؒ͋ͨΓͷੜ࢈ੑ޲্͕ඞཁ αʔϏε"
 ʢӡ༻தʣ αʔϏε# ʢӡ༻தʣ

    αʔϏε$ ʢӡ༻தʣ αʔϏε% ʢ৽ن։ൃʣ αʔϏε&
 ʢ৽ن։ൃʣ ɾɾɾ ؅ཧऀʢޫ໺ʣ ॳظߏங
 ςϯϓϨʔτԽ ηΩϡϦςΟϧʔϧߋ৽ɺΦϓγϣϯมߋ
 มߋҙਤɺཤྺͷه࿥ *DPOTNBEFCZ'SFFQJLGSPNXXXqBUJDPODPNJTMJDFOTFECZ$$#:
  14. © 2017 VASILY,Inc. ໨࣍ ▸ ࣗݾ঺հ ▸ ੜ࢈ੑ޲্΁ͷϞνϕʔγϣϯ ▸ *20/ͱͦΕΛࢧ͑Δٕज़

    ▸ ࣾ֎΁ͷల։ ▸ ՝୊ ▸ Πϯϑϥߏஙͷੜ࢈ੑ޲্ ▸ $MPVE'PSNBUJPO0QT8PSLT ▸ ز͔ͭͷ޻෉
  15. © 2017 VASILY,Inc. Πϯϑϥߏஙͷੜ࢈ੑ޲্

  16. © 2017 VASILY,Inc. ໨ࢦ࢟͢ ▸ ࡞ۀ͕ه࿥ɾ࠶ݱՄೳͰ͋Δ͜ͱ ▸ ઃఆͷมߋ͕௥੻ՄೳͰ͋Δ͜ͱ ▸ ઃఆͷมߋΛ൓ө͢Δखॱཱ͕֬͞Ε͍ͯΔ͜ͱ

  17. © 2017 VASILY,Inc. ຊ೔ͷओ໾ $MPVE'PSNBUJPO 0QT8PSLT

  18. © 2017 VASILY,Inc. CloudFormationͱOpsWorksʹΑΔߏ੒؅ཧ &-# &$ 3%4 4 $MPVE'SPOU *".

    FUD 4VCOFU ωοτϫʔΫ ߏ੒؅ཧ Πϯελϯε ߏ੒؅ཧ
  19. © 2017 VASILY,Inc. CloudFormationͱOpsWorksʹΑΔߏ੒؅ཧ &-# &$ 3%4 4 $MPVE'SPOU *".

    FUD 4VCOFU
  20. © 2017 VASILY,Inc. CloudFormationͱOpsWorksʹΑΔߏ੒؅ཧ &-# &$ 3%4 4 $MPVE'SPOU *".

    FUD 4VCOFU
  21. © 2017 VASILY,Inc. CloudFormation ▸ "84ͷϦιʔεʢ71$ɺ4VCOFUɺ&-#ɺ&$ɺ3%4ɺʣΛʮςϯϓϨʔτʯͰ؅ཧ ▸ ςϯϓϨʔτ͸+40/:".-Ͱهड़͞ΕΔςΩετ ▸ ςϯϓϨʔτΛݩʹ࡞ΒΕΔϦιʔεͷ·ͱ·ΓΛʮελοΫʯͱݺͿ

    ▸ ʮύϥϝʔλʯΛ࢖ͬͯɺςϯϓϨʔτ͔ΒҟͳΔ஋Λ࣋ͬͨελοΫΛ࡞੒Մೳ ▸ ྫ͑͹ɺ71$΍4VCOFUͷ$JES#MPDLΛม͑Δɺ&$ΠϯελϯελΠϓΛม͑Δ ▸ &$ͷ44)ݤొ࿥ͳͲɺͰ͖ͳ͍͜ͱ΋एׯ͋Δ
  22. © 2017 VASILY,Inc. AWSTemplateFormatVersion: 2010-09-09 Parameters: VPCCidrBlock: Type: 'String' Default:

    '10.0.0.0/16' PublicFirstCidrBlock: Type: 'String' Default: '10.0.0.0/24' Resources: # ------------------------------ EC2VPC: Type: 'AWS::EC2::VPC' Properties: CidrBlock: !Ref VPCCidrBlock EnableDnsSupport: true EnableDnsHostnames: true InstanceTenancy: 'default' # ------------------------------ EC2SubnetPublicFirst: Type: 'AWS::EC2::Subnet' Properties:
 VpcId: !Ref EC2VPC AvailabilityZone: Fn::Select: - 1 - Fn::GetAZs: !Ref 'AWS::Region' CidrBlock: !Ref PublicFirstCidrBlock MapPublicIpOnLaunch: true ྫ 71$ͱ4VCOFUΛ ࡞੒͢ΔςϯϓϨʔτ ύϥϝʔλ 71$ఆٛ 4VCOFUఆٛ
  23. © 2017 VASILY,Inc. AWSTemplateFormatVersion: 2010-09-09 Parameters: VPCCidrBlock: Type: 'String' Default:

    '10.0.0.0/16' PublicFirstCidrBlock: Type: 'String' Default: '10.0.0.0/24' Resources: # ------------------------------ EC2VPC: Type: 'AWS::EC2::VPC' Properties: CidrBlock: !Ref VPCCidrBlock EnableDnsSupport: true EnableDnsHostnames: true InstanceTenancy: 'default' # ------------------------------ EC2SubnetPublicFirst: Type: 'AWS::EC2::Subnet' Properties:
 VpcId: !Ref EC2VPC AvailabilityZone: Fn::Select: - 1 - Fn::GetAZs: !Ref 'AWS::Region' CidrBlock: !Ref PublicFirstCidrBlock MapPublicIpOnLaunch: true ྫ 71$ͱ4VCOFUΛ ࡞੒͢ΔςϯϓϨʔτ ύϥϝʔλࢀর Ϧιʔεࢀর ૊ΈࠐΈؔ਺ͰΑΓॊೈͳॲཧ΋Մೳ ʮ͋ΔϦʔδϣϯͷͭ໨ͷ";Λऔಘʯ
  24. © 2017 VASILY,Inc. CloudFormation ▸ ৽نߏங࣌ͷʮS3όέοτ࡞ͬͯɺVPC࡞ͬͯɺDHCPΦϓγϣϯ࡞ͬͯɺNATήʔτ΢ΣΠ ࡞ͬͯɺϧʔτςʔϒϧ࡞ͬͯɺαϒωοτ࡞ͬͯɺϧʔτ࡞ͬͯɺΠϯλʔωοτήʔτ΢Σ Π࡞ͬͯɺ؂ࠪϩάΛ༗ޮʹͯ͠ɺ౿Έ୆࡞ͬͯɺΞϓϦέʔγϣϯαʔό࡞ͬͯɺηΩϡϦςΟ άϧʔϓ࡞ͬͯɺ...ʯΛຕͷςΩετͰදݱ͢Δ ▸

    :".-͕ར༻Ͱ͖ΔΑ͏ʹͳͬͨͨΊɺઃܭҙਤΛίϝϯτͰ࢒͢͜ͱ΋Մೳ ▸ ӡ༻தͷมߋ΋ςϯϓϨʔτ͕ৗʹϚελʔͱͯ͠ػೳ͢Δ
  25. © 2017 VASILY,Inc. CloudFormationͱOpsWorksʹΑΔߏ੒؅ཧ &-# &$ 3%4 4 $MPVE'SPOU *".

    FUD 4VCOFU
  26. © 2017 VASILY,Inc. OpsWorks ▸ 0QT8PSLTελοΫͱ0QT8PSLTGPS$IFG"VUPNBUFͷछྨ͕͋Δ ▸ લऀ͸$IFGϕʔεͷΞϓϦέʔγϣϯ؅ཧαʔϏεʢฐࣾͰར༻͍ͯ͠Δͷ͸ͪ͜Βʣ ▸ ޙऀ͸Ϛωʔδυ$IFG"VUPNBUF

    ▸ ʮελοΫʯͷதʹɺෳ਺ͷʮϨΠϠʔʯΛ࡞੒ʢ$MPVE'PSNBUJPOͷελοΫͱ͸ผʣ ▸ ΠϯελϯεΛʮϨΠϠʔʯʹొ࿥ ▸ ϨΠϠʔʹର࣮ͯ͠ߦ͢ΔDPPLCPPLΛઃఆ ▸ ελοΫʷ*".Ϣʔβ୯ҐͰɺ44)ݤɾTVEPݖݶͷ؅ཧ͕Մೳ ▸ ྫ͑͹ɺ*".ϢʔβʮBMJDFʯΛελοΫʢ"1*ϨΠϠʔΛ࣋ͭʣʹ௥Ճ͠TVEPݖݶΛ෇༩
 "1*ϨΠϠʔͷΠϯελϯεʹϢʔβ͕௥Ճ͞Εɺ44)TVEP͕ར༻ՄೳʹͳΔ
  27. © 2017 VASILY,Inc.

  28. © 2017 VASILY,Inc.

  29. © 2017 VASILY,Inc. OpsWorks ▸ 0QT8PSLTࣗମ΋$MPVE'PSNBUJPOͰ؅ཧՄೳ ▸ 0QT8PSLTʹొ࿥͞ΕͨΠϯελϯε͸ىಈ࣌ʹ·ͣ$IFG͕࣮ߦ͞ΕΔ ▸ ೚ҙͷλΠϛϯάͰɺ8FC6*͔Β$IFGͷ࠶࣮ߦ΋Մೳ

    ▸ 44)ݤΛ࣋ͨͳ͍σϑΥϧτϢʔβʢ"NB[PO-JOVYͳΒFDVTFSʣ΋࡞੒Մೳ
  30. © 2017 VASILY,Inc. CloudFormationͱOpsWorksʹΑΔߏ੒؅ཧ &-# &$ 3%4 4 $MPVE'SPOU *".

    FUD 4VCOFU
  31. © 2017 VASILY,Inc. ςετͱσϓϩΠ ▸ ςϯϓϨʔτͷݕূͱ൓өΛ෼͔Γ΍͍͢Ϩʔϧʹ৐ͤΔ ▸ ςετϑΣʔζͰ$MPVE'PSNBUJPOςϯϓϨʔτΛݕূ ▸ BXTDMPVEGPSNBUJPOWBMJEBUFUFNQMBUFUFNQMBUF

    ▸ NBTUFSϒϥϯν΁ͷϚʔδͱಉ࣌ʹ$MPVE'PSNBUJPOͷʮมߋηοτʯΛ࡞੒ ▸ Ϧιʔεͷมߋ಺༰ʢߋ৽ɾஔ׵ɾ࡟আʣ͕8FCίϯιʔϧʹͯ֬ೝՄೳͳঢ়ଶʹͳΔ ▸ ໰୊ͳ͚Ε͹ʮ൓өʯΛ࣮ߦ ▸ $PEF1JQFMJOF͸ະ࢖༻ ▸ ѻ͑ΔςϯϓϨʔτʹαΠζ্ݶ͕͋ΔʢCZUFT
  32. © 2017 VASILY,Inc. ςϯϓϨʔτͷݕূ มߋηοτͷ࡞੒ มߋ಺༰ͷ֬ೝɾঝೝ ςετͱσϓϩΠ

  33. © 2017 VASILY,Inc. &-# &$ 3%4 4 $MPVE'SPOU *". FUD

    4VCOFU ωοτϫʔΫ ߏ੒؅ཧ Πϯελϯε ߏ੒؅ཧ ςετͱσϓϩΠ
  34. © 2017 VASILY,Inc. ໨ࢦ࢟͢ͱୡ੒ঢ়گ ▸ ✅࡞ۀ͕ه࿥ɾ࠶ݱՄೳͰ͋Δ͜ͱ ▸ $MPVE'PSNBUJPOͷςϯϓϨʔτʹߏ੒ͷҰ੾Λهड़ɾิ଍ίϝϯτ௥Ճ ▸ 0QT8PSLTʹΑΔߏ੒؅ཧ

    ▸ ✅ઃఆͷมߋ͕௥੻ՄೳͰ͋Δ͜ͱ ▸ (JU)VCʹΑΔςϯϓϨʔτͷมߋཤྺͱ$MPVE'PSNBUJPOଆͷมߋཤྺ ▸ ✅ઃఆͷมߋΛ൓ө͢Δखॱཱ͕֬͞Ε͍ͯΔ͜ͱ ▸ $JSDMF$*ʹΑΔςϯϓϨʔτͷݕূͱߋ৽ηοτ࡞੒ ▸ ࠷ऴతͳ൓ө͸ਓͷखΛհ͢ʢա౓ͳࣗಈԽ͸ආ͚Δʣ
  35. © 2017 VASILY,Inc. ز͔ͭͷ޻෉

  36. © 2017 VASILY,Inc. CloudFormation Stackͷ෼཭ํ਑ ▸ *OGSB4UBDLͱ4FSWJDF4UBDLʹ෼཭ ▸ *OGSB4UBDL"84ΞΧ΢ϯτ୯ҐͰͭ ▸

    *".Ϣʔβ΍$MPVE5SBJMͳͲ ▸ 4FSWJDF4UBDLαʔϏεͷ؀ڥ୯Ґʢྫ͑͹QSPEʣ͝ͱʹͭ ▸ 4όέοτ΍71$ͳͲ ▸ 4FSWJDF4UBDL͸ಉ͡ςϯϓϨʔτͷύϥϝʔλΛมߋͯ͠࡞੒͞ΕΔ ▸ ͋Δ"84ΞΧ΢ϯτͰϢχʔΫʹඞཁͳ΋ͷͱෳ਺ଘࡏ͠͏Δ΋ͷΛ෼཭͓ͯ͘͠
  37. © 2017 VASILY,Inc. CloudFormation Stackͷ෼཭ํ਑ *". 4 αʔϏε༻ςϯϓϨʔτ Πϯϑϥ༻ςϯϓϨʔτ *DPOTNBEFCZ'SFFQJLGSPNXXXqBUJDPODPNJTMJDFOTFECZ$$#:

    ɾɾɾ ඞཁʹԠͯ͡ɺ αʔϏε༻ςϯϓϨʔτͷ ύϥϝʔλΛมߋ͠࡞੒
  38. © 2017 VASILY,Inc. OpsWorksͱAutoScaling ▸ 0QT8PSLT͸ศརͳ൒໘ɺ੍໿΋͋Δ ▸ "-#ͷαϙʔτ͕ͳ͍ɺ6TFS%BUBͷαϙʔτ͕ͳ͍ͳͲ ▸ ೚ҙͷ&$ΠϯελϯεΛޙ෇Ͱ0QT8PSLTͷ؅ཧԼʹஔ͚Δ͜ͱΛར༻

    ▸ $MPVE'PSNBUJPOʹͯ6TFS%BUB෇͖ͷ"VUP4DBMJOHΛఆٛ ▸ 6TFS%BUBͰඞཁͳॲཧ 0QT8PSLT΁ͷొ࿥ ▸ 0QT8PSLTͷొ࿥ʹඞཁͳ*%͸ɺ$MPVE'PSNBUJPOͷ૊ΈࠐΈؔ਺ʹΑΔࢀরͰऔಘͰ͖Δ
  39. © 2017 VASILY,Inc. OpsWorksͱAutoScaling AutoScalingLaunchConfiguration: Type: 'AWS::AutoScaling::LaunchConfiguration' Properties: IamInstanceProfile: !Ref

    IAMInstanceProfile ImageId: !Ref AMI InstanceType: 'm4.large' UserData: Fn::Base64: !Sub - | #cloud-config repo_update: true repo_upgrade: security packages: - python-pip runcmd: - LC_ALL=C sudo pip install awscli - export INSTANCE_ID=$(/usr/local/bin/aws opsworks --region us-east-1 register --infrastructure-class ec2 --stack-id ${OpsWorksStackId} --local --use-instance-profile 2>&1 | grep 'Instance ID' | grep -oE '[a-z0-9\-]+$') - while ! /usr/local/bin/aws opsworks --region us-east-1 assign-instance --instance-id $INSTANCE_ID --layer-ids $ {OpsWorksLayerId}; do echo 'wait...'; sleep 20; done - { OpsWorksStackId: !Ref OpsWorksStack, OpsWorksLayerId: !Ref OpsWorksLayer } 6TFS%BUB͔Β0QT8PSLT΁ొ࿥͢Δྫ
  40. © 2017 VASILY,Inc. ·ͱΊ

  41. © 2017 VASILY,Inc. Πϯϑϥ෦෼ͷੜ࢈ੑ޲্΁ͷऔΓ૊Έ ▸ αʔϏε਺ͷ૿Ճͱӡ༻ෛՙͷ૿ՃΛઢܗʹ͠ͳ͍࢓૊Έ ▸ $MPVE'PSNBUJPOͱ0QT8PSLTʹΑΔΠϯϑϥͷจॻԽɾ࠶ར༻ ▸ $JSDMF$*ʹΑΔςετͱσϓϩΠ

  42. © 2017 VASILY,Inc. ஥ؒืूத IUUQTXXXXBOUFEMZDPNDPNQBOJFTWBTJMZ