Skip to content

Commit

Permalink
feat: add localstack endpoint resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
thrau authored and whummer committed Dec 9, 2022
1 parent 170220c commit d7252b1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/pkg/aws/sessions/sessions.go
Expand Up @@ -7,6 +7,7 @@ package sessions
import (
"context"
"fmt"
"github.com/aws/copilot-cli/internal/pkg/endpoints"
"net/http"
"runtime"
"sync"
Expand Down Expand Up @@ -210,7 +211,8 @@ func newConfig() *aws.Config {
return aws.NewConfig().
WithHTTPClient(c).
WithCredentialsChainVerboseErrors(true).
WithMaxRetries(maxRetriesOnRecoverableFailures)
WithMaxRetries(maxRetriesOnRecoverableFailures).
WithEndpointResolver(endpoints.NewResolver())
}

// userAgentHandler returns a http request handler that sets the AWS Copilot custom user agent to all aws requests.
Expand Down
52 changes: 52 additions & 0 deletions internal/pkg/endpoints/localstack.go
@@ -0,0 +1,52 @@
package endpoints

import (
"github.com/aws/aws-sdk-go/aws/endpoints"
"os"
"strings"
)

type localStackResolver struct {
defaultResolver endpoints.Resolver
}

func getLocalStackUrl() string {
hostname := os.Getenv("LOCALSTACK_HOSTNAME")

if hostname == "" {
hostname = "localhost"
}

port := os.Getenv("EDGE_PORT")
if port == "" {
port = "4566"
}

ssl := os.Getenv("USE_SSL")
disableSSL := true
if ssl == "1" || strings.ToLower(ssl) == "true" {
disableSSL = false
}

return endpoints.AddScheme(hostname + ":" + port, disableSSL)
}

func (l *localStackResolver) EndpointFor(service, region string, opts ...func(*endpoints.Options)) (endpoints.ResolvedEndpoint, error) {
endpointFor, err := l.defaultResolver.EndpointFor(service, region, opts...)

if err != nil {
return endpointFor, err
}

endpointFor.URL = getLocalStackUrl()

return endpointFor, err
}

func NewLocalstackResolver() *localStackResolver {
resolver := &localStackResolver{
defaultResolver: endpoints.DefaultResolver(),
}

return resolver
}
17 changes: 17 additions & 0 deletions internal/pkg/endpoints/resolver.go
@@ -0,0 +1,17 @@
package endpoints

import (
"github.com/aws/aws-sdk-go/aws/endpoints"
"os"
"strings"
)

func NewResolver() endpoints.Resolver {
flag := os.Getenv("LOCALSTACK_DISABLE")

if flag == "1" || strings.ToLower(flag) == "true" {
return endpoints.DefaultResolver()
} else {
return NewLocalstackResolver()
}
}

0 comments on commit d7252b1

Please sign in to comment.