lamda関数を新規作成した
前提 : vpc、elastic ipの設定は終わっている
lamda作成ユーザにIAM権限が必要(ロール割当で必要になる)
- サービスからlamdaを選択
- 関数の作成
・ 一から作成 (あくまで自分の場合)
を選択
・ 関数名
任意でつける
・ ランタイム
nodejs20を選択
・ アーキテクチャ
x86_64 (特に何も考えず)
・ デフォルトの実行ロールの変更
→ 基本的な Lambda アクセス権限で新しいロールを作成
を選択した。(使いまわしのロールなければこれで良さそう)
→ この時はS3の権限が必要だったので以下を追加
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject",
"s3:PutObjectAcl"
],
"Resource": [
"arn:aws:s3:::{本番用S3名}/*",
"arn:aws:s3:::{ステージング用S3名}/*"
]
}
]
}
(参考) 基本的な Lambda アクセス権限で新しいロールを作成でつくると以下権限が付与された
AWSLambdaBasicExecutionRole-・・・・
AWSLambdaVPCAccessExecutionRole-・・・・
・ VPCを有効化(このときは既存があったので使いまわし)
VPCを有効化
→ VPC,サブネット ,セキュリティグループの割当が必要
自分のIP確認のAPIサービスを使用して固定化されているのを確認した。
https://ipinfo.io/ip
固定IP,S3配置確認用js
// ESM構文を使用したモジュールのインポート
import https from 'https';
import { PutObjectCommand, S3Client } from '@aws-sdk/client-s3';
export const handler = async (event, context) => {
try {
// HTTPSでデータを取得
const data = await httpRequest('https://ipinfo.io/ip');
console.log(data);
const client = new S3Client({});
const command = new PutObjectCommand({
Bucket: "ftp-server-stg",
Key: "mng-evl.txt",
Body: data,
});
const response = await client.send(command);
console.log(response);
// // S3にアップロード
// const s3 = new S3();
// const params = {
// Bucket: 'バケット名',
// Key: 'ファイル名',
// Body: data
// };
// const uploadStatus = await s3.putObject(params).promise();
// console.log('Upload successful:', uploadStatus);
} catch (error) {
console.error('Error:', error);
}
};