1. 程式人生 > >Best Practices for Implementing Custom CloudFormation Resources with Lambda

Best Practices for Implementing Custom CloudFormation Resources with Lambda

When implementing Lambda-backed custom resources in your CloudFormation stack, consider the following best practices:

Build your custom resources to report, log, and handle failure gracefully

Exceptions can cause your function code to exit without sending a response. Because CloudFormation requires an HTTPS response to confirm whether the operation was a success or a failure, an unreported exception will cause CloudFormation to wait until the operation times out before starting a stack rollback. If the exception occurs again on rollback, CloudFormation will wait again for a timeout before ultimately ending in a rollback failure. During this time, your stack is unusable, and timeout issues can be time-consuming to troubleshoot.

To avoid this, make sure that your function's code has logic to handle exceptions, the ability to log the failure to help you troubleshoot, and if needed, the ability to respond back to CloudFormation with an HTTPS response confirming that an operation failed.

Set reasonable timeout periods, and report when they're about to be exceeded

If an operation doesn't execute within its defined timeout period, the function raises an exception and no response is sent to CloudFormation.

To avoid this, ensure that the timeout value for your Lambda functions is set high enough to handle variations in processing time and network conditions. Consider also setting a timer in your function to respond to CloudFormation with an error when a function is about to time out; this can help prevent function timeouts from causing custom resource timeouts and delays.

Understand and build around Create, Update, and Delete events

Depending on the stack action, CloudFormation sends your function a Create, Update, or Delete event. Each event is handled distinctively, so you should ensure that there are no unintended behaviors when any of the three event types is received.

Understand how CloudFormation identifies and replaces resources

When an update triggers replacement of a physical resource, CloudFormation compares the PhysicalResourceId returned by your Lambda function to the previous PhysicalResourceId; if the IDs differ, CloudFormation assumes the resource has been replaced with a new physical resource.

However, the old resource is not implicitly removed to allow a rollback if necessary. When the stack update is completed successfully, a Delete event request is sent with the old ID as an identifier. If the stack update fails and a rollback occurs, the new physical ID is sent in the Delete event.

With this in mind, returning a new PhysicalResourceId should be done with care, and delete events must consider the input PhysicalId to ensure that updates that require replacement are properly handled.

Make sure that your functions are designed with idempotency in mind

An idempotent function can be repeated any number of times with the same inputs, and the result will be the same as if it had been done only once. Idempotency is valuable when working with CloudFormation to ensure that retries, updates, and rollbacks don't cause the creation of duplicate resources, errors on rollback or delete, or other unintended effects.

For example, if CloudFormation invokes your function to create a resource, but doesn't receive a response that the resource was created successfully, CloudFormation might invoke the function again, resulting in the creation of a second resource; the first resource may become orphaned.

How to address this can differ depending on the action your function is intended to perform, but a common technique is to use a uniqueness token that CloudFormation can use to check for preexisting resources. For example, a hash of the StackId and LogicalResourceId could be stored in the resource's metadata or in a DynamoDB table.

Rollbacks

If a stack operation fails, CloudFormation attempts to roll back, reverting all resources to their prior state. This results in different behaviors depending on whether the update caused a resource replacement.

Ensuring that replacements are properly handled and the old resources are not implicitly removed until a delete event is received will help ensure that rollbacks are executed smoothly.

To help implement best practices when using custom resources, consider using the Custom Resource Helper provided by awslabs, which can assist with exception and timeout trapping, sending responses to CloudFormation, and logging.

相關推薦

Best Practices for Implementing Custom CloudFormation Resources with Lambda

When implementing Lambda-backed custom resources in your CloudFormation stack, consider the following best practices: Build your cus

Learn Best Practices for Securing Your Account and Resources

AWS offers a number of tools to help secure your account. Many of these measures are not active by default, and you must take direct action to

Fw: EPM 11.1.2.x – Planning/PBCS Best Practices for BSO Business Rule Optimisation

trigge rec oval sage depend opera manage 1.2 group 1. Introduction This document is intended to provide best practices for Business Rule

Best Practices for QML and Qt Quick

ins proto IT fault qmake scala simple text view Despite all of the benefits that QML and Qt Quick offer, they can be challenging in certa

轉錄組分析綜述A survey of best practices for RNA-seq data analysis

轉錄組分析綜述 轉錄組   文獻解讀   Trinity   cufflinks 轉錄組研究綜述文章解讀 今天介紹下小編最近閱讀的關於RNA-seq分析的文章,文章發在Genome Biology 上的A survey of

PBR最佳實踐(Best Practices For Physically Based Content Creation)

該視訊是Anton Hand在Unite 大會上做的分享,比較老的視訊了,但是PBR理論及最佳實踐永遠不會過時。Anton Hand在Youtube上還有一個頻道 (需科學上網),每隔一段時間會上傳一個開發日誌,演示他做的VR專案進展,有興趣的可以看一下,是關於他的VR槍戰遊戲,目前

Google and Uber’s Best Practices for Deep Learning

Google and Uber’s Best Practices for Deep LearningThere is more to building a sustainable Deep Learning solution than what is provided by Deep Learning fra

Best practices for building API Keys

Best practices for building API KeysHello there, we all know how valuable APIs are, its a gateway to explore other services, integrate with them and build

Best Practices for Running Apache Cassandra on Amazon EC2

Apache Cassandra is a commonly used, high performance NoSQL database. AWS customers that currently maintain Cassandra on-premises may want to take

What are best practices for identifying users? Documentation

This tutorial will help you track newly registered users the right way. The process of shifting from an anonymous visitor to an identified user requires so

Best Practices for Spies, Stubs and Mocks in Sinon.js

Introduction Testing code with Ajax, networking, timeouts, databases, or other dependencies can be difficult. For example, if you use A

Ask HN: Good resources for software architecture best practices?

I'm looking for resources on topics like DRY, modular programming, reusable code, single responsibility ... basically, all of the higher-level principles o

Ask HN: Resources for small devs to deal with app stores and bureaucracies?

For some background, I write a couple of minor, free and open source extensions for Chrome and Google Sheets. Writing the extensions was pretty easy, but i

Resolve Issues with Failing Stack Updates After Deleting CloudFormation Resources

You might be able to re-create a resource that is created manually by name. For example, IAM roles are tracked by name. If you accidentally de

Overhead async/await for Unity by custom async statemachine with C# 7.0

Zero-Overhead async/await for Unity by custom async statemachine with C# 7.0Unity supports async/await from Unity 2017, it is great feature but not used wi

What is Web Application Architecture? How It Works, Trends, Best Practices and More

som put type ruby async sin porting whole ldb At Stackify, we understand the amount of effort that goes into creating great applications.

ORC Creation Best Practices

reducer order perf als amp etc uri optimize pan Short Description: ORC Creation Best Practices with examples and references. Article Sy

[CVPR2015] Is object localization for free? – Weakly-supervised learning with convolutional neural networks論文筆記

sed pooling was 技術分享 sco 評測 5.0 ict highest p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 15.0px "Helvetica Neue"; color: #323333 } p.p2

【USE】《An End-to-End System for Automatic Urinary Particle Recognition with CNN》

Urine Sediment Examination(USE) JMOS-2018 目錄 目錄 1 Background and Motivation 2 Innovation

LeetCode:309. Best Time to Buy and Sell Stock with Cooldown

買賣股票系列的第5題。題目是: Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the m