An infrastructure-forward modern worker template built with Rust for AWS Lambda
cargo build --release --target x86_64-unknown-linux-musl
- Run the lambda ci docker image
docker run --rm \
-e DOCKER_LAMBDA_STAY_OPEN=1 -p 9001:9001 \
-v "$PWD"/target/x86_64-unknown-linux-musl/release/bootstrap:/var/task/bootstrap:ro,delegated \
lambci/lambda:provided main
- In a separate terminal, invoke the lambda
aws lambda invoke \
--endpoint http://localhost:9001 \
--no-sign-request --function-name=darkowl \
--invocation-type=RequestResponse \
--payload $(echo '{ "externalRequester": "cron" }' | base64 ) \
output.json
-
Ensure you have a command line role belonging to a group with IAMFullAccess, and LambdaFullAccess
-
Using that role, run:
aws iam create-role --role-name lambda-basic-execution --assume-role-policy-document '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}'
- Then:
aws iam attach-role-policy \
--role-name lambda-basic-execution \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- Zip the build
zip -r9 -j bootstrap.zip ./target/x86_64-unknown-linux-musl/release/bootstrap
- Deploy to a new function
AWS_ACCOUNT_ID=`aws sts get-caller-identity --query Account --output text` && \
aws lambda create-function \
--function-name darkowl \
--runtime provided \
--role arn:aws:iam::$AWS_ACCOUNT_ID:role/lambda-basic-execution \
--zip-file fileb://bootstrap.zip \
--description "Simple Rust function" \
--timeout 5 \
--handler main
-
Rebuild
-
Rezip
-
Run:
aws lambda update-function-code --function-name darkowl --zip-file fileb://bootstrap.zip
- Add the Cloudwatch rule
aws events put-rule \
--name darkowl-schedule \
--schedule-expression 'rate(5 minutes)'
- Give the rule permission to invoke your function
aws lambda add-permission \
--function-name darkowl \
--statement-id my-scheduled-event \
--action 'lambda:InvokeFunction' \
--principal events.amazonaws.com \
--source-arn {your_rule_arn}
- Create a
targets.json
file for the rule
[
{
"Id": "1",
"Arn": "{your_lambda_arn}"
}
]
- Add the targets to your rule
aws events put-targets --rule darkowl-schedule --targets file://targets.json