Из-за периодической блокировки нашего сайта РКН сервисами, просим воспользоваться резервным адресом:
Загрузить через dTub.ru Загрузить через ClipSaver.ruУ нас вы можете посмотреть бесплатно How to Effectively Mock AWS SSM Parameters in Python Tests или скачать в максимальном доступном качестве, которое было загружено на ютуб. Для скачивания выберите вариант из формы ниже:
Роботам не доступно скачивание файлов. Если вы считаете что это ошибочное сообщение - попробуйте зайти на сайт через браузер google chrome или mozilla firefox. Если сообщение не исчезает - напишите о проблеме в обратную связь. Спасибо.
Если кнопки скачивания не
загрузились
НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием, пожалуйста напишите в поддержку по адресу внизу
страницы.
Спасибо за использование сервиса savevideohd.ru
Learn how to successfully mock AWS SSM parameters in your Python tests using `moto`, enabling you to build and test retrofittable AWS applications effectively. --- This video is based on the question https://stackoverflow.com/q/69082090/ asked by the user 'Abhishek Patil' ( https://stackoverflow.com/u/6279687/ ) and on the answer https://stackoverflow.com/a/69083019/ provided by the user 'Niel Godfrey Pablo Ponciano' ( https://stackoverflow.com/u/11043825/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions. Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Python mock AWS SSM Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l... The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license. If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com. --- Introduction When working with AWS services in Python applications, you often need to fetch configuration data stored in AWS Systems Manager (SSM) Parameter Store. For example, you might have a Lambda function that retrieves parameters such as database passwords and API keys. However, testing these functions can be challenging, especially when you want to avoid hitting the actual AWS services during your tests. This is where mocking comes into play. In this guide, we will discuss how to successfully mock the AWS SSM service in Python using the moto library, which is designed to easily simulate AWS services locally for testing purposes. We'll go through common pitfalls and provide multiple solutions to ensure your tests run as expected. The Problem You might have written a simple Lambda function like this: [[See Video to Reveal this Text or Code Snippet]] While this code is functional, it poses a problem when you want to test it using the moto library. Users frequently encounter the issue where tests unexpectedly return the actual value from the SSM store instead of the mocked parameters. Here's a typical approach using pytest and moto: [[See Video to Reveal this Text or Code Snippet]] Here, the issue arises because the ssm client is being instantiated before the -mock_ssm decorator is applied, leading to your tests always interacting with the live AWS environment. Solutions Solution 1: Initialize the Client at Runtime A straightforward fix is to move the client instantiation inside the lambda_handler function so it is created at runtime. This ensures the mocked version is used. [[See Video to Reveal this Text or Code Snippet]] Solution 2: Import After Patching Another effective strategy is to import the lambda_handler after the -mock_ssm decorator to ensure the patch is in effect beforehand. [[See Video to Reveal this Text or Code Snippet]] Solution 3: Reload the Module If you need to keep your imports as they were, consider reloading the module after the mock is applied. This forces the use of the patched versions. [[See Video to Reveal this Text or Code Snippet]] Final Thoughts Testing code that interacts with AWS can be tricky, but with the right approach, such as using the moto library to mock SSM parameters, it becomes far more manageable. By ensuring that your SSM client is properly instantiated in a way that aligns with the mocking, you can successfully write and run your tests without dependency on live AWS infrastructure. By implementing one of the outlined solutions, you can take full advantage of local testing while ensuring accuracy and reliability in your applications. If you have any further questions or need more clarification on mocking AWS components, feel free to reach out or leave a comment below!