이것저것 해보기🌼

Github Actions에서 AWS RDS 접속 허용하기 (보안그룹 인바운드 규칙 추가) 본문

AWS

Github Actions에서 AWS RDS 접속 허용하기 (보안그룹 인바운드 규칙 추가)

realtree 2024. 9. 12. 17:52

 

현재 Node.js 로 만든 서버를 Github Actions로 빌드 및 배포(파이어베이스) 하고 있다.

문제는 server 실행 시 AWS RDS를 접근 불가하여 ECONNREFUSED 에러가 발생하는 것이다.

Github Actions와 Firebase에서도 RDS 에 접근 가능하게 허용해주어야한다.

 

 

Github Actions 의 경우, IP가 유동적으로 바뀌기 때문에

배포 과정에 현재의 IP를 알아내어 그것을 보안그룹 인바운드 규칙에 추가해줄것이다.

그러기 위해서 보안그룹을 수정할 수 있는 AWS Credentials를 만들어야한다.

 

AWS EC2 CLI 공식문서

https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html

 

authorize-security-group-ingress — AWS CLI 1.34.17 Command Reference

Note: You are viewing the documentation for an older major version of the AWS CLI (version 1). AWS CLI version 2, the latest major version of AWS CLI, is now stable and recommended for general use. To view this page for the AWS CLI version 2, click here. F

docs.aws.amazon.com

Security Group 에 inbound 규칙을 추가하는 CLI 명령어에 대한 도쿄멘트다.

 

 

AWS IAM 추가 및 Credentials 만들기

IAM User를 만들고 권한정책을 등록한다.

 

IAM 권한 정책

아래 게시글을 참고해서 권한을 주었다.

https://repost.aws/questions/QUYH9ZcRolRqWXS4W5YTHYaw/iam-role-needed-to-assign-a-security-group-to-a-running-ec2-instance

 

IAM role needed to assign a security group to a running EC2 instance

What is the proper IAM role required to assign an existing security group to a running EC2 instance? My current permissions are: ``` AuthorizeSecurityGroupEgress AuthorizeSecurityGroupIngress Rev...

repost.aws

 

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Statement1",
            "Effect": "Allow",
            "Action": [
                "rds:AuthorizeDBSecurityGroupIngress",
                "ec2:DescribeSecurityGroups",
                "ec2:DescribeSecurityGroupRules",
                "ec2:AuthorizeSecurityGroupIngress",
                "ec2:DescribeNetworkInterfaceAttribute",
                "ec2:DescribeNetworkInterfaces",
                "ec2:ModifyInstanceAttribute",
                "ec2:ModifyNetworkInterfaceAttribute",
                "ec2:DescribeInstances",
                "ec2:AuthorizeSecurityGroupEgress",
                "ec2:AuthorizeSecurityGroupIngress",
                "ec2:RevokeSecurityGroupEgress",
                "ec2:RevokeSecurityGroupIngress",
                "ec2:UpdateSecurityGroupRuleDescriptionsEgress",
                "ec2:UpdateSecurityGroupRuleDescriptionsIngress"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

 

 

해당 User의 액세스 키를 새로 발급받는다.

 

 

Github Secret에 발급받은 액세스 키를 등록한다.

- Access Key ID

- Secret Access Key

 

 

내 Security Group을 무엇을 사용중인지 확인해서, SG ID도 Secret에 추가한다.

 

Workflow 수정하기

AWS Credentials 및 Security Group에 현재 ip 주소 추가하기

jobs:
  build_and_preview:
    if: ${{ github.event.pull_request.head.repo.full_name == github.repository }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Get Github action IP
        id: ip
        uses: haythem/public-ip@v1.2

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} 
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} 
          aws-region: ap-northeast-2

      - name: .env setting
        run: |
          echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env
          echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env
          echo "DB_USER=${{ secrets.DB_USER }}" >> .env
          echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env
          echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env

      - name: Add Github Actions IP to Security group
        run: |
          aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 3306 --cidr ${{ steps.ip.outputs.ipv4 }}/32 
      
      - name: install npm dependencies and run backend
        run: cd backend && npm install && npm run server

 

 

문제점

문제는 이렇게 build 실행해도 안된다.

 

AWS 콘솔에서는 해당  IP 주소로도 DB 포트 (3306)으로 접속 가능하게 규칙이 추가된 상황이다.

 

 

무엇이 문제일까?

우선 여기까지 정리하고 다음에 알아볼 예정이다.

 

 

참고

https://makethree.tistory.com/19

 

AWS EC2 인스턴스 인바운드 IP 제한 상태에서 Github Action 배포

AWS EC2 인스턴스 인바운드 보안 설정 상태에서 Github Action 배포 자동화하기 일단 먼저 인스턴스를 만드는 방법이 궁금하신 분들은 AWS 프리티어 인스턴스 만들기 에 자세하고 쉽게 설명해놨으니

makethree.tistory.com