-
Notifications
You must be signed in to change notification settings - Fork 3
/
config.go
65 lines (56 loc) · 2.02 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package awsHelpers
import (
"context"
"fmt"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
)
// GetConfig returns an AWS SDK v2 Config with a custom resolver that resolves SDK requests
// to an endpoint at http://$LOCALSTACK_HOSTNAME:4566 when $LOCALSTACK_HOSTNAME is configured
// in the current environment.
// $EDGE_PORT will override port 4566 only when $LOCALSTACK_HOSTNAME is also set.
// If no $LOCALSTACK_HOSTNAME variable exists in the current environment, the resolver falls
// back to the SDK's default endpoint resolution behavior.
func GetConfig(ctx context.Context) (aws.Config, error) {
useLocalStack, lsHostname, lsPort := getLocalStackEndpoint()
if !useLocalStack {
return config.LoadDefaultConfig(ctx)
}
optionsFunc := func(service, region string, options ...interface{}) (aws.Endpoint, error) {
awsEndpoint := aws.Endpoint{
PartitionID: "aws",
URL: fmt.Sprintf("http://%s:%s", lsHostname, lsPort),
}
if awsRegion, isSet := os.LookupEnv("AWS_REGION"); isSet {
awsEndpoint.SigningRegion = awsRegion
}
return awsEndpoint, nil
}
customResolver := aws.EndpointResolverWithOptionsFunc(optionsFunc)
return config.LoadDefaultConfig(ctx, config.WithEndpointResolverWithOptions(customResolver))
}
func GetSQSClient(ctx context.Context) (*sqs.Client, error) {
cfg, err := GetConfig(ctx)
if err != nil {
return nil, fmt.Errorf("could not create AWS SDK config: %w", err)
}
useLocalStack, lsHostname, lsPort := getLocalStackEndpoint()
if !useLocalStack {
return sqs.NewFromConfig(cfg), nil
}
return sqs.NewFromConfig(cfg, func(o *sqs.Options) {
o.BaseEndpoint = aws.String(fmt.Sprintf("http://%s:%s", lsHostname, lsPort))
}), nil
}
func getLocalStackEndpoint() (isConfigured bool, hostname, port string) {
hostname, isConfigured = os.LookupEnv("LOCALSTACK_HOSTNAME")
if isConfigured {
port = "4566"
if lsPort, isSet := os.LookupEnv("EDGE_PORT"); isSet {
port = lsPort
}
}
return isConfigured, hostname, port
}