Spaces:
Sleeping
Sleeping
Here's an example of how you can deploy an AWS Lambda function using CloudFormation. This Lambda function will be triggered when an object is created in a specified S3 bucket, and it will convert all uploaded files to PDF format: | |
1. **Template Preparation:** | |
- Create a new directory on your local machine where you will store the template file and Lambda function code. | |
- Write a Python code for the Lambda function that converts files to PDF. Here's a simple example using the `python-pdf` library: | |
```python | |
import boto3 | |
import pdfkit | |
s3 = boto3.client('s3') | |
def convert_to_pdf(event, context): | |
# Extract file name and bucket name from the event | |
bucket_name = event['Records'][0]['s3']['bucket']['name'] | |
file_name = event['Records'][0]['s3']['object']['key'] | |
# Download the file from S3 | |
response = s3.get_object(Bucket=bucket_name, Key=file_name) | |
original_file_data = response['Body'].read() | |
# Convert the file to PDF | |
pdf_file = pdfkit.from_string(original_file_data) | |
pdf_filename = file_name + '.pdf' | |
# Upload the PDF back to S3 | |
upload_response = s3.put_object( | |
Body=pdf_file, | |
Bucket= |