You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/AWSLambdaRuntimeCore/Documentation.docc/Deployment.md
+171-4Lines changed: 171 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -422,13 +422,180 @@ Deleted successfully
422
422
423
423
## Deploy your Lambda function with the AWS Cloud Development Kit (CDK)
424
424
425
-
TODO
425
+
The AWS Cloud Development Kit is an open-source software development framework to define cloud infrastructure in code and provision it through AWS CloudFormation. The CDK provides high-level constructs that preconfigure AWS resources with best practices, and you can use familiar programming languages like TypeScript, Javascript, Python, Java, C#, and Go to define your infrastructure.
426
426
427
-
### Create the function
427
+
To use the CDK, you need to [install the CDK CLI](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html) on your machine. The CDK CLI provides a set of commands to manage your CDK projects.
428
428
429
-
### Invoke the function
429
+
Use the CDK when you want to define your infrastructure in code and manage the deployment of your Lambda functionand other AWS services.
430
430
431
-
### Delete the function
431
+
### Create a CDK project
432
+
433
+
To create a new CDK project, use the `cdk init` command. The command creates a new directory with the project structure and the necessary files to define your infrastructure.
434
+
435
+
```sh
436
+
# In your Swift Lambda project folder
437
+
mkdir infra && cd infra
438
+
cdk init app --language typescript
439
+
```
440
+
441
+
In this example, the code to create a Swift Lambda functionwith the CDK is written in TypeScript. The following code creates a new Lambda functionwith the `swift` runtime.
442
+
443
+
It requires the `@aws-cdk/aws-lambda` package to define the Lambda function. You can install the dependency with the following command:
444
+
445
+
```sh
446
+
npm install aws-cdk-lib constructs
447
+
```
448
+
449
+
Then, in the lib folder, create a new file named `swift-lambda-stack.ts` with the following content:
The code assumes you already built the Swift Lambda functionwith the `swift package archive --allow-network-connections docker` command. The ZIP file is located in the `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyLambda/MyLambda.zip` folder.
475
+
476
+
You can write code to add an API Gateway to invoke your Lambda function. The following code creates an HTTP API Gateway that triggers the Lambda function.
477
+
478
+
```typescript
479
+
// in the import section at the top
480
+
import * as apigateway from 'aws-cdk-lib/aws-apigatewayv2';
481
+
import { HttpLambdaIntegration } from 'aws-cdk-lib/aws-apigatewayv2-integrations';
482
+
483
+
// in the constructor, after having created the Lambda function
484
+
// ...
485
+
486
+
// Create the API Gateway
487
+
const httpApi = new apigateway.HttpApi(this, 'HttpApi', {
488
+
defaultIntegration: new HttpLambdaIntegration({
489
+
handler: lambdaFunction,
490
+
}),
491
+
});
492
+
493
+
// Output the API Gateway endpoint
494
+
new cdk.CfnOutput(this, 'APIGatewayEndpoint', {
495
+
value: httpApi.url!,
496
+
});
497
+
498
+
// ...
499
+
```
500
+
501
+
### Deploy the infrastructure
502
+
503
+
To deploy the infrastructure, type the following commands.
504
+
505
+
```sh
506
+
# Change to the infra directory
507
+
cd infra
508
+
509
+
# Install the dependencies (only before the first deployment)
0 commit comments