@@ -21,7 +21,6 @@ import (
2121 "github.com/aws/aws-sdk-go/aws"
2222 "github.com/aws/aws-sdk-go/aws/awserr"
2323 "github.com/aws/aws-sdk-go/aws/credentials"
24- "github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
2524 "github.com/aws/aws-sdk-go/aws/session"
2625 "github.com/aws/aws-sdk-go/service/ecr"
2726 "github.com/juju/errors"
@@ -33,26 +32,22 @@ func CreateEcrRepo(repoName string, reg string, accessKey string, secretKey stri
3332 region := reg
3433 //fmt.Printf("repoName %s, reg %s, accessKey %s, secretKey %s\n", repoName, reg, accessKey, secretKey)
3534
36- var creds * credentials.Credentials
35+ var sess * session.Session
36+ var err error
3737
3838 if len (accessKey ) == 0 || len (secretKey ) == 0 {
39- //fmt.Println("empty accessKey or secretKey" )
40- sess , err : = session .NewSession (& aws.Config {
39+ // Case 1: IAM role — use default credential chain (IRSA, instance profile, task role, env vars )
40+ sess , err = session .NewSession (& aws.Config {
4141 Region : & region ,
4242 })
43- if err != nil {
44- log .Println (err )
45- return err
46- }
47- creds = ec2rolecreds .NewCredentials (sess )
4843 } else {
49- creds = credentials .NewStaticCredentials (accessKey , secretKey , "" )
44+ // Case 2: Static credentials
45+ creds := credentials .NewStaticCredentials (accessKey , secretKey , "" )
46+ sess , err = session .NewSession (& aws.Config {
47+ Region : & region ,
48+ Credentials : creds ,
49+ })
5050 }
51-
52- sess , err := session .NewSession (& aws.Config {
53- Region : & region ,
54- Credentials : creds ,
55- })
5651 if err != nil {
5752 log .Println (err )
5853 return err
0 commit comments