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

Perl MongersのためのAWS_CDK入門

F184d9a69a53293895f36730ca0b8289?s=47 karupanerura
February 19, 2021
48

Perl MongersのためのAWS_CDK入門

Japan.pm 2021

F184d9a69a53293895f36730ca0b8289?s=128

karupanerura

February 19, 2021
Tweet

Transcript

  1. (?:Perl MongersͷͨΊͷ)? AWS CDKೖ໳ Japan.pm 2021 id:karupanerura

  2. @karupanerura • ୅දཧࣄ @ Japan Perl Association • ΤϯδχΞ @

    DeNA, Co,. LTD. • Perl/Go/Type Script/Java/etc… • PAUSE: KARUPA
  3. ͸ͳ͢͜ͱ • AWS CDKͱͦͷ࢓૊Έ • AWS CDKͷֶͼํ • AWS CDKͷצॴ

    • ·ͱΊ
  4. AWS CDKͱͦͷ࢓૊Έ

  5. AWS CDK #ͱ͸

  6. AWS Cloud Development Kit https://aws.amazon.com/jp/cdk/

  7. AWS Cloud Development Kit • ΞϓϦέʔγϣϯΫϥ΢υϦιʔεΛఆٛ͢ΔϑϨʔϜϫʔΫ • TypeScript, JavaScript, Python,

    Java, C# Ͱ࢖͑Δ • TypeScript͔Β֤ݴޠʹτϥϯεύΠϧ͞Ε͍ͯΔҝ • AWS CloudFormationΛ࢖ͬͯϓϩϏδϣχϯά͢Δ
  8. AWS CloudFormation?

  9. AWS CloudFormation (Cfn) https://aws.amazon.com/jp/cloudformation/

  10. AWS CloudFormation (Cfn) • ܾΊΒΕͨܗࣜͷYAML(ςϯϓϨʔτ)ͰAWSϦιʔεΛఆٛ • ͦΕΛ࣮ࡍͷϦιʔεͱͯ͠ϓϩϏδϣχϯά • Ϧιʔεͷঢ়ଶΛAWS্ʹอ࣋͢Δ •

    มߋ͸ͦͷࠩ෼͚ͩΛ൓ө
  11. ͭ·Γ

  12. AWS CDK͸͜͏ಈ͘ "84$%, "84 $MPVE'PSNBUJPO "843FTPVSDFT ςϯϓϨʔτੜ੒ ϓϩϏδϣχϯά

  13. AWS CDK͸ AWS CloudFormationͷςϯϓϨʔτΛ ಈతʹੜ੒͢Δଠ࿠

  14. ςϯϓϨʔτΤϯδϯ͡ΌΜ

  15. ʢͪΐͬͱҧ͍·͢ʣ

  16. ͳʹ͕ҧ͏ͷ͔

  17. ςΩετͷϨϕϧͰ ͋Ε͜Ε΍ΔΘ͚Ͱ͸ͳ͍

  18. AWS CDKͷಛ௃ • ίʔυͰந৅తʹϦιʔε΍ͦͷґଘؔ܎ͳͲΛදݱ͢Δ • ConstructͱStackͱ͍͏֓೦Λ༻͍Δ • Construct: ࣮ࡍʹߏ੒͢ΔϦιʔε܈ͷந৅දݱ •

    Stack: ϓϩϏδϣχϯά͢Δ୯Ґͷදݱ
  19. ͭ·ΓͲ͏͍͏͜ͱͳͷ͔

  20. ࣮ࡍͷߏ੒ྫ

  21. cdk init app --language=typescript . !"" README.md !"" bin #

    %"" foo.ts !"" cdk.json !"" jest.config.js !"" lib # %"" foo-stack.ts !"" package-lock.json !"" package.json !"" test # %"" foo.test.ts %"" tsconfig.json 3 directories, 9 files
  22. . !"" README.md !"" bin # %"" foo.ts !"" cdk.json

    !"" jest.config.js !"" lib # %"" foo-stack.ts !"" package-lock.json !"" package.json !"" test # %"" foo.test.ts %"" tsconfig.json 3 directories, 9 files ࣮ߦͷى఺ Stackͷఆٛ cdk init app --language=typescript
  23. bin/${project}.ts • ࣮ߦͷى఺ͱͳΔϑΝΠϧ • StackΦϒδΣΫτΛ࡞੒͢Δ • ͜ΕʹΑͬͯStackͷIDΛCDK͕ೝࣝͰ͖ΔΑ͏ʹͳΔ • ίϯετϥΫλʹύϥϝʔλΛ౉͢͜ͱ΋Ͱ͖Δ

  24. lib/${project}-stack.ts • StackͷఆٛΛ͢Δ • ͜ͷStackͰͲͷConstructΛߏ੒͢Δͷ͔Λදݱ͢Δ • StackͷίϯετϥΫλ͔Βߏ੒͢ΔConstructͷΠϯελϯε Λੜ੒͢Δ

  25. import * as cdk from '@aws-cdk/core'; export class ${Project}Stack extends

    cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // The code that defines your stack goes here } } lib/${project}-stack.ts
  26. ConstructΛ࡞͍ͬͯͳ͍ ۭͷStackʹͳ͍ͬͯΔ

  27. ެࣜʹఏڙ͞Ε͍ͯΔConstructΛ௥Ճ

  28. import * as cdk from '@aws-cdk/core'; import * as s3

    from '@aws-cdk/aws-s3'; export class FooStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); new s3.Bucket(this, "SampleBucket", { encryption: s3.BucketEncryption.S3_MANAGED, bucketName: 'japan-pm-2021-sample-bucket', publicReadAccess: true, websiteIndexDocument: 'index.html', accessControl: s3.BucketAccessControl.PUBLIC_READ, }) } } lib/${project}-stack.ts
  29. import * as cdk from '@aws-cdk/core'; import * as s3

    from '@aws-cdk/aws-s3'; export class FooStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); new s3.Bucket(this, "SampleBucket", { encryption: s3.BucketEncryption.S3_MANAGED, bucketName: 'japan-pm-2021-sample-bucket', publicReadAccess: true, websiteIndexDocument: 'index.html', accessControl: s3.BucketAccessControl.PUBLIC_READ, }) } } lib/${project}-stack.ts FooStackͷείʔϓͰ࡞੒
  30. import * as cdk from '@aws-cdk/core'; import * as s3

    from '@aws-cdk/aws-s3'; export class FooStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); new s3.Bucket(this, "SampleBucket", { encryption: s3.BucketEncryption.S3_MANAGED, bucketName: 'japan-pm-2021-sample-bucket', publicReadAccess: true, websiteIndexDocument: 'index.html', accessControl: s3.BucketAccessControl.PUBLIC_READ, }) } } lib/${project}-stack.ts SampleBucketͬͯIDͰѻ͏
  31. import * as cdk from '@aws-cdk/core'; import * as s3

    from '@aws-cdk/aws-s3'; export class FooStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); new s3.Bucket(this, "SampleBucket", { encryption: s3.BucketEncryption.S3_MANAGED, bucketName: 'japan-pm-2021-sample-bucket', publicReadAccess: true, websiteIndexDocument: 'index.html', accessControl: s3.BucketAccessControl.PUBLIC_READ, }) } } lib/${project}-stack.ts ͲͷΑ͏ͳ΋ͷ͔ৄࡉΛࢦఆ
  32. ࣮ࡍʹ࡞ͬͯΈΔ (DEMO) http://japan-pm-2021-sample-bucket.s3-website-ap-northeast-1.amazonaws.com/

  33. ͳʹΛͨ͠ͷ͔

  34. npm run cdk -- bootstrap • CDK toolkit stackΛAWS্ʹ࡞੒͢Δ •

    cdk͕ಈ࡞͢ΔͨΊʹඞཁͳϦιʔεΛ࡞੒ • ར༻͢ΔϦʔδϣϯຖʹ࠷௿1౓͸࣮ߦ͢Δඞཁ͕͋Δ • ॳΊͯར༻͢Δ৔߹͸·࣮ͣߦ͢Δඞཁ͕͋Δ
  35. npm run cdk -- deploy FooStack • FooStackΛϓϩϏδϣχϯά͢Δ • FooStackΛCloudFormation্ʹ࡞੒͢Δ

    • FooStackͷঢ়ଶͱࠩ෼ΛऔΓɺࠩ෼ΛϓϩϏδϣχϯά͢Δ • ੒ޭͨ͠ΒFooStackͷঢ়ଶΛ࠷৽ͷঢ়ଶʹߋ৽͢Δ
  36. ͭ·Γ

  37. Constructͱ͸ͳΜͩͬͨͷ͔ • Construct͸ʮΫϥ΢υίϯϙʔωϯτʯΛදݱ͢Δ֓೦ • جຊ͸ s3.Bucket ͳͲެࣜʹఏڙ͞Ε͍ͯΔ΋ͷΛ࢖͏ • ಺෦తʹ͸ෳ਺ͷϦιʔεΛ࡞͍ͬͯΔ΋ͷ΋͋Δ •

    ந৅౓͸ConstructʹΑͬͯҟͳΔ
  38. Constructͱ͸ͳΜͩͬͨͷ͔ • ࠷΋ந৅౓͕௿͍Construct͸Cfnͷදݱͱ΄ͱΜͲಉ͡ • ͨͱ͑͹ s3.CfnBucket ͳͲ͕ଘࡏ͢Δ • શͯͷConstruct͸࠷ऴతʹCfnͷConstructΛߏ੒͢Δ

  39. Stackͱ͸ͳΜͩͬͨͷ͔ • AWS CDK (Cfn) ʹ͓͚ΔσϓϩΠͷ୯Ґ • 1ͭͷStack͸1ͭͷ୯ҐͰϓϩϏδϣχϯά͞ΕΔ • ೚ҙͷ਺ͷStackΛఆٛ͢Δ͜ͱ͕Ͱ͖Δ

    • StackؒͰConstructʹґଘؔ܎Λ΋ͨͤΔ͜ͱ΋Ͱ͖Δ
  40. AWS CDKͷֶͼํ

  41. AWS CDKͷֶशϦιʔε • ެࣜυΩϡϝϯτ • https://docs.aws.amazon.com/cdk/latest/guide/ getting_started.html • CDK Workshop

    • https://cdkworkshop.com/
  42. ެࣜυΩϡϝϯτ • ࠷΋৴པͰ͖Δ಺༰͕ࡌ͍ͬͯΔʢ͸ͣʣ • Concepts ʹ͸ͦΕͧΕͷཁૉͷ֓೦͕ࡌ͍ͬͯΔ • Stack΍ConstructͳͲͷ֓೦͸͜͜ΛಡΊ͹ཧղͰ͖Δ • ΄͔ʹ΋৭ʑͳ΋ͷ͕͋Δ

  43. CDK Workshop • CDKΛ࣮ࡍʹ࢖ͬͯΈΔνϡʔτϦΞϧ • खݩͰίϚϯυ΍ίʔυΛ࣮ࡍʹଧͬͨΓॻ͍ͨΓͯ͠ࢼ͢ • ͻͱͱ͓ΓͷࣄΛ͞ΒͬͱֶͿ͜ͱ͕Ͱ͖Δ • ࣮ࡍʹCDKΛ৽نʹ࢖ͬͯΈΔ৔໘Ͱ͸ྑ͍ڭࡐͱͳΔ

  44. ͦͷଞ

  45. AWS Construct library • AWS͕ެࣜʹఏڙ͢ΔجૅతͳConstructू • https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.html • ΄ͱΜͲͷαʔϏε͕໢ཏ͞Ε͍ͯΔ •

    جຊతʹ͸͜ΕΛ࢖ͬͯߏ੒͢Δ • ந৅౓͸΋ͷʹΑ༷ͬͯʑͳͷͰΑ͘આ໌ΛಡΉඞཁ͕͋Δ
  46. AWS Solutions Constructs • AWS͕ެࣜʹఏڙ͢ΔఆܗύλʔϯͷConstructू • https://aws.amazon.com/jp/solutions/constructs/patterns/ • Α͘࢖͏AWS Resourceͷ૊Έ߹ΘͤΛఏڙ

    • ͦΕͳΓʹந৅౓͕ߴ͍΋ͷ͕ଟ͍ • S3ͱLambdaͷηοτͳͲ
  47. cdk.out • cdk deployͷࡍʹStackͷCfn TemplateΛ͜͜ʹੜ੒͢Δ • cdk synth $Stack Ͱੜ੒͚ͩ͢Δ͜ͱ΋Ͱ͖Δ

    • ࣮ࡍͷCfn TemplateͱίʔυΛಡΈൺ΂ͯཧղΛਂΊΔ • Construct͕ͳʹΛந৅Խ͍ͯ͠Δͷ͔ཧղ͢Δͷʹ໾ཱͭ
  48. AWS CDKͷצॴ

  49. KANDOKORO • CloudFormationͷཧղ • Constructͷந৅౓ • Stackͷڥք • ςετ

  50. CloudFormation NO RIKAI • ࠷ऴతʹ͸CloudFormationʹߦ͖ண͘ • ͲΜͳτϥϒϧ΋CloudFormationͷTemplateΛಡΊ͹෼͔Δ • CloudFormationΛखܰʹѻ͏ͨΊͷCDKͱ͍͏ؾ࣋ͪͩͱָ •

    CloudFormationͰग़དྷͳ͍͜ͱ͸ग़དྷͳ͍
  51. Constructͷந৅౓ʹ஫ҙ͢Δ • AWS Construct library͸༷ʑͳந৅౓ͷConstructΛఏڙ • ΍Γ͍ͨࣄ΍ίϯτϩʔϧ͍ͨ͠ϨϕϧʹԠͯ͡࢖͍෼͚ • TIMTOWDY •

    ந৅౓ͷίϯτϩʔϧ͕CDKʹΑΔಡΈ΍͍͢IaCͷΧΪ
  52. StackͷڥքΛίϯτϩʔϧ͢Δ • Stackؒͷؔ܎ੑ͸ޙ͔Βม͑ʹ͍͘ → ઃܭ͕ඞཁ • ద੾ʹ෼͚ΔͨΊͷߟ͑ํͷྫ • ϓϩϏδϣχϯάͷΦϖϨʔγϣϯΛͲͷ୯ҐͰߦ͏͔ •

    ෳ਺ͷStackͷߏ੒ʹڞ௨ͯ͠ґଘ͢ΔϦιʔε͸Կ͔
  53. StackͷڥքΛίϯτϩʔϧ͢Δ (Tips) • StackؒͰϦιʔεͷґଘؔ܎Λ࡞Δ͜ͱ͕Ͱ͖Δ • ͨͱ͑͹ɺStackͷϓϩύςΟͷConstructΛೖΕͯڞ༗Ͱ͖Δ • ଞͷStackͷConstructʹߦͬͨมߋ͸ͦΕΛ࣋ͭStackʹ൓ө • ഁյతͳૢ࡞͕Ͱ͖ͳ͍Α͏ʹInterfaceܕͰڞ༗͢Δ΂͠

  54. ςετ • Snapshotςετ͚ͩͰ΋े෼ಋೖ͢ΔՁ஋͸͋Δ • յͣ͞ʹϦϑΝΫλϦϯάͰ͖Δ͔֬ೝͰ͖Δ • ΫϦςΟΧϧͳͱ͜Ζ͸ݸผͷςετ΋ॻ͍͓ͯ͘ͱΑΓ҆৺ • cdk.Contextͷmock͕ඞཁʹͳΔ৔߹΋͋ΔͷͰ஫ҙ •

    طଘϦιʔεͷimportΛߦ͏৔߹͸contextʹґଘ͢Δҝ
  55. ·ͱΊ

  56. ·ͱΊ • CDK͸CloudFormationΛखܰʹ࢖͑Δπʔϧ • ಠࣗͷ֓೦͕৭ʑͱొ৔͢ΔͷͰͪΌΜͱཧղͯ͠ѻ͏ͱྑ͍ • ಛʹConstruct͸ͲͷΑ͏ʹந৅Խ͞Ε͍ͯΔ͔ཧղ͕ඞཁ • ઃܭΛؒҧ͏ͱޙ໭Γ͠ʹ͍͘ͷͰಛʹStackઃܭ͸৻ॏʹ

  57. ͓·͚

  58. ϦϑΝΫλϦϯάͷ͓ڙʹ͸ PerlͷϫϯϥΠφʔΛॻ͘ͱ ศརͩͧ

  59. OWARI