Keep getting build fail and unable to save import for main.go file for lambda function using SAM CLI

437 Views Asked by At

I'm trying to import "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue" for my main.go file for my lambda function but every time I save the import disappears.

I have some simple Golang code trying to update a visitor count by updating a dynamodb table.

The build keeps failing saying that attributevalue are undefined but I can't save the import for attribute value.

package main

import (
    "context"
    "log"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/config"
    "github.com/aws/aws-sdk-go-v2/feature/dynamodb"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb"
)

func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {

    cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion("us-east-1"))
    if err != nil {
        log.Fatalf("unable to load SDK config, %v", err)
    }

    svc := dynamodb.NewFromConfig(cfg)

    // Build the request with its input parameters
    resp, err := svc.UpdateItem(context.TODO(), &dynamodb.UpdateItemInput{
        TableName: aws.String("table-name"),
        Key: map[string]*dynamodb.attributevalue{
            "ID": {
                S: aws.String("visitors"),
            },
        },
        UpdateExpression: aws.String("ADD visitors :inc"),
        ExpressionAttributeValues: map[string]*dynamodb.attributevalue{
            ":inc": {
                N: aws.String("1"),
            },
        },
    })

    if err != nil {
        log.Fatalf("Got error callingUpdateItem: %s", err)
    }

    return events.APIGatewayProxyResponse{
        Headers: map[string]string{
            "Access-Control-Allow-Origin":  "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "*",
        },
        StatusCode: 200,
    }, nil
}

func main() {
    lambda.Start(handler)
}

When I do go mod vendor I get

github.com/aws/aws-sdk-go-v2/featrues/dynamodb: no required module provides package github.com/aws/aws-sdk-go-v2/featrues/dynamodb; to add it:

Doing go get github.com/aws/aws-sdk-go-v2/featrues/dynamodb I get

go: module github.com/aws/aws-sdk-go-v2@upgrade found (v1.16.11), but does not contain package github.com/aws/aws-sdk-go-v2/featrues/dynamodb

I am very new to Go and have no idea what to do to resolve this. Any help would be greatly appreciated.

1

There are 1 best solutions below

2
On

if you check github.com/aws/aws-sdk-go-v2/feature. available packages are dynamodb/attributevalue or dynamodb/expression. Looks like you are using attribute value so the import should have "github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"