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

つらみが解決! Rainを使ってCloudFormationをもっと便利に

9955f1755a5ae81bc294db7291e56af2?s=47 Nakahara
October 12, 2021

つらみが解決! Rainを使ってCloudFormationをもっと便利に

DevelopersIO 2021 Decadeという弊社オンラインイベントにて、「つらみが解決!Rainを使ってCloudFormationをもっと便利に」というテーマで話しました。資料を公開します。
動画→ https://youtu.be/AVOux4D377A

9955f1755a5ae81bc294db7291e56af2?s=128

Nakahara

October 12, 2021
Tweet

Transcript

  1.  "84ࣄۀຊ෦ίϯαϧςΟϯά෦ͳ͔͸Β ͭΒΈ͕ղܾʂ 3BJOΛ࢖ͬͯ$MPVE'PSNBUJPOΛ΋ͬͱศརʹ

  2. ηογϣϯͷ֓ཁ $MPVE'PSNBUJPOςϯϓϨʔτͷ࡞੒΍ߋ৽ɺ ελοΫͷσϓϩΠͷͭΒΈΛղফ͢Δ3BJOΛ঺հ

  3. ηογϣϯͷઆ໌ w૝ఆ͢Δௌߨऀ  $MPVE'PSNBUJPOςϯϓϨʔτΛ؅ཧɺӡ༻͍ͯ͠Δਓ  $MPVE'PSNBUJPOςϯϓϨʔτͰ*B$Λ࢝Ί͍ͨͱࢥ͍ͬͯΔਓ wΰʔϧ  3BJOΛ࢖ͬͨελοΫͷૢ࡞ख๏Λ஌Δ wࠓ೔࿩͞ͳ͍͜ͱ

     3BJOͷΠϯετʔϧํ๏  *B$πʔϧͷൺֱ  ֤ϦιʔελΠϓͷࡉ͔͍هड़ํ๏
  4. ࣗݾ঺հ ͳ͔͸Β w"84ࣄۀຊ෦ίϯαϧςΟϯά෦ w޷͖ͳαʔϏε$MPVE'PSNBUJPO

  5. "HFOEB w3BJOͷ঺հ w3BJOͷ࢖͍ํ wݸਓత3BJOͷϕετϓϥΫςΟε w3BJOͷྑ͍఺ɺ஫ҙ఺ w·ͱΊ

  6. 3BJOͷ঺հ $MPVE'PSNBUJPOΛ೔ʑӡ༻͞Ε͍ͯΔํɺ͜ΜͳͭΒΈ͋Γ·ͤΜ͔ʁ wϚωίϯͷ6*มߋɻεΫγϣΛషΓ෇͚ͨ࡞ۀखॱॻͷख௚͕͠ൃੜ w"84$-*Ͱ$MPVE'PSNBUJPOʹؔ࿈͢Δ࣮ߦίϚϯυ͕ଟ͍  ελοΫͷ࡞੒ߋ৽ͰίϚϯυ͕ҟͳΔ DSFBUFTUBDLɺVQEBUFTUBDL  ελοΫͷঢ়ଶΛ֬ೝ BXTDMPVEGPSNBUJPOXBJU

     TUBDLDSFBUFDPNQMFUFɺTUBDLVQEBUFDPNQMFUFɺDIBOHFTFUDSFBUF DPNQMFUFɺFYFDVUFDIBOHFTFUɺTUBDLEFMFUFDPNQMFUF  Φϓγϣϯ·ͰؚΊΔͱ֮͑ΒΕΜ  Ϛωίϯ͔ΒݟΕ͹͍͍͚Ͳ$-*࢖ͬͯΔͷʹϚωίϯݟΔͷ͸ݏ
  7. 3BJOͷ঺հ ͦΜͳͭΒΈΛղফͯ͘͠ΕΔπʔϧ͕3BJO

  8. 3BJOͷ঺հ 3BJOͱ͸ w"84$MPVE'PSNBUJPOͷςϯϓϨʔτ΍ελοΫΛૢ࡞͢ΔͨΊͷίϚϯυ ϥΠϯπʔϧ w"84ͷதͷਓ͕࡞ͬͨ ͋Γ͕ͱ͏ʂʂ  wར༻Ͱ͖Δ04͸ɺ-JOVYɺ.BDɺ8JOEPXTͲΕͰ΋0,

  9. 3BJOͷ঺հ 3BJOͱ͸ wˠͷςϯϓϨʔτͰελοΫΛ࡞੒ AWSTemplateFormatVersion: "2010-09-09" Description: Template generated by rain

    Resources: MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 Tags: - Key: Name Value: nkhr-vpc % rain deploy vpc.yml Creating change set ˙ · CloudFormation will make the following changes: Stack vpc: + AWS::EC2::VPC MyVPC Do you wish to continue? (Y/n) Deploying template 'vpc.yml' as stack 'vpc' in ap- northeast-1. Stack vpc: CREATE_IN_PROGRESS - 1 resource pending .· 4s Stack vpc: CREATE_COMPLETE Successfully deployed vpc ˡมߋηοτͷ࡞੒ ˡ࣮ߦ֬ೝ ˡঢ়ଶ֬ೝ ˡঢ়ଶ֬ೝ
  10. 3BJOͷ঺հ 3BJOͱ͸ wελοΫͷ࡞੒΋ߋ৽΋lSBJOEFQMPZz wEFQMPZதʹελοΫͷঢ়ଶΛ֬ೝͰ͖Δ SBJOEFQMPZ͚ͩͰ΄ͱΜͲͷͭΒΈ͕ղফ

  11. "HFOEB w3BJOͷ঺հ w3BJOͷ࢖͍ํ wݸਓత3BJOͷϕετϓϥΫςΟε w3BJOͷྑ͍఺ɺ஫ҙ఺ w·ͱΊ

  12. 3BJOͷ࢖͍ํ wೝূ JOGP   ೝূ৘ใͷࢀরઌ͸"84$-*΍4%,ͱมΘΒͳ͍  SBJOJOGPͰೝূ৘ใΛ֬ೝ % rain

    info Account: 123456789012 Region: ap-northeast-1 Identity: arn:aws:iam::123456789012:user/iamuser  Q QSP fi MFΦϓγϣϯͰଞͷೝূ৘ใΛࢀরՄ % rain info --profile profile-name Account: 123456789101 Region: ap-northeast-1 Identity: arn:aws:sts::123456789101:assumed-role/rolename/Rain-v1.2.0 Profile: profile-name  Q QSP fi MFΦϓγϣϯͰଞͷೝূ৘ใΛࢀরՄ % rain deploy ./vpc.yml —profile profile-name
  13. 3BJOͷ࢖͍ํ wࢀর MT   ελοΫͷҰཡΛදࣔɻೝূ৘ใͷσϑΥϧτϦʔδϣϯ͕දࣔ % rain ls CloudFormation

    stacks in ap-northeast-1: FutureVulsAssumeRole: CREATE_COMPLETE StackSetAdministrationRole-Stack: CREATE_COMPLETE StackSetExecutionRole-Stack: CREATE_COMPLETE  B BMMΦϓγϣϯͰ͢΂ͯͷϦʔδϣϯͷελοΫ͕දࣔ % rain ls --all CloudFormation stacks in ap-northeast-1: FutureVulsAssumeRole: CREATE_COMPLETE StackSetAdministrationRole-Stack: CREATE_COMPLETE StackSetExecutionRole-Stack: CREATE_COMPLETE CloudFormation stacks in us-east-1: dev-frontacm: CREATE_COMPLETE
  14. 3BJOͷ࢖͍ํ wࢀর DBU   ελοΫͷςϯϓϨʔτΛग़ྗ rain cat dev-vpc AWSTemplateFormatVersion:

    "2010-09-09" Description: Template generated by rain Resources: MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: 10.0.0.0/16 Tags: - Key: Name Value: nkhr-vpc  σϑΥϧτ͸ϑΥʔϚοτ͞Εͨܗࣜ U USBOTGPSNFE Ͱग़ྗɻϑΥʔ Ϛοτ͠ͳ͍ܗࣜ V VOGPSNFE Ͱग़ྗՄ
  15. 3BJOͷ࢖͍ํ wม׵ GNU   ϩʔΧϧςϯϓϨʔτΛɺ:".-ܗࣜͰग़ྗ ҰԠɺ+40/ʹ΋ม׵Ͱ͖Δ   X

    XSJUFΦϓγϣϯͰग़ྗ಺༰ΛϩʔΧϧςϯϓϨʔτʹॻ͖ࠐΈͰ͖Δ % rain fmt -w ./vpc.yml  W WFSJGZΦϓγϣϯͰϑΥʔϚοτνΣοΫՄ % rain fmt -v ./vpc.yml ./vpc.yml: formatted OK
  16. 3BJOͷ࢖͍ํ wൺֱ EJ ff   ϩʔΧϧςϯϓϨʔτΛൺֱ % rain cat

    dev-vpc > dev-vpc.yml % rain diff dev-vpc.yml vpc.yml (|) Resources: (|) MyVPC: (|) Properties: (|) Tags: (|) [0]: (>) Value: test-vpc
  17. 3BJOͷ࢖͍ํ w࡞੒ CVJME   ࢦఆͨ͠ϦιʔελΠϓ "84&$71$ ͷς ϯϓϨʔτΛग़ྗɹˠग़ྗྫ 

    σϑΥϧτ͢΂ͯͷϓϩύςΟ͕ग़ྗ  ϦιʔελΠϓ͸ෳ਺ࢦఆՄ  l$)"/(&.&zमਖ਼ɺz0QUJPOBMz͸ඞཁʹԠ͡ ͯهࡌ  C CBSFΦϓγϣϯͰ࠷௿ݶͷϓϩύςΟ͚ͩ ςϯϓϨʔτΛग़ྗ  M MJTUΦϓγϣϯΦϓγϣϯͰࢦఆͰ͖ΔϦ ιʔελΠϓ͕֬ೝͰ͖Δ % rain build AWS::EC2::VPC AWSTemplateFormatVersion: "2010-09-09" Description: Template generated by rain Resources: MyVPC: Type: AWS::EC2::VPC Properties: CidrBlock: CHANGEME EnableDnsHostnames: false # Optional EnableDnsSupport: false # Optional InstanceTenancy: CHANGEME # Optional Tags: - Key: CHANGEME Value: CHANGEME Outputs: MyVPCCidrBlock: Value: !GetAtt MyVPC.CidrBlock MyVPCCidrBlockAssociations: Value: !GetAtt MyVPC.CidrBlockAssociations MyVPCDefaultNetworkAcl: Value: !GetAtt MyVPC.DefaultNetworkAcl MyVPCDefaultSecurityGroup: Value: !GetAtt MyVPC.DefaultSecurityGroup MyVPCIpv6CidrBlocks: Value: !GetAtt MyVPC.Ipv6CidrBlocks
  18. 3BJOͷ࢖͍ํ wελοΫ࡞੒ɺߋ৽ EFQMPZ   ελοΫͷ࡞੒ɺߋ৽ͲͪΒ΋ಉ͡ίϚϯυͰ࣮ߦɻมߋηοτ࡞੒ˠε λοΫΛσϓϩΠ % rain deploy

    template-filename stack-name --params env=dev,sysName=sample,billingTag=test,vpcCidr=10.0.0.0/16,domainName=example.com,hostedZoneId =ZZZZZZZZZZZZZZ,instanceType=t3.micro,instanceClass=db.t3.small,masterPassword=P@ssw0rd  Z ZFTΦϓγϣϯͰର࿩ͳ͠ͰσϓϩΠ  E EFUBDIΦϓγϣϯͰσϓϩΠ։࢝ޙɺॲཧ݁ՌΛ଴ͨͣʹίϚϯυऴ ྃɻSBJOMPHT΍SBJOXBUDIίϚϯυͰελοΫͷঢ়ଶΛ֬ೝ wελοΫ࡟আ SN   ελοΫͷ࡟আɻΦϓγϣϯ͸SBJOEFQMPZͱಉ౳ % rain rm -y stack-name
  19. "HFOEB w3BJOͷ঺հ w3BJOͷ࢖͍ํ wݸਓత3BJOͷϕετϓϥΫςΟε w3BJOͷྑ͍఺ɺ஫ҙ఺ w·ͱΊ

  20. ݸਓత3BJOͷϕετϓϥΫςΟε ݸਓతͳ3BJOͷϕετϓϥΫςΟεΛ঺հ ˞ςϯϓϨʔτͳͲͷιʔείʔυ͸ผ్ϒϩάͰల։

  21. ݸਓత3BJOͷϕετϓϥΫςΟε

  22. ݸਓత3BJOͷϕετϓϥΫςΟε wςϯϓϨʔτͷ࡞੒͸ɺSBJOCVJMEίϚϯυͰ࡞੒  SBJOCVJMEίϚϯυ͸ɺϦιʔελΠϓΛෳ਺ࢦఆͰ͖Δ  ωοτϫʔΫؔ࿈ͷςϯϓϨʔτΛ࡞੒͢Δ৔߹͸ҎԼͷίϚϯυ ྫ  % rain

    build "AWS::EC2::InternetGateway" "AWS::EC2::VPCGatewayAttachment" "AWS::EC2::VPC" "AWS::EC2::Subnet" "AWS::EC2::Subnet" "AWS::EC2::Subnet" “AWS::EC2::Subnet” > vpc.yml  ಉ͡ϦιʔελΠϓ໊΋ࢦఆͰ͖Δɻ࿦ཧ*%͕ॏෳ͠ͳ͍Α͏ग़ྗ .Z4VCOFUɺ.Z4VCOFUɺ.Z4VCOFUɺ.Z4VCOFUʜ  wςϯϓϨʔτ͸GNU͢Δ  SBJOEFQMPZίϚϯυͰϑΥʔϚοτ͞ΕΔͷͰɺSBJOGNUͰϑΥʔϚοτ͠ ͓ͯ͘
  23. ݸਓత3BJOͷϕετϓϥΫςΟε wςϯϓϨʔτ͸෼ׂͯ͠ωετͨ͠ςϯϓϨʔτΛ࡞੒  ͭͷςϯϓϨʔτʹू໿͢Δͱ๲େͳߦ਺ͰՄಡੑ͕ѱ͘ͳΔҝɺςϯϓ Ϩʔτ͸෼ׂ͢Δͷ͕Φεεϝ system.yml template/ └ vpc.yml └

    securitygroup.yml └ iam.yml └ acm.yml └ ec2.yml └ rds.yml └ alb.yml └ route53.yml ←ωετͨ͠ςϯϓϨʔτ
  24. ݸਓత3BJOͷϕετϓϥΫςΟε wςϯϓϨʔτ͸෼ׂͯ͠ωετͨ͠ςϯϓϨʔτΛ࡞੒  3BJOಠࣗͷσΟϨΫςΟϒ 3BJO4)UUQ Λ࢖ͬͯSBJOEFQMPZ͢Δͱ4ό έοτʹςϯϓϨʔτΛΞοϓϩʔυ͠ɺ463-ΛຒΊࠐΉ͜ͱ͕Ͱ͖Δ Resources: ### VPC

    Vpc: Type: AWS::CloudFormation::Stack Properties: Parameters: env: !Ref env sysName: !Ref sysName billingTag: !Ref billingTag vpcCidr: !Ref vpcCidr Tags: - Key: Name Value: !Sub ${env}-${sysName}-vpc-stack - Key: BillingGroup Value: !Ref billingTag TemplateURL: !Rain::S3Http ./template/vpc.yml TemplateURL: https://rain-artifacts-ACCOUNTID-REGION.s3.REGION.amazonaws.com/ 28bcbc2d2191b0c77a.. ↓ ҎԼͷܗࣜͰςϯϓϨʔτʹຒΊࠐΉ
  25. ݸਓత3BJOͷϕετϓϥΫςΟε wςϯϓϨʔτ͸෼ׂͯ͠ωετͨ͠ςϯϓϨʔτΛ࡞੒  EFQMPZલʹςϯϓϨʔτΛΞοϓϩʔυ͠ɺ463-͕ຒΊࠐ·Εͨςϯϓ ϨʔτΛ༻ҙ͢Δ৔߹͸ɺSBJOQLHΛ࣮ߦ͢Δ % rain pkg system.yml ###

    VPC Vpc: Type: AWS::CloudFormation::Stack . . TemplateURL: https://rain-artifacts-ACCOUNTID-REGION.s3.REGION.amazonaws.com/ 28bcbc2d2191b0c77a..  ϦμΠϨΫτͰωετͨ͠ςϯϓϨʔτΛߋ৽͢Δ  3BJO4)UUQ͸طଘςϯϓϨʔτͱࠩ෼͕͋Δ৔߹͚ͩΞοϓϩʔυ
  26. ݸਓత3BJOͷϕετϓϥΫςΟε wϩʔΧϧ؀ڥͰQVTIલͷςϯϓϨʔτνΣοΫ  ελοΫߋ৽લʹطଘελοΫͷςϯϓϨʔτͱൺֱɻҙਤͨ͠ϓϩύςΟ ͕ࠩ෼ͱͯ͠දࣔ͞ΕΔ͜ͱΛνΣοΫ % rain cat dev-stack-Vpc-R3VTC96OOPTQ >

    dev-stack.yml % rain diff dev-stack.yml vpc.yml  ݱ࣌఺Ͱ͸ɺςϯϓϨʔτશମͰ֬ೝ͢Δํ๏͕ͳ͍ҝɺςϯϓϨʔτຖʹ νΣοΫ͢Δඞཁ͋Γ ←طଘελοΫͷςϯϓϨʔτϑΝΠϧΛϩʔΧϧʹ࡞੒
  27. ݸਓత3BJOͷϕετϓϥΫςΟε w(JU)VC"DUJPOTͰEFQMPZ  ؀ڥຖͷσϓϩΠ͸(JU)VC࿈ܞ͕ศར  ೥݄ɺӬଓత$SFEFOUJBMT͕ෆཁͰ*".3PMF͕ར༻Մೳɻ ʮ(JU)VC"DUJPOTͰ"84ͷӬଓతͳΫϨσϯγϟϧΛ౉͢͜ͱͳ͘*".3PMF͕ར༻Ͱ͖ΔΑ͏ʹͳͬͨΑ͏Ͱ͢ʯ IUUQTEFWDMBTTNFUIPEKQBSUJDMFTHJUIVCBDUJPOTXJUIPVUQFSNBOFOUDSFEFOUJBM ˞"84͔ΒެࣜΞφ΢ϯεແ͠ 

     ೝূ৘ใ 3%4ͷύεϫʔυͳͲ ͸ɺ(JU)VCͷ4FDSFUTʹ֨ೲ  ϒϥϯνຖʹ8PSL fl PXΛ࡞੒ɻSBJOEFQMPZ࣮ߦ - name: Template deploy. run: | rain deploy -y ./system.yml $ENV-stack --params \ env=$ENV,\ sysName=$SYSNAME,\ billingTag=$BILLINGTAG,\ vpcCidr=$CIDR,\ domainName=$DOMAIN_NAME,\ hostedZoneId=$HOSTED_ZONE_ID,\ instanceType=$INSTANCE_TYPE,\ instanceClass=$INSTANCE_CLASS,\ masterPassword=$MASTERPASSWORD,\
  28. "HFOEB w3BJOͷ঺հ w3BJOͷ࢖͍ํ wݸਓత3BJOͷϕετϓϥΫςΟε w3BJOͷྑ͍఺ɺ஫ҙ఺ w·ͱΊ

  29. 3BJOͷྑ͍఺ɺ஫ҙ఺ w3BJOͷ͍͍ͱ͜Ζ  طଘͷςϯϓϨʔτɺελοΫ͕׆༻Ͱ͖Δ ελοΫͷૢ࡞ΛSBJOʹ͢Δ͚ͩ  αϒίϚϯυ͕Θ͔Γ΍͍͢ DBUɺMTɺSNɺEFQMPZMPHTͳͲ4IFMMʹ׳Ε͍ͯΔਓʹ͸ೃછΈ΍͍͢ Φϓγϣϯ΋গͳ͍ͷͰ։ൃ୲౰ɺӡ༻୲౰ͷڭҭίετ͕গͳ͘͢Ή 

    ༷ʑͳ$*$%πʔϧʹ࣮૷͠΍͍͢ "84$PEFγϦʔζ͚ͩͰͳ͘(JU)VC"DUJPOT΍$JSDMF$*ʹ΋࣮૷͕༰қ
  30. 3BJOͷྑ͍఺ɺ஫ҙ఺ w3BJOͷ஫ҙ఺  DGOMJOUͰ͸ɺSBJOಠࣗͷσΟϨΫςΟϒ͕ΤϥʔͱΈͳ͞ΕΔ ճආํ๏ͳ͠ɻɻ (JU)VC"DUJPOTʹDGOMJOUͳͲςετʹ૊ΈࠐΊͳ͍  .'"͕ඞਢ BXT.VMUJ'BDUPS"VUI1SFTFOU ͷ৔߹ɺQ

    QSP fi MFΦϓγϣ ϯΛࢦఆͨ͠SBJOίϚϯυΛ࣮ߦ͢Δͱɺ.'"$PEF͕౎౓ཁٻ͞ΕΔ BXTQ΍EJSFOWͰϩʔϧΛ੾Γସ͑ͯͷૢ࡞͕˓  ࡟আอޢɺ%SJGUॲཧ͸Ͱ͖ͳ͍ SBJO͚ͩʹͩ͜ΘΒͣɺ"84$-*ͳͲͰॲཧͰ͖ΔΑ͏ʹ͓ͯ͘͠ͱ˓
  31. "HFOEB w3BJOͷ঺հ w3BJOͷ࢖͍ํ wݸਓత3BJOͷϕετϓϥΫςΟε w3BJOͷྑ͍఺ɺ஫ҙ఺ w·ͱΊ

  32. ·ͱΊ ଞͷ*B$πʔϧʹ৐Γ׵͑ΔલʹSBJOΛ࢖ͬͯΈͯ खಈͷσϓϩΠɺ$*$%ͲͪΒ΋ศརʹελοΫૢ࡞͕Ͱ͖Δ Ϛωδϝϯτίϯιʔϧ͔Βͷ୤٫Λૂ͑Δ ·ͩ·ͩ$'OςϯϓϨʔτ΋ؤுΕΔʂ ͸ͣ

  33. None