Ceci est le guide du développeur du AWS CDK v2. L'ancien CDK v1 est entré en maintenance le 1er juin 2022 et a pris fin le 1er juin 2023.
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.
Tester des applications AWS CDK en local avec SAM AWS
Vous pouvez utiliser la CLI AWS SAM pour tester localement vos applications AWS CDK en exécutant les commandes suivantes depuis le répertoire racine du projet de votre application AWS CDK :
Avant d'exécuter l'une des sam local
commandes avec une application AWS CDK, vous devez exécutercdk synth
.
Lors de l'exécution, sam local invoke
vous avez besoin de l'identifiant de construction de la fonction que vous souhaitez invoquer et du chemin d'accès à votre AWS CloudFormation modèle synthétisé. Si votre application utilise des piles imbriquées, pour résoudre les conflits de noms, vous avez également besoin du nom de la pile dans laquelle la fonction est définie.
- Utilisation
-
# Invoke the function FUNCTION_IDENTIFIER declared in the stack STACK_NAME $ sam local invoke <OPTIONS> <STACK_NAME/FUNCTION_IDENTIFIER> # Start all APIs declared in the AWS CDK application $ sam local start-api -t <./cdk.out/CdkSamExampleStack.template.json> <OPTIONS> # Start a local endpoint that emulates AWS Lambda $ sam local start-lambda -t <./cdk.out/CdkSamExampleStack.template.json> <OPTIONS>
exemple
Envisagez d'utiliser des piles et des fonctions déclarées avec l'exemple suivant :
app = new HelloCdkStack(app, "HelloCdkStack", ... ) class HelloCdkStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { ... new lambda.Function(this, 'MyFunction', { ... }); new HelloCdkNestedStack(this, 'HelloNestedStack' ,{ ... }); } } class HelloCdkNestedStack extends cdk.NestedStack { constructor(scope: Construct, id: string, props?: cdk.NestedStackProps) { ... new lambda.Function(this, 'MyFunction', { ... }); new lambda.Function(this, 'MyNestedFunction', { ... }); } }
Les commandes suivantes appellent localement les fonctions Lambda définies dans l'exemple présenté ci-dessus :
# Invoke MyFunction from the HelloCdkStack $ sam local invoke -t <./cdk.out/HelloCdkStack.template.json> <MyFunction>
# Invoke MyNestedFunction from the HelloCdkNestedStack $ sam local invoke -t <./cdk.out/HelloCdkStack.template.json> <MyNestedFunction>
# Invoke MyFunction from the HelloCdkNestedStack $ sam local invoke -t <./cdk.out/HelloCdkStack.template.json> <HelloNestedStack/MyFunction>