Description: Demo of things Joe learned while messing around with Lambdas
- We needed a way to provision certificates in the new AWS account
- We didn't have Rundeck to help us out
- There would be more overhead and cost to exploring ECS or EKS
- Lambdas are usually cheap to run
- We already have lambdas in production, but no one is currently supporting/owning them from a standardization and deployment perspective
- Install awscli and configure an account named
lambda-demo
with the default regionus-east-1
:aws configure --profile lambda-demo
- Install Docker
- Install Terraform
- Make sure you have access to the docker-python-lambda-builder repo before starting
- Start 80s Synthwave Music:
make start-music
- Docker was necessary because the AWS documentation wants you to provision an Amazon Linux EC2 instance
- Instead I created docker-python-lambda-builder which will take an app and its dependencies and zip them
- Download/Build the lambda builder container:
make docker-init
- After the build has finished, package the lambda:
make lambda-build
- This will zip the application and dependencies
- Terraform will create the structure needed to support the lambda in AWS
- Lambda
- IAM Role
- IAM Policy
- Initialize Terraform:
make tf-init
- Use Terraform to provision new lambda:
make tf-apply
- You will be prompted to review and confirm the changes
- After terraform has completed, you can
invoke
the function and its handler:make lambda-invoke
- Think of the handler as a substitute of
main
in python
- Think of the handler as a substitute of
- You decide you want to make changes to your function, simply go to the
lambda
folder and editapp.py
- After making changes, rebuild the lambda packages, upload the lambda, and invoke the function:
make lambda-build lambda-upload lambda-invoke
ormake lambda-refresh
- Log into the AWS console for the account you specified in
awscli
- Navigate to the Lambda control panel
- Click on the function that you just created
demo_testservice_stg_useast1_example_com
- Here you can review the function code
- Note: If the function is deemed too big by AWS, you will not be able to use this IDE.
- You can also change the
handler
to thetest_handler
specified inapp.py
- Press the
Save
button on the top right corner, click the empty drop-down menu to the left of theTest
button and selectConfigure test event
- Give the
Event name
a name and then pressCreate
- You can now press the
Test
button - Review the
Execution results
in the console to see what happened with thetest_handler
- You can also see more robust logs by pressing the
Logs
link next to theExecution results
- Destroy lambda resources:
make tf-destroy
- Confirm deletion by submitting
yes
Sample Destroy Output
$ make tf-destroy
terraform destroy terraform/
data.template_file.lambda_assume_role: Refreshing state...
aws_iam_role.lambda_role: Refreshing state... (ID: demo_testservice_stg_useast1_example_com)
aws_lambda_function.lambda_demo: Refreshing state... (ID: demo_testservice_stg_useast1_example_com)
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
- aws_iam_role.lambda_role
- aws_lambda_function.lambda_demo
Plan: 0 to add, 0 to change, 2 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value: yes
aws_lambda_function.lambda_demo: Destroying... (ID: demo_testservice_stg_useast1_example_com)
aws_lambda_function.lambda_demo: Destruction complete after 5s
aws_iam_role.lambda_role: Destroying... (ID: demo_testservice_stg_useast1_example_com)
aws_iam_role.lambda_role: Still destroying... (ID: demo_testservice_stg_useast1_example_com, 10s elapsed)
aws_iam_role.lambda_role: Destruction complete after 11s
- Remove docker images:
make docker-cleanup
- Lambdas aren't always fun to debug
- Keep Lambdas small, don't implement more than a few functions
- Lambdas/Serverless won't fix all problems. It actually introduces new problems too
- There are a bunch more things that lambdas can do
- Interface with other AWS resources
- We didn't even touch on the API Gateway
- Deployment pipelines need to be figured out for each runtime (python/ruby/nodejs)
- Terraform is great for provisioning the resources, but not great for updating the lambda after initialization
- Lambdas are great for monitoring checks