File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3232 - name : Build
3333 run : pip install -r requirements.txt
3434
35+ - name : Load environment variables from .env file
36+ id : load-env
37+ run : |
38+ if [ -f .env ]; then
39+ echo "Loading environment variables from .env file"
40+ # .envファイルから環境変数を読み取りJSON形式に変換
41+ env_json="{"
42+ while IFS='=' read -r key value || [ -n "$key" ]; do
43+ # コメント行と空行をスキップ
44+ if [[ "$key" =~ ^#.*$ ]] || [[ -z "$key" ]]; then
45+ continue
46+ fi
47+ # 値をクォートで囲んでJSONに追加
48+ if [ -n "$env_json" ] && [ "$env_json" != "{" ]; then
49+ env_json="$env_json,"
50+ fi
51+ env_json="$env_json\"$key\":\"$value\""
52+ done < .env
53+ env_json="$env_json}"
54+ echo "env-vars=$env_json" >> $GITHUB_OUTPUT
55+ else
56+ echo "No .env file found, using default environment variables"
57+ echo 'env-vars={"ENVIRONMENT":"production","LOG_LEVEL":"info","PYTHON_VERSION":"${{ env.PYTHON_VERSION }}"}' >> $GITHUB_OUTPUT
58+ fi
59+
3560 - name : Configure AWS credentials
3661 uses : aws-actions/configure-aws-credentials@v4
3762 with :
4873 runtime : python${{ env.PYTHON_VERSION }}
4974 role : ${{ env.lambda-role-arn }}
5075 dry-run : false
76+ environment-variables : ${{ steps.load-env.outputs.env-vars }}
Original file line number Diff line number Diff line change 1+ # Lambda環境変数設定例
2+ ENVIRONMENT = production
3+ LOG_LEVEL = info
4+ DEBUG = false
5+ APP_NAME = hello_lambda_actions
Original file line number Diff line number Diff line change 11import json
2+ import os
23
34
45def lambda_handler (event , context ):
6+ # 環境変数を取得
7+ environment = os .environ .get ("ENVIRONMENT" , "development" )
8+ log_level = os .environ .get ("LOG_LEVEL" , "debug" )
9+ app_name = os .environ .get ("APP_NAME" , "hello_lambda" )
10+
511 return {
612 "statusCode" : 200 ,
713 "body" : json .dumps (
814 {
915 "message" : "hello_world" ,
16+ "environment" : environment ,
17+ "log_level" : log_level ,
18+ "app_name" : app_name ,
1019 }
1120 ),
1221 }
You can’t perform that action at this time.
0 commit comments