This script will leverage AWS lambda to arm and disarm your Blink Camera system with an AWS IoT Button.
All secrets are stored in AWS secrets manager (thanks @eXodus1440])
- One Press - Arm
- Two Presses - Disarm
Optionally, the lambda function will send a notification to a Slack Webhook url of your choice.
This is a SAM template for the lambda-blink function - Below is a brief explanation of what each item is:
.
├── README.md <-- This instructions file
├── blink <-- Source code for the lambda function
│ ├── __init__.py
│ ├── main.py <-- Lambda function code
│ ├── requirements.txt <-- Lambda function code
└── template.yaml <-- SAM Template file
- AWS CLI already configured with appropriate permission
- Python 3 installed
NOTE: If using slack to send an update follow the instructions here to create a url and pass the webhook url in the command below.
Otherwise, delete the last line.
aws cloudformation deploy \
--template-file secret-manager.yaml \
--stack-name blink-secret-cf \
--parameter-overrides \
BlinkUser=REPLACE_WITH_YOUR_BLINK_USERNAME \
BlinkPassword=REPLACE_WITH_YOUR_BLINK_PASSWORD \
SlackUrl=REPLACE WITH YOUR SLACK WEBHOOK URL OR DELETE LINE IF NOT USING SLACK
After deployment is complete you can run the following command to retrieve the Secret ARN, this will be referenced by the SAM template:
aws cloudformation describe-stacks \
--stack-name blink-secret-cf \
--query "Stacks[].Outputs[?OutputKey=='BlinkSecretARN']" \
--output table
The AWS Lambda Python runtime requires a flat folder with all dependencies including the application. SAM will use the CodeUri
property to know where to look for both application and dependencies:
...
BlinkFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: blink/
...
Firstly, we need an S3 bucket
where SAM can upload the Lambda function packaged as a ZIP file before we deploy anything - If you don't have an S3 bucket to store code artifacts then this is a good time to create one:
aws s3 mb s3://BUCKET_NAME
Next, run the following command to build and package the Lambda function to S3:
sam build && \
sam package \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_WITH_YOUR_S3_BUCKET_NAME
Next, the following command will create a Cloudformation Stack and deploy the SAM resources.
sam deploy \
--template-file packaged.yaml \
--stack-name lambda-blink-cf \
--parameter-overrides ButtonDSN='REPLACE_WITH_YOUR_BUTTON_DSN' \
--capabilities CAPABILITY_IAM
See Serverless Application Model (SAM) HOWTO Guide for more details in how to get started.
After deployment is complete you can run the following command to retrieve the function ARN:
aws cloudformation describe-stacks \
--stack-name lambda-blink-cf \
--query "Stacks[].Outputs[?OutputKey=='BlinkFunction']" \
--output table
To simplify troubleshooting, SAM CLI has a command called sam logs. sam logs lets you fetch logs generated by the Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find bugs.
NOTE
: This command works for all AWS Lambda functions; not just the ones you deploy using SAM.
sam logs -n BlinkFunction --stack-name lambda-blink-cf --tail
You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.
In order to delete the Serverless Application recently deployed you can use the following AWS CLI Command:
aws cloudformation delete-stack --stack-name lambda-blink-cf
aws cloudformation delete-stack --stack-name blink-secret-cf
Here are a few things you can try to get more acquainted with building serverless applications using SAM:
- Uncomment lines on
app.py
- Build the project with
sam build --use-container
- Invoke with
sam local invoke BlinkFunction --event event.json
- Update tests
- Enable step-through debugging docs for supported runtimes
Next, you can use AWS Serverless Application Repository to deploy ready to use Apps and learn how authors developed their applications: AWS Serverless Application Repository main page
Started with altonplace's lambda-blink script, added support for storing username & password via SSM Secrets, and wrapped up inside the AWS SAM wrapper.
Blink API details were referenced from MattTW's BlinkMonitorProtocol repo.
AWS Lambda requires a flat folder with the application as well as its dependencies in the deployment package. When you make changes to your source code or dependency manifest, run the following command to build your project locally:
sam build
If your dependencies contain native modules that need to be compiled specifically for the operating system running on AWS Lambda, use this command to build inside a Lambda-like Docker container instead:
sam build --use-container
By default, this command writes built artifacts to .aws-sam/build
folder.
All commands used throughout this document
# Deploy Blink SSM Secrets template as a prerequisite
aws cloudformation deploy \
--template-file secret-manager.yaml \
--stack-name blink-secret-cf \
--parameter-overrides \
BlinkUser=REPLACE_WITH_YOUR_BLINK_USERNAME \
BlinkPassword=REPLACE_WITH_YOUR_BLINK_PASSWORD \
SlackUrl=REPLACE WITH YOUR SLACK WEBHOOK URL OR DELETE LINE IF NOT USING SLACK
# Describe Output section of CloudFormation stack previously created
aws cloudformation describe-stacks \
--stack-name blink-secret-cf \
--query "Stacks[].Outputs[?OutputKey=='BlinkSecretARN']" \
--output table
# Create S3 bucket
aws s3 mb s3://BUCKET_NAME
# Build Lambda function locally
sam build
# Package Lambda function defined locally and upload to S3 as an artifact
sam package \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
# Deploy SAM template as a CloudFormation stack
sam deploy \
--template-file packaged.yaml \
--stack-name lambda-blink-cf \
--parameter-overrides ButtonDSN=REPLACE_WITH_YOUR_BUTTON_DSN \
--capabilities CAPABILITY_IAM
# Describe Output section of CloudFormation stack previously created
aws cloudformation describe-stacks \
--stack-name lambda-blink-cf \
--query "Stacks[].Outputs[?OutputKey=='BlinkFunction']" \
--output table
# Tail Lambda function Logs using Logical name defined in SAM Template
sam logs -n BlinkFunction --stack-name lambda-blink-cf --tail