amazon-web-servicesgo

I am trying to add a cloudfront behavior to my an origin and I am getting InvalidArgument: The parameter CachedMethods is required


I am trying to add a cloudfront behavior to my an origin and I am getting InvalidArgument: The parameter CachedMethods is required. While the aws sdk go v2 says cache method is deprecated.

func AddCloudFrontBehavior(client *cloudfront.Client, distributionID, originID, pathPattern string) error {

    distribution, err := client.GetDistributionConfig(context.TODO(), &cloudfront.GetDistributionConfigInput{
        Id: aws.String(distributionID),
    })
    if err != nil {
        return fmt.Errorf("failed to get CloudFront distribution: %v", err)
    }

    // Create new behavior
    newBehavior := cloudfrontTypes.CacheBehavior{
        TargetOriginId:       aws.String(originID),
        PathPattern:          aws.String(pathPattern),
        ViewerProtocolPolicy: cloudfrontTypes.ViewerProtocolPolicyAllowAll,
        AllowedMethods: &cloudfrontTypes.AllowedMethods{
            Quantity: aws.Int32(2),
            Items: []cloudfrontTypes.Method{
                cloudfrontTypes.MethodGet,
                cloudfrontTypes.MethodHead,
                // cloudfrontTypes.MethodPost,
                // cloudfrontTypes.MethodPut,
                // cloudfrontTypes.MethodDelete,
                // cloudfrontTypes.MethodOptions,
                // cloudfrontTypes.MethodPatch,
            },
        },
        Compress:        aws.Bool(true),
        SmoothStreaming: aws.Bool(false),
        MinTTL:          aws.Int64(0),
        ForwardedValues: &cloudfrontTypes.ForwardedValues{
            QueryString: aws.Bool(false),
            Cookies: &cloudfrontTypes.CookiePreference{
                Forward: cloudfrontTypes.ItemSelectionAll,
            },
                Headers: &cloudfrontTypes.Headers{
                    Quantity: aws.Int32(0),
                },
                QueryStringCacheKeys: &cloudfrontTypes.QueryStringCacheKeys{
                    Quantity: aws.Int32(0),
                },
        },
        DefaultTTL:             aws.Int64(0),
        MaxTTL:                 aws.Int64(3600),
        FieldLevelEncryptionId: aws.String(""),
        // CachePolicyId:         aws.String(originID),
        // OriginRequestPolicyId: aws.String(originID),
    }

    // Append to cache behaviors
    distribution.DistributionConfig.CacheBehaviors.Items = append(distribution.DistributionConfig.CacheBehaviors.Items, newBehavior)
    distribution.DistributionConfig.CacheBehaviors.Quantity = aws.Int32(int32(len(distribution.DistributionConfig.CacheBehaviors.Items)))

    // Update distribution
    _, err = client.UpdateDistribution(context.TODO(), &cloudfront.UpdateDistributionInput{
        Id:                 aws.String(distributionID),
        DistributionConfig: distribution.DistributionConfig,
        IfMatch:            distribution.ETag,
    })
    if err != nil {
        return fmt.Errorf("failed to update CloudFront distribution: %v", err)
    }

    fmt.Println("CloudFront behavior added successfully!")
    return nil
}type here

I am trying to add a cloudfront behavior to my an origin and I am getting InvalidArgument: The parameter CachedMethods is required. While the aws sdk go v2 says cacheMethod is deprecated.


Solution

  • CachedMethods is required in AllowedMethods, and is not deprecated.

    ForwardedValues is deprecated, though.