How To: Interact with AWS S3 Using the Go SDK and not lose your mind
After these messages we will carry on with our regularly scheduled programming…
Yesterday ( during the scribbling of this article ) AWS suffered one of it’s worst outages in history in the us-east-1 region. A reminder to us all to be multi-region and more importantly multi-cloud. Please see my other articles on HA deployments using AWS and my perspective & caution on the path to centralization or singularity we appear to be on (though the outage may help people wake up).
Now back to your regularly scheduled program…
My team and I are building a CMDB for AWS, which provides us with everything happening in our AWS environment + OS level metadata + change history. There will be a separate article on the CMDB journey, but today I want to focus on a specific service in AWS called S3, which is their object store. S3 is a bit of a special snowflake when it comes to AWS services and because of that I ran into challenges structuring my code, because up until S3 (which was the last service I wrote code for) everything had been very similar, and easily modularized. We will get to more detail, but let’s start this article by covering how to use the Go SDK for AWS.
Dependencies
This article assumes you already program in Go and have Go installed on your machine. To get started you will need a couple additional items.
- Download and install the SDK here : https://github.com/aws/aws-sdk-go
- This is the documentation for the SDK, you will need it, bookmark it : http://docs.aws.amazon.com/sdk-for-go/api/
- It is extremely helpful when working with the API’s to have aws-shell installed : https://github.com/awslabs/aws-shell
- This enables you to interact with AWS API’s on the fly so you can understand the output of commands as you are searching for what you are trying to accomplish.
The Collector Structure
The collector is the component in my CMDB architecture that does all the work of collecting the metadata that we shove into our CMDB. The collector is heavily threaded using go routines for performance. The basic structure looks like this.
- Call a go routine for each service you want to collect
- //pass in all accounts, regions (from config-file) and pre-established awsSessions to each account you are collecting
- Inside of a services go routine, loop overs accounts & regions
- Launch a go routine for each account & region
- Inside of those go routines make your AWS API call(s), example DescribeInstances
- Store the response (I loop through mine and store it in a map using the resource-id as the key)
- Finally, kick off another go routine to write to our API and store the data.
- Launch a go routine for each account & region
Ok, so hopefully that seems straight forward as a basic structure…let’s get to why S3 through me for a loop.
S3 Challenges
It will be best if I show you what I tried first, basically I tried to marry my existing pattern to S3 and that certainly was a bad idea from the start. Here was the structure of the S3 part of the code.
- The S3 go routine gets called from main.go
- //all accounts, regions and AWS Sessions are past into the next go routine
- Inside of the S3 go routine, loop over accounts & regions
- Launch a go routine for each account & region
- Inside of those go routines List S3 Buckets
- For each S3 buckets returned
- Call additional API’s such as GetBucketTagging()
- For each S3 buckets returned
- Inside of those go routines List S3 Buckets
- Launch a go routine for each account & region
- Inside of the S3 go routine, loop over accounts & regions
Ok so what happened ? I got a lot of errors that’s what 🙂 Ones like this….
BucketRegionError: incorrect region, the bucket is not in 'us-west-2' region status code: 301, request id:
At first, I thought maybe my code wasn’t thread safe…but that didn’t make much sense given the other services had no issues like this.
So as I debugged my code, I began to realize the buckets list I was getting, wasn’t limited to the region I was passing in/ establishing a session for.
Naturally, I googled can I list buckets for a single region ?
https://github.com/aws/aws-sdk-java/issues/920 (even though this is the Java SDK it still applies)..
"spfink commented on Nov 16, 2016 It is not possible to list the buckets in a single region. Regardless of the endpoint or region that you set, when calling list buckets you will get buckets from all regions. In order to determine the region of a bucket you can use getBucketLocation(String bucketName). https://github.com/aws/aws-sdk-java/blob/master/aws-java-sdk-s3/src/main/java/com/amazonaws/services/s3/AmazonS3.java#L1026”
Ah ok, the BucketList being returned on an AWS Session established with a specific account and region, ignores the region. Because S3 Buckets are global to an account, thus all buckets under an account are returned in the ListBuckets() call. I knew S3 buckets were global per account, but failed to expect a matching behavior/output when a specific region is passed into the SDK/API.
Ok so how then can I distinguish where a bucket actually lives?
As spfink says above, I needed to run GetBucketLocation() per bucket. Thus my code structure started to look like this…
- For each account, region
- ListBuckets
- For each bucket returned in that account, region
- GetBucketLocation
- If a LocationConstraint (region) is returned, set the new region (otherwise if response is null, do nothing)
- Get tags for the bucket in account, region
- For each bucket returned in that account, region
- ListBuckets
With this code I was still getting errors about region, but why ?
Well I made the mistake of thinking a ‘null’ response from the API for LocationConstraint had no meaning (or meant query it from any region), wrong (null actually means us-east-1 see from my google below) thus the IF condition evaluated false and the existing region from the outer loop was used because GetBucketLocation() returned null and this resulted in many errors.
Here’s what the google turned up..
https://github.com/aws/aws-cli/issues/564
"kyleknap commented on Mar 16, 2015 @xurume For buckets located in US Standard, the location constraint will be null. For S3, here is the list of region names with their corresponding regions: http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region. Notice that the location constraint is none for US Standard. The CLI uses the values in the region column for the --region parameter. So for S3's US Standard, you need to use us-east-1 as the region.”
So let’s clarify my mistakes…
- The S3 ListBuckets call returns all buckets under an account globally.
- It does not abide by a region configured in an API Session
- Thus I/you should not loop over regions from a config file for the S3 service.
- Instead I/you need to find a buckets ‘real’ location using GetBucketLocation
- Then set the region for actions other than ListBuckets (which is global per account and ignores region passed).
- GetBucketLocation returning null, doesn’t mean the bucket is global or that you can interact with the bucket from endpoint you please…it actually means us-east-1 http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region
The Working Code
So in the end the working code for S3 looks like this…
- collector/main.go fires off a bunch of go routines per service we are collecting for.
- It passes in accounts, and regions from a config file.
- For the S3 service/file under the ‘services’ package the entry point is a function called StoreS3Resources.
Everything in the code should be self explanatory from that point on. You will note a function call to ‘writeToCis’… CIS is the name of our internal CMDB project/service. Again, I will later be blogging about the entire system in detail once we open source the code. Please keep in mind this code is MVP, it will be changed a lot (optimization, modularized, bug fixes, etc) before & after we open source it, but for now he is the quick and dirty, but hopefully functional code 🙂 Use at your own risk !
package services import ( "github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws" "sync" "fmt" "time" "encoding/json" "strings" ) var wgS3BucketList sync.WaitGroup var wgS3GetBucketDetails sync.WaitGroup var accountRegionsMap = make(map[string]map[string][]string) var accountToBuckets = make(map[string][]string) var bucketToAccount = make(map[string]string) var defaultRegion string = "us-east-1" func writeS3ResourceToCis(resType string, resourceData map[string]interface{}, account string, region string){ b, err := json.Marshal(resourceData) check(err) err, status, url := writeToCisBulk(resType, region, b) check(err) fmt.Printf("%s - %s - %s - %s - Bytes: %d\n", status, url, account, region, cap(b)) } func StoreS3Resources(awsSessions map[string]*session.Session, accounts []string, configuredRegions []string) { s3Start := time.Now() wgS3BucketList.Add(1) go func () { defer wgS3BucketList.Done() for _, account := range accounts { awsSession := awsSessions[account] getS3AccountBucketList(awsSession, account) } }() wgS3BucketList.Wait() getS3BucketDetails(awsSessions, configuredRegions) s3Elapsed := time.Since(s3Start) fmt.Printf("S3 completed in: %s\n", s3Elapsed) } func getS3AccountBucketList(awsSession *session.Session, account string) { svcS3 := s3.New(awsSession, &aws.Config{Region: aws.String(defaultRegion)}) //list returned is for all buckets in an account ( no regard for region ) resp, err := svcS3.ListBuckets(nil) check(err) var buckets []string for _,bucket := range resp.Buckets { buckets = append(buckets, *bucket.Name) //reverse mapping needed for lookups in other funcs bucketToAccount[*bucket.Name] = account } //a list of buckets per account accountToBuckets[account] = buckets } func getS3BucketLocation(awsSession *session.Session, bucket string, bucketToRegion map[string]string, regionToBuckets map[string][]string) { wgS3GetBucketDetails.Add(1) go func() { defer wgS3GetBucketDetails.Done() svcS3 := s3.New(awsSession, &aws.Config{Region: aws.String(defaultRegion)}) // default var requiredRegion string locationParams := &s3.GetBucketLocationInput{ Bucket: aws.String(bucket), } respLocation, err := svcS3.GetBucketLocation(locationParams) check(err) //We must query the bucket based on the location constraint if strings.Contains(respLocation.String(), "LocationConstraint") { requiredRegion = *respLocation.LocationConstraint } else { //if getBucketLocation is null us-east-1 used //http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region requiredRegion = "us-east-1" } bucketToRegion[bucket] = requiredRegion regionToBuckets[requiredRegion] = append(regionToBuckets[requiredRegion], bucket) accountRegionsMap[bucketToAccount[bucket]] = regionToBuckets }() } func getS3BucketsTags(awsSession *session.Session, buckets []string, account string, region string) { wgS3GetBucketDetails.Add(1) go func() { defer wgS3GetBucketDetails.Done() svcS3 := s3.New(awsSession, &aws.Config{Region: aws.String(region)}) var resourceData = make(map[string]interface{}) for _, bucket := range buckets { taggingParams := &s3.GetBucketTaggingInput{ Bucket: aws.String(bucket), } respTags, err := svcS3.GetBucketTagging(taggingParams) check(err) resourceData[bucket] = respTags } writeS3ResourceToCis("buckets", resourceData, account, region) }() } func getS3BucketDetails(awsSessions map[string]*session.Session, configuredRegions []string) { for account, buckets := range accountToBuckets { //reset regions for each account var bucketToRegion = make(map[string]string) var regionToBuckets = make(map[string][]string) for _,bucket := range buckets { awsSession := awsSessions[account] getS3BucketLocation(awsSession, bucket, bucketToRegion, regionToBuckets) } } wgS3GetBucketDetails.Wait() //Preparing configured regions to make sure we only write to CIS for regions configured var configuredRegionsMap = make(map[string]bool) for _,region := range configuredRegions { configuredRegionsMap[region] = true } for account := range accountRegionsMap { awsSession := awsSessions[account] for region, buckets := range accountRegionsMap[account] { //Only proceed if it's a configuredRegion from the config file. if _, ok := configuredRegionsMap[region]; ok { fmt.Printf("%s %s has %d buckets\n", account, region, len(buckets)) getS3BucketsTags(awsSession, buckets, account, region) } else { fmt.Printf("Skipping buckets in %s because is not a configured region\n", region) } } } wgS3GetBucketDetails.Wait() }
How To: Interact with AWS S3 Using the Go SDK and not lose your mind Read More »