0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS-Parameters-and-Secrets-Lambda-Extensionを試す

Last updated at Posted at 2024-08-08

背景

Lambda上でSSMパラメータストアの値を都度取得するのが嫌で調べてたら以下を見つけたので試して見る

  • Lambda Layer は以下を指定
    arn:aws:lambda:ap-northeast-1:133490724326:layer:AWS-Parameters-and-Secrets-Lambda-Extension-Arm64:11

コード

index.mjs
import {SSMClient, GetParameterCommand} from '@aws-sdk/client-ssm';

export const handler = async () => {
    const path = "/hoge/secure/params";
    let start;
    let end;

    // SDK経由
    start = new Date()
    for (let i = 0; i < 20; i++) {
        await sendGetParameter(path);
    }
    end = new Date() - start;
    console.info('sendGetParameter time: %dms', end)

    // Extention経由
    start = new Date()
    for (let i = 0; i < 20; i++) {
        await sendGetParameterUseExtension(path);
    }
    end = new Date() - start;
    console.info('sendGetParameterUseExtension time: %dms', end)

    return {};
};

const sendGetParameter = async (path) => {
    const ssmClient = new SSMClient();
    return await ssmClient.send(new GetParameterCommand({
        Name: path,
        WithDecryption: true
    }));
}

const sendGetParameterUseExtension = async (path) => {
    const ssmPStore = await fetch(
        `http://localhost:2773/systemsmanager/parameters/get?name=${path}&withDecryption=true`,
        {
            headers: {
                "X-Aws-Parameters-Secrets-Token": process.env.AWS_SESSION_TOKEN,
            }
        }
    )
    return await ssmPStore.json();
}

結果

20回連続で呼び出してみて比較したところウォームスタートだと圧倒的に速い。
あたりまえか…

  • コールドスタート時
INFO	sendGetParameter time: 4463ms
INFO	sendGetParameterExtension time: 5714ms
  • ウォームスタート時
INFO	sendGetParameter time: 3404ms
INFO	sendGetParameterExtension time: 694ms

今のところパラメータ名はフルパス指定しかなさそうだから、
GetParametersCommandとかGetParametersByPathCommandみたいな指定の仕方が出来ると嬉しいなー

こんな既出な情報なのに相変わらず自分は見つけるの下手だなーという感じ…

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?