Utilisation de la CLI  AWS SAM 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.

Utilisation de la CLI  AWS SAM avec Terraform pour le débogage et les tests locaux

Cette rubrique explique comment utiliser les commandes d'interface de ligne de AWS Serverless Application Model commande (AWS SAMCLI) prises en charge avec vos Terraform projets etTerraform Cloud.

Pour soumettre des commentaires et des demandes de fonctionnalités, créez un Ticket GitHub.

Tests locaux avec sam local invoke

Note

Pour utiliser la CLI  AWS SAMà des fins de tests locaux, Docker doit être installé et configuré. Pour obtenir des instructions, veuillez consulter Installation de Docker pour une utilisation avec la CLI  AWS SAM.

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

Pour utiliser sam local start-api avec Terraform, procédez comme suit :

$ 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 mécanismes d'autorisation Lambda, la CLI  AWS SAM invoquera automatiquement votre mécanisme d'autorisation Lambda avant d'invoquer 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 la CLI  AWS SAM 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.

Restrictions liées à Terraform

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

  • Fonctions Lambda liées à plusieurs couches.

  • Variables locales Terraform définissant 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 REST API ressource.

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