Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access lazy tbl after assumed role session expired? #188

Open
willshen99 opened this issue Jan 13, 2025 · 1 comment
Open

Access lazy tbl after assumed role session expired? #188

willshen99 opened this issue Jan 13, 2025 · 1 comment

Comments

@willshen99
Copy link

willshen99 commented Jan 13, 2025

The credentials from STS assume role has a duration. Moreover, if using role chaining (using one role to assume a different role), AWS cap the duration at 1 hr. link.

In our use case, we are using AWS SageMaker with a role, then within an R session, we assume a second role to access Athena. However, after 1 hour, the connection expires. To refresh the credentials and restore the connection, I need to re-run assume_role and DBI::dbConnect. While this works to create new tables or queries using the refreshed credentials, I cannot reference any lazy tbl objects that were created using the expired connection, even after refreshing the connection and credentials. As I inspect the lazy tbl object, the ptr in con is unchanged and still pointing to the connection expired.

Is there a workaround for this?

Reprex

library(RAthena)
library(DBI)
library(dbplyr)

crd <- RAthena::assume_role(role_arn = "arn:aws:sts::123456789012:assumed-role/role_name/role_session_name"
    set_env = FALSE,
    duration = 3600L # the maximum 
)

con <- DBI::dbConnect(
    RAthena::athena(), 
    aws_access_key_id=crd$AccessKeyId,
    aws_secret_access_key=crd$SecretAccessKey,
    aws_session_token=crd$SessionToken
)

my_iris <- tbl(con, sql("SELECT * FROM iris"))
my_iris

# after 1 hr
# Refresh credentials and connection
crd <- RAthena::assume_role(role_arn = "arn:aws:sts::123456789012:assumed-role/role_name/role_session_name"
    set_env = FALSE,
    duration = 3600L # the maximum 
)

con <- DBI::dbConnect(
    RAthena::athena(), 
    aws_access_key_id=crd$AccessKeyId,
    aws_secret_access_key=crd$SecretAccessKey,
    aws_session_token=crd$SessionToken
)

# this works
my_iris_new <- tbl(con, sql("SELECT * FROM iris"))
my_iris_new

# this doesn't work
my_iris # credentials expired error
@DyfanJones
Copy link
Owner

Hmm interesting, for the current implementation I don't think so as tbl doesn't create a reference link to the con object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants