This is starter iOS Swift 4.2 client for sending single and batch messages directly to Amazon SQS. It demonstrates the event-driven design pattern for using SQS as an event source to AWS Lambda. The "Launch Stack" button in Part 1 will deploy and configure the necessary backend resources. Part 2 is this client app connecting to the backend resources via the AWS SDK for iOS.
Check out my blog below to read more about SQS as an event source and deep dive into the inner workings of this architecture:
I created a CloudFormation template that will provision:
- A standard Amazon SQS queue
- A dead letter Amazon SQS queue
- An AWS Lambda function (Node.js 8) for handling messages
- Event source mapping
- Lambda execution permissions to SQS and SNS for sending SMS text message.
- Amazon Cognito Identity Pool (with IAM roles) allowing unauthenticated and authenticated mobile users to send messages directly to the source queue.
Click on the template to launch the CloudFormation console to begin building your stack. The template defaults to the US West Oregon region (us-west-2). Should only take about 2 minutes.
Note: The stack creation will ask for your phone number as an optional parameter so that it'll automatically send you an SMS via SNS when a message arrives into the queue :) Your phone number is stored as a Lambda function environment variable.
Quick Test
: Use the following AWS CLI command (replace SourceSQSQueueURL with the SourceSQSQueueURL output of the stack results from above) to immediately test that new messages sent directly to SQS are getting picked up by your Lambda function. If you provided a valid E.164 formatted mobile number, you should see an SMS text messages with "Test message from AWS CLI" sent from an Amazon SNS phone number.
$ aws sqs send-message --queue-url <SourceSQSQueueURL>
--message-body "Test message from AWS CLI"
In this part, we'll clone this repo, update Cocoapods, update the awsconfiguration.json file with your own backend Cognito Identity pool Id generated in PART 1, and update the ViewController.swift file with your SQS queue URL, also created in PART 1. Note: To see the outputs from CloudFormation, head to the CloudFormation management console, select your stack, and then choose the Output tab.
-
Download or clone this project
$ git clone https://github.com/mobilequickie/SQSSwift.git $ cd SQSSwift
-
Install Cocoapods
$ sudo gem install cocoapods $ pod install --repo-update
-
Launch project in Xcode
$ open SQSSwift.xcworkspace
-
Update the awsconfiguration.json file with YOUR Cognito Identity Pool Id and AWS region. Shown as
CognitoIdentityPoolId
in the output tab of the CloudFormation stack created in Step 1."CredentialsProvider": { "CognitoIdentity": { "Default": { "PoolId": "<YOUR-IDENTITY-POOL-ID-HERE>", "Region": "us-west-2" } } },
-
Change the string value in the ViewController.swift file to match YOUR Amazon SQS queue. Shown as
SourceSQSQueueURL
in the output tab of the CloudFormation stack created in Step 1.let myQueueURL: String = "<YOUR-SQS-SOURCE-QUEUE-URL-HERE>"
- Cocoapods 1.5.0 +
- iOS 11.0+ / Mac OS X 10.13+
- Xcode 10.0+