The shared configuration files capability provides access to default file paths and manages AWS shared configuration and credentials files.
AWS SDK for Go v2 loads configuration from shared configuration files located at standard paths. The config package provides functions to retrieve these default paths and variables containing the file lists.
func DefaultSharedConfigFilename() string
func DefaultSharedCredentialsFilename() stringDefaultSharedConfigFilename returns the SDK's default file path for the shared config file based on the OS platform:
$HOME/.aws/config%USERPROFILE%\.aws\configDefaultSharedCredentialsFilename returns the SDK's default file path for the shared credentials file based on the OS platform:
$HOME/.aws/credentials%USERPROFILE%\.aws\credentialsvar DefaultSharedConfigFiles = []string{
DefaultSharedConfigFilename(),
}
var DefaultSharedCredentialsFiles = []string{
DefaultSharedCredentialsFilename(),
}DefaultSharedConfigFiles is a slice of the default shared config files that will be used to load the SharedConfig.
DefaultSharedCredentialsFiles is a slice of the default shared credentials files that will be used to load the SharedConfig.
const DefaultSharedConfigProfile = "default"DefaultSharedConfigProfile is the default profile to be used when loading configuration from the config files if another profile name is not provided.
package main
import (
"fmt"
"github.com/aws/aws-sdk-go-v2/config"
)
func main() {
configPath := config.DefaultSharedConfigFilename()
credentialsPath := config.DefaultSharedCredentialsFilename()
fmt.Printf("Default config file: %s\n", configPath)
fmt.Printf("Default credentials file: %s\n", credentialsPath)
}import (
"context"
)
// Load config from custom files
customConfigFiles := []string{
"/etc/aws/config",
"/home/user/custom-aws-config",
}
customCredsFiles := []string{
"/etc/aws/credentials",
"/home/user/custom-aws-credentials",
}
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedConfigFiles(customConfigFiles),
config.WithSharedCredentialsFiles(customCredsFiles),
)
if err != nil {
log.Fatal(err)
}import (
"context"
)
// Load config using a specific profile
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedConfigProfile("production"),
)
if err != nil {
log.Fatal(err)
}The shared configuration file (~/.aws/config) uses INI format with sections for profiles:
[default]
region = us-east-1
output = json
[profile production]
region = us-west-2
output = json
role_arn = arn:aws:iam::123456789012:role/ProductionRole
source_profile = default
[profile development]
region = eu-west-1
output = jsonImportant: Non-default profiles must be prefixed with profile in the config file.
The shared credentials file (~/.aws/credentials) uses INI format without the profile prefix:
[default]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
[production]
aws_access_key_id = AKIAI44QH8DHBEXAMPLE
aws_secret_access_key = je7MtGbClwBF/2Zp9Utk/h3yCo8nvbEXAMPLEKEY
[development]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEYImportant: Profile names in the credentials file should NOT include the profile prefix.
When duplicate profiles are provided:
Profiles can include various configuration options:
aws_access_key_id - AWS access key IDaws_secret_access_key - AWS secret access keyaws_session_token - AWS session tokenregion - AWS regionoutput - Output format (json, text, table)role_arn - ARN of the role to assumesource_profile - Profile to use for credentialsexternal_id - External ID for assume rolemfa_serial - MFA device serial numberrole_session_name - Session name for the assumed roleduration_seconds - Session duration in secondssso_session - SSO session namesso_account_id - AWS account IDsso_role_name - SSO role namesso_region - SSO regionsso_start_url - SSO start URLcredential_process - External credential process commandweb_identity_token_file - Web identity token file paths3_use_arn_region - Allow S3 ARNs to direct regions3_disable_multiregion_access_points - Disable S3 multi-region access pointsendpoint_discovery_enabled - Enable endpoint discoveryec2_metadata_service_endpoint_mode - EC2 IMDS endpoint modeec2_metadata_service_endpoint - EC2 IMDS endpointec2_metadata_v1_disabled - Disable EC2 IMDSv1use_dualstack_endpoint - Use dual-stack endpointsuse_fips_endpoint - Use FIPS endpointsdefaults_mode - SDK defaults modemax_attempts - Maximum retry attemptsretry_mode - Retry modeca_bundle - Custom CA bundle pathsdk_ua_app_id - SDK app ID