|
| 1 | +# Lambda code examples for the SDK for Swift |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Shows how to use the AWS SDK for Swift to work with AWS Lambda. |
| 6 | + |
| 7 | +<!--custom.overview.start--> |
| 8 | +<!--custom.overview.end--> |
| 9 | + |
| 10 | +_Lambda allows you to run code without provisioning or managing servers._ |
| 11 | + |
| 12 | +## ⚠ Important |
| 13 | + |
| 14 | +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). |
| 15 | +* Running the tests might result in charges to your AWS account. |
| 16 | +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). |
| 17 | +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). |
| 18 | + |
| 19 | +<!--custom.important.start--> |
| 20 | +<!--custom.important.end--> |
| 21 | + |
| 22 | +## Code examples |
| 23 | + |
| 24 | +### Prerequisites |
| 25 | + |
| 26 | +For prerequisites, see the [README](../../README.md#Prerequisites) in the `swift` folder. |
| 27 | + |
| 28 | + |
| 29 | +<!--custom.prerequisites.start--> |
| 30 | +<!--custom.prerequisites.end--> |
| 31 | + |
| 32 | +### Basics |
| 33 | + |
| 34 | +Code examples that show you how to perform the essential operations within a service. |
| 35 | + |
| 36 | +- [Learn the basics](basics/increment/Package.swift) |
| 37 | + |
| 38 | + |
| 39 | +### Single actions |
| 40 | + |
| 41 | +Code excerpts that show you how to call individual service functions. |
| 42 | + |
| 43 | +- [CreateFunction](basics/lambda-basics/Sources/entry.swift#L177) |
| 44 | +- [GetFunction](basics/lambda-basics/Sources/entry.swift#L142) |
| 45 | +- [Invoke](basics/lambda-basics/Sources/entry.swift#L313) |
| 46 | +- [ListFunctions](basics/lambda-basics/Sources/entry.swift#L283) |
| 47 | +- [UpdateFunctionCode](basics/lambda-basics/Sources/entry.swift#L236) |
| 48 | + |
| 49 | + |
| 50 | +<!--custom.examples.start--> |
| 51 | +<!--custom.examples.end--> |
| 52 | + |
| 53 | +## Run the examples |
| 54 | + |
| 55 | +### Instructions |
| 56 | + |
| 57 | +To build any of these examples from a terminal window, navigate into its |
| 58 | +directory, then use the following command: |
| 59 | + |
| 60 | +``` |
| 61 | +$ swift build |
| 62 | +``` |
| 63 | + |
| 64 | +To build one of these examples in Xcode, navigate to the example's directory |
| 65 | +(such as the `ListUsers` directory, to build that example). Then type `xed.` |
| 66 | +to open the example directory in Xcode. You can then use standard Xcode build |
| 67 | +and run commands. |
| 68 | + |
| 69 | +<!--custom.instructions.start--> |
| 70 | +<!--custom.instructions.end--> |
| 71 | + |
| 72 | + |
| 73 | +#### Learn the basics |
| 74 | + |
| 75 | +This example shows you how to do the following: |
| 76 | + |
| 77 | +- Create an IAM role and Lambda function, then upload handler code. |
| 78 | +- Invoke the function with a single parameter and get results. |
| 79 | +- Update the function code and configure with an environment variable. |
| 80 | +- Invoke the function with new parameters and get results. Display the returned execution log. |
| 81 | +- List the functions for your account, then clean up resources. |
| 82 | + |
| 83 | +<!--custom.basic_prereqs.lambda_Scenario_GettingStartedFunctions.start--> |
| 84 | +<!--custom.basic_prereqs.lambda_Scenario_GettingStartedFunctions.end--> |
| 85 | + |
| 86 | + |
| 87 | +<!--custom.basics.lambda_Scenario_GettingStartedFunctions.start--> |
| 88 | +##### Build and deploy the example |
| 89 | + |
| 90 | +This example consists of the main program and two AWS Lambda functions. To |
| 91 | +build and deploy it: |
| 92 | + |
| 93 | +1. Build the `increment` lambda function (`cd increment && swift build`). |
| 94 | +2. Archive the `increment` lambda function for use as a Lambda function: |
| 95 | + `swift package archive --allow-network-connections docker`. When archiving |
| 96 | + is complete, the archive's path is displayed. Take note of it. |
| 97 | +3. Build the `calculator` lambda function (`cd ../calculator && swift build`). |
| 98 | +4. Archive it for use as a Lambda function: `swift package archive |
| 99 | + --allow-network-connections docker`. Take note of this created Zip file's |
| 100 | + path, too. |
| 101 | +5. Build the main program in the `lambda-basics` directory. |
| 102 | +6. Run the main program with the command `swift run lambda-basics --incpath <path-of-increment-archive> --calcpath <path-of-calculator-archive>`. |
| 103 | +<!--custom.basics.lambda_Scenario_GettingStartedFunctions.end--> |
| 104 | + |
| 105 | + |
| 106 | +### Tests |
| 107 | + |
| 108 | +⚠ Running tests might result in charges to your AWS account. |
| 109 | + |
| 110 | + |
| 111 | +To find instructions for running these tests, see the [README](../../README.md#Tests) |
| 112 | +in the `swift` folder. |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +<!--custom.tests.start--> |
| 117 | +<!--custom.tests.end--> |
| 118 | + |
| 119 | +## Additional resources |
| 120 | + |
| 121 | +- [Lambda Developer Guide](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) |
| 122 | +- [Lambda API Reference](https://docs.aws.amazon.com/lambda/latest/dg/API_Reference.html) |
| 123 | +- [SDK for Swift Lambda reference](https://sdk.amazonaws.com/swift/api/awslambda/latest/documentation/awslambda) |
| 124 | + |
| 125 | +<!--custom.resources.start--> |
| 126 | +<!--custom.resources.end--> |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 131 | + |
| 132 | +SPDX-License-Identifier: Apache-2.0 |
0 commit comments