Setup ECR Terraform, update GitHub Actions for OIDC #14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy via OIDC | |
| on: | |
| push: | |
| branches: [ "main" ] | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| id-token: write | |
| contents: read | |
| steps: | |
| # 1. Checkout repo | |
| - uses: actions/checkout@v4 | |
| # 2. Configure AWS OIDC | |
| - name: Configure AWS Credentials via OIDC | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| role-to-assume: arn:aws:iam::011664843975:role/github-permissions | |
| aws-region: ap-south-1 | |
| # 2a. Install Terraform | |
| - name: Install Terraform | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y gnupg software-properties-common curl | |
| curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg | |
| echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list | |
| sudo apt update | |
| sudo apt install -y terraform | |
| terraform -version | |
| # 3. Terraform Init & Apply (create ECR, RDS, etc.) | |
| - name: Terraform Init & Apply | |
| working-directory: terraform | |
| run: | | |
| terraform init | |
| terraform fmt -check | |
| terraform validate | |
| terraform apply -auto-approve | |
| # 4. Get ECR repository URL from Terraform output | |
| - name: Get ECR Repo URL | |
| id: ecr | |
| working-directory: terraform | |
| run: | | |
| echo "REPO_URL=$(terraform output -raw ecr_repository_url)" >> $GITHUB_ENV | |
| # 5. Login to ECR (via OIDC) | |
| - name: Login to ECR | |
| uses: aws-actions/amazon-ecr-login@v2 | |
| # 6. Build Docker image | |
| - name: Build Docker Image | |
| run: | | |
| docker build -t serverless-crud-api-terraform . | |
| # 7. Tag & push Docker image | |
| - name: Tag and Push Docker Image | |
| run: | | |
| docker tag serverless-crud-api-terraform:latest $REPO_URL:latest | |
| docker push $REPO_URL:latest | |
| # 8. Update Lambda to use new image (Terraform) | |
| - name: Terraform Deploy Lambda | |
| working-directory: terraform | |
| run: | | |
| terraform apply -auto-approve |