Skip to content

Commit db53729

Browse files
authored
docs: update localstack example for testcontainers-go (#1743)
1 parent 0116009 commit db53729

File tree

1 file changed

+17
-15
lines changed
  • content/en/user-guide/integrations/testcontainers

1 file changed

+17
-15
lines changed

content/en/user-guide/integrations/testcontainers/index.md

+17-15
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ await localStackContainer.StartAsync()
5858
.ConfigureAwait(false);
5959
{{< /tab >}}
6060
{{< tab header="Go" lang="go">}}
61-
container, err := localstack.StartContainer(ctx, localstack.NoopOverrideContainerRequest)
61+
container, err := localstack.Run(ctx, "localstack/localstack:latest")
6262
{{< /tab >}}
6363
{{< tab header="Java" lang="java">}}
6464
LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:3"));
@@ -77,42 +77,44 @@ config.ServiceURL = localStackContainer.GetConnectionString();
7777
using var client = new AmazonS3Client(config);
7878
{{< /tab >}}
7979
{{< tab header="Go" lang="go">}}
80+
type resolverV2 struct {
81+
// you could inject additional application context here as well
82+
}
83+
84+
func (*resolverV2) ResolveEndpoint(ctx context.Context, params s3.EndpointParameters) (smithyendpoints.Endpoint, error) {
85+
// delegate back to the default v2 resolver otherwise
86+
return s3.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params)
87+
}
88+
8089
func s3Client(ctx context.Context, l *localstack.LocalStackContainer) (*s3.Client, error) {
81-
// the Testcontainers Docker provider is used to get the host of the Docker daemon
82-
provider, err := testcontainers.NewDockerProvider()
90+
mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp"))
8391
if err != nil {
8492
return nil, err
8593
}
8694

87-
host, err := provider.DaemonHost(ctx)
95+
provider, err := testcontainers.NewDockerProvider()
8896
if err != nil {
8997
return nil, err
9098
}
99+
defer provider.Close()
91100

92-
mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp"))
101+
host, err := provider.DaemonHost(ctx)
93102
if err != nil {
94103
return nil, err
95104
}
96105

97-
customResolver := aws.EndpointResolverWithOptionsFunc(
98-
func(service, region string, opts ...interface{}) (aws.Endpoint, error) {
99-
return aws.Endpoint{
100-
PartitionID: "aws",
101-
URL: fmt.Sprintf("http://%s:%d", host, mappedPort.Int()),
102-
SigningRegion: region,
103-
}, nil
104-
})
105-
106106
awsCfg, err := config.LoadDefaultConfig(context.TODO(),
107107
config.WithRegion(region),
108-
config.WithEndpointResolverWithOptions(customResolver),
109108
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accesskey, secretkey, token)),
110109
)
111110
if err != nil {
112111
return nil, err
113112
}
114113

114+
// reference: https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/endpoints/#with-both
115115
client := s3.NewFromConfig(awsCfg, func(o *s3.Options) {
116+
o.BaseEndpoint = aws.String("http://" + host + ":" + mappedPort.Port())
117+
o.EndpointResolverV2 = &resolverV2{}
116118
o.UsePathStyle = true
117119
})
118120

0 commit comments

Comments
 (0)