Skip to content

docs: update localstack example for testcontainers-go #1743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions content/en/user-guide/integrations/testcontainers/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ await localStackContainer.StartAsync()
.ConfigureAwait(false);
{{< /tab >}}
{{< tab header="Go" lang="go">}}
container, err := localstack.StartContainer(ctx, localstack.NoopOverrideContainerRequest)
container, err := localstack.Run(ctx, "localstack/localstack:latest")
{{< /tab >}}
{{< tab header="Java" lang="java">}}
LocalStackContainer localstack = new LocalStackContainer(DockerImageName.parse("localstack/localstack:3"));
Expand All @@ -77,42 +77,44 @@ config.ServiceURL = localStackContainer.GetConnectionString();
using var client = new AmazonS3Client(config);
{{< /tab >}}
{{< tab header="Go" lang="go">}}
type resolverV2 struct {
// you could inject additional application context here as well
}

func (*resolverV2) ResolveEndpoint(ctx context.Context, params s3.EndpointParameters) (smithyendpoints.Endpoint, error) {
// delegate back to the default v2 resolver otherwise
return s3.NewDefaultEndpointResolverV2().ResolveEndpoint(ctx, params)
}

func s3Client(ctx context.Context, l *localstack.LocalStackContainer) (*s3.Client, error) {
// the Testcontainers Docker provider is used to get the host of the Docker daemon
provider, err := testcontainers.NewDockerProvider()
mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp"))
if err != nil {
return nil, err
}

host, err := provider.DaemonHost(ctx)
provider, err := testcontainers.NewDockerProvider()
if err != nil {
return nil, err
}
defer provider.Close()

mappedPort, err := l.MappedPort(ctx, nat.Port("4566/tcp"))
host, err := provider.DaemonHost(ctx)
if err != nil {
return nil, err
}

customResolver := aws.EndpointResolverWithOptionsFunc(
func(service, region string, opts ...interface{}) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: fmt.Sprintf("http://%s:%d", host, mappedPort.Int()),
SigningRegion: region,
}, nil
})

awsCfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithRegion(region),
config.WithEndpointResolverWithOptions(customResolver),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accesskey, secretkey, token)),
)
if err != nil {
return nil, err
}

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

Expand Down