$30 off During Our Annual Pro Sale. View Details »

それでも俺はAWS CDKが作るリソースに物理名を付けたい 〜CDKのベストプラクティスは本当にベストなのか〜 / AWS CDK resource name can be a physical name

tomoki10
April 09, 2022

それでも俺はAWS CDKが作るリソースに物理名を付けたい 〜CDKのベストプラクティスは本当にベストなのか〜 / AWS CDK resource name can be a physical name

AWS CDK Conference Japan で発表した内容です。
https://jawsug.connpass.com/event/240422/

tomoki10

April 09, 2022
Tweet

More Decks by tomoki10

Other Decks in Technology

Transcript

  1. AWS CDK Conference Japan
 ͦΕͰ΋Զ͸AWS CDK͕࡞ΔϦιʔεʹ෺ཧ໊Λ෇͚͍ͨ ʙCDKͷϕετϓϥΫςΟε͸ຊ౰ʹϕετͳͷ͔ʙ CXࣄۀຊ෦ IoTࣄۀ෦ 2022/04/09

    ࠤ౻ஐथ 1
  2. ࣗݾ঺հ @tmk2154 @tomoki10 • ࠤ౻ஐथ • Ϋϥεϝιουגࣜձࣾ • CXࣄۀຊ෦ IoTࣄۀ෦

    • ݱࡏɿαʔόʔαΠυ݉ΠϯϑϥΤϯδχΞ ݉QAͱ͔AWSΞΧ΢ϯτͷηΩϡϦςΟ౳ • झຯɿAPEXɺࢄา • ޷͖ͳAWSαʔϏεɿLambdaɺCDK
  3. AWS CDKͷϕετϓϥΫςΟεͰ͸Ϧιʔεͷࣗಈ໊෇͚͕ਪ঑͞Εͯ·͢
 ͚Ͳɺࣗಈ໊෇͚ͷϦιʔε໊ਏ͘ͳ͍Ͱ͔͢ʁʁ 3 ໰୊ఏى Fargate Cluster/ServiceͱConstructͷidʹ؀ڥ໊+ݻఆ໊+Ϧιʔε໊Λઃఆͨ͠৔߹ͷදࣔྫ

  4. 4 ઌਓͷݴ༿ https://www.amazon.co.jp/ιϑτ΢ΣΞΞʔΩςΫνϟͷجૅ-—ΤϯδχΞϦϯάʹجͮ͘ମܥతΞϓϩʔν-Mark-Richards/dp/4873119820/ ʮΞʔΩςΫτʹ͸աڈͷ࣌୅͔Β࢒͞Ε͍ͯΔ લఏ΍ެཧΛٙ͏ͱ͍͏ॏཁͳ੹೚͕͋Δʯ
 P.viiΑΓ

  5. 5 ໨࣍ • AWS CDKϕετϓϥΫςΟεͱ͸ • ࠓճऔΓ্͛Δ߲໨ • Ϧιʔεͷࣗಈ໊෇͚ͷ֓ཁ •

    ໰୊ͱߟ͑Δཧ༝ • ղܾࡦ
  6. 6 AWS CDKϕετϓϥΫςΟεͱ͸ AWS CDKͷϕετϓϥΫςΟεʹ͍ͭͯॻ͔ΕͨAWSϒϩά https://aws.amazon.com/jp/blogs/news/best-practices-for-developing-cloud-applications-with-aws-cdk/ ओͳ಺༰ • CDKͷ఩ֶ •

    ૊৫ͰͷऔΓ૊Ήࡍͷ৺ಘ • ίʔυ؅ཧํ๏ • ConstructϥΠϒϥϦͷϕετϓϥΫςΟε • AWS CDK ΞϓϦέʔγϣϯͷ
 ϕετϓϥΫςΟε
  7. 7 AWS CDKϕετϓϥΫςΟεͱ͸ AWS CDKͷϕετϓϥΫςΟεʹ͍ͭͯॻ͔ΕͨAWSϒϩά https://aws.amazon.com/jp/blogs/news/best-practices-for-developing-cloud-applications-with-aws-cdk/ ओͳ಺༰ • CDKͷ఩ֶ •

    ૊৫ͰͷऔΓ૊Ήࡍͷ৺ಘ • ίʔυ؅ཧํ๏ • ConstructϥΠϒϥϦͷϕετϓϥΫςΟε • AWS CDK ΞϓϦέʔγϣϯͷ
 ϕετϓϥΫςΟε
  8. 8 ϕετϓϥΫςΟε͸·ͩૣ͍ͱײͨ͡Β ॳ~தڃऀ޲͚ͷ࿩ͰҎલొஃͨ͠ͷͰͪ͜Β΋ݟ͍ͯͩ͘͞(એ఻) https://dev.classmethod.jp/articles/aws-devday-online-japan-know-how-from-initial-development-to-operation-on-how-to-use-aws-cdk/ ओͳ಺༰ • CDKͷ಺෦ߏ଄ͷུ֓ • ։ൃ࣌Α͘೰Ή෦෼ͷิ଍ •

    ։ൃ࣌ͷϊ΢ϋ΢ • ύΠϓϥΠϯߏங࣌ͷબ୒ࢶ • ӡ༻࣌ͷϊ΢ϋ΢
  9. 9 AWS CDK ΞϓϦέʔγϣϯͷϕετϓϥΫςΟε ॻ͔Ε͍߲ͯͨ໨(Օ৚ॻ͖ɿ߲໨໊ɺվߦԼͷίϝϯτɿࣗ෼ͷղऍ) • σϓϩΠ࣌Ͱ͸ͳ͘ɺ߹੒࣌ʹܾఆ͢Δ
 CfnͷύϥϝʔλͰͳ͘CDKͷςϯϓϨʔτੜ੒࣌ʹ஋ΛຒΊࠐ΋͏ • ࣗಈͰੜ੒͞ΕΔϦιʔε໊Λ࢖༻͠ɺ෺ཧతͳ໊લΛ࢖༻͠ͳ͍

    • σϓϩΠཁ݅ʹԠͯ͡ɺΞϓϦέʔγϣϯͷStageΛෳ਺ͷStackʹ෼ׂ͢Δ
 ׬ᘳͳਖ਼ղ͸ͳ͍͕ελοΫ͸ঢ়گʹԠͯ͡෼ׂ • cdk.context.jsonΛίϛοτͯ͠ɺ֎෦తͳཁҼͰ߹੒݁Ռ͕มΘͬͯ͠·͏͜ͱΛආ͚Δ
 ҙਤͤͣϦιʔεͷߋ৽͕ൃੜ͠ͳ͍Α͏ʹ͢ΔͨΊͷ஌ܙ • AWS CDKͰϩʔϧͱηΩϡϦςΟάϧʔϓΛ؅ཧͰ͖ΔΑ͏ʹ͢Δ
 CDKͰҰ෦ఏڙ͞Ε͍ͯΔϩʔϧ΍ηΩϡϦςΟάϧʔϓੜ੒ػೳΛ࢖͏ͱ࠷খݖݶͰ࣮૷͠΍͍͢ • શͯͷStageΛίʔυͰϞσϧԽ͢Δ
 ؀ڥ͝ͱʹStackϑΝΠϧΛ࡞Βͣɺ؀ڥ͝ͱͷ஋͸ύϥϝʔλͰ੾Γସ͑Δͱ؀ڥࠩҟ͕࠷খʹͳΔ • શͯΛଌఆ͢Δ
 ϝτϦΫε΍μογϡϘʔυͳͲΛ࡞ΕΔϝιουΛ׆༻͠Α͏
  10. 10 AWS CDK ΞϓϦέʔγϣϯͷϕετϓϥΫςΟε ॻ͔Ε͍߲ͯͨ໨(Օ৚ॻ͖ɿ߲໨໊ɺվߦԼͷίϝϯτɿࣗ෼ͷղऍ) • σϓϩΠ࣌Ͱ͸ͳ͘ɺ߹੒࣌ʹܾఆ͢Δ
 CfnͷύϥϝʔλͰͳ͘CDKͷςϯϓϨʔτੜ੒࣌ʹ஋ΛຒΊࠐ΋͏ • ࣗಈͰੜ੒͞ΕΔϦιʔε໊Λ࢖༻͠ɺ෺ཧతͳ໊લΛ࢖༻͠ͳ͍ɹɹˡ

    ʂʂʂʂʂʂʂʂʂʂʂʂ • σϓϩΠཁ݅ʹԠͯ͡ɺΞϓϦέʔγϣϯͷStageΛෳ਺ͷStackʹ෼ׂ͢Δ
 ׬ᘳͳਖ਼ղ͸ͳ͍͕ελοΫ͸ঢ়گʹԠͯ͡෼ׂ • cdk.context.jsonΛίϛοτͯ͠ɺ֎෦తͳཁҼͰ߹੒݁Ռ͕มΘͬͯ͠·͏͜ͱΛආ͚Δ
 ҙਤͤͣϦιʔεͷߋ৽͕ൃੜ͠ͳ͍Α͏ʹ͢ΔͨΊͷ஌ܙ • AWS CDKͰϩʔϧͱηΩϡϦςΟάϧʔϓΛ؅ཧͰ͖ΔΑ͏ʹ͢Δ
 CDKͰҰ෦ఏڙ͞Ε͍ͯΔϩʔϧ΍ηΩϡϦςΟάϧʔϓੜ੒ػೳΛ࢖͏ͱ࠷খݖݶͰ࣮૷͠΍͍͢ • શͯͷStageΛίʔυͰϞσϧԽ͢Δ
 ؀ڥ͝ͱʹStackϑΝΠϧΛ࡞Βͣɺ؀ڥ͝ͱͷ஋͸ύϥϝʔλͰ੾Γସ͑Α͏ • શͯΛଌఆ͢Δ
 ϝτϦΫε΍μογϡϘʔυͳͲΛ࡞ΕΔϝιουΛ׆༻͠Α͏
  11. 11 ϕετϓϥΫςΟεͷৄࡉ ࣗಈͰੜ੒͞ΕΔϦιʔε໊Λ࢖༻͠ɺ෺ཧతͳ໊લΛ࢖༻͠ͳ͍

  12. ෺ཧ໊Λ࢖͏৔߹ͷσϝϦοτ • ΠϯϑϥͷҰ෦Λෳ਺σϓϩΠ͢Δ͜ͱ͕Ͱ͖ͳ͍ • Ϧιʔεʹഁյతมߋ͕൐͏৔߹ɺ࠶࡞੒ʹࣦഊ͢Δ 12 ϕετϓϥΫςΟεͷৄࡉ Amazon DynamoDB AWS

    CDK Table A Hoge Table A Fuga Amazon DynamoDB AWS CDK Table A ෺ཧ໊Λݻఆ͠ͳ͍৔߹ ෺ཧ໊Λݻఆ͢Δ৔߹ 1ճ໨ 2ճ໨ ςʔϒϧ ࡞੒ ςʔϒϧ ࡞੒ 1ճ໨ 2ճ໨
  13. ܦݧ͔Β෺ཧ໊Λ࢖͏৔߹ͷσϝϦοτ • ࡟আ࣌ͷϙϦγʔΛอޢ(RemovalPolicy.RETAIN)Ͱઃఆͨ͠/͞ΕͯΔ৔߹
 ελοΫ࡟আޙɺ࠶σϓϩΠ࣌ʹલճ࣮ߦͨ͠Ϧιʔε͕ॏෳࣦ͠ഊ͢Δ
 ҙਤͤͣอޢઃఆʹͳ͍ͬͯΔϦιʔε͕͋ΔͱσϓϩΠ͢Δ·Ͱؾ͚ͮͳ͍
 13 ෺ཧ໊ͷσϝϦοτ AWS CDK AWS

    CDK ෺ཧ໊Λݻఆ͠ͳ͍৔߹ ෺ཧ໊Λݻఆ͢Δ৔߹ 1ճ໨ 2ճ໨ Lambda ࡞੒࣌ Lambda ࡞੒࣌ 1ճ໨ 2ճ໨ Amazon CloudWatch Amazon CloudWatch Logs Hoge Logs Fuga Logs
  14. 14 ຊ౰ʹͦ͏͔ࠓҰ౓ٙͬͯΈΔ

  15. ෺ཧ໊Λ࢖͏৔߹ͷσϝϦοτ • ΠϯϑϥͷҰ෦Λෳ਺σϓϩΠ͢Δ͜ͱ͸Ͱ͖ͳ͍
 →ෳ਺ਓ։ൃ࣌ͷ૝ఆ?Ϧιʔε໋໊نଇ͸CDKͷίʔυͱCfnͷ࢓༷Λಡ·ͳ͍ͱ෼͔Βͣ
 ɹࣅͨΑ͏ͳ໊લͷϦιʔε͕ෳ਺Ͱ͖Δͱؔ࿈͕෼͔ΒͣٯʹෆศͰ͸ʁ
 →Ϧιʔε໊ͷিಥ͸໋໊نଇ(ex.Ϧιʔεʹݸਓ͝ͱͷ؀ڥ໊Λ෇͚Δ)Ͱճආ͸Մೳ
 →ͦ΋ͦ΋Stack໊͕ҟͳͬͯ΋Constructͷid͕ಉͩ͡ͱॏෳ͢ΔϦιʔε͕Ұ෦ଘࡏ͢Δ
 • Ϧιʔεʹഁյతมߋ͕൐͏৔߹ɺ࠶࡞੒ʹࣦഊ͢Δ
 →ྫʹ͋ΔDynamoDBͷKeySchemaͷมߋ͸ςʔϒϧ͕Ұ౓ഁغ͞ΕΔ


    ɹϢʔβଆ͸ͦͷةݥੑʹؾ͚ͮΔΑ͏ʹ࡞੒ʹࣦഊͨ͠ํ͕ྑ͍ͷͰ͸ʁ 15 ϕετϓϥΫςΟεͷৄࡉ
  16. ࢀর੾Εϩά ࢀর੾Εϩά • ࠶σϓϩΠ࣌ʹલճ࣮ߦͨ͠Ϧιʔε໊͕ॏෳ͠ͳ͍
 →ҎલͷϦιʔε͸࢒ΔͷͰ͜·Ίʹ࡟আ͠ͳ͍ͱେྔʹϦιʔε͕࢒Γ
 ɹͲΕ͕ݟ͍ͨ΋ͷ͔ɺ࡟আͯ͠ྑ͍ͷ͔͕෼͔ΓͮΒ͘ͳΔ 16 ϕετϓϥΫςΟεͷৄࡉ AWS CDK

    1ճ໨ Logs AWS CDK 2ճ໨࠶σϓϩΠ Logs Logs AWS CDK nճ໨࠶σϓϩΠ Logs Logs Logs Logs Logs Logs Logs Logs ;͏ɺ៉ྷʹͳͬͨͧʂ ͲͷϩάΛ ݟΕ͹ྑ͍ͷʁ ։ൃऀ
  17. 17 ผελοΫͰಉ͡ϦιʔεΛ࡞Δ஌ܙ https://speakerdeck.com/tomoki10/know-how-from-initial-development-to-operation-on-how-to-use-aws-cdk?slide=11

  18. 18 ผελοΫͰಉ͡ϦιʔεΛ࡞Δ஌ܙ ը૾Ҿ༻ݩɿ https://aws.amazon.com/jp/blogs/aws/boost-your-infrastructure-with-cdk/ new dynamodb.Table(this, `${id}-items`, { tableName: `${id}-items`,

    partitionKey: { name: "itemId", type: dynamodb.AttributeType.STRING, }, }); const app = new cdk.App(); new UserAStack(app, `${envName}-${projectName}-UserAStack`, {} ); ໋໊نଇ͕ϦιʔεͰҟͳΓҰ෦(Aurora/WAFͳͲ)͸ ελοΫ໊͕ҟͳͬͯ΋Ϧιʔε໊͕ಉ͜͡ͱ΋͋Δ ͷͰ id Λ͚ͭΔ͜ͱͰॏෳΛճආͰ͖Δ envNameͱprojectName͸cdk.json͔Β஫ೖ͢Δ envName͸ݸਓ͝ͱ projectName͸PJΛࣝผ͢ΔͨΊઃఆ
  19. ௐࠪ/ӡ༻ͰԿ౓΋֬ೝ͢Δ৔߹ɺର৅ͷϦιʔε͕෼͔Γʹͯ͘͘ਏ͍ 19 ܦݧ͔Βࣗಈ໊෇͚ͷ৔߹ͷਏ͍෦෼ Fargate Cluster/ServiceͱConstructͷidʹ؀ڥ໊+ݻఆ໊+Ϧιʔε໊Λઃఆͨ͠৔߹ͷදࣔྫ

  20. • ೝ஌ෛՙͷதͷ಺ࡏੑೝ஌(ࠓճ͸ௐࠪʹඞཁͳهԱ)ෛՙ͕ߴ͘ͳΔ
 →։ൃ΍ௐࠪͳͲຊདྷ࣮ࢪ͍ͨ͠࡞ۀ༻ͷϫʔΩϯάϝϞϦ͕গͳ͘ͳΔ
 →ർฐ͠ੜ࢈ੑ௿Լ(ݸਓͷײ૝Ͱ͢) • ҰͭϦιʔεΛࣗಈͰ͚ͭΔͱCloudWatch LogsͷϩάάϧʔϓͳͲ
 ؔ࿈͢ΔϦιʔε΋ࣗಈͰ໊෇͚͞ΕΔ
 20 ܦݧ͔Βࣗಈ໊෇͚ͷ৔߹ͷਏ͍෦෼

    ࢖͍͍ͨ ϦιʔεA ϦιʔεAͷ ϋογϡ໊ ϦιʔεA͕ ؔ࿈ͯ͠ੜ੒ ͨ͠ϦιʔεB ϦιʔεBͷ ϋογϡ໊
  21. 21 ϕετϓϥΫςΟεΛ͋ΒͨΊͯݟฦ͢

  22. 22 ϕετϓϥΫςΟεΛ͋ΒͨΊͯݟฦ͢

  23. 23 Ͱ͖·ͤΜͰͨ͠ʂ

  24. 24 ݁ہϦιʔε໊͸Ͳ͏͢΂͖͔ ࣗ෼ͳΓͷ݁࿦ • ։ൃ/ௐࠪ/ӡ༻ͳͲͰΑ֬͘ೝ͢ΔϦιʔε͸໊લΛݻఆ͢Δ
 αʔϏεͷྫ • ECS΍LambdaͳͲͷίϯϐϡʔςΟϯάαʔϏε • DynamoDB΍S3ͳͲͷετϨʔδ

    • μογϡϘʔυ໊΍Ξϥʔτ໊ • ্هҎ֎ͷαʔϏε͸Ͱ͖Δ͚ͩݻఆ͠ͳ͍
  25. 25 ·ͱΊ • CDKͷϕετϓϥΫςΟε͕ຊ౰͔վΊͯߟ͑ͯΈͨ • ެཧΛٙ͏͜ͱ͸ΞʔΩςΫτͷ໾໨ • ࣗಈͰੜ੒͞ΕΔϦιʔε໊Λ࢖༻͠ɺ෺ཧతͳ໊લΛ࢖༻͠ͳ͍ • ϕετϓϥΫςΟεͰड़΂ΒΕ͍ͯΔར఺ΛվΊͯ֬ೝ

    • ϕετͰͳ͔ͬͨܦݧͷ঺հ • ζϘϥͳਓؒ(ࢲ)ͷ৔߹ɺෆཁϦιʔε͕େྔʹ࢒Δ • ࣗಈϦιʔε໊Ͱೝ஌ෛՙ͕ߴ͘ͳΔ • Ϧιʔε໊ͷࣗಈੜ੒͸΄Ͳ΄Ͳʹɻਏ͍࣌͸ݻఆԽ͠Α͏
  26. 26 additional JAWS CDK Λ্ཱ͍͖͍ͪ͛ͯͨʂ΋ͬͱ஌ݟΛڞ༗͍ͨ͠ʂ
 CDKͷ஌ݟΛൃද͍ͨ͠ํ΍CDKΛ੝Γ্͍͛ͨํืूʂʂ CDK @tmk2154 #jawsug-cdk νϟϯωϧ

    https://jaws-ug.jp/jaws-ug-slack/