Search code examples
amazon-web-servicesamazon-ec2aws-security-groupamazon-elb

AWS: File saving into S3 from EC2 if only Elastic IP is associated to the instance in the target group. Else file not writing


I am currently learning AWS by following Pluralsight course AWS Developer: Getting Started. As a part of this course, a nodejs app(a pizza creating an app) need to deploy in AWS. This app runs on EC2 instance and when we create a new pizza and click create, a png file will be saved into an S3 bucket(Code running in the EC2 instance will write to S3).

When I triggered this app with load balancer URL, app loaded and when I try to save the newly created pizza(a png image), web page freezes and file not saving into s3.

But, if I associate an Elastic IP to one of the instances in the target group, the file is saving properly into S3.

Regarding permissions to the EC2 instance, the security group has only load balancer other than SSH.

enter image description here

And EC2 has full S3 access role.

enter image description here

But, still the app is working only if Elastic IP is associated even though the Elastic IP is unusable(as target group EC2 security group has only load balancer access other then SSH)

Why Elastic IP is needed in this case for the correct behavior.

Edit: Additional information:

S3 bucket: full public access is given.

s3 public access S3 bucket policy

Here below PUT allowed methods might be invalid. As a trail and error I provided this.

S3 CORS access

My Subnet Route configuration. I thought this is public subnet. Please correct me if I am wrong.

enter image description here

My assumption: I might be completely wrong but my understanding is that my subnet is public subnet and my ec2 instances are creating in this subnet. The EC2 instance(with full s3 access role) has a code to write PNG files to S3 with above settings(all public as per my understanding).

Hence I am expecting this ec2 should write file to S3 without Elastic IP.

But understood if auto assign IP addresses are enabled it is exactly in similar state as I am assigning Elastic IP's manually to EC2 and hence this scenario is working.

I understood that each EC2 instance will be assigned a different public IP address after enabling auto assign IP addresses. For this kind of requirements, Is this good method to go?

Having multiple public IP addresses assigned even though not using it(means will run only load balancer URL browser to access app not public IP) will cost more money?


Solution

  • There are generally three ways in which your instance can access S3 to upload your pizza file:

    1. Connect to the S3 public endpoint. This means, that your instance needs to be able connect to this endpoint over internet. If your instance is in public subnet with automatically assigned public IPs then it works out of the box. If your instance does not get auto-assigned public IP, you can use elastic IP to get a public IP for your instance.
    2. Connect to the S3 public endpoint using NAT gateway. This is useful when your instance is in private subnet, without direct access to internet nor any public IPs.
    3. Use S3 VPC gateway. This allow for fully private connections to S3 without the need for any internet access.

    Your description seems to indicate that option 1 applies to you.

    There could be other possibilities, e.g. S3 bucket has a bucket policy which allows uploads only from whitelisted IPs or IP ranges. Thus, maybe your elastic IP is whitelisted, while other IPs are not.