Skip to content

Commit 52b3e2e

Browse files
authored
Support passing in aws.Config to supply external credentials when scripting (#326)
1 parent ace0594 commit 52b3e2e

File tree

7 files changed

+93
-80
lines changed

7 files changed

+93
-80
lines changed

README.md

+15-4
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,19 @@ Dry run mode is only available within:
194194
You can import cloud-nuke into other projects and use it as a library for programmatically inspecting and counting resources.
195195

196196
```golang
197+
197198
package main
198199

199200
import (
200201
"fmt"
201202
"time"
202203

204+
"github.com/aws/aws-sdk-go/aws"
203205
nuke_aws "github.com/gruntwork-io/cloud-nuke/aws"
206+
"github.com/gruntwork-io/cloud-nuke/externalcreds"
204207
)
205208

206209
func main() {
207-
208210
// You can scan multiple regions at once, or just pass a single region for speed
209211
targetRegions := []string{"us-east-1", "us-west-1", "us-west-2"}
210212
excludeRegions := []string{}
@@ -214,6 +216,17 @@ func main() {
214216
// excludeAfter is parsed identically to the --older-than flag
215217
excludeAfter := time.Now()
216218

219+
// Any custom settings you want
220+
myCustomConfig := &aws.Config{}
221+
222+
myCustomConfig.WithMaxRetries(3)
223+
myCustomConfig.WithLogLevel(aws.LogDebugWithRequestErrors)
224+
// Optionally, set custom credentials
225+
// myCustomConfig.WithCredentials()
226+
227+
// Be sure to set your config prior to calling any library methods such as NewQuery
228+
externalcreds.Set(myCustomConfig)
229+
217230
// NewQuery is a convenience method for configuring parameters you want to pass to your resource search
218231
query, err := nuke_aws.NewQuery(
219232
targetRegions,
@@ -222,7 +235,6 @@ func main() {
222235
excludeResourceTypes,
223236
excludeAfter,
224237
)
225-
226238
if err != nil {
227239
fmt.Println(err)
228240
}
@@ -246,15 +258,14 @@ func main() {
246258
// countOfEc2InUsWest1: 2
247259

248260
fmt.Printf("usWest1Resources.ResourceTypePresent(\"ec2\"):%b\n", usWest1Resources.ResourceTypePresent("ec2"))
249-
//usWest1Resources.ResourceTypePresent("ec2"): true
261+
// usWest1Resources.ResourceTypePresent("ec2"): true
250262

251263
// Get all the resource identifiers for a given resource type
252264
// In this example, we're only looking for ec2 instances
253265
resourceIds := usWest1Resources.IdentifiersForResourceType("ec2")
254266

255267
fmt.Printf("resourceIds: %s", resourceIds)
256268
// resourceIds: [i-0c5d16c3ef28dda24 i-09d9739e1f4d27814]
257-
258269
}
259270
```
260271

0 commit comments

Comments
 (0)