티스토리 뷰

aws component를 프로그래밍에 이용할 때 아래와 같이 sdk를 추가해 사용할 것이다
s3와 appliation.yml에 들어갈 프로퍼티를 암호화하기 위해 aws parameter store를 사용할 예정이다

// AWS
implementation("software.amazon.awssdk:s3:$awsVersion")
implementation("software.amazon.awssdk:sts:$awsVersion")

// Parameter Store
implementation(platform("io.awspring.cloud:spring-cloud-aws-dependencies:$springCloudVersion"))
implementation("io.awspring.cloud:spring-cloud-aws-starter-parameter-store")

 

 

순정 상태 그대로 실행한다면 아래 에러를 만날 것이다. 

Unable to load region from system settings. Region must be specified either via environment variable (AWS_REGION) or system property (aws.region).

말 그대로 system settings에서 aws region 관련 정보를 찾지 못 하겠다는 의미이고 이를 해결하기 위한 몇 가지 방법이 있다

 

1. .env 파일에 AWS_REGION 추가

AWS_REGION=ap-northeast-2

2. 애플리케이션 실행 시 system property 추가

@PostConstruct
void setUpRegion() {
  System.setProperty("aws.region", "ap-northeast-2")
}

3. intellij 사용 시 aws toolkit 설치

settings > plugins > aws toolkit 설치 시 알아서 aws core까지 설치된다

재시작 후 사이드 바 어딘가에 추가된 aws toolkit 클릭 후 default region을 설정해준다

테스트 용도라면 굳이 실제 사용할 aws region으로 바꾸지 않아도 된다

 

추가로 edit configurations에서 AWS Connection이 None -> Use the currently... 로 바꿔주면 된다

 

4. aws cli 설치

맥 기준 awscli 설치 후 사용자의 정보로 세팅을 맞춰주면 된다

aws toolkit 설치 후 행했던 작업들이 aws configure 작업을 대신하는 것이다

# 설치
brew install awscli

# 설정
aws configure

AWS Access Key ID [None]:
AWS Secret Access Key [None]:
Default region name [None]:
Default output format [None]:

 

개발, 운영 환경에서는 aws 내부적으로 metadata service를 이용해 가져오니 별도 설정이 필요없다

로컬, 테스트 환경에서 애플리케이션 실행을 위해선 .env 파일이나 System Property를 사용하면 된다

.env는 git에 노출되지 않도록 주의해야하며 System.setProperty는 multi region 사용 시엔 문제가 있다

또한 .env 파일이나 property를 수동으로 처리해야하고 까먹을 수도 있으니 aws toolkit or cli를 사용하는게 더 낫겠다

 

가장 좋은 방법은 팀원 모두가 awscli 설정을 마치는 것이겠으나 때려죽어도 하기 싫다면

앱 실행 시 .env 파일 참조, 테스트 코드 작성 시 공통 클래스에 system property 설정으로 해도 문제 없을 것 같다

import org.junit.jupiter.api.BeforeAll;
import software.amazon.awssdk.regions.Region;

public abstract class AbstractTest {

  @BeforeAll
  static void beforeAll() {
    System.setProperty("aws.region", Region.AP_NORTHEAST_2.toString());
  }
}
댓글
링크
글 보관함
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Total
Today
Yesterday