5 주차 - AWS 실습 - SNS 시나리오 1. SNS Topic 생성 2. 3. 4. 5. Subscriptions 생성및 Confirm [ Email Test ] Message 발송 코드로보기 번외 ) SMS 발송하기 실습준비 HTML 파일, AWS 계정및 secretaccesskey, accesskeyid 간단설명 1. 2. 3. 4. SNS : 이메일, 문자, 모바일, 시스템으로 Message를전달하는서비스. ARN 구성방식 : [ arn:aws:sns: region: account_id : topic_name ] SMS Region : http://docs.aws.amazon.com/ko_kr/sns/latest/dg/sms_supported-countries.html 여기를클릭하여펼치기... 엔드포인트유형 프리티어 요금 모바일푸시알림 1백만 백만건당 0.50 USD Worldwide SMS 100 자세히알아보기 email/email-json 1,000 100,000건당 2.00 USD HTTP/s 100,000건 백만건당 0.60 USD Simple Queue Service(SQS) Lambda 함수 SQS 대기열로전송하는비용은부과되지않음 Lambda 로전송하는비용은부과되지않음 국내 [ SMS ], 미국번호발송은 100건무료 네트워크 / HNI 트랜잭션 SMS 프로모션 SMS KT / 450 004 $ 0.03712 $ 0.03712 KT 다이렉트 / 450 002 $ 0.03712 $ 0.03712 LG 데이콤 / 450 006 $ 0.03712 $ 0.03712 기타 / 450 999 $ 0.0498 $ 0.0306 SK 텔레콤 / 450 003 $ 0.03712 $ 0.03712 SK 텔레콤 / 450 005 $ 0.03712 $ 0.03712 올레 KT / 450 008 $ 0.03712 $ 0.03712 2017.03.07 기준 실습
1. SNS Topic 생성여기를클릭하여펼치기... Create Topic 실행 Topic Name, Display name 입력후 Create topic * Topic name : 특수문자는 hyphens( - ), underscores( _ ) 만허용한다. Topics 메뉴를실행후 Topic 확인및 ARN 복사
2. Subscriptions 생성및 Confirm [ Email Test ] 여기를클릭하여펼치기... Subscriptions 메뉴에서 Create subscription 실행 [ Topic ARN : ARN 붙여넣기, Protocol : Email 선택, Endpoint : Email 입력 ] 후 Create subscription 실행
입력한 Email 주소에서메일확인후 Confirm Subscription 실행 [ PendingConfirmation 에서 ARN 으로변경확인 ] 3. Message 발송여기를클릭하여펼치기... Publish Message 실행 [ Topic ARN : ARN 붙여넣기, Subject : 제목입력, Message format : JSON 선택 ] 후 JSON message generator 실행
[ Message : 입력, Check : Email ] 후 Generate Json 실행 Json format 확인후 Publish message 실행
메일확인 * EndPoint별 Message 전송 4. 코드로보기 a. 사전준비 Javascript <script src="https://sdk.amazonaws.com/js/aws-sdk-2.22.0.min.js"></scr ipt> Java, Maven <dependency> <groupid>com.amazonaws</groupid> <artifactid>aws-java-sdk</artifactid> <version>1.11.98</version> <scope>compile</scope> </dependency> b. secretaccesskey, accesskeyid 준비하기
Javascript var sns = new AWS.SNS({secretAccessKey:'[secretAccessKey]', accesskeyid:'[accesskeyid]', region : '[region]'}); Java BasicAWSCredentials awscreds = new BasicAWSCredentials( "[accesskey]", "[secretkey]" ); AmazonSNS snsclient = AmazonSNSClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(awsCreds)).withRegion(Regions.US_ EAST_1).build(); c. Topic 생성 Javascript var params = { Name : 'sns-topic' }; sns.createtopic(params, function (err, data){ if (err){ console.log(err, err.stack); } else { var topicarntag = document.createelement("span") topicarntag.innerhtml = 'topicarn : ' + data.topicarn; document.body.appendchild(topicarntag); } }); Java CreateTopicRequest createtopicrequest = new CreateTopicRequest("sns-topic"); CreateTopicResult createtopicresult = snsclient.createtopic(createtopicrequest); d. Message 발송
Javascript var params = { Message: 'Hello SNS', Subject: 'Hello', TopicArn : '[TopicArn]' }; sns.publish(params, function (err, data) { if (err){ console.log(err, err.stack); } else { var topicarntag = document.createelement("span") topicarntag.innerhtml = 'MessageId : ' + data.messageid; document.body.appendchild(topicarntag); } }); Java String msg = "My Money..."; String subject = "Hello"; PublishRequest publishrequest = new PublishRequest( "[TopicArn]", msg, subject ); PublishResult publishresult = snsclient.publish(publishrequest); System.out.println("MessageId - " + publishresult.getmessageid()); e. Topic 삭제 Javascript var params = { TopicArn: '[TopicArn]' }; sns.deletetopic(params, function(err, data) { if (err){ console.log(err, err.stack); // an error occurred } else{ console.log(data); var topicarntag = document.createelement("span") topicarntag.innerhtml = 'RequestId : ' + data.responsemetadata.requestid; document.body.appendchild(topicarntag); } });
Java DeleteTopicRequest deletetopicrequest = new DeleteTopicRequest("[TopicArn]"); snsclient.deletetopic(deletetopicrequest); System.out.println("DeleteTopicRequest - " + snsclient.getcachedresponsemetadata(deletetopicrequest)); f. subscribe Javascript var params = { Protocol: 'email', TopicArn: '[TopicArn]', Endpoint: '[email]' }; sns.subscribe(params, function(err, data) { if (err) console.log(err, err.stack); // an error occurred else console.log(data); // successful response }); 5. 번외 ) SMS 발송하기 Javascript var params = { Message: 'Hello SNS', PhoneNumber : '+8210[8]' }; sns.publish(params, function (err, data) { if (err){ console.log(err, err.stack); } else { var topicarntag = document.createelement("span") topicarntag.innerhtml = 'MessageId : ' + data.messageid; document.body.appendchild(topicarntag); } });
String msg = "My Money..."; String phonenumber = "+8210[8]"; Java Map<String, MessageAttributeValue> smsattributes = new HashMap<String, MessageAttributeValue>(); smsattributes.put("aws.sns.sms.senderid", new MessageAttributeValue().withStringValue("senderID").withDataType("S tring")); smsattributes.put("aws.sns.sms.smstype", new MessageAttributeValue().withStringValue("Promotional").withDataType ("String")); PublishResult publishresult = snsclient.publish(new PublishRequest().withMessage(msg).withPhoneNumber(phoneNumber).with MessageAttributes(smsAttributes)); System.out.println("MessageId - " + publishresult.getmessageid()); 말하지못한것들. CloudWatch Service Metrics,,, Message Size. Topic 의 delivery Policy 설정으로재시도및발송정책에대해서변경할수있습니다. 준비하면서느낀단점. ( 홀로생각 ) SNS 를이용한 Topic 설계는한계가있는것으로보여진다. - Topic 의계층구조를이용하기어렵다. 계층구조를이용하기위해서는 Iot 를이용하여야할것같다. CRM 서비스가약간 (?) 은어려울것같다. - 쉽게구현하려면 Pinpoint 를추천합니다.
장점 SNS : Simple Notification Service,. SNS : https://pyrasis.com/book/theartofamazonwebservices/chapter26 Javascript API Reference : http://docs.aws.amazon.com/awsjavascriptsdk/latest/aws/sns.html Javascript : https://pyrasis.com/book/theartofamazonwebservices/chapter30/11 SNS : https://aws.amazon.com/ko/sns/pricing/ SMS : https://aws.amazon.com/ko/sns/sms-pricing/