forked from hideokamoto/lambda-edge-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.js
More file actions
106 lines (85 loc) · 3.1 KB
/
config.js
File metadata and controls
106 lines (85 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '.env') });
// 設定値を管理するクラス
class Config {
constructor() {
this.loadEnvironmentVariables();
}
// 環境変数を読み込み
loadEnvironmentVariables() {
// 必須設定
this.lambdaArn = process.env.LAMBDA_ARN;
this.distributionId = process.env.CLOUDFRONT_DISTRIBUTION_ID;
// オプション設定
this.eventType = process.env.EVENT_TYPE || 'viewer-request';
this.awsRegion = process.env.AWS_REGION || 'us-east-1';
this.awsProfile = process.env.AWS_PROFILE;
// AWS認証情報
this.awsAccessKeyId = process.env.AWS_ACCESS_KEY_ID;
this.awsSecretAccessKey = process.env.AWS_SECRET_ACCESS_KEY;
this.awsSessionToken = process.env.AWS_SESSION_TOKEN;
// デバッグ設定
this.debug = process.env.DEBUG === 'true';
this.logLevel = process.env.LOG_LEVEL || 'info';
}
// 必須設定が揃っているかチェック
validate() {
const errors = [];
if (!this.lambdaArn) {
errors.push('LAMBDA_ARN が設定されていません');
}
if (!this.distributionId) {
errors.push('CLOUDFRONT_DISTRIBUTION_ID が設定されていません');
}
if (errors.length > 0) {
throw new Error(`設定エラー:\n${errors.map(err => ` - ${err}`).join('\n')}`);
}
return true;
}
// 設定状況を表示
display() {
console.log('🔍 現在の設定状況:');
console.log('');
console.log('必須設定:');
console.log(` LAMBDA_ARN: ${this.lambdaArn ? '✅ 設定済み' : '❌ 未設定'}`);
console.log(` CLOUDFRONT_DISTRIBUTION_ID: ${this.distributionId ? '✅ 設定済み' : '❌ 未設定'}`);
console.log('');
console.log('オプション設定:');
console.log(` EVENT_TYPE: ${this.eventType}`);
console.log(` AWS_REGION: ${this.awsRegion}`);
console.log(` AWS_PROFILE: ${this.awsProfile || '未設定'}`);
console.log(` DEBUG: ${this.debug}`);
console.log(` LOG_LEVEL: ${this.logLevel}`);
console.log('');
console.log('AWS認証情報:');
if (this.awsAccessKeyId && this.awsSecretAccessKey) {
console.log(' ✅ 環境変数で設定済み');
} else if (this.awsProfile) {
console.log(` ✅ AWSプロファイル: ${this.awsProfile}`);
} else {
console.log(' ⚠️ 未設定(IAMロールまたはプロファイルを使用してください)');
}
}
// AWS SDK設定オブジェクトを取得
getAwsConfig() {
const config = {
region: this.awsRegion
};
if (this.awsAccessKeyId && this.awsSecretAccessKey) {
config.credentials = {
accessKeyId: this.awsAccessKeyId,
secretAccessKey: this.awsSecretAccessKey
};
if (this.awsSessionToken) {
config.credentials.sessionToken = this.awsSessionToken;
}
}
if (this.awsProfile) {
config.profile = this.awsProfile;
}
return config;
}
}
// シングルトンインスタンスを作成
const config = new Config();
module.exports = config;