Skip to content

lambda関数追加 - #3

Merged
Koonosuke merged 1 commit into
mainfrom
feature/0001
Dec 2, 2025
Merged

Koonosuke merged 1 commit into
mainfrom
feature/0001

Conversation

@Koonosuke

Copy link
Copy Markdown
Collaborator

概要

lambda関数の追加

その他

@Koonosuke
Koonosuke merged commit b710005 into main Dec 2, 2025
2 checks passed
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @Koonosuke, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

このプルリクエストは、AWS Lambda関数を導入し、REST API GatewayとAWS IoT Core間のブリッジとして機能させます。この関数は、HTTPリクエストを受信し、その内容を検証した後、指定されたIoTデバイスを制御するためのMQTTメッセージとしてAWS IoT Coreに転送することを目的としています。これにより、外部システムからIoTデバイスへのコマンド発行が容易になります。

Highlights

  • 新規Lambda関数の追加: AWS_Handson_2025/lambda/index.mjs に新しいLambda関数が追加されました。この関数は、REST APIからのリクエストを処理し、AWS IoT CoreにMQTTメッセージをパブリッシュします。
  • リクエストボディの処理と検証: Lambda関数は、API Gatewayから受け取ったリクエストボディをJSONとしてパースし、deviceIdtypepayloadの必須フィールドが存在するかを検証します。無効なJSONや必須フィールドの欠落がある場合は、適切なエラーレスポンスを返します。
  • AWS IoT CoreへのMQTTパブリッシュ: 検証されたデータを使用して、iot/xiao/${deviceId}/controlという形式のMQTTトピックにコマンドメッセージをパブリッシュします。QoSレベルは1に設定されており、メッセージが少なくとも1回は配信されることを保証します。
  • エラーハンドリング: JSONパースエラーとIoT Coreへのパブリッシュエラーの両方に対して、適切なエラーメッセージとステータスコードを含むレスポンスを返すエラーハンドリングが実装されています。
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions

github-actions Bot commented Dec 2, 2025

Copy link
Copy Markdown

🔗 関連するNotionタスク

状態: 進行中

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

このプルリクエストは、API Gatewayからのリクエストを受け取り、AWS IoT Coreにメッセージをパブリッシュする新しいLambda関数を追加するものです。コードは全体的によく書かれていますが、保守性と堅牢性を向上させるためのいくつかの改善点があります。具体的には、リージョンやトピックなどの設定値をハードコードするのではなく環境変数から読み込むようにすること、リクエストボディの検証を改善すること、コードとコメントの不整合を修正することを提案しました。

//API Gateway → Lambda の body は JSON ではなく "文字列"だからJSON.parse() しないと中の値を取り出せない
let body;
try {
body = JSON.parse(event.body);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

event.bodynullundefined の場合、JSON.parse はエラーをスローします。現在の実装では catch ブロックで捕捉され、「Invalid JSON in request body」というメッセージで400エラーが返されますが、これは「リクエストボディが空である」という問題とは異なります。
パースする前に event.body の存在をチェックすることで、より的確なエラーメッセージを返すことができます。

    if (!event.body) {
      return {
        statusCode: 400,
        body: JSON.stringify({
          status: "error",
          message: "Request body is missing",
        }),
      }
    }
    body = JSON.parse(event.body)

//前者はコンソールとかでいじった証明書の作成など設定をいじるチャンネル
//後者はMQTT メッセージの通り道→つまりこのlambda関数でやるのは後者

const iot = new IoTDataPlaneClient({ region: "ap-northeast-1" });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

リージョン ap-northeast-1 がハードコードされています。Lambda関数は実行されるリージョンの情報を AWS_REGION 環境変数として持っているため、これを利用することで、コードを修正せずに他のリージョンへデプロイできるようになります。

Suggested change
const iot = new IoTDataPlaneClient({ region: "ap-northeast-1" });
const iot = new IoTDataPlaneClient({ region: process.env.AWS_REGION })

// ---------------------------
// Step 3: Publish 先 Topic
// ---------------------------
const topic = `iot/xiao/${deviceId}/control`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

トピックのプレフィックス (iot/xiao/) とサフィックス (/control) がハードコードされています。Lambda関数の再利用性や設定の管理性を高めるために、これらを環境変数から設定できるようにすることをお勧めします。

  const topicPrefix = process.env.TOPIC_PREFIX || 'iot/xiao'
  const topicSuffix = process.env.TOPIC_SUFFIX || 'control'
  const topic = `${topicPrefix}/${deviceId}/${topicSuffix}`

// ---------------------------
const topic = `iot/xiao/${deviceId}/control`;

// XIAO が必要な形式(type + payload)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

コメントではXIAOが必要な形式は type + payload と記載されていますが、commandMessage オブジェクトには deviceId も含まれています。
もし deviceId が不要なのであれば、commandMessage から削除してください。もし必要なのであれば、コメントを (deviceId + type + payload) のように修正して、コードとドキュメントの整合性を保つことをお勧めします。

// XIAO が必要な形式(deviceId + type + payload)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant