15
15
package aws
16
16
17
17
import (
18
+ "context"
18
19
"time"
19
20
20
- "github.com/aws/aws-sdk-go/aws"
21
- "github.com/aws/aws-sdk-go/aws/client "
22
- "github.com/aws/aws-sdk-go/aws /credentials"
23
- "github.com/aws/aws-sdk-go/aws/session "
24
- "github.com/aws/aws-sdk-go/service/ec2"
25
- "github.com/aws/aws-sdk-go/service/iam"
26
- "github.com/aws/aws-sdk-go/service/s3"
27
- "github.com/aws/aws-sdk-go/service/sts"
21
+ "github.com/aws/aws-sdk-go-v2 /aws"
22
+ "github.com/aws/aws-sdk-go-v2/config "
23
+ "github.com/aws/aws-sdk-go-v2 /credentials"
24
+ "github.com/aws/aws-sdk-go-v2/service/ec2 "
25
+ ec2types "github.com/aws/aws-sdk-go-v2 /service/ec2/types "
26
+ "github.com/aws/aws-sdk-go-v2 /service/iam"
27
+ "github.com/aws/aws-sdk-go-v2 /service/s3"
28
+ "github.com/aws/aws-sdk-go-v2 /service/sts"
28
29
"github.com/coreos/pkg/capnslog"
29
30
30
31
"github.com/coreos/coreos-assembler/mantle/platform"
@@ -57,11 +58,12 @@ type Options struct {
57
58
}
58
59
59
60
type API struct {
60
- session client.ConfigProvider
61
- ec2 * ec2.EC2
62
- iam * iam.IAM
63
- s3 * s3.S3
64
- opts * Options
61
+ config aws.Config
62
+ ec2 * ec2.Client
63
+ iam * iam.Client
64
+ s3 * s3.Client
65
+ sts * sts.Client
66
+ opts * Options
65
67
}
66
68
67
69
// New creates a new AWS API wrapper. It uses credentials from any of the
@@ -71,28 +73,38 @@ type API struct {
71
73
// preflight check is recommended via api.PreflightCheck
72
74
// Note that this method may modify Options to update the AMI ID
73
75
func New (opts * Options ) (* API , error ) {
74
- awsCfg := aws.Config {Region : aws .String (opts .Region )}
76
+ ctx := context .Background ()
77
+
78
+ // Build configuration options
79
+ configOpts := []func (* config.LoadOptions ) error {
80
+ config .WithRegion (opts .Region ),
81
+ }
82
+
75
83
if opts .AccessKeyID != "" {
76
- awsCfg .Credentials = credentials .NewStaticCredentials (opts .AccessKeyID , opts .SecretKey , "" )
84
+ configOpts = append (configOpts , config .WithCredentialsProvider (
85
+ credentials .NewStaticCredentialsProvider (opts .AccessKeyID , opts .SecretKey , "" ),
86
+ ))
77
87
} else if opts .CredentialsFile != "" {
78
- awsCfg .Credentials = credentials .NewSharedCredentials (opts .CredentialsFile , opts .Profile )
88
+ configOpts = append (configOpts , config .WithSharedCredentialsFiles ([]string {opts .CredentialsFile }))
89
+ if opts .Profile != "" {
90
+ configOpts = append (configOpts , config .WithSharedConfigProfile (opts .Profile ))
91
+ }
92
+ } else if opts .Profile != "" {
93
+ configOpts = append (configOpts , config .WithSharedConfigProfile (opts .Profile ))
79
94
}
80
95
81
- sess , err := session .NewSessionWithOptions (session.Options {
82
- SharedConfigState : session .SharedConfigEnable ,
83
- Profile : opts .Profile ,
84
- Config : awsCfg ,
85
- })
96
+ awsCfg , err := config .LoadDefaultConfig (ctx , configOpts ... )
86
97
if err != nil {
87
98
return nil , err
88
99
}
89
100
90
101
api := & API {
91
- session : sess ,
92
- ec2 : ec2 .New (sess ),
93
- iam : iam .New (sess ),
94
- s3 : s3 .New (sess ),
95
- opts : opts ,
102
+ config : awsCfg ,
103
+ ec2 : ec2 .NewFromConfig (awsCfg ),
104
+ iam : iam .NewFromConfig (awsCfg ),
105
+ s3 : s3 .NewFromConfig (awsCfg ),
106
+ sts : sts .NewFromConfig (awsCfg ),
107
+ opts : opts ,
96
108
}
97
109
98
110
return api , nil
@@ -107,17 +119,17 @@ func (a *API) GC(gracePeriod time.Duration) error {
107
119
// PreflightCheck validates that the aws configuration provided has valid
108
120
// credentials
109
121
func (a * API ) PreflightCheck () error {
110
- stsClient := sts . New ( a . session )
111
- _ , err := stsClient . GetCallerIdentity (& sts.GetCallerIdentityInput {})
122
+ ctx := context . Background ( )
123
+ _ , err := a . sts . GetCallerIdentity (ctx , & sts.GetCallerIdentityInput {})
112
124
113
125
return err
114
126
}
115
127
116
- func tagSpecCreatedByMantle (name , resourceType string ) []* ec2 .TagSpecification {
117
- return []* ec2 .TagSpecification {
128
+ func tagSpecCreatedByMantle (name string , resourceType ec2types. ResourceType ) []ec2types .TagSpecification {
129
+ return []ec2types .TagSpecification {
118
130
{
119
- ResourceType : aws . String ( resourceType ) ,
120
- Tags : []* ec2 .Tag {
131
+ ResourceType : resourceType ,
132
+ Tags : []ec2types .Tag {
121
133
{
122
134
Key : aws .String ("CreatedBy" ),
123
135
Value : aws .String ("mantle" ),
0 commit comments