Back to Home

Automating AWS S3 Deployments with GitHub CI/CD Pipeline

Set up automated deployments to your AWS S3 bucket directly from a GitHub repository using IAM credentials and CI/CD workflows.

Step 1. Create an IAM User Using CloudShell

Open AWS CloudShell and create a dedicated deployer user:

aws iam create-user --user-name github-s3-deployer

Step 2. Attach S3 Bucket Policy to User

Create a policy file defining the required S3 permissions for deployment:

cat <<EOF > user-policy.json
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "s3:PutObject",
        "s3:GetObject",
        "s3:ListBucket",
        "s3:DeleteObject"
      ],
      "Resource": [
        "arn:aws:s3:::your-bucket-name",
        "arn:aws:s3:::your-bucket-name/*"
      ]
    }
  ]
}
EOF
aws iam put-user-policy --user-name github-s3-deployer --policy-name GitHubS3DeployPolicy --policy-document file://user-policy.json

Step 3. Generate Access Keys

Generate access keys for the deployer user (make sure to save the output of this command):

aws iam create-access-key --user-name github-s3-deployer

Step 4. Configure GitHub Repository Secrets

In your GitHub repository, navigate to Settings > Secrets and variables > Actions, then click New repository secret to add the following secrets:

Name: AWS_ACCESS_KEY_ID — paste the access key ID as the value.
Name: AWS_SECRET_ACCESS_KEY — paste the secret access key as the value.

Step 5. Create GitHub Actions Workflow File

In your repository, create a file at .github/workflows/deploy.yml with the following content:

name: Deploy Website to AWS S3

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1

      - name: Sync files to S3
        run: |
          aws s3 sync . s3://your-bucket-name --delete --exclude ".git/*" --exclude ".github/*"

And that's it! Try committing to your main branch and check your S3 bucket to verify the automated deployment.