Skip to content

Fix env vars override bug. #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions experimental/aws-lambda-java-profiler/update-function.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,27 @@ aws lambda update-function-configuration \
aws lambda wait function-updated \
--function-name "$FUNCTION_NAME"

# Add environment variables
# Get existing environment variables (handle null case)
EXISTING_VARS=$(aws lambda get-function-configuration --function-name "$FUNCTION_NAME" --query "Environment.Variables" --output json 2>/dev/null)
if [[ -z "$EXISTING_VARS" || "$EXISTING_VARS" == "null" ]]; then
EXISTING_VARS="{}"
fi

# Define new environment variables in JSON format
NEW_VARS=$(jq -n --arg bucket "$BUCKET_NAME" \
--arg java_opts "-XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -javaagent:/opt/profiler-extension.jar" \
'{AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME: $bucket, JAVA_TOOL_OPTIONS: $java_opts}')

# Merge existing and new variables (compact JSON output)
UPDATED_VARS=$(echo "$EXISTING_VARS" | jq -c --argjson new_vars "$NEW_VARS" '. + $new_vars')

# Convert JSON to "Key=Value" format for AWS CLI
ENV_VARS_FORMATTED=$(echo "$UPDATED_VARS" | jq -r 'to_entries | map("\(.key)=\(.value)") | join(",")')

# Update Lambda function with correct format
aws lambda update-function-configuration \
--function-name "$FUNCTION_NAME" \
--environment "Variables={AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME=$BUCKET_NAME, JAVA_TOOL_OPTIONS=-XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -javaagent:/opt/profiler-extension.jar}"
--function-name "$FUNCTION_NAME" \
--environment "Variables={$ENV_VARS_FORMATTED}"

# Update the function's permissions to write to the S3 bucket
# Get the function's execution role
Expand Down Expand Up @@ -73,4 +90,4 @@ echo "Setup completed for function $FUNCTION_NAME with S3 bucket $BUCKET_NAME"
echo "S3 write permissions added to the function's execution role"

# Clean up temporary files
rm s3-write-policy.json
rm s3-write-policy.json