LoginSignup
0
0

More than 1 year has passed since last update.

ブラウザからLambdaを実行

Last updated at Posted at 2022-07-20

IDプールの作成

Cognito > IDプール管理 > 新しいIDプールの作成
認証時のロール、未認証時のロールがcognitoによって作成される

IAM権限設定

用途に合わせて認証時のロールまたは、未認証時のロールにInvokeFunction権限を付与する

javascript(ES6)

const { CognitoIdentityClient }   = require("@aws-sdk/client-cognito-identity");
const { fromCognitoIdentityPool } = require("@aws-sdk/credential-provider-cognito-identity");
const client_lambda               = require("@aws-sdk/client-lambda");
const REGION = "ap-northeast-1";
const lambda = new client_lambda.Lambda({
    region:      REGION,
    credentials: fromCognitoIdentityPool({
                     client:         new CognitoIdentityClient({ region: REGION }),
                     identityPoolId: "(cognito_pool_id)",
                 })
});

let param = {
    FunctionName:   '(lambda_function_name)',
    Payload:        JSON.stringify({ aaa: 'aaa', bbb: 'bbb' }),
    InvocationType: 'RequestResponse'
};

lambda.invoke(param, (err, data) => {
    if (err) {
        console.log(err, err.stack);
    }
    let resp = JSON.parse((new TextDecoder).decode(new Uint8Array(data.Payload)));
    console.log(resp);
})

あとはwebpackとかでいい感じにしてからブラウザで実行

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0