Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

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

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

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

Tatsuro Mitsuno

June 16, 2017
Tweet

More Decks by Tatsuro Mitsuno

Other Decks in Technology

Transcript

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

    ▸ ΠϯϑϥΤϯδχΞ ▸ 5XJUUFS(JU)VC2JJUB!LPUBUTV *DPOJMMVTUSBUFECZ:04)*
 IUUQTKBKQGBDFCPPLDPNZPTIJZPOF ฐٕࣾज़ސ໰·ͭ΋ͱΏ͖ͻΖࢯ
  2. © 2017 VASILY,Inc. ໨࣍ ▸ ࣗݾ঺հ ▸ ੜ࢈ੑ޲্΁ͷϞνϕʔγϣϯ ▸ *20/ͱͦΕΛࢧ͑Δٕज़

    ▸ ࣾ֎΁ͷల։ ▸ ՝୊ ▸ Πϯϑϥߏஙͷੜ࢈ੑ޲্ ▸ $MPVE'PSNBUJPO0QT8PSLT ▸ ز͔ͭͷ޻෉
  3. © 2017 VASILY,Inc. Google Play ϕετΞϓϦ J04ΞϓϦ ɾ"QQ4UPSF#&45 ɾ&TTFOUJBMೝఆ "OESPJEΞϓϦ

    ɾ೥(PPHMF1MBZϕετΞϓϦ ɾ೥(PPHMF1MBZϕετΞϓϦ ɾ೥(PPHMF1MBZϕετΞϓϦ
 ɹɹϕετΠϊϕʔςΟϒΞϓϦେ৆ ɾ(PPHMF͔Βτοϓσϕϩούʔೝఆ ೥࿈ଓϕετΞϓϦड৆͸ ੈքͰ7"4*-:͚ͩ
  4. © 2017 VASILY,Inc. ՝୊ɿ؅ཧωοτϫʔΫͷ૿Ճ αʔϏε"
 ʢӡ༻தʣ αʔϏε# ʢӡ༻தʣ αʔϏε$ ʢӡ༻தʣ

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

    αʔϏε% ʢ৽ن։ൃʣ αʔϏε&
 ʢ৽ن։ൃʣ ɾɾɾ ؅ཧऀʢޫ໺ʣ ॳظߏங
 ܾ·ͬͨखॱ͕ଘࡏ ηΩϡϦςΟϧʔϧߋ৽ɺΦϓγϣϯมߋ
 มߋཧ༝͸ʁมߋཤྺ͸ʁ ܦݧ஋ͷ஝ੵɾڞ༗ʹ΋՝୊ *DPOTNBEFCZ'SFFQJLGSPNXXXqBUJDPODPNJTMJDFOTFECZ$$#:
  6. © 2017 VASILY,Inc. ՝୊ɿ؅ཧωοτϫʔΫͷ૿Ճ লྗԽɾޮ཰ԽɾจॻԽʹΑΔ
 ୯Ґ࣌ؒ͋ͨΓͷੜ࢈ੑ޲্͕ඞཁ αʔϏε"
 ʢӡ༻தʣ αʔϏε# ʢӡ༻தʣ

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

    ▸ ࣾ֎΁ͷల։ ▸ ՝୊ ▸ Πϯϑϥߏஙͷੜ࢈ੑ޲্ ▸ $MPVE'PSNBUJPO0QT8PSLT ▸ ز͔ͭͷ޻෉
  8. © 2017 VASILY,Inc. CloudFormationͱOpsWorksʹΑΔߏ੒؅ཧ &-# &$ 3%4 4 $MPVE'SPOU *".

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

    ▸ ʮύϥϝʔλʯΛ࢖ͬͯɺςϯϓϨʔτ͔ΒҟͳΔ஋Λ࣋ͬͨελοΫΛ࡞੒Մೳ ▸ ྫ͑͹ɺ71$΍4VCOFUͷ$JES#MPDLΛม͑Δɺ&$ΠϯελϯελΠϓΛม͑Δ ▸ &$ͷ44)ݤొ࿥ͳͲɺͰ͖ͳ͍͜ͱ΋एׯ͋Δ
  10. © 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ఆٛ
  11. © 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Λ ࡞੒͢ΔςϯϓϨʔτ ύϥϝʔλࢀর Ϧιʔεࢀর ૊ΈࠐΈؔ਺ͰΑΓॊೈͳॲཧ΋Մೳ ʮ͋ΔϦʔδϣϯͷͭ໨ͷ";Λऔಘʯ
  12. © 2017 VASILY,Inc. OpsWorks ▸ 0QT8PSLTελοΫͱ0QT8PSLTGPS$IFG"VUPNBUFͷछྨ͕͋Δ ▸ લऀ͸$IFGϕʔεͷΞϓϦέʔγϣϯ؅ཧαʔϏεʢฐࣾͰར༻͍ͯ͠Δͷ͸ͪ͜Βʣ ▸ ޙऀ͸Ϛωʔδυ$IFG"VUPNBUF

    ▸ ʮελοΫʯͷதʹɺෳ਺ͷʮϨΠϠʔʯΛ࡞੒ʢ$MPVE'PSNBUJPOͷελοΫͱ͸ผʣ ▸ ΠϯελϯεΛʮϨΠϠʔʯʹొ࿥ ▸ ϨΠϠʔʹର࣮ͯ͠ߦ͢ΔDPPLCPPLΛઃఆ ▸ ελοΫʷ*".Ϣʔβ୯ҐͰɺ44)ݤɾTVEPݖݶͷ؅ཧ͕Մೳ ▸ ྫ͑͹ɺ*".ϢʔβʮBMJDFʯΛελοΫʢ"1*ϨΠϠʔΛ࣋ͭʣʹ௥Ճ͠TVEPݖݶΛ෇༩
 "1*ϨΠϠʔͷΠϯελϯεʹϢʔβ͕௥Ճ͞Εɺ44)TVEP͕ར༻ՄೳʹͳΔ
  13. © 2017 VASILY,Inc. ςετͱσϓϩΠ ▸ ςϯϓϨʔτͷݕূͱ൓өΛ෼͔Γ΍͍͢Ϩʔϧʹ৐ͤΔ ▸ ςετϑΣʔζͰ$MPVE'PSNBUJPOςϯϓϨʔτΛݕূ ▸ BXTDMPVEGPSNBUJPOWBMJEBUFUFNQMBUFUFNQMBUF

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

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

    ▸ ✅ઃఆͷมߋ͕௥੻ՄೳͰ͋Δ͜ͱ ▸ (JU)VCʹΑΔςϯϓϨʔτͷมߋཤྺͱ$MPVE'PSNBUJPOଆͷมߋཤྺ ▸ ✅ઃఆͷมߋΛ൓ө͢Δखॱཱ͕֬͞Ε͍ͯΔ͜ͱ ▸ $JSDMF$*ʹΑΔςϯϓϨʔτͷݕূͱߋ৽ηοτ࡞੒ ▸ ࠷ऴతͳ൓ө͸ਓͷखΛհ͢ʢա౓ͳࣗಈԽ͸ආ͚Δʣ
  16. © 2017 VASILY,Inc. CloudFormation Stackͷ෼཭ํ਑ ▸ *OGSB4UBDLͱ4FSWJDF4UBDLʹ෼཭ ▸ *OGSB4UBDL"84ΞΧ΢ϯτ୯ҐͰͭ ▸

    *".Ϣʔβ΍$MPVE5SBJMͳͲ ▸ 4FSWJDF4UBDLαʔϏεͷ؀ڥ୯Ґʢྫ͑͹QSPEʣ͝ͱʹͭ ▸ 4όέοτ΍71$ͳͲ ▸ 4FSWJDF4UBDL͸ಉ͡ςϯϓϨʔτͷύϥϝʔλΛมߋͯ͠࡞੒͞ΕΔ ▸ ͋Δ"84ΞΧ΢ϯτͰϢχʔΫʹඞཁͳ΋ͷͱෳ਺ଘࡏ͠͏Δ΋ͷΛ෼཭͓ͯ͘͠
  17. © 2017 VASILY,Inc. OpsWorksͱAutoScaling ▸ 0QT8PSLT͸ศརͳ൒໘ɺ੍໿΋͋Δ ▸ "-#ͷαϙʔτ͕ͳ͍ɺ6TFS%BUBͷαϙʔτ͕ͳ͍ͳͲ ▸ ೚ҙͷ&$ΠϯελϯεΛޙ෇Ͱ0QT8PSLTͷ؅ཧԼʹஔ͚Δ͜ͱΛར༻

    ▸ $MPVE'PSNBUJPOʹͯ6TFS%BUB෇͖ͷ"VUP4DBMJOHΛఆٛ ▸ 6TFS%BUBͰඞཁͳॲཧ 0QT8PSLT΁ͷొ࿥ ▸ 0QT8PSLTͷొ࿥ʹඞཁͳ*%͸ɺ$MPVE'PSNBUJPOͷ૊ΈࠐΈؔ਺ʹΑΔࢀরͰऔಘͰ͖Δ
  18. © 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΁ొ࿥͢Δྫ