route 53 검색해서 들어가기

내 도메인 이름 확인: backend-ec2.com
Hosted Zone 생성하기: 내 도메인 이름만 정확히 적어주고 생성하면 됨


하지만 여기까지 적용하면서 내가 이 도메인의 주인이라는걸 검증하는 절차가 전혀 없었음 .. 남의 도메인 가져다가 설정해도 가능하다는 말임.
➡️ 따라서, 우리는 생성된 Hosted Zone의 네모칸 안에 있는 정보들을 기존 Lightsail의 domain (우리가 생성한 곳)으로 가서 Name server로 추가함으로서 우리가 이 domain의 주인임을 검증함 (만약 내 domain이면 아래와 같이 lightsail에서 name server를 수정할 수 있을 것이므로)


Lightsail의 Name server 추가하기

DNS Zone 삭제



http://backend-ec2.com











https://backend-ec2.com로 접속하면 다시 무한로딩이 시작됨.
https-public-sg라는 이름으로 Security Group 하나 새로 만들기



https://backend-ec2.com



docker-compose.yml 파일 생성version: "3"
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
volumes:
- ./app:/usr/src/my-app/app
ports:
- "4000:4000"
environment:
REDIS_URL: redis://redis:6379
PORT: 4000
depends_on:
- redis
redis:
image: "redis:alpine"
docker-compose.test.yml 파일 생성version: "3"
services:
web:
build:
context: .
dockerfile: Dockerfile.dev
environment:
TEST_REDIS_URL: redis://redis:6379
command: npm run test:ci
depends_on:
- redis
redis:
image: "redis:alpine"
index.test.ts 파일 수정// const REDIS_URL = "redis://default:test_env@localhost:6380";
beforeAll(async () => {
client = redis.createClient({ url: process.env.TEST_REDIS_URL });
await client.connect();
app = createApp(client);
});
docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit 으로 테스트해보기

code 0, 코드 실패 시 code 1으로 멈춤.github/workflows 폴더 안에 있던 다른 .yml 파일들 꺼내고 testAndDeploy.yml 넣기name: testAndDeploy
on:
push:
branches:
- main
jobs:
tests:
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build and test with Docker Compose
run: docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
git add ., git commit -m "updated github action workflow", git push origin dev 로 깃허브 업로드
Github docs 참고해서 ProviderURL, Audience 등 설정


testAndDeploy.yml 수정하기name: testAndDeploy
on:
push:
branches:
- main
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
tests:
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build and test with Docker Compose
run: docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
deploy:
runs-on: ubuntu-latest
needs: tests
steps:
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::864899835120:role/github_minzix_express_OIDC
role-session-name: sampleSessionName
aws-region: ${{ env.AWS_REGION }}
.yml 파일 하나로 변경 시, git dev 에서 checkout하고 git master로 브랜치 이동해서 pull 해야 함git checkout master, git pulltask_definition.json 파일로 저장
"image": "864899835120.dkr.ecr.ap-northeast-2.amazonaws.com/express:3", 부분은 삭제!TabShift + Tab# 최종 testAndDeploy.yml 파일
name: testAndDeploy
on:
push:
branches:
- main
# 공식문서 복붙
env:
AWS_REGION: ap-northeast-2
ECR_REPOSITORY: express
ECS_SERVICE: express-service
ECS_CLUSTER: my-express-cluster
ECS_TASK_DEFINITION: ./task_definition.json
CONTAINER_NAME: express
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
tests:
runs-on: ubuntu-22.04
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Build and test with Docker Compose
run: docker-compose -f docker-compose.test.yml up --build --abort-on-container-exit
deploy:
runs-on: ubuntu-latest
needs: tests
steps:
# 여기부터 공식문서 복붙
- name: Checkout
uses: actions/checkout@v4
- name: Configure AWS credentials # 이 부분은 공식문서 update 가 되지 않아서 수정
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::864899835120:role/github_minzix_express_OIDC
role-session-name: sampleSessionName
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@62f4f872db3836360b72999f4b87f1ff13310f3a
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ github.sha }}
run: |
# Build a docker container and
# push it to ECR so that it can
# be deployed to ECS.
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT
- name: Fill in the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@c804dfbdd57f713b6c079302a4c01db7017a36fc
with:
task-definition: ${{ env.ECS_TASK_DEFINITION }}
container-name: ${{ env.CONTAINER_NAME }}
image: ${{ steps.build-image.outputs.image }}
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@df9643053eda01f169e64a0e60233aacca83799a
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: ${{ env.ECS_SERVICE }}
cluster: ${{ env.ECS_CLUSTER }}
wait-for-service-stability: true
git add ., git commit -m "updated github action deploy", git pushdev에서 master branch로 checkout하는 과정에서 갑자기 github action이 trigger 되지 않아서 의아했는데, github action이 trigger 되기 위한 조건으로는 testAndDeploy.yml 파일이 .github/workflows/ 디렉터리에 위치해야 한다고 한다.