À l'aide du AWS SAM CLI avec Terraform pour le débogage et les tests locaux - AWS Serverless Application Model

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

À l'aide du AWS SAM CLI avec Terraform pour le débogage et les tests locaux

Cette rubrique explique comment utiliser l'interface de ligne de AWS Serverless Application Model commande prise en charge (AWS SAM CLI) commandes avec votre Terraform projets et Terraform Cloud.

Pour fournir des commentaires et soumettre des demandes de fonctionnalités, créez un GitHub Problème.

Tests locaux avec sam local invoke

Note

Pour utiliser le AWS SAM CLI pour effectuer un test local, Docker doit être installé et configuré. Pour obtenir des instructions, consultez Installation de Docker à utiliser avec AWS SAM CLI.

Voici un exemple de test local de votre fonction Lambda en transférant un événement :

$ sam local invoke --hook-name terraform hello_world_function -e events/event.json -

Pour en savoir plus sur l'utilisation de cette commande, veuillez consulter la section Présentation des tests avec sam local invoke.

Tests locaux avec sam local start-api

À utiliser sam local start-api avec Terraform, exécutez la commande suivante :

$ sam local start-api --hook-name terraform

Voici un exemple :

$ sam local start-api --hook-name terraform Running Prepare Hook to prepare the current application Executing prepare hook of hook "terraform" Initializing Terraform application ... Creating terraform plan and getting JSON output .... Generating metadata file Unresolvable attributes discovered in project, run terraform apply to resolve them. Finished generating metadata file. Storing in... Prepare hook completed and metadata file generated at: ... Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET] Mounting None at http://127.0.0.1:3000/hello [POST] You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template 2023-06-26 13:21:20 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

Pour en savoir plus sur la commande, consultez Présentation des tests avec sam local start-api.

Fonctions Lambda qui utilisent des mécanismes d'autorisation Lambda

Pour les fonctions Lambda configurées pour utiliser des autorisateurs Lambda, le AWS SAM CLI invoquera automatiquement votre autorisateur Lambda avant d'appeler le point de terminaison de votre fonction Lambda.

Tests locaux avec sam local start-lambda

Voici un exemple de test local de votre fonction Lambda avec le AWS Command Line Interface ()AWS CLI :

  1. Utilisez le AWS SAM CLI pour créer un environnement de test local :

    $ sam local start-lambda --hook-name terraform hello_world_function
  2. Utilisez le AWS CLI pour appeler votre fonction localement :

    $ aws lambda invoke --function-name hello_world_function --endpoint-url http://127.0.0.1:3001/ response.json --cli-binary-format raw-in-base64-out --payload file://events/event.json

Pour en savoir plus sur la commande, consultez Présentation des tests avec sam local start-lambda.

Terraform limites

Les limites suivantes s'appliquent à l'utilisation du AWS SAM CLI avec Terraform:

  • Fonctions Lambda liées à plusieurs couches.

  • Terraform variables locales qui définissent les liens entre les ressources.

  • Référence à une fonction Lambda qui n'a pas encore été créée. Cela inclut les fonctions définies dans l'attribut body de la ressource de l'API REST.

Pour éviter ces limitations, vous pouvez exécuter terraform apply lorsqu'une nouvelle ressource est ajoutée.