Skip to content

Commit 9a41ec3

Browse files
committed
add CDK deployment
1 parent 0043d9f commit 9a41ec3

File tree

1 file changed

+171
-4
lines changed

1 file changed

+171
-4
lines changed

Sources/AWSLambdaRuntimeCore/Documentation.docc/Deployment.md

Lines changed: 171 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -422,13 +422,180 @@ Deleted successfully
422422
423423
## Deploy your Lambda function with the AWS Cloud Development Kit (CDK)
424424
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.
426426
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.
428428
429-
### Invoke the function
429+
Use the CDK when you want to define your infrastructure in code and manage the deployment of your Lambda function and other AWS services.
430430
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 function with the CDK is written in TypeScript. The following code creates a new Lambda function with 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:
450+
451+
```typescript
452+
import * as cdk from 'aws-cdk-lib';
453+
import * as lambda from 'aws-cdk-lib/aws-lambda';
454+
455+
export class LambdaApiStack extends cdk.Stack {
456+
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
457+
super(scope, id, props);
458+
459+
// Create the Lambda function
460+
const lambdaFunction = new lambda.Function(this, 'SwiftLambdaFunction', {
461+
runtime: lambda.Runtime.PROVIDED_AL2,
462+
architecture: lambda.Architecture.ARM_64,
463+
handler: 'bootstrap',
464+
code: lambda.Code.fromAsset('../.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/APIGatewayLambda/APIGatewayLambda.zip'),
465+
memorySize: 512,
466+
timeout: cdk.Duration.seconds(30),
467+
environment: {
468+
LOG_LEVEL: 'debug',
469+
},
470+
});
471+
}
472+
}
473+
```
474+
The code assumes you already built the Swift Lambda function with 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)
510+
npm install
511+
512+
# Deploy the infrastructure
513+
cdk deploy
514+
515+
✨ Synthesis time: 2.88s
516+
... redacted for brevity ...
517+
Do you wish to deploy these changes (y/n)? y
518+
... redacted for brevity ...
519+
✅ LambdaApiStack
520+
521+
✨ Deployment time: 42.96s
522+
523+
Outputs:
524+
LambdaApiStack.ApiUrl = https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com/
525+
Stack ARN:
526+
arn:aws:cloudformation:eu-central-1:012345678901:stack/LambdaApiStack/e0054390-be05-11ef-9504-065628de4b89
527+
528+
✨ Total time: 45.84s
529+
```
530+
531+
### Invoke your Lambda function
532+
533+
To invoke the Lambda function, use this `curl` command line.
534+
535+
```bash
536+
curl https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com
537+
```
538+
539+
Be sure to replace the URL with the API Gateway endpoint returned in the previous step.
540+
541+
This should print a JSON similar to
542+
543+
```bash
544+
{"version":"2.0","rawPath":"\/","isBase64Encoded":false,"rawQueryString":"","headers":{"user-agent":"curl\/8.7.1","accept":"*\/*","host":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","content-length":"0","x-amzn-trace-id":"Root=1-66fb0388-691f744d4bd3c99c7436a78d","x-forwarded-port":"443","x-forwarded-for":"81.0.0.43","x-forwarded-proto":"https"},"requestContext":{"requestId":"e719cgNpoAMEcwA=","http":{"sourceIp":"81.0.0.43","path":"\/","protocol":"HTTP\/1.1","userAgent":"curl\/8.7.1","method":"GET"},"stage":"$default","apiId":"a5q74es3k2","time":"30\/Sep\/2024:20:01:12 +0000","timeEpoch":1727726472922,"domainPrefix":"a5q74es3k2","domainName":"a5q74es3k2.execute-api.us-east-1.amazonaws.com","accountId":"012345678901"}
545+
```
546+
547+
If you have `jq` installed, you can use it to pretty print the output.
548+
549+
```bash
550+
curl -s https://tyqnjcawh0.execute-api.eu-central-1.amazonaws.com | jq
551+
{
552+
"version": "2.0",
553+
"rawPath": "/",
554+
"requestContext": {
555+
"domainPrefix": "a5q74es3k2",
556+
"stage": "$default",
557+
"timeEpoch": 1727726558220,
558+
"http": {
559+
"protocol": "HTTP/1.1",
560+
"method": "GET",
561+
"userAgent": "curl/8.7.1",
562+
"path": "/",
563+
"sourceIp": "81.0.0.43"
564+
},
565+
"apiId": "a5q74es3k2",
566+
"accountId": "012345678901",
567+
"requestId": "e72KxgsRoAMEMSA=",
568+
"domainName": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
569+
"time": "30/Sep/2024:20:02:38 +0000"
570+
},
571+
"rawQueryString": "",
572+
"routeKey": "$default",
573+
"headers": {
574+
"x-forwarded-for": "81.0.0.43",
575+
"user-agent": "curl/8.7.1",
576+
"host": "a5q74es3k2.execute-api.us-east-1.amazonaws.com",
577+
"accept": "*/*",
578+
"x-amzn-trace-id": "Root=1-66fb03de-07533930192eaf5f540db0cb",
579+
"content-length": "0",
580+
"x-forwarded-proto": "https",
581+
"x-forwarded-port": "443"
582+
},
583+
"isBase64Encoded": false
584+
}
585+
```
586+
587+
### Delete the infrastructure
588+
589+
When done testing, you can delete the infrastructure with this command.
590+
591+
```bash
592+
cdk destroy
593+
594+
Are you sure you want to delete: LambdaApiStack (y/n)? y
595+
LambdaApiStack: destroying... [1/1]
596+
... redacted for brevity ...
597+
✅ LambdaApiStack: destroyed
598+
```
432599
433600
## Third-party tools
434601

0 commit comments

Comments
 (0)