![見出し画像](https://assets.st-note.com/production/uploads/images/69570979/rectangle_large_type_2_eedb2577500ce20bee96ae1bd0fcef48.png?width=1200)
【AWS + Swift】 API Gateway + Lambda + SNS でEメール通知機能を実装してみた。
はじめに
本記事は、API Gateway + Lambda + SNSを使用したEメール通知機能の実装を紹介します。
以下、使用した各サービスの概要を簡単に説明します。
API Gateway
Amazon API Gateway は、あらゆる規模の REST、HTTP、および WebSocket API を作成、公開、維持、モニタリング、およびセキュア化するための AWS のサービスです。
https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/welcome.html
Lambda
Lambda はサーバーをプロビジョニングしたり管理しなくてもコードを実行できるコンピューティングサービスです。
https://docs.aws.amazon.com/ja_jp/ja_jp/lambda/latest/dg/welcome.html
SNS
Amazon Simple Notification Service (Amazon SNS) は、配信者から受信者 (または生産者から消費者) へのメッセージ配信を提供するマネージドサービスです。発行者は、論理アクセスポイントおよび通信チャネルであるトピックにメッセージを送信することで、受信者と非同期的に通信します。クライアントは、Amazon Kinesis Data Firehose、Amazon SQS、AWS Lambda、HTTP、E メール、モバイルプッシュ通知、モバイルテキストメッセージ (SMS) などのサポートされたエンドポイントを使用して SNS トピックにサブスクライブし発行されたメッセージを受信できます。
https://docs.aws.amazon.com/ja_jp/sns/latest/dg/welcome.html
使用環境
● OS:macOS Big Sur 11.3.1
● Xcode:12.5
● Swift:5.4
● Postman
実装
AWS側
1、AWSマネジメントコンソールのページへ飛ぶ。
URL ⇨ https://aws.amazon.com/jp/console/
![](https://assets.st-note.com/img/1641786877569-z9BH1pw6L7.png?width=1200)
2、検索欄に「API Gateway」と入力し、一覧から【API Gateway】を押下する。
![](https://assets.st-note.com/img/1641787438490-JGpt77c5b5.png?width=1200)
3、【APIを作成】ボタンを押下する。
![](https://assets.st-note.com/img/1641787811861-muBub6WZOD.png?width=1200)
4、APIタイプ選択一覧の【REST API】を選択する。
![](https://assets.st-note.com/img/1641788242149-LDMjQsJav1.png?width=1200)
5、各項目を下記の画像の様に設定し、【APIの作成】を押下する。
![](https://assets.st-note.com/img/1641788744286-U07O4Iuzrc.png?width=1200)
6、リソース設定画面の【アクション】⇨【リソースの作成】の順にリソース作成画面に遷移し、リソース名を入力する。
![](https://assets.st-note.com/img/1641789445391-pQXV3LXPv3.png?width=1200)
![](https://assets.st-note.com/img/1641790139942-mQWYKzLZxP.png?width=1200)
7、【アクション】⇨【メソッドの作成】⇨【POST】⇨【✅】の順に選択する。
![](https://assets.st-note.com/img/1641791104481-0wa6WhoWMW.png?width=1200)
![](https://assets.st-note.com/img/1641791117697-Bj9qNdsIqr.png?width=1200)
8、検索欄に「Lambda」と入力し、一覧から【Lambda】を押下する。
![](https://assets.st-note.com/img/1641791506863-tiDFIOO0vz.png?width=1200)
9、【関数の作成】を押下する。
![](https://assets.st-note.com/img/1641792773249-NZ39UZQNv3.png?width=1200)
10、各項目を下記の画像の様に設定し、【関数の作成】を押下する。
![](https://assets.st-note.com/img/1641792880287-FISQ8RWl6q.png?width=1200)
![](https://assets.st-note.com/img/1641792907605-xXuSSUjjvF.png?width=1200)
![](https://assets.st-note.com/img/1641793412015-Ba7PxNQCLQ.png?width=1200)
11、検索欄に「IAM」と入力し、一覧から【IAM】を押下する。
![](https://assets.st-note.com/img/1641801206394-mliLhcFBRz.png?width=1200)
12、IAM画面の【アクセス管理】⇨【ロール】⇨【ロールを作成】の順に押下する。
![](https://assets.st-note.com/img/1641801942585-y2VMdrwP2t.png?width=1200)
13、【Lambda】⇨【次のステップ:アクセス権限】の順に押下する。
![](https://assets.st-note.com/img/1641801961385-DFWq8ZNVAX.png?width=1200)
14、検索欄から「SNS」を入力し、【AmazonSNSFullAccess】を選択する。
![](https://assets.st-note.com/img/1641803035539-6Q7zrbJXK8.png?width=1200)
15、【次のステップ:確認】を押下する。
![](https://assets.st-note.com/img/1641803235668-X2Lk6dR8ut.png?width=1200)
16、ロール名を入力し、【ロールの作成】を押下する。
![](https://assets.st-note.com/img/1641803393085-sz5li14PWx.png?width=1200)
17、検索欄に「Lambda」と入力し、一覧から【Lambda】を押下する。
![](https://assets.st-note.com/img/1641803500710-5npdwSBdnf.png?width=1200)
18、【設定】⇨【編集】の順に、編集画面に遷移する。
![](https://assets.st-note.com/img/1641804181984-OLWVVO0CDf.png?width=1200)
19、IAMで作成したロールを選択し、【保存】を押下する。
![](https://assets.st-note.com/img/1641804547659-90oN5vDZ6u.png?width=1200)
20、実行するコードを書き換えを行う。
![](https://assets.st-note.com/img/1641804747786-HdgIJXkAuO.png?width=1200)
![](https://assets.st-note.com/img/1641809988641-ca9OWmKv6L.png?width=1200)
21、検索欄に「SNS」と入力し、一覧から【Simple Notification Service】を押下する。
![](https://assets.st-note.com/img/1641810164822-stfRmTXvBs.png?width=1200)
22、【トピックの作成】を押下し、トピック作成画面に遷移する。
![](https://assets.st-note.com/img/1641810421270-5vPUsWdupu.png?width=1200)
23、【タイプ】はスタンダードを選択を行い、【名前】、【表示名】を好きなものを入力し、トピックを作成する。
![](https://assets.st-note.com/img/1641810652032-aPgKfZKZ1u.png?width=1200)
24、【サブスクリプションの作成】を押下し、サブスクリプション作成画面に遷移する。
![](https://assets.st-note.com/img/1641810790346-JOs6d6btZQ.png?width=1200)
25、【プロトコル】にはEメール選択を行い、【エンドポイント】には送信先メールアドレスを入力し、サブスクリプションを作成する。
![](https://assets.st-note.com/img/1641811749376-3Ra3zT74DD.png?width=1200)
26、サブスクリプション作成後、入力したメールアドレス宛にサブスクリプション認証許可通知が届くので、【Confirm subscription】を押下する。
押下後、認証が完了するためサブスクリプション詳細画面で【ステータス】が「確認済み」なっているか確認する。
![](https://assets.st-note.com/img/1641812154169-kngAlopa2F.png?width=1200)
![](https://assets.st-note.com/img/1641812172422-1IJoKygzyN.png?width=1200)
![](https://assets.st-note.com/img/1641812189134-qia74Uaiz0.png?width=1200)
27、検索欄に「API Gateway」と入力し、一覧から【API Gateway】を押下する。
![](https://assets.st-note.com/img/1641812491659-0NHfbtm665.png?width=1200)
28、【Lambda関数】に作成したLambda関数を選択し、【保存】を押下する。
![](https://assets.st-note.com/img/1641812861523-brB9hYqY2i.png?width=1200)
29、検索欄に「SNS」と入力し、一覧から【Simple Notification Service】を押下する。
![](https://assets.st-note.com/img/1641813369567-AHltsV6GIC.png?width=1200)
30、作成したトピックの詳細画面で表示されているARNをコピーする。
![](https://assets.st-note.com/img/1641813394081-Pi6hoAb64p.png?width=1200)
31、検索欄に「Lambda」と入力し、一覧から【Lambda】を押下する。
![](https://assets.st-note.com/img/1641813544450-AzvFmpTWbn.png?width=1200)
32、トピック詳細画面でコピーしたARNをペーストする。
![](https://assets.st-note.com/img/1641814029615-cFeICPEkYL.png?width=1200)
33、検索欄に「API Gateway」と入力し、一覧から【API Gateway】を押下する。
![](https://assets.st-note.com/img/1641814046977-pKXwIRa10H.png?width=1200)
34、【アクション】⇨【APIのデプロイ】の順にデプロイ実行画面に遷移する。
![](https://assets.st-note.com/img/1641814533532-WH3CYDdKHk.png?width=1200)
35、下記の画像の様に設定を行い、【デプロイ】を押下する。
![](https://assets.st-note.com/img/1641814546039-RnHd17DoBP.png?width=1200)
36、生成されたURLをコピーする。
![](https://assets.st-note.com/img/1641814557819-iVnwW303H9.png?width=1200)
37、コピーしたURLをPostmanを使用して、APIを叩く。
![](https://assets.st-note.com/img/1641814942891-skMhEfhY5C.png?width=1200)
38、指定したメールアドレスに通知が届く。
![](https://assets.st-note.com/img/1641815638549-LNQTCn959K.png?width=1200)
Xcode側
UI
![](https://assets.st-note.com/img/1641746315176-DZhkThIoce.png?width=1200)
コード
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var sendBtn: UIButton! {
didSet {
sendBtn.layer.borderColor = UIColor.blue.cgColor
sendBtn.layer.borderWidth = 1
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func sendMessageAction(_ sender: Any) {
EmailMessageSendApi()
}
private func EmailMessageSendApi() {
var params = Dictionary<String,String>()
params["username"] = "Hanagasaki"
params["email"] = "sample@gmail.com"
params["contact"] = "123456789"
let request = NSMutableURLRequest(url: NSURL(string: "https://rf6u8jsv1a.execute-api.ap-northeast-1.amazonaws.com/test/user")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
do {
request.httpBody = try JSONSerialization.data(withJSONObject: params, options: [])
} catch let error as NSError {
print(error.localizedDescription)
}
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
do {
let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments)
print(json)
}
catch let error as NSError {
print(error.localizedDescription)
}
})
dataTask.resume()
}
}
送信結果
![](https://assets.st-note.com/img/1641815620812-1CtY5MGpBK.png?width=1200)
おわりに
API Gateway + Lambda + SNSでモバイル端末にEメール通知する機能について、紹介しました。
次はCognito(認証機能)について、学習しようと思います。