-
Notifications
You must be signed in to change notification settings - Fork 241
Expand file tree
/
Copy pathcreate_function.sh
More file actions
executable file
·85 lines (72 loc) · 3.16 KB
/
create_function.sh
File metadata and controls
executable file
·85 lines (72 loc) · 3.16 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
#!/bin/bash
# Set variables
FUNCTION_NAME="aws-lambda-java-profiler-function-${GITHUB_RUN_ID}"
FUNCTION_NAME_CUSTOM_PROFILER_OPTIONS="aws-lambda-java-profiler-function-custom-${GITHUB_RUN_ID}"
ROLE_NAME="aws-lambda-java-profiler-role-${GITHUB_RUN_ID}"
HANDLER="helloworld.Handler::handleRequest"
RUNTIME="java21"
LAYER_ARN=$(cat /tmp/layer_arn)
JAVA_TOOL_OPTIONS="-XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -javaagent:/opt/profiler-extension.jar"
AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME="aws-lambda-java-profiler-bucket-${GITHUB_RUN_ID}"
AWS_LAMBDA_PROFILER_START_COMMAND="start,event=wall,interval=1us,file=/tmp/profile.jfr"
AWS_LAMBDA_PROFILER_STOP_COMMAND="stop,file=%s"
# Compile the Hello World project
cd integration_tests/helloworld
gradle :buildZip
cd ../..
# Create IAM role for Lambda
ROLE_ARN=$(aws iam create-role \
--role-name $ROLE_NAME \
--assume-role-policy-document '{"Version": "2012-10-17","Statement": [{ "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole"}]}' \
--query 'Role.Arn' \
--output text)
# Attach basic Lambda execution policy to the role
aws iam attach-role-policy \
--role-name $ROLE_NAME \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
# Attach s3:PutObject policy to the role so we can upload profiles
POLICY_DOCUMENT=$(cat <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::$AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME/*"
}
]
}
EOF
)
aws iam put-role-policy \
--role-name "$ROLE_NAME" \
--policy-name "s3PutObject" \
--policy-document "$POLICY_DOCUMENT"
# Wait for the role to be created and policy to be attached
echo "Waiting for IAM role to be ready..."
sleep 10
# Create Lambda function
aws lambda create-function \
--function-name "$FUNCTION_NAME" \
--runtime "$RUNTIME" \
--role "$ROLE_ARN" \
--handler "$HANDLER" \
--timeout 30 \
--memory-size 512 \
--zip-file fileb://integration_tests/helloworld/build/distributions/code.zip \
--environment "Variables={JAVA_TOOL_OPTIONS='$JAVA_TOOL_OPTIONS',AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME='$AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME',AWS_LAMBDA_PROFILER_DEBUG='true'}" \
--layers "$LAYER_ARN"
# Create Lambda function custom profiler options
aws lambda create-function \
--function-name "$FUNCTION_NAME_CUSTOM_PROFILER_OPTIONS" \
--runtime "$RUNTIME" \
--role "$ROLE_ARN" \
--handler "$HANDLER" \
--timeout 30 \
--memory-size 512 \
--zip-file fileb://integration_tests/helloworld/build/distributions/code.zip \
--environment "Variables={JAVA_TOOL_OPTIONS='$JAVA_TOOL_OPTIONS',AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME='$AWS_LAMBDA_PROFILER_RESULTS_BUCKET_NAME',AWS_LAMBDA_PROFILER_DEBUG='true',AWS_LAMBDA_PROFILER_START_COMMAND='$AWS_LAMBDA_PROFILER_START_COMMAND',AWS_LAMBDA_PROFILER_STOP_COMMAND='$AWS_LAMBDA_PROFILER_STOP_COMMAND'}" \
--layers "$LAYER_ARN"
echo "Lambda function '$FUNCTION_NAME' created successfully with Java 21 runtime"
echo "Waiting the function to be ready so we can invoke it..."
sleep 10