Amazon Elastic Container Registry (Amazon ECR) is the best choice here because repositories are private by default and integrate directly with AWS services like ECS, EKS, and Lambda. Instead of storing long-lived AWS secret keys in GitHub, the recommended approach is to use OpenID Connect (OIDC). This lets GitHub Actions request temporary, short-lived credentials from AWS for each workflow run. ----------------------------------------- Step 1: Set Up AWS ========================================= 1. Create an ECR Repository: In the AWS Console, go to Elastic Container Registry > Repositories, click Create repository, ensure visibility is set to Private, and copy your repository URI (e.g. ://amazonaws.com). 2. Configure GitHub OIDC in AWS IAM: • In AWS IAM, add ://githubusercontent.com as an Identity provider (with audience ://amazonaws.com). • Create an IAM Role trusted by that provider (scoped to your GitHub organization/repo) and attach the AmazonEC2ContainerRegistryPowerUser managed policy. Copy the Role ARN. ----------------------------------------- Step 2: Add GitHub Secrets/Variables ========================================= In your GitHub repository, go to Settings > Secrets and variables > Actions and add: • AWS_ROLE_ARN: The ARN of the IAM role you just created. • AWS_REGION: Your ECR region (e.g., ap-south-1 or us-east-1). • ECR_REPOSITORY: The name of your ECR repository (e.g. my-app). ----------------------------------------- Step 3: GitHub Actions Workflow ========================================= Create .github/workflows/deploy-ecr.yml using the official AWS actions to authenticate and push your image: yaml name: Build and Push to Amazon ECR on: push: branches: [main] permissions: id-token: write # Required for AWS OIDC authentication contents: read # Required to checkout code jobs: build-and-push: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} aws-region: ${{ vars.AWS_REGION }} - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v2 - name: Build, tag, and push image to Amazon ECR env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} IMAGE_TAG: ${{ github.sha }} run: | docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG Note: Tagging images with ${{ github.sha }} (the commit ID) makes it easy to track exact versions and roll back deployments if needed. To deploy a private ECR image to an EC2 instance automatically, it is recommended to use AWS Systems Manager (SSM) send-command inside an existing GitHub Actions workflow. Using SSM is far more secure and reliable than traditional SSH because you do not need to open port 22 on your EC2 Security Group, manage SSH private keys in GitHub Secrets, or worry about dynamic EC2 IP addresses. Step 1: Prepare Your EC2 Instance 1. Install Docker & AWS CLI: Ensure Docker is running and the AWS CLI v2 is installed on your EC2 instance (Amazon Linux 2023 and Ubuntu AMIs include the SSM Agent by default). 2. Attach an IAM Instance Profile: Create an IAM role for EC2 with these two managed policies and attach it to your instance: • AmazonEC2ContainerRegistryReadOnly (allows EC2 to pull your private image from ECR) • AmazonSSMManagedInstanceCore (allows GitHub Actions to send commands to EC2 via SSM) Step 2: Update GitHub IAM Role Permissions In AWS IAM, add ssm:SendCommand permission to the GitHub Actions IAM role (AWS_ROLE_ARN) you created earlier so the workflow can trigger commands on your EC2 instance. Step 3: Add EC2 Instance ID to GitHub Variables In your repository under Settings > Secrets and variables > Actions > Variables, add: • EC2_INSTANCE_ID: Your target EC2 instance ID (e.g., i-0abcd1234efgh5678). Step 4: Complete Build & Deploy Workflow Update .github/workflows/deploy-ecr.yml to build, push, and trigger the deployment on EC2: name: Build, Push to ECR, and Deploy to EC2 on: push: branches: [main] permissions: id-token: write contents: read jobs: deploy: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Configure AWS credentials uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} aws-region: ${{ vars.AWS_REGION }} - name: Login to Amazon ECR id: login-ecr uses: aws-actions/amazon-ecr-login@v2 - name: Build, tag, and push image to Amazon ECR env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} IMAGE_TAG: ${{ github.sha }} run: | docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG - name: Deploy container to EC2 via SSM env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }} IMAGE_TAG: ${{ github.sha }} INSTANCE_ID: ${{ vars.EC2_INSTANCE_ID }} AWS_REGION: ${{ vars.AWS_REGION }} run: | aws ssm send-command \ --instance-ids "$INSTANCE_ID" \ --document-name "AWS-RunShellScript" \ --parameters 'commands=[ "aws ecr get-login-password --region '"$AWS_REGION"' | docker login --username AWS --password-stdin '"$ECR_REGISTRY"'", "docker pull '"$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"'", "docker stop my-app || true", "docker rm my-app || true", "docker run -d --name my-app --restart unless-stopped -p 80:8080 '"$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"'", "docker image prune -f" ]' Key Details in the Deployment Script • docker stop ... || true: Safely ignores errors on the very first deployment when no old container exists yet. • --restart unless-stopped: Automatically restarts your container if the app crashes or the EC2 instance reboots. • -p 80:8080: Maps host port 80 to container port 8080—adjust these numbers to match the port your application listens on. • docker image prune -f: Cleans up old unused images so your EC2 disk space doesn't fill up over time.