Objectif
Ce lab vous guide dans la mise en place d’un workflow avec AWS Step Functions pour orchestrer plusieurs fonctions AWS Lambda. Vous apprendrez à :
- Créer un workflow Step Functions.
- Définir plusieurs étapes (states) exécutant des fonctions Lambda.
- Configurer les permissions nécessaires.
- Tester le workflow et observer son exécution
1. Architecture du Lab
L’architecture repose sur AWS Step Functions pour orchestrer l’exécution de plusieurs fonctions Lambda :
- Une première fonction Lambda reçoit une entrée utilisateur.
- Une deuxième fonction Lambda effectue un traitement.
Une troisième fonction Lambda enregistre le résultat final.
2. Étapes du Déploiement
1. Créer les Fonctions Lambda
1.1. Fonction Lambda “StartFunction” (Déclencheur du workflow)
- Allez sur AWS Lambda > Create Function.
- Sélectionnez Author from scratch.
- Nommez la fonction StartFunction.
- Choisissez Python 3.9 comme runtime.
- Ajoutez ce code dans l’éditeur :
python
import json
def lambda_handler(event, context):
input_data = event.get("input", "No input provided")
return {"message": f"Received input: {input_data}", "data": input_data
Cliquez sur Deploy.
1.2. Fonction Lambda “ProcessFunction” (Traitement des données)
- Créez une nouvelle fonction Lambda nommée ProcessFunction.
- Collez ce code :
import json
def lambda_handler(event, context):
data = event.get("data", "No data received")
processed_data = data.upper() # Transformation simple
return {"processed_data": processed_data}
Cliquez sur Deploy.
1.3. Fonction Lambda “SaveFunction” (Enregistrement du résultat)
- Créez une nouvelle fonction Lambda nommée SaveFunction.
- Collez ce code :
import json
def lambda_handler(event, context):
result = event.get("processed_data", "No result available")
return {"final_message": f"Data saved: {result}"}
Cliquez sur Deploy.
2. Configurer les Permissions
- Accédez à IAM > Roles.
- Recherchez le rôle IAM utilisé par Lambda (ex: AWSLambdaBasicExecutionRole).
Ajoutez la permission AWS Step Functions Full Access.
3. Créer un Workflow AWS Step Functions
- Accédez à Step Functions > Create state machine.
- Choisissez Author with code et sélectionnez Standard Workflow.
- Ajoutez ce code JSON pour définir les étapes du workflow :
{
"Comment": "Step Functions Lab",
"StartAt": "StartFunction",
"States": {
"StartFunction": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:StartFunction",
"Next": "ProcessFunction"
},
"ProcessFunction": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:ProcessFunction",
"Next": "SaveFunction"
},
"SaveFunction": {
"Type": "Task",
"Resource": "arn:aws:lambda:REGION:ACCOUNT_ID:function:SaveFunction",
"End": true
}
}
}
- Remplacez REGION et ACCOUNT_ID par vos informations AWS.
Cliquez sur Create state machine.
4. Tester le Workflow
- Allez sur votre state machine et cliquez sur Start execution.
Entrez un input JSON :
{
"input": "hello world"
}
- Cliquez sur Start Execution.
- Observez le workflow en action et consultez les résultats de chaque fonction.
5. Nettoyer les Ressources
- Supprimez la state machine dans Step Functions.
- Supprimez les fonctions Lambda.
- Supprimez les rôles IAM inutilisés.