$ aws sts get-caller-identity
Using AWS credentials in RStudio Pro
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.
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.
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:
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)
<- paws::sts()
svc $get_caller_identity() sts
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
<- paws::s3(config = list(region = "us-east-2"))
s3
s3
# locate the s3 bucket you want
= 'colorado-projects'
bucket $list_objects(Bucket = bucket)
s3
# upload data to s3 bucket
$put_object(
s3Bucket = bucket,
Key = 'data.csv'
)
# read data from s3 bucket
<- s3$get_object(
s3_download Bucket = bucket,
Key = 1
)