Skip to content

Commit

Permalink
add aws-vault profile switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
pdiakumis committed Oct 16, 2023
1 parent c3aa59f commit d26f09e
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions R/aws.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
awsvault_profile <- function(p) {
assertthat::assert_that(system("command -v aws-vault") == 0, msg = "aws-vault needs to be installed - see https://github.com/99designs/aws-vault")
creds <- file.path(tempdir(), "creds.tsv")
system(glue("unset AWS_VAULT && aws-vault exec {p} -- env | grep AWS > {creds}"))
x <- readr::read_tsv(creds, col_names = "var_eq_value", col_types = "c") |>
tidyr::separate_wider_delim("var_eq_value", delim = "=", names = c("var", "value"), too_many = "merge") |>
tidyr::pivot_wider(names_from = "var", values_from = "value") |>
as.list()

Sys.setenv(AWS_VAULT = p)
Sys.setenv(AWS_ACCESS_KEY_ID = x[["AWS_ACCESS_KEY_ID"]])
Sys.setenv(AWS_SECRET_ACCESS_KEY = x[["AWS_SECRET_ACCESS_KEY"]])
Sys.setenv(AWS_SESSION_TOKEN = x[["AWS_SESSION_TOKEN"]])
Sys.setenv(AWS_DEFAULT_REGION = x[["AWS_DEFAULT_REGION"]])
Sys.setenv(AWS_REGION = x[["AWS_REGION"]])
cat(glue::glue("Switched to AWS Profile: {p}"))
}

0 comments on commit d26f09e

Please sign in to comment.