Using AWS credentials in RStudio Pro

Enhanced | Advanced

Workbench can provide user-specific AWS credentials for RStudio Pro sessions tied to their Single Sign-On credentials. These credentials are not long-lived Personal Access Tokens (PATs) but rather short-lived OAuth tokens and are refreshed automatically while your session is active.

If your administrator has configured and enabled the AWS credentials integration, a new drop-down displays in the New Session dialog. This allows you to select which AWS role to use.

Session selection pane showing AWS role dropdown

After selecting the role and starting the session, AWS credentials needed to connect programmatically to an AWS account (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) should already be available within the session.

Note

AWS credentials are only available in an RStudio Pro session if the feature in Posit Workbench has been successfully set up following this guide. Work with your Posit administrator to configure this before using this feature.

Checking for credentials

To verify what credentials are available, use the aws cli in the Terminal tab of RStudio Pro:

$ aws sts get-caller-identity

The output should look similar to this:

{
    "UserId": "xxxx:xxxxx",
    "Account": "xxxxxx",
    "Arn": "arn:aws:sts::xxxxx:assumed-role/yourrole-xxxx/
i-xxxxx"
}

If for some reason you do not have aws cli installed, you can use the R paws package. The output of function sts$get_caller_identity() is also the same as the command above:

library(paws)

svc <- paws::sts()
sts$get_caller_identity()

Example workflow

Now that we have confirmed that AWS credentials are available, use the paws package to access AWS resources programmatically. The following example shows how to write and read from an s3 bucket:

library(paws)

# create an S3 service object in the region you are working on
s3 <- paws::s3(config = list(region = "us-east-2"))
s3

# locate the s3 bucket you want
bucket = 'colorado-projects'
s3$list_objects(Bucket = bucket)

# upload data to s3 bucket
s3$put_object(
  Bucket = bucket,
  Key = 'data.csv'
)

# read data from s3 bucket
s3_download <- s3$get_object(
  Bucket = bucket,
  Key = 1
)
Back to top