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

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

Nakahara
October 12, 2021

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

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

Nakahara

October 12, 2021
Tweet

More Decks by Nakahara

Other Decks in Technology

Transcript

  1. 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 ˡมߋηοτͷ࡞੒ ˡ࣮ߦ֬ೝ ˡঢ়ଶ֬ೝ ˡঢ়ଶ֬ೝ
  2. 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
  3. 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
  4. 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 Ͱग़ྗՄ
  5. 3BJOͷ࢖͍ํ wม׵ GNU   ϩʔΧϧςϯϓϨʔτΛɺ:".-ܗࣜͰग़ྗ ҰԠɺ+40/ʹ΋ม׵Ͱ͖Δ   X

    XSJUFΦϓγϣϯͰग़ྗ಺༰ΛϩʔΧϧςϯϓϨʔτʹॻ͖ࠐΈͰ͖Δ % rain fmt -w ./vpc.yml  W WFSJGZΦϓγϣϯͰϑΥʔϚοτνΣοΫՄ % rain fmt -v ./vpc.yml ./vpc.yml: formatted OK
  6. 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
  7. 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
  8. 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
  9. ݸਓత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ͰϑΥʔϚοτ͠ ͓ͯ͘
  10. ݸਓత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.. ↓ ҎԼͷܗࣜͰςϯϓϨʔτʹຒΊࠐΉ
  11. ݸਓత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͸طଘςϯϓϨʔτͱࠩ෼͕͋Δ৔߹͚ͩΞοϓϩʔυ
  12. ݸਓత3BJOͷϕετϓϥΫςΟε wϩʔΧϧ؀ڥͰQVTIલͷςϯϓϨʔτνΣοΫ  ελοΫߋ৽લʹطଘελοΫͷςϯϓϨʔτͱൺֱɻҙਤͨ͠ϓϩύςΟ ͕ࠩ෼ͱͯ͠දࣔ͞ΕΔ͜ͱΛνΣοΫ % rain cat dev-stack-Vpc-R3VTC96OOPTQ >

    dev-stack.yml % rain diff dev-stack.yml vpc.yml  ݱ࣌఺Ͱ͸ɺςϯϓϨʔτશମͰ֬ೝ͢Δํ๏͕ͳ͍ҝɺςϯϓϨʔτຖʹ νΣοΫ͢Δඞཁ͋Γ ←طଘελοΫͷςϯϓϨʔτϑΝΠϧΛϩʔΧϧʹ࡞੒
  13. ݸਓత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,\
  14. 3BJOͷྑ͍఺ɺ஫ҙ఺ w3BJOͷ஫ҙ఺  DGOMJOUͰ͸ɺSBJOಠࣗͷσΟϨΫςΟϒ͕ΤϥʔͱΈͳ͞ΕΔ ճආํ๏ͳ͠ɻɻ (JU)VC"DUJPOTʹDGOMJOUͳͲςετʹ૊ΈࠐΊͳ͍  .'"͕ඞਢ BXT.VMUJ'BDUPS"VUI1SFTFOU ͷ৔߹ɺQ

    QSP fi MFΦϓγϣ ϯΛࢦఆͨ͠SBJOίϚϯυΛ࣮ߦ͢Δͱɺ.'"$PEF͕౎౓ཁٻ͞ΕΔ BXTQ΍EJSFOWͰϩʔϧΛ੾Γସ͑ͯͷૢ࡞͕˓  ࡟আอޢɺ%SJGUॲཧ͸Ͱ͖ͳ͍ SBJO͚ͩʹͩ͜ΘΒͣɺ"84$-*ͳͲͰॲཧͰ͖ΔΑ͏ʹ͓ͯ͘͠ͱ˓