A simple AWS Lambda function.
- Sign up for AWS Lambda
- Install and configure the
aws
command-line client - Create a
lambda-default
role - Install the
echo
function
Sign up for AWS here.
The Lambda free tier includes 1M free requests per month and 400,000 GB-seconds of compute time per month.
To install the aws
command-line client use pip
:
pip install awscli --upgrade --user
To configure aws
, follow these quick configuration steps.
Once configured, you should see config
and credentials
files in ~/.aws
.
The Lambda function will assume a role when executing and will be granted permissions based on the policies attached to this role.
At the very least, we need to create a basic execution role that grants write permissions to CloudWatch Logs. AWS provides one that we can use, arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
.
Use aws iam create-role
and aws iam attach-role-policy
to create the role and attach the desired policy as follows:
# Create the role
aws iam create-role \
--role-name lambda-default \
--assume-role-policy-document file://.aws/lambda-default-role-policy.json \
--description "Lambda execution role"
# Attach the policy to the role just created
aws iam attach-role-policy \
--role-name lambda-default \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Look up the role to confirm everything looks good, and make a note of the Arn
returned:
aws iam get-role \
--role-name lambda-default
Generate a .zip
of the source code in package.zip
:
# Generate package.zip
zip package echo.py
Copy package.zip
to an S3 bucket that you own:
# Copy package.zip to your S3 bucket
aws s3 cp package.zip s3://${YOUR_AWS_S3_BUCKET}/lambda/
Finally, create the function, remember to specify the Arn
of your lambda-default
role:
# Create your function
aws lambda create-function \
--publish \
--runtime python2.7 \
--role ${YOUR_ARN_LAMBDA_DEFAULT_ROLE} \
--handler echo.handler \
--function-name echo \
--code S3Bucket=${YOUR_AWS_S3_BUCKET},S3Key=lambda/package.zip
Invoke using aws lambda invoke
:
# Invoke the function and save the output to output.txt
aws lambda invoke --function-name echo --payload '["hello", "world"]' output.txt
Review the output via:
cat output.txt