title: Is using InstanceProfileCredentialsProvider correct way to connect to AWS S3 from Ec2 instance?
body: <p>Until my code works, I have set bucket policy too wide open. </p>
  
  <pre><code>{
      "Version": "2012-10-17",
      "Id": "Policy15",
      "Statement": [
          {
              "Sid": "Stmt1",
              "Effect": "Allow",
              "Principal": "*",
              "Action": "s3:*",
              "Resource": [
                  "arn:aws:s3:::bcktName",
                  "arn:aws:s3:::bcktName/*"
              ]
          }
      ]
  }
  </code></pre>
  
  <p>I am using Elastic BeanStalk which in turn uses AWS Ec2. 
  The Ec2 instance has aws-elasticbeanstalk-ec2-role which was provided FullAccess to S3 (I understand full access is dangerous).</p>
  
  <p>I'm using the following java code in my webapp which is deployed to Ec2.</p>
  
  <pre><code>AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(new InstanceProfileCredentialsProvider(false))
                    .build();
                versionId = s3Client.putObject(new PutObjectRequest("bucketName", name, convFile))
                    .getVersionId();
  </code></pre>
  
  <p>1) Is this the correct way to connect to S3 from Ec2 in a java code?
  2) <a href="http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html" rel="nofollow noreferrer">This</a> documentation confuses me as I'm not sure if I have to set up Environment variables and/or Java system properties and/or The default credential profiles file and/or Amazon ECS container credentials and/or Instance profile credentials ?</p>
  
  <p>Please help me. I'm struck with this issue for almost a week. </p>
  
  <p>PS: 
  Following is the ec2InstanceRole assigned to my profile</p>
  
  <pre><code>Role ARN  arn:aws:iam::111111111111:role/aws-elasticbeanstalk-ec2-role
  Role description  
  Instance Profile ARNs  arn:aws:iam::111111111111:instance-profile/aws-elasticbeanstalk-ec2-role
  Path   /
  Creation time  2017-10-17 11:27 PDT
  </code></pre>
  
  <p>And the policies assigned to above role are</p>
  
  <pre><code>  AmazonS3FullAccess
    AWSElasticBeanstalkWebTier
    AWSElasticBeanstalkMulticontainerDocker
    AWSElasticBeanstalkWorkerTier
  </code></pre>