Start your car from an AWS Lambda.
This tutorial does not cover authentication. You will need to keep your URL secret in order to avoid others from starting your car!! The minimum viable security for this tutorial is to require that a user pass a specific, secret header string or GET param. Do not leave this endpoint open to the world after completing this tutorial.
This is an example script that uses PyMyFord/prefect from an AWS Lambda running Python 3 to start a car remotely.
You can either clone this repository or copy the code into your own handler.py
. You will also need to create a new virtual environment:
$ virtualenv v-env
Once you have the following in your directory, you can zip it into a .zip file to upload to AWS Lambda.
$ ls
v-env/
handler.py
Zip the files like this:
$ zip -r9q prefect-lambda *
Navigate to the AWS lambda console and click on "Create Function":
Select the option to 'author the lambda from scratch.' Set the runtime to Python 3 (the latest as of writing is 3.8). The other default settings are fine for now:
Scroll to "Function Code" and select "upload a .zip". Upload the .zip you created in the previous step.
You can also use the AWS command line utility (aws
) to do this:
$ AWS_PROFILE=personal aws lambda update-function-code --function-name my-prefect-lambda --zip-file fileb://prefect-lambda.zip
Under "handler" in the lambda web console, set the handler function to handler.handler
. This will run the handler
function inside the handler.py
file. (main.foo
will run the function foo()
in main.py
when the API is called, for another example.)
You must set prefectuser
and prefectpass
in the Environment Variables section of the console.
Scroll to the "Designer" section of the lambda page and click "Add Trigger":
Select "API Gateway" from the dropdown:
Create a new API Gateway:
Press "Add".
Back on the lambda function screen, under the Designer section, click on the API Gateway to highlight it (it will turn light blue). The next section will become an API Gateway inspector, and will show you your new API Gateway trigger URL. Note this, but protect it: This tutorial does not cover authentication, so anyone with this URL will be able to start your car!
$ curl https://[your_api_gateway_url].execute-api.us-east-1.amazonaws.com/[your_extension]