{"id":13579,"date":"2016-03-29T19:00:22","date_gmt":"2016-03-29T15:00:22","guid":{"rendered":"http:\/\/www.msp360.com\/blog\/?p=13579"},"modified":"2023-11-02T17:25:17","modified_gmt":"2023-11-02T13:25:17","slug":"how-to-automatically-invalidate-dynamic-objects-in-amazon-cloudfront-using-aws-lambda","status":"publish","type":"post","link":"https:\/\/www.msp360.com\/resources\/blog\/how-to-automatically-invalidate-dynamic-objects-in-amazon-cloudfront-using-aws-lambda\/","title":{"rendered":"How to Automatically Invalidate Dynamic Objects in Amazon CloudFront Using AWS Lambda"},"content":{"rendered":"<p>Learn how to leverage Amazon Lambda functionality to automate object invalidation from this step-by-step guide.<\/p>\n<p><!--more--><\/p>\n<p>If you use Amazon CloudFront for content delivery, the webmaster can set up time frames, in which object in its exact state will be available for users. When this Time-To-Live (TTL) frame elapses, the network consults the origin server and replaces cached copy with the new version. Big TTL allows better performance, while doesn\u2019t allow the webmaster to serve frequently updated content at once. Small TTL in its turn allows to serve updated content faster but reduces page performance (due to multiple cache misses). The solution is to invalidate cached objects in CloudFront edge locations manually (which can consume a lot of time) or automate the process.<\/p>\n<h2>How to Create an Amazon Lambda Function to Invalidate Objects in Amazon CloudFront<\/h2>\n<ul>\n<li>In <a href=\"https:\/\/console.aws.amazon.com\/console\/home\" target=\"_blank\" rel=\"noopener noreferrer\">AWS Console<\/a> go to <b>AWS Lambda<\/b>, choose <b>Create a Lambda function<\/b>, it will show you a set of predefined examples. Skip it and specify a name for your function.\u00a0Choose <b>Python 2.7<\/b> in the <b>Runtime <\/b>list box.<\/li>\n<\/ul>\n<blockquote><p>You can use any other language supported by the service. We are using Python just to demonstrate the concept.<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-13609 aligncenter\" src=\"https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/AWS-Lambda-function-code-entry-example.png\" alt=\"AWS-Lambda-function-code-entry example\" width=\"945\" height=\"515\" srcset=\"https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/AWS-Lambda-function-code-entry-example.png 945w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/AWS-Lambda-function-code-entry-example-300x163.png 300w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/AWS-Lambda-function-code-entry-example-768x419.png 768w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/AWS-Lambda-function-code-entry-example-624x340.png 624w\" sizes=\"auto, (max-width: 945px) 100vw, 945px\" \/><\/p>\n<ul>\n<li><b>Copy and paste<\/b> the following code to the AWS Lambda function code:<\/li>\n<\/ul>\n<pre>from __future__ import print_function\r\n\r\nimport boto3\r\nimport time\r\n\r\ndef lambda_handler(event, context):\r\n    for items in event[\"Records\"]:\r\n        path = \"\/\" + items[\"s3\"][\"object\"][\"key\"]\r\n    print(path)\r\n    client = boto3.client('cloudfront')\r\n    invalidation = client.create_invalidation(DistributionId='E3U10PP27IQTKJ',\r\n        InvalidationBatch={\r\n            'Paths': {\r\n                'Quantity': 1,\r\n                'Items': [path]\r\n        },\r\n        'CallerReference': str(time.time())\r\n    })\r\n<\/pre>\n<ul>\n<li>Define the permissions for your function by choosing <b>Basic Execution<\/b> IAM role in the <b>Role <\/b>list box. Use list permissions below to successfully execute the script.<\/li>\n<\/ul>\n<pre>{\r\n  \"Version\": \"2012-10-17\",\r\n  \"Statement\": [\r\n    {\r\n      \"Effect\": \"Allow\",\r\n      \"Action\": [\r\n        \"logs:CreateLogGroup\",\r\n        \"logs:CreateLogStream\",\r\n        \"logs:PutLogEvents\"\r\n      ],\r\n      \"Resource\": \"arn:aws:logs:*:*:*\"\r\n    },\r\n\t{\r\n        \"Effect\": \"Allow\",\r\n        \"Action\": [\r\n            \"cloudfront:CreateInvalidation\"\r\n        ],\r\n        \"Resource\": [\r\n            \"*\"\r\n        ]\r\n    }\r\n  ]\r\n}\r\n<\/pre>\n<ul>\n<li>In <b>Advanced settings <\/b>choose the amount of memory should be dedicated for processing the code. Keep it <b>128 MB<\/b>, as it\u2019s enough.<\/li>\n<\/ul>\n<p id=\"last\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13610\" src=\"https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/Configuring-Lambda-function-IAM-role-and-timeout.png\" alt=\"Configuring-Lambda-function-IAM-role-and-timeout\" width=\"923\" height=\"398\" srcset=\"https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/Configuring-Lambda-function-IAM-role-and-timeout.png 923w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/Configuring-Lambda-function-IAM-role-and-timeout-300x129.png 300w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/Configuring-Lambda-function-IAM-role-and-timeout-768x331.png 768w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/Configuring-Lambda-function-IAM-role-and-timeout-624x269.png 624w\" sizes=\"auto, (max-width: 923px) 100vw, 923px\" \/><\/p>\n<div id=\"slidebox\"><a class=\"close\"> <\/a><!--HubSpot Call-to-Action Code --><span class=\"hs-cta-wrapper hs-cta-deferred\" id=\"hs-cta-wrapper-aa07fdb8-7776-46a5-9fa0-ec6e93f0f0a6\" data-portal=\"5442029\" data-id=\"aa07fdb8-7776-46a5-9fa0-ec6e93f0f0a6\"><span class=\"hs-cta-node hs-cta-aa07fdb8-7776-46a5-9fa0-ec6e93f0f0a6\" id=\"hs-cta-aa07fdb8-7776-46a5-9fa0-ec6e93f0f0a6\"><!--[if lte IE 8]><div id=\"hs-cta-ie-element\"><\/div><![endif]--><a href=\"https:\/\/cta-redirect.hubspot.com\/cta\/redirect\/5442029\/aa07fdb8-7776-46a5-9fa0-ec6e93f0f0a6\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"hs-cta-img\" id=\"hs-cta-img-aa07fdb8-7776-46a5-9fa0-ec6e93f0f0a6\" style=\"border-width:0px;\" src=\"https:\/\/no-cache.hubspot.com\/cta\/default\/5442029\/aa07fdb8-7776-46a5-9fa0-ec6e93f0f0a6.png\" alt=\"CTA\"><\/a><\/span><\/span><!-- end HubSpot Call-to-Action Code --><\/div>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Increase a <b>Timeout<\/b> for a function, if you have a lot of objects that need invalidation<\/li>\n<li>Press <b>Next<\/b> and choose the\u00a0<b>Create function<\/b><\/li>\n<li>In the function edit wizard, go to <b>Event<\/b> <b>sources <\/b>and press <b>Add event source<\/b><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-13607 aligncenter\" src=\"https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-event-sources-configuration.png\" alt=\"cloudfront-invalidation-aws-lambda-event-sources-configuration\" width=\"600\" height=\"450\" srcset=\"https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-event-sources-configuration.png 692w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-event-sources-configuration-300x225.png 300w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-event-sources-configuration-624x468.png 624w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<ul>\n<li><b>Choose<\/b> <b>S3 <\/b>as your Event source, then choose your origin bucket and in the <b>Event type<\/b>, choose <b>Object Created (All)<\/b> and press <b>Submit<\/b><\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-13611 aligncenter\" src=\"https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-add-event-source-configuration.png\" alt=\"cloudfront-invalidation-aws-lambda-add-event-source-configuration\" width=\"553\" height=\"433\" srcset=\"https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-add-event-source-configuration.png 814w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-add-event-source-configuration-300x235.png 300w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-add-event-source-configuration-768x601.png 768w, https:\/\/www.msp360.com\/resources\/wp-content\/uploads\/2016\/03\/cloudfront-invalidation-aws-lambda-add-event-source-configuration-624x488.png 624w\" sizes=\"auto, (max-width: 553px) 100vw, 553px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>Now you\u2019ve enabled an AWS Lambda function that invalidates objects in Amazon CloudFront edge locations and synchronizes objects automatically. In practice this means the object will be delivered to the end-user as soon as it is uploaded to the origin server, saving a lot of time for a webmaster.<\/p>\n<p>Use <a href=\"https:\/\/www.msp360.com\/explorer\/windows\/\">CloudBerry Explorer for Amazon S3<\/a> to ensure the success of object invalidation by viewing the CloudFront Invalidation List and also to invalidate objects in non-S3 origin distributions.<\/p>\n<p>Know how to improve AWS CloudFront invalidation or our solution? Feel free to share your thoughts in the comments section below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to leverage Amazon Lambda functionality to automate object invalidation from this step-by-step guide.<\/p>\n","protected":false},"author":2,"featured_media":56475,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[877,885],"tags":[],"class_list":["post-13579","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-articles","category-other"],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/posts\/13579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/comments?post=13579"}],"version-history":[{"count":4,"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/posts\/13579\/revisions"}],"predecessor-version":[{"id":56474,"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/posts\/13579\/revisions\/56474"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/media\/56475"}],"wp:attachment":[{"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/media?parent=13579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/categories?post=13579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.msp360.com\/resources\/wp-json\/wp\/v2\/tags?post=13579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}