Skip to content

Commit c95f635

Browse files
committed
add an example README with common sections + link to each example
1 parent d3890ae commit c95f635

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

Examples/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
This directory contains example code for Lambda functions.
2+
3+
## Pre-requisites
4+
5+
- Ensure you have the Swift 6.x toolchain installed. You can [install Swift toolchains](https://www.swift.org/install/macos/) from Swift.org
6+
7+
- When developing on macOS, be sure you use macOS 15 (Sequoia) or a more recent macOS version.
8+
9+
- To build and archive your Lambda functions, you need to [install docker](https://docs.docker.com/desktop/install/mac-install/).
10+
11+
- To deploy your Lambda functions and invoke them, you must have [an AWS account](https://docs.aws.amazon.com/accounts/latest/reference/manage-acct-creating.html) and [install and configure the `aws` command line](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).
12+
13+
- Some examples are using [AWS SAM](https://aws.amazon.com/serverless/sam/). Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) before deploying these examples.
14+
15+
## Examples
16+
17+
- **[API Gateway](APIGateway/README.md)**: an HTTPS REST API with [Amazon API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html) and a Lambda function as backend (requires [AWS SAM](https://aws.amazon.com/serverless/sam/)).
18+
19+
- **[BackgroundTasks](BackgroundTasks/README.md)**: a Lambda function that continues to run background tasks after having sent the response (requires [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)).
20+
21+
- **[HelloWorld](HelloWorld/README.md)**: a simple Lambda function (requires [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)).
22+
23+
- **[S3_AWSSDK](S3_AWSSDK/README.md)**: a Lambda function that uses the [AWS SDK for Swift](https://docs.aws.amazon.com/sdk-for-swift/latest/developer-guide/getting-started.html) to invoke an [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) API (requires [AWS SAM](https://aws.amazon.com/serverless/sam/)).
24+
25+
- **[S3_Soto](S3_Soto/README.md)**: a Lambda function that uses [Soto](https://github.com/soto-project/soto) to invoke an [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) API (requires [AWS SAM](https://aws.amazon.com/serverless/sam/)).
26+
27+
- **[Streaming]**: create a Lambda function exposed as an URL. The Lambda function streams its response over time. (requires [AWS SAM](https://aws.amazon.com/serverless/sam/)).
28+
29+
## AWS Credentials and Signature
30+
31+
This section is a short tutorial on the AWS Signature protocol and the AWS credentials.
32+
33+
**What is AWS SigV4?**
34+
35+
AWS SigV4, short for "Signature Version 4," is a protocol AWS uses to authenticate and secure requests. When you, as a developer, send a request to an AWS service, AWS SigV4 makes sure the request is verified and hasn’t been tampered with. This is done through a digital signature, which is created by combining your request details with your secret AWS credentials. This signature tells AWS that the request is genuine and is coming from a user who has the right permissions.
36+
37+
**How to Obtain AWS Access Keys and Session Tokens**
38+
39+
To start making authenticated requests with AWS SigV4, you’ll need three main pieces of information:
40+
41+
1. **Access Key ID**: This is a unique identifier for your AWS account, IAM (Identity and Access Management) user, or federated user.
42+
43+
2. **Secret Access Key**: This is a secret code that only you and AWS know. It works together with your access key ID to sign requests.
44+
45+
3. **Session Token (Optional)**: If you're using temporary security credentials, AWS will also provide a session token. This is usually required if you're using temporary access (e.g., through AWS STS, which provides short-lived, temporary credentials, or for federated users).
46+
47+
To obtain these keys, you need an AWS account:
48+
49+
1. **Sign up or Log in to AWS Console**: Go to the [AWS Management Console](https://aws.amazon.com/console/), log in, or create an AWS account if you don’t have one.
50+
51+
2. **Create IAM User**: In the console, go to IAM (Identity and Access Management) and create a new user. Ensure you set permissions that match what the user will need for your application (e.g., permissions to access specific AWS services, such as AWS Lambda).
52+
53+
3. **Generate Access Key and Secret Access Key**: In the IAM user settings, find the option to generate an "Access Key" and "Secret Access Key." Save these securely! You’ll need them to authenticate your requests.
54+
55+
4. **(Optional) Generate Temporary Security Credentials**: If you’re using temporary credentials (which are more secure for short-term access), use AWS Security Token Service (STS). You can call the `GetSessionToken` or `AssumeRole` API to generate temporary credentials, including a session token.
56+
57+
With these in hand, you can use AWS SigV4 to securely sign your requests and interact with AWS services from your Swift app.

Examples/Streaming/README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ Anyone with a valid signature from your AWS account will have permission to invo
6868
aws lambda add-permission \
6969
--function-name StreamingNumbers \
7070
--action lambda:InvokeFunctionUrl \
71-
--principal $AWS_ACCOUNT_ID \
71+
--principal ${AWS_ACCOUNT_ID} \
7272
--function-url-auth-type AWS_IAM \
7373
--statement-id allowURL
7474
```
7575

76-
Be sure to replace <YOUR_ACCOUNT_ID> with your actual AWS account ID (for example: 012345678901).
77-
7876
### Step3: Create the URL
7977

8078
This creates [a URL with IAM authentication](https://docs.aws.amazon.com/lambda/latest/dg/urls-auth.html). Only calls with a valid signature will be authorized.
@@ -100,6 +98,11 @@ This calls return various information, including the URL to invoke your function
10098
### Invoke your Lambda function
10199

102100
To invoke the Lambda function, use `curl` with the AWS Sigv4 option to generate the signature.
101+
102+
Read the [AWS Credentials and Signature](../README.md/#AWS-Credentials-and-Signature) section for more details about the AWS Sigv4 protocol and how to obtain AWS credentials.
103+
104+
When you have the `aws` command line installed and configured, you will find the credentials in the `~/.aws/credentials` file.
105+
103106
```bash
104107
URL=https://ul3nf4dogmgyr7ffl5r5rs22640fwocc.lambda-url.us-east-1.on.aws/
105108
REGION=us-east-1
@@ -108,9 +111,9 @@ SECRET_KEY=...
108111
AWS_SESSION_TOKEN=...
109112

110113
curl "$URL" \
111-
--user "$ACCESS_KEY":"$SECRET_KEY" \
112-
--aws-sigv4 "aws:amz:$REGION:lambda" \
113-
-H "x-amz-security-token: $AWS_SESSION_TOKEN" \
114+
--user "${ACCESS_KEY}":"${SECRET_KEY}" \
115+
--aws-sigv4 "aws:amz:${REGION}:lambda" \
116+
-H "x-amz-security-token: ${AWS_SESSION_TOKEN}" \
114117
--no-buffer
115118
```
116119

@@ -205,7 +208,7 @@ Once the function is deployed, you can invoke it with `curl`, similarly to what
205208
```bash
206209
curl "$URL" \
207210
--user "$ACCESS_KEY":"$SECRET_KEY" \
208-
--aws-sigv4 "aws:amz:$REGION:lambda" \
211+
--aws-sigv4 "aws:amz:${REGION}:lambda" \
209212
-H "x-amz-security-token: $AWS_SESSION_TOKEN" \
210213
--no-buffer
211214
```
@@ -216,4 +219,4 @@ When done testing, you can delete the infrastructure with this command.
216219

217220
```bash
218221
sam delete
219-
```
222+
```

0 commit comments

Comments
 (0)