Instant Traffic Switch for Lambda Functions - Efficient and Effortless Method | AWS Certified Developer - Associate Exam Prep

Effortlessly Switch Traffic for Lambda Functions: Best Practice | AWS Certified Developer - Associate

Prev Question Next Question

Question

You have a SAM template used to deploy a Lambda function, and you are now working to create a new version.

Your manager has asked you to instantly switch traffic once you have built and tested your version.

What is the most efficient, effortless and simple way to achieve this?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Answer: D.

Option A is incorrect because using the Management Console might tempt a one-off deployment inefficient when it needs to be done several times.

Option B is incorrect because pointing an alias will not instantly switch traffic as it doesn't state that the PROD alias is actually deployed.

Option C is incorrect.Although it is the next closest answer; setting DeploymentPreference will immediately switch traffic but is used for a more complex deployment.

With this property, you can mention alarms that need to be monitored, hooks that can be run pre and post traffic shifting, and also a traffic switching type needs to be defined.

Option D is CORRECT as the AutoPublishAlias property will create a new alias, create and publish a new version of the Lambda code, point alias to this version and point all event sources to this alias.

The question states find the most effortless and efficient way to achieve this, and the above actions are performed by setting the AutoPublishAlias property with the alias name.

You are also asked to switch traffic to the new version instantly.

So finding an automated way to this will always be the most efficient.

Reference:

https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/automating-updates-to-serverless-apps.html

The most efficient, effortless and simple way to switch traffic from an existing version of an AWS Lambda function to a newly built and tested version is to point an alias to the new version. Therefore, option B is the correct answer.

Here's a more detailed explanation:

AWS Lambda allows you to create aliases, which are pointers to a specific version of a function. Aliases enable you to decouple your function code from the function ARN, so you can update your function without updating the ARN used by other services that depend on it.

When you deploy a new version of a function, you can create an alias that points to the new version. You can then update other services to use the alias instead of the specific function version. This allows you to easily switch traffic to the new version by updating the alias.

In the context of a SAM (Serverless Application Model) template, you can create an alias using the AWS::Lambda::Alias resource. Here's an example:

yaml
Resources: MyFunction: Type: 'AWS::Serverless::Function' Properties: ... MyAlias: Type: 'AWS::Lambda::Alias' Properties: FunctionName: !Ref MyFunction Name: PROD FunctionVersion: '1'

In this example, the MyAlias resource creates an alias named PROD that points to version 1 of the MyFunction function. To switch traffic to a new version, you can simply update the FunctionVersion property of the alias to point to the new version.

For example, if you deploy a new version of the function and it gets assigned version 2, you can update the FunctionVersion property of the alias to '2':

yaml
Resources: MyFunction: Type: 'AWS::Serverless::Function' Properties: ... MyAlias: Type: 'AWS::Lambda::Alias' Properties: FunctionName: !Ref MyFunction Name: PROD FunctionVersion: '2'

Once you've updated the alias, traffic will be routed to the new version of the function.

Option A is incorrect because Qualifiers are used to differentiate between different versions of a Lambda function when you invoke it, not to switch traffic between them.

Option C is incorrect because the DeploymentPreference property only controls the way in which Lambda deploys new versions of your function. It does not control how traffic is routed between different versions.

Option D is also incorrect because the AutoPublishAlias property is used to automatically create an alias for new versions of a function. It does not control how traffic is routed between different versions.