Error creating an s3Bucket with CorsRule in Go

Hi,

I am trying to create an S3 bucket with a CORS rule:

        s3.NewS3Bucket(*stack, &bucketName, &s3.S3BucketConfig{
            Bucket: &bucketName,
            CorsRule: &s3.S3BucketCorsRule{
                AllowedMethods: &([]*string{jsii.String("GET")}),
            },
        })

I get a runtime error:

default - panic: "Value did not match any type in union: Expected array type, got {\"$jsii.struct\":{\"fqn\":\"hashicorp_aws.s3.S3BucketCorsRule\",\"data\":{\"allowedMethods\":[\"GET\"]}}}, Expected object reference, got {\"$jsii.struct\":{\"fqn\":\"hashicorp_aws.s3.S3BucketCorsRule\",\"data\":{\"allowedMethods\":[\"GET\"]}}}"

github.com/aws/jsii-runtime-go/runtime.Create({0x1c7be9d, 0x19}, {0xc00041fc88, 0x3
[2022-05-27T13:14:12.915] [ERROR] default - , 0x3}, {0x1c63c00, 0xc00039f0d0})
   /go/pkg/mod/github.com/aws/jsii-runtime-go@v1.59.0/runtime/runtime.go:186 +0x885
bricks/generated/hashicorp/aws/s3.NewS3Bucket({0x2ed73688, 0xc0000ea2e0}, 0xc00039f0b0, 0xc0000bc200)
   /generated/hashicorp/aws/s3/s3.go:5685 +0xea

I get a similar error when I try to add a Grant to the bucket like this:

s3.NewS3Bucket(*stack, &bucketName, &s3.S3BucketConfig{
    Bucket: &bucketName,
    Grant: &s3.S3BucketGrant{
    Permissions: &([]*string{jsii.String("FULL_CONTROL")}),
    Type:        jsii.String("CanonicalUser"),
    Id:          canonicalUser.Id(),
    },
)}

It makes me think that I am passing the arguments in the wrong way (I am not just new to CDKTF, but to Go too! :slight_smile: )

Help appreciated. Thanks,

  • A

Both CorsRule and Grant actually take an array rather than a single value.

1 Like

Got it --thanks! This works great:

CorsRule: []s3.S3BucketCorsRule{
    {AllowedMethods: jsii.Strings("GET", "POST"), AllowedOrigins: jsii.Strings("*")},
},

What is the best way to find the expected types of these fields? The field is simply an interface{}, so it’s hard to know for sure what it expects…

Thanks again for your help, @jsteinich

  • A

At the moment the best way is to look at the source for TypeScript. The AWS pre-built provider can be found here. The construct hub documentation for TypeScript also works.

Hopefully these types will be better represented in other languages in the future.

1 Like

This is very helpful --thanks, @jsteinich !