Production AWS setup - restarone/violet_rails GitHub Wiki

AWS IAM setup

We will create a user with programmatic access and not grant any permissions to that user. Make sure to download the credentials Screenshot from 2021-05-21 16-34-04 Next we navigate to policies and create a policy that we can attach directly (so the programmatic user will ONLY have access to this specific S3 bucket) Screenshot from 2021-05-21 16-34-57 Create a customer managed policy with the following configuration (replace example.com with the name of your bucket-- which you will create later), give it a name you can quickly lookup later:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": "arn:aws:s3:::example.com"
        },
        {
            "Action": "s3:*",
            "Effect": "Allow",
            "Resource": [
                "arn:aws:s3:::example.com",
                "arn:aws:s3:::example.com/*"
            ]
        }
    ]
}

Next, attach that policy directly to the user (by searching for it, because its customer managed):

Screenshot from 2021-05-21 16-40-36 Once the policy is attached (feel free to use the policy simulator to test if it works) hop over to S3.

AWS S3 setup

create a bucket with the name you provided before (for me its example.com). Navigate to the permissions tab on the bucket and click on CORS configuration: Screenshot from 2021-05-21 16-36-32 Set the following CORS config (allow all origins) and click save.

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "PUT",
            "POST",
            "DELETE"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": []
    }
]

Hopefully everything is correctly set and your user is able to programmatically access that specific S3 bucket (and limited only to that one). Now go to Heroku and set the following variables related to S3:

AWS_ACCESS_KEY_ID=foo
AWS_SECRET_ACCESS_KEY=foo
AWS_REGION=us-east-1
AWS_BUCKET=example.com

To test if storage works, login to the admin panel (yourdomain.com/admin) and then click on web settings to upload a company logo/favicon: Screenshot from 2021-05-21 17-02-56