byyoungjin / myblog-next

https://log.byyoung.me
0 stars 1 forks source link

[FIX] post publish / deploy issue #18

Closed byyoungjin closed 3 years ago

byyoungjin commented 3 years ago
byyoungjin commented 3 years ago

Post publish 401 error && get post error

  1. API Key 가 만료되어서 나타나는 오류 https://github.com/aws-amplify/amplify-js/issues/2783 https://github.com/aws-amplify/amplify-js/issues/3089#issuecomment-490835965

  2. API key 는 계속 만료될텐데 그럼 Public access 는 어떻게 처리? https://docs.amplify.aws/cli/graphql-transformer/auth#public-authorization public access 에 대한 default provider 로 IAM 을 설정하는게 가능하다. -> 그럼 cognito pool 의 Unauthenticated Role 을 사용해서 접근한다!

예상 해결방법

1. expire 하지 않는 api key ?

만료되었을 경우 cli 상에서 갱신하는 법 https://docs.amplify.aws/cli/graphql-transformer/config-params#createapikey

아래에 따르면 api Key 는 무조건 만료된다. develop 모드에서만 쓰게 의도되었다. https://acloud.guru/forums/aws-appsync/discussion/-LXL99MF1lbDhIvu3Nnu/How%20can%20I%20make%20tokens%20without%20expiry%3F

2. public provider 를 iam 으로 설정

@auth directive with 'iam' provider found, but the project has no IAM authentication provider configured. 비슷한 이슈 https://github.com/aws-amplify/amplify-cli/issues/2396

amplify update api 실행

HYJui-MacBook-Pro:myblog-next hyj$ amplify update api
? Please select from one of the below mentioned services: GraphQL
? Select from the options below Update auth settings
? Choose the default authorization type for the API Amazon Cognito User Pool
Use a Cognito user pool configured as a part of this project.
? Configure additional auth types? Yes
? Choose the additional authorization types you want to configure for the API AP
I key, IAM
API key configuration
? Enter a description for the API key: blognext api key
? After how many days from now the API key should expire (1-365): 7

amplify push

byyoungjin commented 3 years ago

Env 와 redirect url

1. amplify 말고 env variable 이용해서 사용

amplify 상에서 redirect Url 을 여러가지 쓸수 있게 해주고, 코드상에서 그중하나를 선택할 수있지만, 사용처는

  1. 로컬에서 localhost:3000 으로 잘 동작하는지 확인할때.
  2. 로컬에서 확인을 끝내고 staged 로 push, vercel 에서 제공하는 preview 상태에서 잘 동작하는지 확인
  3. deploy 환경에서 vercel 이 제공한 url 에서 잘 동작하는지 확인

amplify code 상에서는 local 만 관리해도 될듯하고, 나머지는 NEXT_PUBLIC_VERCEL_URL 값을 이용해서, previw 일때와, public 일때 redirect url 을 관리하면 될듯하다.

2. build 과정에서 aws-exports 파일 생성!

https://stackoverflow.com/questions/59708481/aws-amplify-deploy-failure-due-to-aws-exports

3. 결론.

aws-exports 파일의 경우, env 에 따라 다르게 생성된다. 따라서 prev, develop, main env 에 따라 amplify env checkout 이 필요한데, 각 env 에 맞춰서 env checkout 하는 기능이 지원되지않는것같다. ( 환경에 따라 다른 build command 지원안되는듯 )

그래서,

1번으로 진행하고, env 마다 env variable 을 다르게 설정해주는게 맞는듯 하다.

byyoungjin commented 3 years ago

env 간 sync 과정에서 error 발생

@auth directive with 'iam' provider found, but the project has no IAM authentication provider configured.
There was an error pushing the API resource
InvalidDirectiveError: @auth directive with 'iam' provider found, but the project has no IAM authentication provider configured.

amplify update api 했던부분이 코드상에 반영이 되어있지 않은것인지 Sync 하는 과정에서 위 에러가 나타남.

그래서 develop branch 에서 바로 amplify push 하니까 정상적으로 실행됨. 이후 git checkout staged && amplify env checkout staged && amplify status

HYJui-MacBook-Pro:myblog-next hyj$ amplify status

Current Environment: staged

| Category | Resource name      | Operation | Provider plugin   |
| -------- | ------------------ | --------- | ----------------- |
| Auth     | myblognext498e72e7 | No Change | awscloudformation |
| Api      | myblognext         | No Change | awscloudformation |
| Storage  | blognextstorage    | No Change | awscloudformation |

제대로 push 됨 확인.

이상한게..

main 으로 checkout 해서 merge 하고 env main 으로 바꾸고 amplify push 하니까 또 잘됨.. 다른점은 merge 하고 Git 에 바로 push 안하고 amplify push 진행

cli 에 blackbox 부분이 많아서 조심해서 사용하자.

byyoungjin commented 3 years ago

redirect url 형식 유의

https:// 로 시작하고 / trailing slash 로 끝나야 한다. https://log.byyoung.me/ 요런 형식