cloudyr / aws.ec2metadata

Access to EC2 Instance Metadata
https://cran.r-project.org/package=aws.ec2metadata
12 stars 10 forks source link

Reading object in s3 bucket from ecs fargate task #9

Open Ada-Nick opened 3 years ago

Ada-Nick commented 3 years ago

Please specify whether your issue is about:

I have a shiny app running on an ecs fargate task using the rocker:shiny-verse base docker image. I can't read an s3 file within the initialisation of the shiny app (global.R file). Interestingly it does work when I change the method of serving the app in the docker file.

The docker command that works is:

CMD ["R", "-e", "shiny::runApp('/srv/shiny-server/my_app', 3838, host='0.0.0.0')"]

And the method I want to use but doesn't work:

CMD ["/usr/bin/shiny-server.sh"]

What is the intended method of accessing the permissions granted to the IAM role, how can I read from the s3 bucket within an ecs fargate task?

My global.R file looks like this:

## load package
library(ini)
library("aws.ec2metadata")

Sys.setenv("AWS_DEFAULT_REGION" = "eu-west-2")
if (is_ecs() == T) {
   metadata$iam_role("ecsTaskExecutionRole")
}

config = aws.s3::s3read_using(read.ini,
                              object = 'my_object.ini',
                              bucket = 'my_bucket')

I can't get the session Info of the Fargate task or any logs but know it's the s3 connection that is failing as removing the s3_read_using() call allows the app to run as expected. I have also double checked that the ecsTaskExecutionRole has s3 read permissions required. The bucket also definitely exists and as does the object and it's in the correct region (eu-west-2).

ghost commented 2 years ago

@Ada-Nick I'm facing the same problem today. Did you fix this with some strategy?

Ada-Nick commented 2 years ago

Yeah I did. I changed my shiny-server.sh file to this:

#!/bin/sh

# Make sure the directory for individual app logs exists
mkdir -p /var/log/shiny-server
chown shiny.shiny /var/log/shiny-server

if [ "$APPLICATION_LOGS_TO_STDOUT" != "false" ];
then
    # push the "real" application logs to stdout with xtail in detached mode
    exec xtail /var/log/shiny-server/ &
fi

env > /home/shiny/.Renviron
chown shiny.shiny /home/shiny/.Renviron

# start shiny server
exec shiny-server 2>&1

Hopefully that works for you

ghost commented 2 years ago

@Ada-Nick That really helped here! Thank you so much, my friend! Greetings from Brazil =)