Skip to content

Alfonso-Corona/aws-lambda-functions-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

CSV Validation Lambda Function (billing-bucket-parser.py)

This Lambda function validates CSV files uploaded to an S3 bucket and moves erroneous files to an error bucket.

📝 Purpose

  • Validation Checks:
    • Validates product_line (Bakery, Meat, Dairy).
    • Validates currency (USD, MXN, CAD).
    • Checks date format (YYYY-MM-DD).
  • Error Handling:
    • Moves files with errors to a designated error bucket.
    • Deletes the original file after moving.

⚙️ Setup

  1. Buckets:

    • Primary Bucket: Upload CSV files here.
    • Error Bucket: Stores files with validation errors (configured as billing-test-error-111 in the code).
  2. Trigger:

    • Configure an S3 PUT event trigger on the primary bucket to invoke this Lambda.
  3. Permissions:

    • Ensure the Lambda has IAM permissions to read/write/delete from both S3 buckets.

🔄 Workflow

  1. A CSV is uploaded to the primary bucket.
  2. The Lambda checks each row for valid data.
  3. If errors are found:
    • File is copied to the error bucket.
    • Original file is deleted.
  4. Returns a 200 status if no errors are detected.

💡 Note: Update the error bucket variable in the code if renaming the error bucket.


CSV to DynamoDB Lambda Function (billing-data-conversion.csv)

This Lambda processes CSV files from an S3 bucket, converts billing amounts to USD, and stores the data in DynamoDB.

🎯 Purpose

  • Process CSV Data: Reads CSV files uploaded to S3.
  • Currency Conversion: Converts bill_amount to USD using hardcoded rates for CAD, MXN, and USD.
  • DynamoDB Storage: Inserts processed records into a DynamoDB table (dynamo-billing).

⚙️ Setup

Prerequisites

  1. S3 Bucket: Configured to trigger this Lambda on PUT events (CSV uploads).
  2. DynamoDB Table:
    • Name: dynamo-billing (in region us-east-1).
    • Required Attributes:
      {
        "id": "Number",
        "company_name": "String",
        "country": "String",
        "city": "String",
        "product_line": "String",
        "item": "String",
        "bill_date": "String",
        "currency": "String",
        "bill_amount": "String",
        "usd_amount": "String"
      }
  3. IAM Permissions:
    • Lambda must have access to:
      • Read from the S3 bucket.
      • Write to DynamoDB (dynamo-billing).

📂 CSV Format Requirements

Columns must include (in order):

id, company_name, country, city, product_line, item, bill_date, currency, bill_amount

🔄 Workflow

  1. Trigger: CSV file uploaded to S3.
  2. Processing:
    • Converts bill_amount to USD using predefined rates.
    • Skips the CSV header row.
  3. Storage: Each row is inserted into DynamoDB.
  4. Output: Prints success/errors to CloudWatch logs.

⚠️ Notes

  • Currency Support: Only USD, CAD, and MXN are supported. Unsupported currencies will log an error but still insert data with usd_amount=0.
  • Hardcoded Rates: Update currency_conversion_usd in the code if rates change.
  • Error Handling: Errors during DynamoDB insertion are logged but do not halt execution.

📝 Example Output

SUCCESS <=======  # Per successful insert
LAMBDA HAS FINISHED <<<<-------  # Final completion message

EC2 Snapshot Creator Lambda Function

This Lambda function automates the creation of EBS volume snapshots for EC2 instances and tags them with the creation date.


🎯 Purpose

  • Automated Snapshots: Creates a snapshot of a specified EBS volume.
  • Tagging: Adds a Name tag with the snapshot date (e.g., My EC2 Snapshot-2023-10-01).
  • Error Handling: Returns success/error responses for debugging.

⚙️ Setup

Prerequisites

  1. Volume ID: Replace the hardcoded vol-025ee49cd5e5db665 with your target EBS volume ID.
  2. IAM Permissions:
    • The Lambda execution role must include:
      • ec2:CreateSnapshot (to create snapshots).
      • ec2:CreateTags (to tag snapshots).
  3. Trigger: Configure a trigger (e.g., CloudWatch Events for scheduled snapshots).

🔄 Workflow

  1. Trigger: Lambda is invoked (manually or via a trigger).
  2. Snapshot Creation:
    • Creates a snapshot of the specified EBS volume.
    • Tags it with a Name containing the current date.
  3. Response:
    • Returns 200 with snapshot details on success.
    • Returns 400 with error details on failure.

📝 Example Response

Success:

{  
  "statusCode": 200,  
  "body": "{'SnapshotId': 'snap-123abc...', 'StartTime': '2023-10-01...'}"  
}  

Error:

{  
  "statusCode": 400,  
  "body": "{'error': 'VolumeInUse: Volume is attached to an instance'}"  
}  

⚠️ Notes

  • Hardcoded Volume ID: Update VolumeId in the code to match your EBS volume.
  • Tagging: Snapshots are tagged for easier identification and cost tracking.
  • Logging: Uncomment the logger lines to enable detailed CloudWatch logging.

📌 Recommended Use Cases

  • Daily/weekly backup schedules.
  • Pre-upgrade/system migration backups.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages