We have a client’s website hosted on AWS infrastructure.The client is a mass media company with presence all over the world. When we tested the website page on PageSpeed Insight Google’s platform https://developers.google.com/speed/pagespeed/insights/, we found that there were some cacheable resources which had no expiration set for the browser, which was a factor for increasing page load time.
Hence the tool was suggesting to Leverage browser caching which would minimise the page load time.
The website was being delivered through CloudFront. When browser caching has to be set, the request should come from the origin headers. In our case the Origin for the static assets was S3 bucket. The header should come with a value set for max-age for Cache-control parameter.
When you have a lot of static content residing within a S3 bucket and you want to have some parameter being set as part of the object metadata, each time updating it would be a manual task. When files to be managed are large in number updating metadata with each upload becomes tedious.
The requirement was to set the following:
S3→ bucket_name→ Object_name → Properties→ Metadata → Add more metadata
Key : Cache-Control Value : max-age = 604800
One work around for above problem could be using Amazon Lambda.
Let’s have a look at how lambda works :
Implementation for above challenge using Lambda :
- Sign-in to your AWS account. Navigate to Compute→ Lambda
- Create a new Lambda function. First add a trigger for your function. Lambda can be integrated with AWS services as shown in image below. As per the trigger event choose from the services and proceed for the further configurations.I would choose S3.
- Specify the bucket name for which metadata to object has to be added. Choose the action for example Creation of object in my case. We can configure the path for which objects the function should execute along with the extension
- Upload function to Lambda . Lambda supports code written in Node.js (JavaScript), Python, and Java (Java 8 compatible)
- Select the Handler and a role. Provide the role with required permissions.
- Review and Create the function.
Testing :
- Upload a file to the S3 bucket which acts as trigger for Lambda
- After Upload check the metadata added.
S3→ Bucket_name → Properties → Metadata
Logs & Graphs :
- Navigate to CloudWatch → Logs → /aws/lambda/your_Lambda_Function_name
- The summary Lambda execution is present within the logs.
More Use Cases for Lambda :
- Log Analysis
- Serverless Websites
- Automating Tasks like backups
- Data transformations/conversions on the fly
Potential Benefits:
- Serverless
- Event-driven
- Sub-second Billing
- Completely Automated Administration
- Built-in Fault Tolerance
Conclusion :
Lambda is a service which can be use for event driven actions. You pay only for the compute time you use. You can run your piece of code from any type of back-end service with fully automated work-flow. We were able to automate the process to add/update the metadata to objects uploaded.Now the client just needs to upload the objects and Lambda takes care of the latter part without any additional manual work.
Resources :
For more resources visit :
Nice blog. You have provided such a useful information in this blog. Thanks for sharing.
ReplyDeleteAWS Training
AWS Online Training
Amazon Web Services Online Training
AWS Training in Hyderabad
AWS Training in Ameerpet