Skip to content

Commit 4497663

Browse files
環境変数もデプロイできるように
1 parent 4c9ca64 commit 4497663

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

.github/workflows/deploy_lambda.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@ jobs:
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:
@@ -48,3 +73,4 @@ jobs:
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 }}

hello_lambda/.env

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Lambda環境変数設定例
2+
ENVIRONMENT=production
3+
LOG_LEVEL=info
4+
DEBUG=false
5+
APP_NAME=hello_lambda_actions

hello_lambda/app.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import json
2+
import os
23

34

45
def 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
}

0 commit comments

Comments
 (0)