Localstack: AWS was not able to validate the provided access credentials

Viewed 200

When trying to use EC2 client, I get the following error:

=== RUN   TestEc2
time="2022-06-10T14:18:43-07:00" level=info msg="starting localstack"
time="2022-06-10T14:18:44-07:00" level=info msg="waiting for localstack to start..."
time="2022-06-10T14:19:01-07:00" level=info msg="localstack: finished waiting"
    all_test.go:305: operation error EC2: RunInstances, https response error StatusCode: 401, RequestID: 080f9f11-67d7-4061-86d3-e581551458c1, api error AuthFailure: AWS was not able to validate the provided access credentials
--- FAIL: TestEc2 (18.47s)
FAIL
FAIL    command-line-arguments  19.197s
FAIL
func TestEc2(t *testing.T) {
    ls, err := localstack.NewInstance()
    if err != nil {
        log.Fatalf("could not connect to docker: %v", err)
    }

    ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
    defer cancel()

    if err := ls.StartWithContext(ctx); err != nil {
        log.Fatalf("could not start Localstack: %v", err)
    }

    ec2Client = ec2.NewFromConfig(awsConfig(ctx, ls, localstack.EC2))
       
    createInstance(t, ec2Client)
}
func awsConfig(ctx context.Context, l *localstack.Instance, s localstack.Service) aws.Config {

    rf := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
        return aws.Endpoint{
            PartitionID:   "aws",
            URL:           l.EndpointV2(s),
            SigningRegion: endpoints.UsEast1RegionID,
        }, nil
     })
    cfg, err := config.LoadDefaultConfig(ctx,
        config.WithRegion("us-east-1"),
        config.WithEndpointResolverWithOptions(rf),
        config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider("dummy", "dummy", "dummy")),
    )

    if err != nil {
        log.Fatalf("could not load config: %v", err)
    }

    return cfg
}

func createInstance(t *testing.T, c *ec2.Client) types.Instance {
    t.Helper()

    result, err := c.RunInstances(
        context.TODO(),
        &ec2.RunInstancesInput{
            ImageId:      aws.String("ami-e7527ed7"),
            InstanceType: types.InstanceTypeT2Micro,
            MinCount:     aws.Int32(1),
            MaxCount:     aws.Int32(1),
        })

    if err != nil {
        t.Fatal(err)
    }

    if result == nil {
        t.Fatal("empty RunInstance result")
    }

    if len(result.Instances) != 1 {
        t.Fatalf("exptected 1 instance, got %d", len(result.Instances))
    }

    return result.Instances[0]
}

But if instead I use the deprecated EndpointResolverFunc everything seem to work OK

    r := aws.EndpointResolverFunc(func(service, region string) (aws.Endpoint, error) {
        return aws.Endpoint{
            PartitionID:   "aws",
            URL:           l.EndpointV2(s),
            SigningRegion: endpoints.UsEast1RegionID,
        }, nil
    })

    cfg, err := config.LoadDefaultConfig(ctx,
        ....
        config.WithEndpointResolver(r),
    )

Anyone has any idea what I might be missing?

0 Answers
Related