AWS

[AWS] sns, sqs aws와 연동해보기

Junuuu 2023. 6. 5. 00:01
반응형

개요

local이 아니라 dev환경에서 직접 aws sns, aws sqs와 연동해 보기

 

AwsSqsConfig

@Configuration
@Profile(value = ["dev"])
class AwsSqsConfig {

  @Bean
  @Primary
  fun amazonSQSAsync(): AmazonSQSBufferedAsyncClient {
    val amazonSQSAsync = AmazonSQSAsyncClientBuilder
      .standard()
      .withRegion(Regions.AP_NORTHEAST_2)
      .build()
    return AmazonSQSBufferedAsyncClient(amazonSQSAsync)
  }
}

dev용으로 amazonSQSConfig를 만들어주었습니다.

 

AwsSnsConfig

@Configuration
@Profile(value = ["dev"])
class AwsSnsConfig {

  @Bean(destroyMethod = "shutdown")
  fun amazonSNS(): AmazonSNS {
    return AmazonSNSClient.builder()
      .withRegion(Regions.AP_NORTHEAST_2)
      .build()
  }

amazonSnsConfig도 dev용으로 생성했습니다.

Credential은 다양한 방식이 가능할 텐데 저는 아래와 같은 방식을 사용했습니다.

 

 

AWS Credentials

https://aws.amazon.com/ko/blogs/security/aws-single-sign-on-now-enables-command-line-interface-access-for-aws-accounts-using-corporate-credentials/

 

위에서 사용한 sso방식을 사용하였습니다.

 

 

AWS 진입 화면의 Command like or programmatic access 클릭!

Option2의 profile을 복사하고 [444455556666_Administrator]를 default로 바꿉니다.

 

여기서는 IntelliJ의 plugin인 AWS Toolkit을 활용했습니다.

 

intellij 우측 하단의 AWS:… 영역 → All Credentials → Edit AWS Credential file(s) 클릭!

 

오픈한 에디터 화면에 AWS에서 복사한 내용을 붙여 넣고 그림과 같이 credential 이름을 default로 변경 후 저장.

이제 로컬에서 AWS 리소스에 접근할 수 있습니다.

 

 

AWS SNS, SQS 만들기

https://ap-northeast-1.console.aws.amazon.com/sns/v3/

 

https://ap-northeast-1.console.aws.amazon.com/sns/v3/home

 

ap-northeast-1.console.aws.amazon.com

sns 생성하기 위해서 이동

 

표준 및 기본값으로 설정 후 생성

https://mchch.tistory.com/279

 

https://ap-northeast-1.console.aws.amazon.com/sqs/v2/

 

https://ap-northeast-1.console.aws.amazon.com/sqs/v2/home

 

ap-northeast-1.console.aws.amazon.com

sqs 생성하기 위해 이동

 

표준 및 기본값으로 설정 후 생성

https://mchch.tistory.com/279

 

 

sns 구독하기

https://mchch.tistory.com/279

 

내가 만든 topic 선택

https://mchch.tistory.com/279

 

로컬에서 실행

로컬에서 intellij active profiles를 dev로 변경하고 테스트를 진행합니다.

 

sns, sqs 모두 잘 동작하지 않았습니다.

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.amazonaws.util.XpathUtils (file:/Users/junuu/.gradle/caches/modules-2/files-2.1/com.amazonaws/aws-java-sdk-core/1.12.220/7b7663197ca9cc34f52f7d10dbe9e8d9794a7705/aws-java-sdk-core-1.12.220.jar) to method com.sun.org.apache.xpath.internal.XPathContext.getDTMManager()
WARNING: Please consider reporting this to the maintainers of com.amazonaws.util.XpathUtils
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
An illegal reflective access operation has occurred

Illegal reflective access by com.amazonaws.util.XpathUtils (file:/Users/junuu/.gradle/caches/modules-2/files-2.1/com.amazonaws/aws-java-sdk-core/1.12.220/7b7663197ca9cc34f52f7d10dbe9e8d9794a7705/aws-java-sdk-core-1.12.220.jar) to method com.sun.org.apache.xpath.internal.XPathContext.getDTMManager()

Please consider reporting this to the maintainers of com.amazonaws.util.XpathUtils

Use --illegal-access=warn to enable warnings of further illegal reflective access operations

All illegal access operations will be denied in a future release

 

비슷한 이슈를 찾아보니 aws sdk v1 -> v2를 사용하라는 이야기가 보였습니다.

https://github.com/aws/aws-sdk-java/issues/1603

 

java 9: "Illegal reflective access by com.amazonaws.util.XpathUtils" · Issue #1603 · aws/aws-sdk-java

I got the following warning, running on Java 9 (running with --illegal-access=debug to get the stack trace): WARNING: Illegal reflective access by com.amazonaws.util.XpathUtils (file:/home/cvmocanu...

github.com

 

sns에서 topicArn을 세팅할 때 이름 대신 모든 url으로 다음과 같이 세팅하니 해결되었습니다.

arn:aws:sns:ap-northeast-2:39481304928341:MemberSignUpTopic

 

sqs의 경우에는 다음과 같은 예외가 발생했습니다.

The queue does not exist or no access to perform action sqs:GetQueueUrl.;
nested exception is com.amazonaws.services.sqs.model.QueueDoesNotExistException:
The specified queue does not exist or you do not have access to it. 
(Service: AmazonSQS; Status Code: 400; 
Error Code: AWS.SimpleQueueService.NonExistentQueue; 
Request ID: b6d2c2d5-deda-558a-bfea-9e1f3d597c27; Proxy: null)

분명 queue를 만들어 두었는데 찾을 수 없다고 합니다.

이럴 경우 이름이 잘못되었을 수 있지만 정책을 살펴보아야 합니다.

 

As-is

"Principal": {
        "AWS": "arn:aws:iam::{id}:root"
      },

To-be

"Principal": {
        "AWS": "*"
      },

 

 

참고자료

https://mchch.tistory.com/279

 

AWS SNS SQS 사용법 / SNS SQS 연동

SNS와 SQS 정리 SNS : Publisher가 Subscriber에게 메시지를 전송하는 서비스 SQS : 메세지 대기열 서비스 SNS 역할 특정 서비스에서 SNS로 지정한 주제(topic)에 대한 메시지를 발행한다. 예)회원가입정보 그

mchch.tistory.com