AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります
翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。
Lambda K for SDK for SDKJavaScript のv3)
次のコード例は、Lambda でのAWS SDK for JavaScript (v3 で一般的なシナリオを実装する方法を示しています。
「アクション」は、個々のサービス関数の呼び出し方法を示すコードの抜粋です。
「シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。
それぞれの例にはGitHub、へのリンクがあり、コンテキストでコードを設定および実行する方法についての説明が記載されています。
アクション
以下のコード例は、Lambda 関数を作成する方法を示しています。
- SDK forJavaScript (v3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 const createFunction = async (funcName, roleArn) => { const client = createClientForDefaultRegion(LambdaClient); const code = await readFile(`${dirname}../functions/${funcName}.zip`); const command = new CreateFunctionCommand({ Code: { ZipFile: code }, FunctionName: funcName, Role: roleArn, Architectures: [Architecture.arm64], Handler: "index.handler", // Required when sending a .zip file PackageType: PackageType.Zip, // Required when sending a .zip file Runtime: Runtime.nodejs16x, // Required when sending a .zip file }); return client.send(command); };
-
API の詳細については、AWS SDK for JavaScriptAPI CreateFunctionリファレンスのを参照してください。
-
以下のコード例は、Lambda 関数を削除する方法を示しています。
- SDK forJavaScript (v3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 const deleteFunction = (funcName) => { const client = createClientForDefaultRegion(LambdaClient); const command = new DeleteFunctionCommand({ FunctionName: funcName }); return client.send(command); };
-
API の詳細については、AWS SDK for JavaScriptAPI DeleteFunctionリファレンスのを参照してください。
-
以下のコード例は、Lambda 関数を取得する方法を示しています。
- SDK forJavaScript (v3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 const getFunction = (funcName) => { const client = createClientForDefaultRegion(LambdaClient); const command = new GetFunctionCommand({ FunctionName: funcName }); return client.send(command); };
-
API の詳細については、AWS SDK for JavaScriptAPI GetFunctionリファレンスのを参照してください。
-
以下のコード例は、Lambda 関数を呼び出す方法を示しています。
- SDK forJavaScript (v3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 const invoke = async (funcName, payload) => { const client = createClientForDefaultRegion(LambdaClient); const command = new InvokeCommand({ FunctionName: funcName, Payload: JSON.stringify(payload), LogType: LogType.Tail, }); const { Payload, LogResult } = await client.send(command); const result = Buffer.from(Payload).toString(); const logs = Buffer.from(LogResult, "base64").toString(); return { logs, result }; };
-
API の詳細については、「AWS SDK for JavaScript API リファレンス」の「[Invoke]」(呼び出し) を参照してください。
-
以下のコード例は、Lambda 関数を一覧表示する方法を示しています。
- SDK forJavaScript (v3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 const listFunctions = async () => { const client = createClientForDefaultRegion(LambdaClient); const command = new ListFunctionsCommand({}); return client.send(command); };
-
API の詳細については、AWS SDK for JavaScriptAPI ListFunctionsリファレンスのを参照してください。
-
以下のコード例は、Lambda 関数を更新する方法を示しています。
- SDK forJavaScript (v3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 const updateFunctionCode = async (funcName, newFunc) => { const client = createClientForDefaultRegion(LambdaClient); const code = await readFile(`${dirname}../functions/${newFunc}.zip`); const command = new UpdateFunctionCodeCommand({ ZipFile: code, FunctionName: funcName, Architectures: [Architecture.arm64], Handler: "index.handler", // Required when sending a .zip file PackageType: PackageType.Zip, // Required when sending a .zip file Runtime: Runtime.nodejs16x, // Required when sending a .zip file }); return client.send(command); };
-
API の詳細については、AWS SDK for JavaScriptAPI UpdateFunctionCodeリファレンスのを参照してください。
-
以下のコード例は、Lambda 関数の設定を更新する方法を示しています。
- SDK forJavaScript (v3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 const updateFunctionConfiguration = (funcName) => { const client = new LambdaClient({}); const config = readFileSync(`${dirname}../functions/config.json`).toString(); const command = new UpdateFunctionConfigurationCommand({ ...JSON.parse(config), FunctionName: funcName, }); return client.send(command); };
-
API の詳細については、AWS SDK for JavaScriptAPI UpdateFunctionConfigurationリファレンスのを参照してください。
-
シナリオ
次のコード例は、以下の操作方法を示しています。
IAM ロールと Lambda 関数を作成し、ハンドラーコードをアップロードします。
1 つのパラメーターで関数を呼び出して、結果を取得します。
関数コードを更新し、環境変数で設定します。
新しいパラメーターで関数を呼び出して、結果を取得します。返された実行ログを表示します。
アカウントの関数を一覧表示し、リソースをクリーンアップします。
詳細については、「コンソールで Lambda 関数を作成する」を参照してください。
- SDK forJavaScript (v3)
-
注記
他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリ
での設定と実行の方法を確認してください。 AWS Identity and Access Management (IAM) ロールを作成して、Lambda にログへの書き込み権限を付与します。
log(`Creating role (${NAME_ROLE_LAMBDA})...`); const response = await createRole({ AssumeRolePolicyDocument: parseString({ Version: "2012-10-17", Statement: [ { Effect: "Allow", Principal: { Service: "lambda.amazonaws.com", }, Action: "sts:AssumeRole", }, ], }), RoleName: NAME_ROLE_LAMBDA, }); import { AttachRolePolicyCommand, IAMClient } from "@aws-sdk/client-iam"; const client = new IAMClient({}); /** * * @param {string} policyArn * @param {string} roleName */ export const attachRolePolicy = (policyArn, roleName) => { const command = new AttachRolePolicyCommand({ PolicyArn: policyArn, RoleName: roleName, }); return client.send(command); };
Lambda 関数を作成し、ハンドラーコードをアップロードします。
const createFunction = async (funcName, roleArn) => { const client = createClientForDefaultRegion(LambdaClient); const code = await readFile(`${dirname}../functions/${funcName}.zip`); const command = new CreateFunctionCommand({ Code: { ZipFile: code }, FunctionName: funcName, Role: roleArn, Architectures: [Architecture.arm64], Handler: "index.handler", // Required when sending a .zip file PackageType: PackageType.Zip, // Required when sending a .zip file Runtime: Runtime.nodejs16x, // Required when sending a .zip file }); return client.send(command); };
1 つのパラメーターで関数を呼び出して、結果を取得します。
const invoke = async (funcName, payload) => { const client = createClientForDefaultRegion(LambdaClient); const command = new InvokeCommand({ FunctionName: funcName, Payload: JSON.stringify(payload), LogType: LogType.Tail, }); const { Payload, LogResult } = await client.send(command); const result = Buffer.from(Payload).toString(); const logs = Buffer.from(LogResult, "base64").toString(); return { logs, result }; };
関数コードを更新し、Lambda 環境を環境可変で設定します。
const updateFunctionCode = async (funcName, newFunc) => { const client = createClientForDefaultRegion(LambdaClient); const code = await readFile(`${dirname}../functions/${newFunc}.zip`); const command = new UpdateFunctionCodeCommand({ ZipFile: code, FunctionName: funcName, Architectures: [Architecture.arm64], Handler: "index.handler", // Required when sending a .zip file PackageType: PackageType.Zip, // Required when sending a .zip file Runtime: Runtime.nodejs16x, // Required when sending a .zip file }); return client.send(command); }; const updateFunctionConfiguration = (funcName) => { const client = new LambdaClient({}); const config = readFileSync(`${dirname}../functions/config.json`).toString(); const command = new UpdateFunctionConfigurationCommand({ ...JSON.parse(config), FunctionName: funcName, }); return client.send(command); };
アカウントの関数を一覧表示します。
const listFunctions = async () => { const client = createClientForDefaultRegion(LambdaClient); const command = new ListFunctionsCommand({}); return client.send(command); };
IAM ロールと Lambda 関数を削除します。
import { DeleteRoleCommand, IAMClient } from "@aws-sdk/client-iam"; const client = new IAMClient({}); /** * * @param {string} roleName */ export const deleteRole = (roleName) => { const command = new DeleteRoleCommand({ RoleName: roleName }); return client.send(command); }; const deleteFunction = (funcName) => { const client = createClientForDefaultRegion(LambdaClient); const command = new DeleteFunctionCommand({ FunctionName: funcName }); return client.send(command); };
-
API の詳細については、「AWS SDK for JavaScript API リファレンス」の以下のトピックを参照してください。
-