Lambda + API Gateway handler for a rent peek email function. Rent peek generation provided by client, modified to work on Lambda.
Get the apiGatewayInvokeURL
output value from the Lambda API Gateway stack outputs. (See 'Setup' below for stack instructions.)
{
url: '<invokeURL>',
method: 'POST', // or other method if configured differently
data: {
zipcode: 12345, // int
rent: 1234, // int
beds: 1, // int
baths: 1.5, // float
email: '[email protected]' // string
}
}
** Note: request body requires types as indicated above
Status code 201
{
"message": "Success"
}
Status code 400
{
"message": "<Error message>"
}
Error message may be a validation error or may be an internal error if there is a bad configuration.
Install python
and git
if not already installed.
References:
- https://rogerdudler.github.io/git-guide/
- https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent?platform=windows
> git clone <repo> [<dir>]
> cd <dir>
# https://docs.python.org/3/library/venv.html
# linux/mac
> python3 -m venv .venv
> source .venv/bin/activate
# windows
> python -m venv .venv
> .venv\Scripts\activate
# linux/mac
> python3 -m pip install -r requirements.txt
# windows
> python -m pip install -r requirements.txt
Create archives of the lambda code and the dependencies layer code. (Skip this step if you have been provided with the archives.)
Linux/mac
# pack lambda archive
> scripts/pack.sh
# ...
# output: dist/lambda.zip
# pack dependencies archive
# installs dependencies in folder for use in lambda layer
> scripts/pack_deps.sh
# ...
# output dist/lambda_deps.zip
Windows
# pack lambda archive
> scripts/pack.bat
# ...
# output: dist/lambda.zip
# pack dependencies archive
# installs dependencies in folder for use in lambda layer
> scripts/pack_deps.bat
# ...
# output dist/lambda_deps.zip
From the AWS Console:
- Navigate to AWS Secrets Manager
- Click "Store a new secret"
- Select "Other type of secret"
- Enter "user" as the first key and the smtp email as the first value
- Click "+ Add row" to add another key/value pair
- Enter "pw" as the key and the smtp password as the value
- Click "Next"
- Enter any name for the secret name (ex. "prod/RentPeek/EmailCreds") and take note of this name
- Click "Next"
- Click "Next" again
- Click "Store"
From the AWS Console:
- Navigate to CloudFormation
- Click "Create stack"
- Under "Specify template" click "Upload a template file"
- Click "Choose file" and select the
stacks/buckets.yml
file from your project - Click "Next"
- Enter a stack name (ex. "RentPeekBuckets")
- Enter stack parameters
- Click "Next"
- Click "Next" again
- Click "Submit"
From the AWS Console:
- Navigate to S3
- Open the newly created bucket
- Create two new folders: "archives" and "data"
- In the "archives" folder, upload the
lambda.zip
andlambda_deps.zip
archives - In the "data" folder, upload the
rental_data.csv
andzipcode_coverage.zip
files - Take note of the names of all the uploaded files
From the AWS Console:
- Navigate to CloudFormation
- Click "Create stack"
- Under "Specify template" click "Upload a template file"
- Click "Choose file" and select the
stacks/lambda-api-gateway.yml
file from your project - Click "Next"
- Enter a stack name (ex. "RentPeekLambdaAPIGateway")
- Enter stack parameters, entering correct names for the bucket, bucket resource keys, and secret name
- Click "Next"
- Click "Next" again
- Check "I acknowledge that AWS CloudFormation might create IAM resources."
- Click "Submit"
- Wait until CloudFormation completes adding the resources
- Click the "Outputs" tab
- Copy the
apiGatewayInvokeURL
output value; this is your invoke URL
Follow usage instructions above to test the endpoint.
This works for both data and archive resources.
From the AWS Console:
- Navigate to S3
- Click the associated bucket
- Navigate to the parent folder of the resource.
- Upload the new file
Best way so as not to accidentally lose code.
- Update the code in the repository
- Create a new lambda archive (Setup Step 1)
- Update the lambda archive in S3 (above)
- Copy the "Object URL"
- Navigate to AWS Lambda
- Select the Lambda function
- From the Code tab, click "Upload from"
- Select "Amazon S3 location"
- Enter the object URL
- Click "Save"
Good for quick testing, but be sure to update the repository when done.
- Navigate to AWS Lambda
- Select the Lambda function
- From the Code tab, select the file to edit and edit in place
- Click "Deploy" when ready
- Update
requirements.txt
in the repository - Create a new dependencies archive (Setup Step 1)
- Update the dependencies archive in S3 (above)
- Copy the "Object URL"
- Navigate to AWS Lambda
- Select "Layers" (on the left)
- Select the layer
- Click "Create version"
- Select "Upload a file from "Amazon S3"
- Enter the object URL
- Click "Create"
- Wait until create process is complete
- Navigate to the Lambda function
- From the Code tab at the bottom of the page in the Layers section, click "Edit"
- Select the most recent layer version
- Click "Save"
From the AWS Console:
- Navigate to CloudFormation
- Select the stack you want to update
- Click "Update"
- Follow the prompts to update the stack