Advertisement
mitrakov

Scala Play AWS SNS

Apr 3rd, 2018
930
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.52 KB | None | 0 0
  1. // Let's test Amazon SNS (Simple Notification Service)
  2.  
  3. // 1. Sign in to AWS Console and create new SNS App
  4. // 2. Specify AWS region (e.g. EU-CENTRAL-1 (Frankfurt))
  5. // 3. Create topic (e.g. "Student-Alerts", display name: "StudAlerts")
  6. // Amazon will generate new ARN (Amazon Resource Name) for your topic. e.g.
  7. arn:aws:sns:eu-central-1:760693603791:Student-Alerts
  8.  
  9. // 4. Specify 1 or more subscriptions, e.g. email address "example@yandex.ru"
  10. // Amazon will send a confirmation to your e-mail, you need to confirm the subscription
  11. // 5. [Troubleshooting] aside from following confirmation URL in your e-mail, it seems that you also need to click
  12. // on 'Confirm subscription' in SNS Console and copy-paste that URL to get it work.
  13.  
  14. // 6. Add AWS credintials in your ENV variables (please note that it may be done via source code as well):
  15. export AWS_ACCESS_KEY_ID=BVSAJ7UKCRP53EXTFQ2A
  16. export AWS_SECRET_ACCESS_KEY=4hg17YxGUyqpFIRTa/k2hwvco8QwqxT/KVH0HwdB
  17.  
  18. // 7. Preliminary work is done! Let's write a code:
  19. // build.sbt:
  20. libraryDependencies += "com.lightbend.akka" %% "akka-stream-alpakka-sns" % "0.18"
  21.  
  22. // SnsStudents.scala:
  23. import javax.inject.Inject
  24.  
  25. import scala.concurrent.Future
  26.  
  27. import akka.Done
  28. import akka.stream.Materializer
  29. import akka.stream.alpakka.sns.scaladsl.SnsPublisher
  30. import akka.stream.scaladsl.Source
  31. import com.amazonaws.regions.Regions
  32. import com.amazonaws.services.sns.{AmazonSNSAsync, AmazonSNSAsyncClientBuilder}
  33.  
  34. class SnsStudents @Inject() (implicit mat: Materializer) {
  35.   protected lazy implicit val snsClient: AmazonSNSAsync = AmazonSNSAsyncClientBuilder
  36.     .standard()
  37.     //.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("a", "b"))) // you can also use ENV vars
  38.     .withRegion(Regions.EU_CENTRAL_1) // may be specified here or via ENV variables
  39.     .build()
  40.  
  41.   def pushNotification(msg: String): Future[Done] = {
  42.     Source.single(msg).runWith(SnsPublisher.sink("arn:aws:sns:eu-central-1:760693603791:Student-Alerts"))
  43.   }
  44. }
  45.  
  46. // TestController.scala:
  47. import javax.inject.Inject
  48. import scala.concurrent.{ExecutionContext, Future}
  49. import play.api.libs.json._
  50. import play.api.mvc._
  51. import services._
  52.  
  53. class MitrakovController @Inject()(
  54.     cc: ControllerComponents,
  55.     snsStudents: SnsStudents)
  56.   (implicit ec: ExecutionContext) extends AbstractController(cc) {
  57.  
  58.   // add to routes: POST    /push    controllers.TestController.pushNotification
  59.   def pushNotification: Action[JsValue] = Action(parse.json).async { request =>
  60.     (request.body \ "text").asOpt[String] match {
  61.       case Some(msg) =>
  62.         snsStudents.pushNotification(msg) map { done =>
  63.           Ok(s"Result is: $done")
  64.         } recover { case ex =>
  65.           BadRequest(s"Error: $ex")
  66.         }
  67.       case None => Future.successful(BadRequest("Incorrect Json: 'text' attribute expected"))
  68.     }
  69.   }
  70. }
  71.  
  72. // 8. Now open curl, Postman or any other tool you wish and run:
  73. http://localhost:9000/push with json body: {"text": "We're gonna make that energy!"}
  74.  
  75.     // Email output: We're gonna make that energy!
  76.     // --
  77.     // If you wish to stop receiving notifications from this topic, please click or visit the link below to unsubscribe:
  78.     // https://sns.eu-central-1.amazonaws.com/unsubscribe.html?SubscriptionArn=arn:aws:sns:eu-central-1:760693603791:Student-Alerts:2df1740c-c7b6-4643-9b1b-ea32c891cdbf&Endpoint=tom-trix@ya.ru
  79.     // Please do not reply directly to this email. If you have any questions or comments regarding this email, please contact us at https://aws.amazon.com/support
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement