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

元・組込みエンジニアが教えるAWSを使ったIoTシステムのバックエンド構築手順(第2部) - SWEST 22 セッションs2c

Y_uuu
August 21, 2020

元・組込みエンジニアが教えるAWSを使ったIoTシステムのバックエンド構築手順(第2部) - SWEST 22 セッションs2c

SWEST2日目 セッションS2Cにて発表。

AWS(Amazon Web Services)を使ってIoTシステムのバックエンドを構築するハンズオンです。

https://swest.toppers.jp/SWEST22/program/s2c.html#s2

※ 第1部、第3部は近日中に公開予定です。
※ 発表時点でP.49とP.50が重複していたためP.50を削除しています

Y_uuu

August 21, 2020
Tweet

More Decks by Y_uuu

Other Decks in Programming

Transcript

  1. ݩɾ૊ࠐΈΤϯδχΞ͕ڭ͑ΔAWSΛ࢖ͬͨ
    IoTγεςϜͷόοΫΤϯυߏஙखॱ - IoTγεςϜ։ൃϋϯζΦϯ -
    2020-08-21(Fri) 10:00-11:10
    SWEST 22 ηογϣϯS2
    ઌਐٕज़෦໳ IoTνʔϜ ΤϯδχΞ
    Ԭቌ ༤ฏ
    1

    View full-size slide

  2. ຊηογϣϯͰ͓࿩͢Δ͜ͱ
    AWS͕͸͡Ίͯͷํ޲͚ʹɺAWSͷಛ௃΍ಋೖͷϝϦοτɺྉۚମܥͳͲΛ͝આ໌͠·͢
    ୈ2෦: IoTγεςϜ։ൃϋϯζΦϯ
    AWSΛ࢖ͬͯIoTγεςϜͷόοΫΤϯυΛ։ൃ͢ΔखॱΛϋϯζΦϯܗࣜͰઆ໌͠·͢ɻ
    ࣮ࡍʹγεςϜΛߏங͢Δ͜ͱͰɺΫϥ΢υ্ͷγεςϜΛ؆୯ʹߏஙͰ͖Δ͜ͱ͕͓෼͔Γ͍͚ͨͩΔ
    ͸ͣͰ͢ɻ
    ୈ3෦: IoT։ൃʹ͓͚Δ૊ࠐΈΤϯδχΞͷՁ஋
    ૊ࠐΈΤϯδχΞͷ࣋ͭͲͷΑ͏ͳεΩϧ͕IoT։ൃͰՁ஋Λൃش͢Δ͔ɺWebΤϯδχΞͷࢹ఺͔Β͝
    ঺հ͠·͢ɻ
    2
    ୈ1෦: ͸͡ΊͯͷAWS
    ͜Ε͔Βઆ໌

    View full-size slide

  3. ʮୈ2෦: IoTγεςϜ։ൃϋϯζΦϯʯͰߏங͢ΔγεςϜ
    3
    IoTࣄྫͰΑ͘ݟ͔͚Δɺ෦԰ͷԹ౓Λ؂ࢹɾՄࢹԽ͢ΔγεςϜΛ։ൃ͠·͢ɻ

    View full-size slide

  4. ʮୈ2෦: IoTγεςϜ։ൃϋϯζΦϯʯͰߏங͢ΔγεςϜ
    4

    View full-size slide

  5. ໨࣍ (1/2)
    5
    1. CloudFormationͰΠϯϑϥҰࣜΛϓϩϏδϣχϯά͠Α͏
    2. Lambda͔ΒDiscordʹϝοηʔδΛૹ৴͠Α͏
    3. IoT CoreͱDynamoDBɾLambdaͷ઀ଓΛ֬ೝ͠Α͏
    4. mockmockΛ࢖ͬͯσʔλΛૹͬͯΈΑ͏
    5. (͜͜Ͱٳܜ)




    View full-size slide

  6. ໨࣍ (2/2)
    6
    6. DynamoDB͔ΒItemΛಡΈग़ͦ͏
    7. API GatewayͰWebAPIΛ࡞Ζ͏
    8. Web্ͰάϥϑΛදࣔ͠Α͏
    9. ޙย෇͚Λ͠Α͏
    ᶈ ᶉ ᶊ

    View full-size slide

  7. ϋϯζΦϯΛਐΊΔ্Ͱͷ஫ҙ
    7
    • ηογϣϯதʹ࣭໰ͳͲ͋Γ·ͨ͠ΒZoomͷνϟοτͰ͓ئ͍͠·͢
    • ΦϯϥΠϯͳͷͰղܾ͕೉͍͠Մೳੑ΋͋Γ·͕͢ɺળॲ͠·͢
    • ηογϣϯޙͷTwitter΍DiscordͰͷ࣭͝໰΋׻ܴ͠·͢

    View full-size slide

  8. ϋϯζΦϯΛਐΊΔ্Ͱͷ஫ҙ
    8
    • ૢ࡞ϛεʹΑΔAWSྉۚͷ૿Ճʹ͝஫ҙ͍ͩ͘͞ɻ(ࣗݾ੹೚Ͱ͓ئ͍͠·͢)
    • Lambda FunctionͷίʔυΛม͑ͯແݶϧʔϓͤ͞ͳ͍
    • mockmockͷઃఆΛม͑ͯɺىಈ࣌ؒΛԆ͹͞ͳ͍

    View full-size slide

  9. 01
    CloudFormationͰΠϯϑϥҰࣜΛ
    ϓϩϏδϣχϯά͠Α͏

    View full-size slide

  10. AWSʹϩάΠϯ
    10
    https://aws.amazon.com/jp/ ΁ΞΫηε ΫϦοΫ

    View full-size slide

  11. AWSʹϩάΠϯ
    11
    ೖྗ
    ΫϦοΫ
    ೖྗ
    ΫϦοΫ

    View full-size slide

  12. CloudFormationͷը໘΁Ҡಈ
    12
    ʮcloudformationʯͱೖྗ
    ΫϦοΫ
    ஫ҙ: ʮ౦ژʯ͕બ୒͞Ε͍ͯΔ͜ͱΛ
    ֬͝ೝ͍ͩ͘͞

    View full-size slide

  13. ʮελοΫͷ࡞੒ʯ΁Ҡಈ
    13
    ΫϦοΫ

    View full-size slide

  14. ʮελοΫͷ࡞੒ʯ΁Ҡಈ ※ελοΫͷҰཡ͕දࣔ͞Εͨ৔߹
    14
    ΫϦοΫ

    View full-size slide

  15. ελοΫͷ࡞੒
    15
    ΫϦοΫ
    ΫϦοΫ
    https://swest22-session-s2c.s3-ap-northeast-1.amazonaws.com/out.yaml
    ͱೖྗ ※ޙ΄ͲZoomͰ΋ڞ༗͠·͢
    ΫϦοΫ

    View full-size slide

  16. ελοΫͷ࡞੒
    16
    ΫϦοΫ
    ʮSWEST22ʯͱೖྗ ※೚ҙͷ໊લͰOK

    View full-size slide

  17. ελοΫͷ࡞੒
    17
    ΫϦοΫ

    View full-size slide

  18. ελοΫͷ࡞੒
    18
    ΫϦοΫ
    3ͭͱ΋νΣοΫ

    View full-size slide

  19. ελοΫͷ࡞੒
    19
    ʮCREATE_COMPLETEʯ
    ʹͳͬͨΒ࡞੒׬ྃ
    ΫϦοΫ
    ͞·͟·ͳαʔϏεͷ
    Ϧιʔε͕࡞੒͞Ε͍ͯΔ

    View full-size slide

  20. !
    ຊ೔ͷߏங࡞ۀͷ9ׂ͕׬ྃʂ

    View full-size slide

  21. Կ͕ى͔ͬͨ͜ʁ
    21
    ελοΫͷ࡞੒ը໘Ͱೖྗͨ͠
    URLʹςϯϓϨʔτ(Ϋϥ΢υߏ੒
    ͷઃܭਤ)͕هड़͞Ε͍ͯΔ
    ΫϦοΫ

    View full-size slide

  22. ઃܭਤʹج͖ͮɺAWS಺ͷϦιʔε͕ߏங
    22
    ߏங׬ྃ

    View full-size slide

  23. ޙͰ࢖͏ͷͰϝϞ͍ͯͩ͘͠͞
    23
    ελοΫͷ࡞੒ը໘Ͱೖྗͨ͠
    URLʹςϯϓϨʔτ(Ϋϥ΢υߏ੒
    ͷઃܭਤ)͕هड़͞Ε͍ͯΔ
    ΫϦοΫ
    ϝϞ·ͨ͸ϦϯΫΛΫϦοΫͯ͠
    ผλϒͰ։͘ɺͰ΋OK

    View full-size slide

  24. 02
    Lambda͔ΒDiscordʹ
    ϝοηʔδΛૹ৴͠Α͏

    View full-size slide

  25. ର৅ͱͳΔαʔϏε
    25

    View full-size slide

  26. DiscordνϟϯωϧʹࢀՃ͍ͩ͘͞
    26
    ͜ͷޙɺট଴ϦϯΫΛZoomʹͯڞ༗͠·͢

    View full-size slide

  27. Lambdaͷը໘΁Ҡಈ
    27
    ΫϦοΫ
    ʮlambdaʯͱೖྗ͠ɺදࣔ͞ΕΔ
    ߲໨ΛΫϦοΫ

    View full-size slide

  28. Lambdaؔ਺ͷը໘΁Ҡಈ
    28
    ʮSWEST22-NotifyTemperatureFunction-xxxxʯ
    ΛΫϦοΫ

    View full-size slide

  29. Lambdaؔ਺ΛҰ෦ฤू
    29
    Λ
    ͜ͷޙZoomͰڞ༗͢ΔURLͰஔ͖׵͑
    Λ೚ҙͷ໊લͰஔ͖׵͑
    ※ૹ৴ݩΛ۠ผ͢Δͷ͕໨తͳͷͰ೚ҙͷ໊લͰOKͰ͢

    View full-size slide

  30. Lambdaؔ਺Λςετ
    30
    ΫϦοΫ
    ઌʹΫϦοΫ

    View full-size slide

  31. Lambdaؔ਺Λςετ
    31
    ʮtemperatureʯͱೖྗ ※೚ҙͷ໊લͰOK
    ͜ͷΑ͏ʹमਖ਼
    ΫϦοΫ

    View full-size slide

  32. Lambdaؔ਺Λςετ
    32
    ΫϦοΫ

    View full-size slide

  33. 03
    IoT CoreͱLambdaͷ઀ଓΛ
    ֬ೝ͠Α͏

    View full-size slide

  34. ର৅ͱͳΔαʔϏε
    34

    View full-size slide

  35. IoT Coreͷը໘΁Ҡಈ
    35
    ΫϦοΫ
    ʮiot coreʯͱೖྗ͠ɺදࣔ͞ΕΔ
    ߲໨ΛΫϦοΫ

    View full-size slide

  36. ʮϧʔϧʯ΁Ҡಈ
    36
    ΫϦοΫ

    View full-size slide

  37. IoT Core→LambdaͷϧʔϧΛ֬ೝ
    37
    ΫϦοΫ

    View full-size slide

  38. IoT Core→LambdaͷϧʔϧΛ֬ೝ
    38
    ૹ৴͞ΕͨJSONͷvalueΩʔͷ஋͕30Λ௒͑ͨ৔߹ʹɺ
    Lambda΁valueΛ௨஌

    View full-size slide

  39. IoT Core→DynamoDBͷϧʔϧΛ֬ೝ
    39
    ΫϦοΫ

    View full-size slide

  40. IoT Core→DynamoDBͷϧʔϧΛ֬ೝ
    40
    ΫϦοΫ
    ૹ৴͞ΕͨτϐοΫ໊ͷ຤୺ͷ୯ޠ͓Αͼ
    JSONͷtimestampͱvalueΛDynamoDB΁௨஌

    View full-size slide

  41. 04
    mockmockΛ࢖ͬͯ
    σʔλΛૹͬͯΈΑ͏

    View full-size slide

  42. ର৅ͱͳΔαʔϏε
    42

    View full-size slide

  43. ʮূ໌ॻʯ΁Ҡಈ
    43
    ΫϦοΫ

    View full-size slide

  44. IoT Coreͱͷೝূʹ࢖͏ূ໌ॻΛ࡞੒
    44
    ΫϦοΫ

    View full-size slide

  45. IoT Coreͱͷೝূʹ࢖͏ূ໌ॻΛ࡞੒
    45
    ΫϦοΫ

    View full-size slide

  46. IoT Coreͱͷೝূʹ࢖͏ূ໌ॻΛμ΢ϯϩʔυ
    46
    3ϑΝΠϧͱ΋
    μ΢ϯϩʔυ
    ΫϦοΫ

    View full-size slide

  47. IoT Coreͱͷೝূʹ࢖͏ূ໌ॻΛμ΢ϯϩʔυ
    47
    ผλϒͰ։͘

    View full-size slide

  48. IoT Coreͱͷೝূʹ࢖͏ূ໌ॻΛμ΢ϯϩʔυ
    48
    ϦϯΫઌΛอଘ

    View full-size slide

  49. IoT Coreͱͷೝূʹ࢖͏ূ໌ॻΛμ΢ϯϩʔυ
    49
    ΫϦοΫ

    View full-size slide

  50. IoT Coreͱͷೝূʹ࢖͏ূ໌ॻΛʹϙϦγʔΛΞλον
    51
    ΫϦοΫ
    ΫϦοΫ

    View full-size slide

  51. ઃఆ΁Ҡಈ
    52
    ΫϦοΫ

    View full-size slide

  52. ΤϯυϙΠϯτΛ֬ೝ
    53
    ΫϦοΫ
    ޙͰ࢖͏ͷͰϝϞ͓ͯ͘͠

    View full-size slide

  53. mockmockͷίϯιʔϧʹϩάΠϯ
    54
    https://console.mock-mock.com/users/sign_in ΁ΞΫηε͠ɺϩάΠϯ

    View full-size slide

  54. ʮϓϩδΣΫτΛίϐʔʯ΁Ҡಈ
    55
    ΫϦοΫ

    View full-size slide

  55. ϓϩδΣΫτΛίϐʔ
    56
    ΫϦοΫ
    બ୒
    ΫϦοΫ

    View full-size slide

  56. ϓϩδΣΫτ࡞੒
    57
    ʮૹ৴ઌϗετʯΛઌͷखॱͰ֬ೝͨ͠
    ʮΤϯυϙΠϯτʯʹมߋ
    ઌͷखॱͰμ΢ϯϩʔυͨ͠
    ূ໌ॻϑΝΠϧ(֦ுࢠ͕crt)Λબ୒
    ઌͷखॱͰμ΢ϯϩʔυͨ͠
    ൿີ伴ϑΝΠϧ(຤ඌ͕private.pem.key)
    Λબ୒
    ઌͷखॱͰμ΢ϯϩʔυͨ͠
    ʮAmazonRootCA1.pemʯΛબ୒
    ΫϦοΫ

    View full-size slide

  57. mock࡞੒
    58
    ΫϦοΫ
    ΫϦοΫ

    View full-size slide

  58. mock࡞੒
    59
    ΫϦοΫ
    ʮ0A-001ʯͱೖྗ
    ※೚ҙͷจࣈྻͰOK
    ʮ1ʯͱೖྗ
    ※೚ҙͷจࣈྻͰOK

    View full-size slide

  59. mockىಈ
    60
    ʮૢ࡞ʯ→ʮىಈʯΛΫϦοΫ

    View full-size slide

  60. DynamoDBͷը໘΁Ҡಈ
    61
    ΫϦοΫ
    ʮdynamoʯͱೖྗ͠ɺදࣔ͞ΕΔ
    ߲໨ΛΫϦοΫ

    View full-size slide

  61. Temperatureςʔϒϧ΁Ҡಈ
    62
    ΫϦοΫ
    ΫϦοΫ

    View full-size slide

  62. mock͔Βૹ৴͞ΕͨσʔλΛ֬ೝ
    63
    ૹ৴͞Εͨσʔλ͕ॻ͖ࠐ·Ε͍ͯΔ

    View full-size slide

  63. 64
    05
    ٳܜ࣌ؒͰ͢(༧ఆ࣌ؒ10෼)
    ͜͜·Ͱͷײ૝΍࣭໰ΛͥͻTwitterͰͭͿ΍͖·͠ΐ͏ʂ
    ϋογϡλά: #SWEST22_s2c

    View full-size slide

  64. 06
    DynamoDB͔ΒItemΛಡΈग़ͦ͏

    View full-size slide

  65. ର৅ͱͳΔαʔϏε
    66

    View full-size slide

  66. Lambdaͷը໘΁Ҡಈ
    67
    ΫϦοΫ
    ʮlambdaʯͱೖྗ͠ɺදࣔ͞ΕΔ
    ߲໨ΛΫϦοΫ

    View full-size slide

  67. Lambdaؔ਺ͷը໘΁Ҡಈ
    68
    ʮSWEST22-FetchTemperatureFunction-xxxxʯ
    ΛΫϦοΫ

    View full-size slide

  68. Lambdaؔ਺ͷϓϩάϥϜ
    69
    DynamoDB͔Βࢦఆͨ͠idͷItemΛऔಘ
    औಘͨ͠ItemΛJSONͰฦ٫
    औಘͨ͠Item͔Βཉ͍͠ΩʔΛൈ͖ग़͢

    View full-size slide

  69. Lambdaؔ਺Λςετ
    70
    ΫϦοΫ
    ઌʹΫϦοΫ

    View full-size slide

  70. Lambdaؔ਺Λςετ
    71
    ʮtestʯͱೖྗ ※೚ҙͷ໊લͰOK
    ͜ͷΑ͏ʹमਖ਼
    ※mockmockͰೖྗͨ͠serial_numberʹ
    ߹Θ͍ͤͯͩ͘͞
    ΫϦοΫ

    View full-size slide

  71. Lambdaؔ਺Λςετ
    72
    ΫϦοΫ
    Lambedaؔ਺ͷฦΓ஋͕දࣔ͞ΕΔ

    View full-size slide

  72. 07
    API GatewayͰWebAPIΛ࡞Ζ͏

    View full-size slide

  73. ର৅ͱͳΔαʔϏε
    74

    View full-size slide

  74. API Gatewayͷը໘΁Ҡಈ
    75
    ΫϦοΫ
    ʮapiʯͱೖྗ͠ɺදࣔ͞ΕΔ߲໨Λ
    ΫϦοΫ

    View full-size slide

  75. APIͷը໘΁Ҡಈ
    76
    ΫϦοΫ

    View full-size slide

  76. APIͷϦιʔεͱΞΫγϣϯ
    77
    ΫϦοΫ
    GET /temperature/{id}
    ΁ͷϦΫΤετ͕
    SWEST22-
    FetchTemperatureFunction
    ʹඥ෇͚ΒΕ͍ͯΔ

    View full-size slide

  77. APIͷςετ
    78
    ઌͷखॱͰϝϞͨ͠ApiURL + serial number΁ΞΫηε
    ※ϝϞΛ๨Εͨ৔߹͸CloudFormationͷ
    ग़ྗλϒͰ֬ೝ͕Ͱ͖·͢

    View full-size slide

  78. APIͷςετ
    79
    DynamoDBʹอଘ͞Εͨσʔλ͕
    JSONܗࣜͰಡΈग़͞ΕΔ

    View full-size slide

  79. 08
    Web্ͰάϥϑΛදࣔ͠Α͏

    View full-size slide

  80. ର৅ͱͳΔαʔϏε
    81

    View full-size slide

  81. දࣔ͢ΔϖʔδͷHTMLϑΝΠϧΛμ΢ϯϩʔυ
    82
    https://swest22-session-s2c.s3-ap-northeast-1.amazonaws.com/pages.zip

    View full-size slide

  82. දࣔ͢ΔϖʔδͷHTMLϑΝΠϧΛमਖ਼
    83
    ઌఔμ΢ϯϩʔυͨ͠ZIPϑΝΠϧΛղౚ͠
    ʮindex.htmlʯΛΤσΟλͰ։͘
    32ߦ໨ͷʮʯΛ
    ApiURL + serial number Ͱஔ͖׵͑Δ

    View full-size slide

  83. S3ͷը໘΁Ҡಈ
    84
    ΫϦοΫ
    ʮs3ʯͱೖྗ͠ɺ
    දࣔ͞ΕΔ߲໨ΛΫϦοΫ

    View full-size slide

  84. ެ։༻όέοτͷը໘΁Ҡಈ
    85
    ʮswest22-s3-bucket-ʯ͔Β࢝·ΔόέοτΛΫϦοΫ

    View full-size slide

  85. HTMLϑΝΠϧΛΞοϓϩʔυ
    86
    ઌఔฤूͨ͠ʮindex.htmlʯΛ
    υϥοάɾΞϯυɾυϩοϓ

    View full-size slide

  86. HTMLϑΝΠϧΛΞοϓϩʔυ
    87
    ΫϦοΫ

    View full-size slide

  87. ެ։༻ͷURLΛ֬ೝ
    88
    ΫϦοΫ

    View full-size slide

  88. ެ։༻ͷURLΛ֬ೝ
    89
    ެ։༻ͷURL

    View full-size slide

  89. ެ։༻ͷURLΛ֬ೝ
    90
    mockmock͔Βૹ৴ͨ͠
    σʔλ͕දࣔ͞ΕΔ

    View full-size slide

  90. 09
    ޙย෇͚Λ͠Α͏

    View full-size slide

  91. S3ͷόέοτͷத਎Λۭʹ͢Δ
    92
    ΫϦοΫ
    όέοτΛબ୒

    View full-size slide

  92. IoT Coreͷը໘΁Ҡಈ
    93
    ΫϦοΫ
    ʮiot coreʯͱೖྗ͠ɺදࣔ͞ΕΔ
    ߲໨ΛΫϦοΫ

    View full-size slide

  93. ϙϦγʔΛ࡟আ
    94
    ΫϦοΫ
    ΫϦοΫ

    View full-size slide

  94. CloudFormationͷը໘΁Ҡಈ
    95
    ΫϦοΫ
    ʮcloudformationʯͱೖྗ͠ɺ
    දࣔ͞ΕΔ߲໨ΛΫϦοΫ

    View full-size slide

  95. ελοΫΛ࡟আ
    96
    ελοΫΛબ୒
    ΫϦοΫ

    View full-size slide

  96. !
    ͓ർΕ͞·Ͱͨ͠ʂ

    View full-size slide