├── .npmignore ├── images ├── banner.png └── architecture.jpg ├── lambda └── functions │ └── swift-lambda-function │ ├── README.md │ ├── .gitignore │ ├── Dockerfile │ ├── Package.swift │ ├── Sources │ └── swift-lambda-function │ │ └── main.swift │ └── Tests │ └── swift-lambda-functionTests │ └── my_swift_lambda_functionTests.swift ├── .gitignore ├── jest.config.js ├── CODE_OF_CONDUCT.md ├── test └── aws-serverless-lambda-with-aws-swift-sdk.test.ts ├── package.json ├── tsconfig.json ├── LICENSE ├── cdk.json ├── bin └── aws-serverless-lambda-with-aws-swift-sdk.ts ├── lib └── aws-serverless-lambda-with-aws-swift-sdk-stack.ts ├── CONTRIBUTING.md └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | *.ts 2 | !*.d.ts 3 | 4 | # CDK asset staging directory 5 | .cdk.staging 6 | cdk.out 7 | -------------------------------------------------------------------------------- /images/banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-lambda-with-aws-sdk-for-swift/HEAD/images/banner.png -------------------------------------------------------------------------------- /lambda/functions/swift-lambda-function/README.md: -------------------------------------------------------------------------------- 1 | # swift-lambda-function 2 | 3 | A description of this package. 4 | -------------------------------------------------------------------------------- /images/architecture.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aws-samples/aws-lambda-with-aws-sdk-for-swift/HEAD/images/architecture.jpg -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.js 2 | !jest.config.js 3 | *.d.ts 4 | node_modules 5 | 6 | # CDK asset staging directory 7 | .cdk.staging 8 | cdk.out 9 | 10 | # MAC OS 11 | .DS_Store 12 | 13 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | testEnvironment: 'node', 3 | roots: ['/test'], 4 | testMatch: ['**/*.test.ts'], 5 | transform: { 6 | '^.+\\.tsx?$': 'ts-jest' 7 | } 8 | }; 9 | -------------------------------------------------------------------------------- /lambda/functions/swift-lambda-function/.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | /.build 3 | /Packages 4 | /*.xcodeproj 5 | xcuserdata/ 6 | DerivedData/ 7 | .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata 8 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | ## Code of Conduct 2 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 3 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 4 | opensource-codeofconduct@amazon.com with any additional questions or comments. 5 | -------------------------------------------------------------------------------- /test/aws-serverless-lambda-with-aws-swift-sdk.test.ts: -------------------------------------------------------------------------------- 1 | // import { expect as expectCDK, matchTemplate, MatchStyle } from '@aws-cdk/assert'; 2 | // import * as cdk from 'aws-cdk-lib'; 3 | // import * as AwsServerlessLambdaWithAwsSwiftSdk from '../lib/aws-serverless-lambda-with-aws-swift-sdk-stack'; 4 | 5 | test('Empty Stack', () => { 6 | // const app = new cdk.App(); 7 | // // WHEN 8 | // const stack = new AwsServerlessLambdaWithAwsSwiftSdk.AwsServerlessLambdaWithAwsSwiftSdkStack(app, 'MyTestStack'); 9 | // // THEN 10 | // expectCDK(stack).to(matchTemplate({ 11 | // "Resources": {} 12 | // }, MatchStyle.EXACT)) 13 | }); 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "aws-serverless-lambda-with-aws-swift-sdk", 3 | "version": "0.1.0", 4 | "bin": { 5 | "aws-serverless-lambda-with-aws-swift-sdk": "bin/aws-serverless-lambda-with-aws-swift-sdk.js" 6 | }, 7 | "scripts": { 8 | "build": "tsc", 9 | "watch": "tsc -w", 10 | "test": "jest", 11 | "cdk": "cdk" 12 | }, 13 | "devDependencies": { 14 | "@types/jest": "^29.2.5", 15 | "@types/node": "18.11.18", 16 | "jest": "^29.3.1", 17 | "ts-jest": "^29.0.3", 18 | "aws-cdk": "2.60.0", 19 | "ts-node": "^10.9.1", 20 | "typescript": "~4.9.4" 21 | }, 22 | "dependencies": { 23 | "aws-cdk-lib": "2.60.0", 24 | "constructs": "^10.0.0", 25 | "source-map-support": "^0.5.21" 26 | } 27 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ES2018", 4 | "module": "commonjs", 5 | "lib": [ 6 | "es2018" 7 | ], 8 | "declaration": true, 9 | "strict": true, 10 | "noImplicitAny": true, 11 | "strictNullChecks": true, 12 | "noImplicitThis": true, 13 | "alwaysStrict": true, 14 | "noUnusedLocals": false, 15 | "noUnusedParameters": false, 16 | "noImplicitReturns": true, 17 | "noFallthroughCasesInSwitch": false, 18 | "inlineSourceMap": true, 19 | "inlineSources": true, 20 | "experimentalDecorators": true, 21 | "strictPropertyInitialization": false, 22 | "typeRoots": [ 23 | "./node_modules/@types" 24 | ] 25 | }, 26 | "exclude": [ 27 | "node_modules", 28 | "cdk.out" 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this 4 | software and associated documentation files (the "Software"), to deal in the Software 5 | without restriction, including without limitation the rights to use, copy, modify, 6 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 7 | permit persons to whom the Software is furnished to do so. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 10 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 11 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 12 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 13 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 14 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /cdk.json: -------------------------------------------------------------------------------- 1 | { 2 | "app": "npx ts-node --prefer-ts-exts bin/aws-serverless-lambda-with-aws-swift-sdk.ts", 3 | "watch": { 4 | "include": [ 5 | "**" 6 | ], 7 | "exclude": [ 8 | "README.md", 9 | "cdk*.json", 10 | "**/*.d.ts", 11 | "**/*.js", 12 | "tsconfig.json", 13 | "package*.json", 14 | "yarn.lock", 15 | "node_modules", 16 | "test" 17 | ] 18 | }, 19 | "context": { 20 | "@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true, 21 | "@aws-cdk/core:stackRelativeExports": true, 22 | "@aws-cdk/aws-rds:lowercaseDbIdentifier": true, 23 | "@aws-cdk/aws-lambda:recognizeVersionProps": true, 24 | "@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": true, 25 | "@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true, 26 | "@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true, 27 | "@aws-cdk/core:target-partitions": [ 28 | "aws", 29 | "aws-cn" 30 | ] 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /lambda/functions/swift-lambda-function/Dockerfile: -------------------------------------------------------------------------------- 1 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | # SPDX-License-Identifier: MIT-0 3 | 4 | # the image used to compile our Swift Lambda Function 5 | FROM --platform=linux/amd64 public.ecr.aws/docker/library/swift:5.7.2-amazonlinux2 as builder 6 | 7 | ARG TARGET_NAME 8 | 9 | RUN yum -y install git jq tar zip openssl-devel 10 | WORKDIR /build-lambda 11 | RUN mkdir -p /Sources/$TARGET_NAME/ 12 | RUN mkdir -p /Tests/$TARGET_NAME/ 13 | ADD /Sources/ ./Sources/ 14 | ADD /Tests/ ./Tests/ 15 | COPY Package.swift . 16 | RUN cd /build-lambda && swift package clean && swift build --static-swift-stdlib -c release 17 | 18 | # the image we will deplpoy to AWS Lambda 19 | FROM public.ecr.aws/lambda/provided:al2-x86_64 20 | 21 | ARG TARGET_NAME 22 | 23 | RUN mkdir -p /var/task/ 24 | RUN mkdir -p /var/runtime/ 25 | COPY --from=builder /build-lambda/.build/release/$TARGET_NAME /var/task/lambdaExec 26 | RUN chmod 755 /var/task/lambdaExec 27 | RUN ln -s /var/task/lambdaExec /var/runtime/bootstrap 28 | RUN chmod 755 /var/runtime/bootstrap 29 | WORKDIR /var/task 30 | CMD ["/var/task/lambdaExec"] -------------------------------------------------------------------------------- /bin/aws-serverless-lambda-with-aws-swift-sdk.ts: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | import 'source-map-support/register'; 3 | import * as cdk from 'aws-cdk-lib'; 4 | import { AwsServerlessLambdaWithAwsSwiftSdkStack } from '../lib/aws-serverless-lambda-with-aws-swift-sdk-stack'; 5 | 6 | const app = new cdk.App(); 7 | new AwsServerlessLambdaWithAwsSwiftSdkStack(app, 'AwsServerlessLambdaWithAwsSwiftSdkStack', { 8 | /* If you don't specify 'env', this stack will be environment-agnostic. 9 | * Account/Region-dependent features and context lookups will not work, 10 | * but a single synthesized template can be deployed anywhere. */ 11 | 12 | /* Uncomment the next line to specialize this stack for the AWS Account 13 | * and Region that are implied by the current CLI configuration. */ 14 | // env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION }, 15 | 16 | /* Uncomment the next line if you know exactly what Account and Region you 17 | * want to deploy the stack to. */ 18 | // env: { account: '123456789012', region: 'us-east-1' }, 19 | 20 | /* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */ 21 | }); 22 | -------------------------------------------------------------------------------- /lambda/functions/swift-lambda-function/Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.7 2 | // The swift-tools-version declares the minimum version of Swift required to build this package. 3 | 4 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 5 | // SPDX-License-Identifier: MIT-0 6 | 7 | import PackageDescription 8 | 9 | let package = Package( 10 | name: "swift-lambda-function", 11 | platforms: [.macOS(.v12)], 12 | dependencies: [ 13 | .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime", branch: "main"), 14 | .package(url: "https://github.com/swift-server/swift-aws-lambda-events", branch: "main"), 15 | .package(url: "https://github.com/awslabs/aws-sdk-swift", from: "0.9.0") 16 | ], 17 | targets: [ 18 | .executableTarget( 19 | name: "swift-lambda-function", 20 | dependencies: [ 21 | .product(name: "AWSLambdaRuntime",package: "swift-aws-lambda-runtime"), 22 | .product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"), 23 | .product(name: "AWSDynamoDB", package: "aws-sdk-swift") 24 | ] 25 | ), 26 | .testTarget( 27 | name: "swift-lambda-functionTests", 28 | dependencies: ["swift-lambda-function"]), 29 | ] 30 | ) -------------------------------------------------------------------------------- /lambda/functions/swift-lambda-function/Sources/swift-lambda-function/main.swift: -------------------------------------------------------------------------------- 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // SPDX-License-Identifier: MIT-0 3 | 4 | // import the packages required by our function 5 | import Foundation 6 | import AWSLambdaRuntime 7 | import AWSLambdaEvents 8 | import AWSDynamoDB 9 | 10 | 11 | // define a structure for returning an error from our function 12 | enum FunctionError: Error { 13 | case envError 14 | } 15 | 16 | @main 17 | struct SwiftLambdaFunction: SimpleLambdaHandler { 18 | 19 | // function handler 20 | // The function's event parameter contains the event triggered from S3 21 | func handle(_ event: S3Event, context: LambdaContext) async throws -> Void { 22 | 23 | // create a client to interact with DynamoDB 24 | let client = try await DynamoDBClient() 25 | 26 | // retrieve the DynamoDB table name of our application from the Lambda environment variable set in the CDK 27 | guard let tableName = ProcessInfo.processInfo.environment["TABLE_NAME"] else { 28 | throw FunctionError.envError 29 | } 30 | 31 | // use the event information to create an input record for DynamoDB with the S3 object's metadata 32 | let id = "\(event.records[0].s3.bucket.name)/\(event.records[0].s3.object.key)" 33 | let name = event.records[0].s3.object.key 34 | let size = event.records[0].s3.object.size! 35 | 36 | let input = PutItemInput(item: ["id": .s(id), "name": .s(name), "size": .n(String(size))], tableName: tableName) 37 | 38 | _ = try await client.putItem(input: input) 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /lambda/functions/swift-lambda-function/Tests/swift-lambda-functionTests/my_swift_lambda_functionTests.swift: -------------------------------------------------------------------------------- 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // SPDX-License-Identifier: MIT-0 3 | 4 | import XCTest 5 | import class Foundation.Bundle 6 | 7 | final class my_swift_lambda_functionTests: XCTestCase { 8 | func testExample() throws { 9 | // This is an example of a functional test case. 10 | // Use XCTAssert and related functions to verify your tests produce the correct 11 | // results. 12 | 13 | // Some of the APIs that we use below are available in macOS 10.13 and above. 14 | guard #available(macOS 10.13, *) else { 15 | return 16 | } 17 | 18 | // Mac Catalyst won't have `Process`, but it is supported for executables. 19 | #if !targetEnvironment(macCatalyst) 20 | 21 | let fooBinary = productsDirectory.appendingPathComponent("swift-lambda-function") 22 | 23 | let process = Process() 24 | process.executableURL = fooBinary 25 | 26 | let pipe = Pipe() 27 | process.standardOutput = pipe 28 | 29 | try process.run() 30 | process.waitUntilExit() 31 | 32 | let data = pipe.fileHandleForReading.readDataToEndOfFile() 33 | let output = String(data: data, encoding: .utf8) 34 | 35 | XCTAssertEqual(output, "Hello, world!\n") 36 | #endif 37 | } 38 | 39 | /// Returns path to the built products directory. 40 | var productsDirectory: URL { 41 | #if os(macOS) 42 | for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") { 43 | return bundle.bundleURL.deletingLastPathComponent() 44 | } 45 | fatalError("couldn't find the products directory") 46 | #else 47 | return Bundle.main.bundleURL 48 | #endif 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /lib/aws-serverless-lambda-with-aws-swift-sdk-stack.ts: -------------------------------------------------------------------------------- 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. 2 | // SPDX-License-Identifier: MIT-0 3 | 4 | import { Construct } from 'constructs'; 5 | import * as cdk from 'aws-cdk-lib'; 6 | import * as path from "path" 7 | import { aws_dynamodb as dynamodb } from 'aws-cdk-lib' 8 | import { aws_lambda as Lambda } from 'aws-cdk-lib'; 9 | import { aws_s3 as s3 } from 'aws-cdk-lib' 10 | import { aws_s3_notifications as s3n } from 'aws-cdk-lib'; 11 | 12 | export class AwsServerlessLambdaWithAwsSwiftSdkStack extends cdk.Stack { 13 | constructor(scope: Construct, id: string, props?: cdk.StackProps) { 14 | super(scope, id, props); 15 | 16 | //create the DynamoDB table 17 | const table = new dynamodb.Table(this, 'SwiftLambdaTable', { 18 | partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }, 19 | removalPolicy:cdk.RemovalPolicy.DESTROY 20 | }); 21 | 22 | // output the table name so it can be identified after the stack deploys 23 | new cdk.CfnOutput(this, "DynamoDBTableName", { 24 | value: table.tableName 25 | }); 26 | 27 | //create the Amazon S3 bucket 28 | const bucket = new s3.Bucket(this, 'SwiftLambdaBucket', { 29 | versioned:false, 30 | encryption:s3.BucketEncryption.S3_MANAGED, 31 | removalPolicy:cdk.RemovalPolicy.DESTROY, 32 | blockPublicAccess:s3.BlockPublicAccess.BLOCK_ALL 33 | } 34 | ) 35 | 36 | // output the bucket name so it can be identified after the stack deploys 37 | new cdk.CfnOutput(this, "BucketName", { 38 | value: bucket.bucketName 39 | }); 40 | 41 | // create the Docker image based Lambda function 42 | // pass in the DynamoDB table name as environment variable 43 | let dockerfile = path.join(__dirname, "../lambda/functions/swift-lambda-function/"); 44 | 45 | const lambdaFunction = new Lambda.DockerImageFunction(this, "SwiftLambdaFunction", { 46 | code: Lambda.DockerImageCode.fromImageAsset(dockerfile, { 47 | buildArgs:{ 48 | "TARGET_NAME": 'swift-lambda-function' 49 | } 50 | }), 51 | memorySize:1024, 52 | timeout:cdk.Duration.seconds(30), 53 | architecture: Lambda.Architecture.X86_64, 54 | environment: { 55 | "TABLE_NAME": table.tableName 56 | } 57 | }); 58 | 59 | // output the function name so it can be identified after the stack deploys 60 | new cdk.CfnOutput(this, "FunctionName", { 61 | value: lambdaFunction.functionName 62 | }); 63 | 64 | //grant function permissions to write to the DynamoDB table 65 | table.grantWriteData(lambdaFunction) 66 | 67 | // create the Amazon S3 event notification for new files created 68 | bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.LambdaDestination(lambdaFunction)); 69 | } 70 | } -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | Thank you for your interest in contributing to our project. Whether it's a bug report, new feature, correction, or additional 4 | documentation, we greatly value feedback and contributions from our community. 5 | 6 | Please read through this document before submitting any issues or pull requests to ensure we have all the necessary 7 | information to effectively respond to your bug report or contribution. 8 | 9 | 10 | ## Reporting Bugs/Feature Requests 11 | 12 | We welcome you to use the GitHub issue tracker to report bugs or suggest features. 13 | 14 | When filing an issue, please check existing open, or recently closed, issues to make sure somebody else hasn't already 15 | reported the issue. Please try to include as much information as you can. Details like these are incredibly useful: 16 | 17 | * A reproducible test case or series of steps 18 | * The version of our code being used 19 | * Any modifications you've made relevant to the bug 20 | * Anything unusual about your environment or deployment 21 | 22 | 23 | ## Contributing via Pull Requests 24 | Contributions via pull requests are much appreciated. Before sending us a pull request, please ensure that: 25 | 26 | 1. You are working against the latest source on the *main* branch. 27 | 2. You check existing open, and recently merged, pull requests to make sure someone else hasn't addressed the problem already. 28 | 3. You open an issue to discuss any significant work - we would hate for your time to be wasted. 29 | 30 | To send us a pull request, please: 31 | 32 | 1. Fork the repository. 33 | 2. Modify the source; please focus on the specific change you are contributing. If you also reformat all the code, it will be hard for us to focus on your change. 34 | 3. Ensure local tests pass. 35 | 4. Commit to your fork using clear commit messages. 36 | 5. Send us a pull request, answering any default questions in the pull request interface. 37 | 6. Pay attention to any automated CI failures reported in the pull request, and stay involved in the conversation. 38 | 39 | GitHub provides additional document on [forking a repository](https://help.github.com/articles/fork-a-repo/) and 40 | [creating a pull request](https://help.github.com/articles/creating-a-pull-request/). 41 | 42 | 43 | ## Finding contributions to work on 44 | Looking at the existing issues is a great way to find something to contribute on. As our projects, by default, use the default GitHub issue labels (enhancement/bug/duplicate/help wanted/invalid/question/wontfix), looking at any 'help wanted' issues is a great place to start. 45 | 46 | 47 | ## Code of Conduct 48 | This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct). 49 | For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact 50 | opensource-codeofconduct@amazon.com with any additional questions or comments. 51 | 52 | 53 | ## Security issue notifications 54 | If you discover a potential security issue in this project we ask that you notify AWS/Amazon Security via our [vulnerability reporting page](http://aws.amazon.com/security/vulnerability-reporting/). Please do **not** create a public github issue. 55 | 56 | 57 | ## Licensing 58 | 59 | See the [LICENSE](LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution. 60 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Swift on AWS: Building your backend app with the AWS SDK for Swift 2 | ![Image description](images/banner.png) 3 | 4 | This sample application demonstrates using the [AWS SDK for Swift](https://aws.amazon.com/sdk-for-swift/) and [Swift AWS Lambda Runtime](https://github.com/swift-server/swift-aws-lambda-runtime) to build a AWS Lambda function. It uses Docker to compile and package the function into a Docker image. It then uses the [AWS Cloud Development Kit (AWS CDK)](https://aws.amazon.com/cdk/) to deploy the image and create the Lambda function in AWS. 5 | 6 | The sample implements Swift Concurrency (async / await) to allow for asynchronous calls to the AWS cloud resources. 7 | 8 | ## The Use Case 9 | To illustrate these capabilities, we have a simple event-driven use case. The application monitors an Amazon Simple Storage Service (Amazon S3) bucket for new files. When a user uploads a new file, Amazon S3 sends an event notification to the Lambda function. The function retrieves metadata about the file and saves it to Amazon DynamoDB. 10 | 11 | ![Image description](images/architecture.jpg) 12 | 13 | ## Prerequisites 14 | 15 | To deploy this application, you need an AWS account and the following tools on your development machine. While it may work with alternative versions, we recommend you deploy the specified minimum version. 16 | 17 | * [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) (^2.4.19) the AWS CLI is used to configure the AWS credentials on your development machine. 18 | * [Node.js](https://nodejs.org/en/download/current/) (^16.8.1) with NPM (^8.12.2) 19 | * [Docker Desktop](https://www.docker.com/products/docker-desktop) (^4.15) The AWS CDK uses Docker to compile the Swift Lambda functions into a Docker image. 20 | 21 | ## Clone this repository 22 | 23 | ```bash 24 | $ git clone https://github.com/aws-samples/aws-lambda-with-aws-sdk-for-swift.git 25 | ``` 26 | 27 | Swich to the application folder and use the Node Package Manager (npm) to install the required AWS CDK modules. 28 | 29 | ```bash 30 | $ cd aws-lambda-with-aws-sdk-for-swift 31 | $ npm install 32 | ``` 33 | 34 | ## Deploy the application to AWS 35 | 36 | If this is the first time you have used the AWS CDK in your AWS account, you must first *bootstrap* your account. 37 | 38 | From the **root** folder of your project execute the following command: 39 | 40 | ```bash 41 | $ npx aws-cdk bootstrap 42 | ``` 43 | 44 | Now you can deploy the application stack. The deployment step uses Docker on your local machine to build a Docker image from your Lambda code. It then generates a AWS CloudFormation template which defines the Amazon S3 bucket, DynamoDB table, and Lambda function, and deploys it to your account. This process can take several minutes. 45 | 46 | ```bash 47 | $ npx aws-cdk deploy 48 | ``` 49 | 50 | When the deployment has completed, it will ouput the name of the Amazon S3 bucket, DynamoDB table, and Lambda function that was created. For example: 51 | 52 | ```bash 53 | Outputs: 54 | AwsServerlessLambdaWithAwsSwiftSdkStack.BucketName = swift-lambda-bucket 55 | AwsServerlessLambdaWithAwsSwiftSdkStack.DynamoDBTableName = swift-lambda-table 56 | AwsServerlessLambdaWithAwsSwiftSdkStack.FunctionName = swift-lambda-function 57 | ``` 58 | 59 | ## Run the application 60 | Once the deployment process has completed, logon to the AWS Management Console to view the new resources created in your AWS account. To test out the application: 61 | 62 | - navigate to the Amazon S3 service in the console and locate the Amazon S3 bucket created by the AWS CDK. 63 | - upload a file to this Amazon S3 bucket 64 | - navigate to the DynamoDB service in the console and select the table created by the AWS CDK 65 | 66 | You should see a record in the table that specifies the Amazon S3 bucket, object key, name, and size of the file you uploaded. 67 | 68 | ## Cleanup 69 | 70 | Once you finish using the application, you can remove all the resources created by the AWS CDK with the *destroy* command. 71 | 72 | *Note - you must first empty the Amazon S3 Bucket before you run the destroy command otherwise it results in an error. The AWS CDK does not remove buckets that contain objects.* 73 | 74 | ```bash 75 | $ npx aws-cdk destroy 76 | ``` 77 | --------------------------------------------------------------------------------