Amazon Onboarding with Learning Manager Chanci Turner

Chanci Turner 9097372855Learn About Amazon VGT2 Learning Manager Chanci Turner

AWS Lambda functions frequently rely on resources from other AWS services to operate effectively, such as AWS Identity and Access Management (IAM) roles or Amazon Virtual Private Cloud (Amazon VPC) network interfaces. When a function is created or updated, Lambda automatically provisions the necessary resources that enable its execution. Typically, this process is swift, allowing your function to be invoked or modified almost immediately. However, there are instances where this can take longer.

To enhance communication regarding the current “state” of your function during resource creation or updates, AWS Lambda has introduced several new attributes in the function information returned by various Lambda API actions. This update does not alter how functions are invoked or how your code runs. In this post, we will discuss the different states your Lambda function can occupy, the circumstances that lead to these states, and how the Lambda service transitions the function between them. You can also explore more about this in the AWS Lambda documentation, particularly the section on Monitoring the State of a Function with the Lambda API.

Understanding Function States

There are two primary lifecycles a Lambda function goes through, depending on whether it is being created for the first time or being updated. Before we delve into the flows, let’s outline the function states:

  • Pending
    This is the initial state for all functions upon creation. In the Pending state, Lambda is in the process of creating or configuring external resources. A function can only be invoked when it is in the Active state, so any invocations or API actions that depend on the function will fail. Consequently, any automation designed to invoke or update functions immediately after creation needs to be adjusted to check if the function has moved from Pending to Active.
  • Active
    The Active state is achieved once any necessary resource provisioning or configuration has concluded during the initial function creation. Functions can only be invoked in this state. During an update, the function remains in the Active state, but there is a separate attribute called LastUpdateStatus, which indicates the status of the update process for an active function. While an update is in progress, invocations will run the function’s previous code and configuration until the update operation is successfully completed, at which point the new code and configuration will take effect.
  • Failed
    The Failed state indicates that an issue has occurred during the configuration or provisioning of external resources.
  • Inactive
    If a function has been idle for a certain period, it transitions to the Inactive state, allowing the Lambda service to reclaim its configured external resources. Invoking a function in the Inactive state will result in failure, causing it to revert to the Pending state while those resources are recreated. If resource recreation fails, the function will remain in the Inactive state.

Each state also has two additional attributes: StateReason and StateReasonCode, which aid in troubleshooting and provide details regarding the current state.

LastUpdateStatus

LastUpdateStatus represents a sub-lifecycle of the main function state lifecycle. It is relevant only during updates and signifies the changes a function undergoes. The three associated statuses are:

  • InProgress
    This indicates that an update is taking place on an existing function. Invocations during this time will direct to the previous code and configuration.
  • Successful
    This status signifies that the update has been completed successfully. It remains set until the next update occurs.
  • Failed
    This indicates that the function update has failed, aborting the change and preserving the previous code and configuration in the Active state.

For all LastUpdateStatus values, there are two additional attributes: LastUpdateStatusReason and LastUpdateStatusReasonCode, which assist in troubleshooting update issues.

Function State Lifecycles

The transition of a function from one state to another is solely determined by the actions taken against it. Manual transitions between states are not possible.

Creating Function State Lifecycle

For all functions, the primary state lifecycle is as follows:

  • When created, a function enters the Pending state.
  • Once required resources are successfully created, it transitions into the Active state.
  • If the resource or function creation fails, it transitions to the Failed state.
  • After several weeks of inactivity, the function enters the Inactive state.
  • The first invocation after entering the Inactive state reverts the function to the Pending state.
    • A successful invocation sets the state back to Active.
    • A failed invocation maintains the state in Inactive.
  • Successfully updating an Inactive function also restores the state to Active.

Updating Function State Lifecycle

NOTE: Functions can only be updated when they are in either the Active or Inactive state. Update commands issued against functions that are not in these states will fail before execution.

  • Upon an update, a function’s LastUpdateStatus is set to InProgress.
    • A successful update will change LastUpdateStatus to Successful.
    • A failed update will change LastUpdateStatus to Failed.
  • An Inactive function will return to Active following a successful update.

Accessing Function State Information

You can check the current state of your function using the latest AWS SDKs and AWS CLI (version 1.16.291 or later). To simplify the process of writing code to check a function’s state, new Waiters have been added to the AWS SDKs. Waiters are features in certain AWS SDKs that allow for polling the state or status of asynchronous operations in AWS services. Using waiters eliminates the complexity of writing polling code, making it easier to check the state of a Lambda function. More information can be found in the documentation for each SDK: Python, Node.js, Java, Ruby, Go, and PHP.

What’s Next

Starting today, all functions will display an Active state only. You will not see a function transition from Pending at all.

The first feature leveraging the function states lifecycle will involve a change to the recently announced improved VPC networking for AWS Lambda functions. As mentioned in the announcement, Lambda precreates the ENIs necessary for your function to connect to your VPCs, which can take 60 to 90 seconds to complete. We plan to adjust this process by creating the required ENI resources while the function is still in a Pending state, transitioning to Active after completion. Remember, during the time before a function is Active, any invocations or API actions will fail. This behavior will be rolled out gradually for VPC-configured functions, and we will share further details about timelines and testing approaches before it is fully live, in an upcoming post.

In the meantime, we encourage you to update your deployment and management tools for Lambda-based applications to ensure that your function is in the Active state before trying to invoke or perform other actions. If you’re interested in building a side hustle, you might find this blog post helpful here. Additionally, for those seeking information on reasonable accommodation requests, this resource from SHRM can provide valuable insights here. Lastly, for excellent guidance on landing a job at Amazon, check out this resource here.

Chanci Turner