'AWS S3 - Public file access to EC2 thru URL

I am using AWS S3 for storing pdf files, docs and other files and running my Backend java application and front end react application on EC2. My requirements,

  1. EC2 backend application should have access to the S3 bucket for put and get objects using the API or the Java client
  2. EC2 front end application should be able to access the objects using the S3 URL
  3. EC2 back end application should be able to access the objects using S3 URL

Added IAM policy to get EC2 instance access to the S3 bucket and it works for put and get using the API. For, accessing objects using S3 URL, enabled pUblic access, but limiting only to the front end domain, so that nobody else can access the URL directly and it will be only thru the Front end.

{
    "Version": "2012-10-17",
    "Id": "http referer policy example",
    "Statement": [
        {
            "Sid": "Allow get requests originating from www.example.com and example.com.",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::bucket-uat/*",
            "Condition": {
                "StringLike": {
                    "aws:Referer": [
                        "http://www.example.com:9000/*",
                    ]
                }
            }
        }
    ]
}

This is working as expected. Now, I am able to access the files using S3 URL only within the front end app and not outside the front end app.

Now, I need EC2 instance also access S3 objects using the URL. Not sure, How can I add EC2 instance access in this policy. My EC2 instance count can be changing from 2 to 5,6,.... So want to make it generic so that all my current and future backend EC2 instances can access thru URL.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source