Skip to content

clalexander/rent-peek-lambda-api

Repository files navigation

rent-peek-lambda-api

Description

Lambda + API Gateway handler for a rent peek email function. Rent peek generation provided by client, modified to work on Lambda.

How to Use

Get Invoke URL

Get the apiGatewayInvokeURL output value from the Lambda API Gateway stack outputs. (See 'Setup' below for stack instructions.)

Request

{
  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

Success Response

Status code 201

{
  "message": "Success"
}

Bad Request Response

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.

Setup

Step 0: Development Environment (Local Machine)

Install python and git if not already installed.

References:

Set up repository:

> git clone <repo> [<dir>]
> cd <dir>

Optional: Create virtual environment

# 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

Optional: Install requirements

# linux/mac
> python3 -m pip install -r requirements.txt

# windows
> python -m pip install -r requirements.txt

Step 1: Archive Lambda Code and Dependencies

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

Step 2: Add Email Credentials Secret

From the AWS Console:

  1. Navigate to AWS Secrets Manager
  2. Click "Store a new secret"
  3. Select "Other type of secret"
  4. Enter "user" as the first key and the smtp email as the first value
  5. Click "+ Add row" to add another key/value pair
  6. Enter "pw" as the key and the smtp password as the value
  7. Click "Next"
  8. Enter any name for the secret name (ex. "prod/RentPeek/EmailCreds") and take note of this name
  9. Click "Next"
  10. Click "Next" again
  11. Click "Store"

Step 3: Create Buckets Stack

From the AWS Console:

  1. Navigate to CloudFormation
  2. Click "Create stack"
  3. Under "Specify template" click "Upload a template file"
  4. Click "Choose file" and select the stacks/buckets.yml file from your project
  5. Click "Next"
  6. Enter a stack name (ex. "RentPeekBuckets")
  7. Enter stack parameters
  8. Click "Next"
  9. Click "Next" again
  10. Click "Submit"

Step 4: Add Resources to New Bucket

From the AWS Console:

  1. Navigate to S3
  2. Open the newly created bucket
  3. Create two new folders: "archives" and "data"
  4. In the "archives" folder, upload the lambda.zip and lambda_deps.zip archives
  5. In the "data" folder, upload the rental_data.csv and zipcode_coverage.zip files
  6. Take note of the names of all the uploaded files

Step 5: Create Lambda API Gateway Stack

From the AWS Console:

  1. Navigate to CloudFormation
  2. Click "Create stack"
  3. Under "Specify template" click "Upload a template file"
  4. Click "Choose file" and select the stacks/lambda-api-gateway.yml file from your project
  5. Click "Next"
  6. Enter a stack name (ex. "RentPeekLambdaAPIGateway")
  7. Enter stack parameters, entering correct names for the bucket, bucket resource keys, and secret name
  8. Click "Next"
  9. Click "Next" again
  10. Check "I acknowledge that AWS CloudFormation might create IAM resources."
  11. Click "Submit"
  12. Wait until CloudFormation completes adding the resources
  13. Click the "Outputs" tab
  14. Copy the apiGatewayInvokeURL output value; this is your invoke URL

Step 6: Test Endpoint

Follow usage instructions above to test the endpoint.

How to Update Resources

Updating S3 Reources

This works for both data and archive resources.

From the AWS Console:

  1. Navigate to S3
  2. Click the associated bucket
  3. Navigate to the parent folder of the resource.
  4. Upload the new file

Updating Lambda Code

Option 1

Best way so as not to accidentally lose code.

  1. Update the code in the repository
  2. Create a new lambda archive (Setup Step 1)
  3. Update the lambda archive in S3 (above)
  4. Copy the "Object URL"
  5. Navigate to AWS Lambda
  6. Select the Lambda function
  7. From the Code tab, click "Upload from"
  8. Select "Amazon S3 location"
  9. Enter the object URL
  10. Click "Save"

Option 2

Good for quick testing, but be sure to update the repository when done.

  1. Navigate to AWS Lambda
  2. Select the Lambda function
  3. From the Code tab, select the file to edit and edit in place
  4. Click "Deploy" when ready

Updating Dependencies

  1. Update requirements.txt in the repository
  2. Create a new dependencies archive (Setup Step 1)
  3. Update the dependencies archive in S3 (above)
  4. Copy the "Object URL"
  5. Navigate to AWS Lambda
  6. Select "Layers" (on the left)
  7. Select the layer
  8. Click "Create version"
  9. Select "Upload a file from "Amazon S3"
  10. Enter the object URL
  11. Click "Create"
  12. Wait until create process is complete
  13. Navigate to the Lambda function
  14. From the Code tab at the bottom of the page in the Layers section, click "Edit"
  15. Select the most recent layer version
  16. Click "Save"

Updating CloudFormation Stacks

From the AWS Console:

  1. Navigate to CloudFormation
  2. Select the stack you want to update
  3. Click "Update"
  4. Follow the prompts to update the stack

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published