How to fix error with AWS Lambda handler, DynamoDB Put req? - amazon-web-services

Trying to create a Lambda to interact with my DynamoDB.
This specific Lambda is to put/write an item to the DB:
package main
import (
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
type Item struct {
Email string `json:"email"`
Password string `json:"password"`
Rname string `json:"rname"`
}
func Put() error {
// Create a session - London Region
session, err := session.NewSession(&aws.Config{
Region: aws.String("eu-west-2")},
)
if err != nil {
fmt.Println(err)
}
svc := dynamodb.New(session)
// Create instance of Item Struct
item := Item{
Email: "123#mail.com",
Password: "12345678",
Rname: "abcde",
}
// Marshall Item
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
fmt.Println("Got error marshalling map:")
fmt.Println(err)
}
// Create Item
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String("accountsTable"),
}
_, err = svc.PutItem(input)
if err != nil {
fmt.Println("Got error calling PutItem:")
fmt.Println(err)
}
return err
}
func main() {
lambda.Start(Put())
}
However getting the error:
{
"errorMessage": "handler is nil",
"errorType": "errorString"
}
I have changed the handler in run time settings to main too so don't think that would be the issue.
Building with:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -a main.go
and putting the zip of the executable into AWS via console (no IAC's)
Any help would be greatly appreciated to resolve this error! Thanks.

You need to pass function handle not function result to lambda.Start
Please update your main function with👇
func main() {
lambda.Start(Put)
}

Related

Golang AWS Firehose using json field name instead of struct field name

I have the following struct:
type ProcessedRecords struct {
CustIndividualID string `json:"individual id"`
Household string `json:"Household"`
}
And I have a slice of many structs that share this value. I'm trying to submit them using the PutRecordBatch operation from the AWS SDK:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/firehose"
)
type ProcessedRecords struct {
CustIndividualID string `json:"individual id"`
Household string `json:"Household"`
}
func main() {
submitToFirehose(recordList)
}
func submitToFirehose(records []ProcessedRecords) {
streamName := "processed-stream"
sess := session.Must(session.NewSession())
// Create a Firehose client with additional configuration
firehoseService := firehose.New(sess, aws.NewConfig().WithRegion("us-east-1"))
recordsBatchInput := &firehose.PutRecordBatchInput{}
recordsBatchInput = recordsBatchInput.SetDeliveryStreamName(streamName)
recordsInput := []*firehose.Record{}
for i := 0; i < len(records); i++ {
if len(recordsInput) == 500 {
recordsBatchInput = recordsBatchInput.SetRecords(recordsInput)
resp, err := firehoseService.PutRecordBatch(recordsBatchInput)
if err != nil {
fmt.Printf("PutRecordBatch err: %v\n", err)
} else {
fmt.Printf("FailedPuts: %v\n", *resp.FailedPutCount)
}
recordsInput = []*firehose.Record{}
}
b, err := json.Marshal(records[i])
if err != nil {
log.Printf("Error: %v", err)
}
record := &firehose.Record{Data: b}
recordsInput = append(recordsInput, record)
}
}
This seems to work and it would appear that my Glue backend is setup correctly, however CustIndividualID is not being written to S3. I suspect it's because it's reading the json:"individual id" as the column name and not the CustIndividualID.
this is a problem because glue tables can't have spaces in the column name. What am I doing wrong?

Unable to perform query on AWS athena using Golang SDK

I am new to AWS and Golang, and I am trying to create a lambda function, which will trigger AWS Athena query and email the result using AWS SES service. Even after searching for an hour, I couldn't find a working example of lambda function (in Golang) to perform a query on Athena and getting the output of the query.
While searching, I found code for the same in Java, Python and Node Js, but not in Golang.
Even the Go-SDK page redirects to Java example. But unfortunately, I don't even understand Java.
I have also looked into this AWS SDK for Go API Reference page. But I don't understand what is the flow of the program and which operation to select.
I have tried to create the program for this, this may be completely wrong, and I don't know what to do next. Below is the code -
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/athena"
)
func main() {
// Create a new session in the us-west-2 region.
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
// Create an Athena session.
client := athena.New(sess)
// Example sending a request using the StartQueryExecutionRequest method.
query := "SELECT * FROM table1 ;"
params := query
req, resp := client.StartQueryExecutionRequest(params)
err1 := req.Send()
if err1 == nil { // resp is now filled
fmt.Println(resp)
}
}
Appreciate if someone can help me to perform an Athena query and to get its result in Golang(Preferably) or can share some resource. Once I get it, I can then send an email using AWS SES.
Use this to get started.
// run as: go run main.go
package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/endpoints"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/athena"
)
const table = "textqldb.textqltable"
const outputBucket = "s3://bucket-name-here/"
func main() {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
fmt.Printf("config error: %v\n", err)
return
}
cfg.Region = endpoints.UsEast2RegionID
client := athena.New(cfg)
query := "select * from " + table
resultConf := &athena.ResultConfiguration{
OutputLocation: aws.String(outputBucket),
}
params := &athena.StartQueryExecutionInput{
QueryString: aws.String(query),
ResultConfiguration: resultConf,
}
req := client.StartQueryExecutionRequest(params)
resp, err := req.Send(context.TODO())
if err != nil {
fmt.Printf("query error: %v\n", err)
return
}
fmt.Println(resp)
}
#Everton's code is executing a query on Athena, and its responses are getting saved on S3 bucket and not getting returned. So, I have added the code to execute the Athena query and get the response back. Hope this may help others.
// run as: go run main.go
package main
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/endpoints"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/athena"
)
const table = "<Database_Name>.<Table_Name>"
const outputBucket = "s3://bucket-name-here/"
// Execute the query and return the query ID
func executeQuery(query string) *string {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
fmt.Printf("config error: %v\n", err)
}
cfg.Region = endpoints.UsEast2RegionID
client := athena.New(cfg)
resultConf := &athena.ResultConfiguration{
OutputLocation: aws.String(outputBucket),
}
params := &athena.StartQueryExecutionInput{
QueryString: aws.String(query),
ResultConfiguration: resultConf,
}
req := client.StartQueryExecutionRequest(params)
resp, err := req.Send(context.TODO())
fmt.Println("Response is: ", resp, " Error is:", err)
if err != nil {
fmt.Printf("Query Error: %v\n", err)
}
fmt.Println("Query Execution Response ID:", resp.QueryExecutionId)
return resp.QueryExecutionId
}
// Takes queryId as input and returns its response
func getQueryResults(QueryID *string) (*athena.GetQueryResultsResponse, error) {
cfg, err := external.LoadDefaultAWSConfig()
if err != nil {
panic("config error")
}
cfg.Region = endpoints.UsEast2RegionID
client := athena.New(cfg)
params1 := &athena.GetQueryResultsInput{
QueryExecutionId: QueryID,
}
req := client.GetQueryResultsRequest(params1)
resp, err := req.Send(context.TODO())
if err != nil {
fmt.Printf("Query Response Error: %v\n", err)
return nil, err
}
return resp, nil
}
func main() {
query := "select * from " + table
// Execute an Athena Query
QueryID := executeQuery(query)
// Get the response of the query
// Wait for some time for query completion
time.Sleep(15 * time.Second) // Otherwise create a loop and try for every x seconds
Resp, err := getQueryResults(QueryID)
if err != nil {
fmt.Printf("Error getting Query Response: %v\n", err)
} else {
fmt.Println(" \nRows:", Resp.ResultSet.Rows)
}
}

I get undefined ethclient when executing main.go

I'am writting a script to interrract with a smart contract:
package main
import (
"context"
"fmt"
"log"
"github.com/ethereum/go-ethereum"
"github.com/joho/godotenv"
)
var myenv map[string]string
const envLoc = ".env"
func loadEnv() {
var err error
if myenv, err = godotenv.Read(envLoc); err != nil {
log.Printf("could not load env from %s: %v", envLoc, err)
}
}
func main() {
loadEnv()
ctx := context.Background()
client, err := ethclient.Dial(os.Getenv("GATEWAY"))
if err != nil {
log.Fatalf("could not connect to Ethereum gateway: %v\n", err)
}
defer client.Close()
accountAddress := common.HexToAddress("786af135e476c3b6061482e90c6273b8ee78c159")
balance, _ := client.BalanceAt(ctx, accountAddress, nil)
fmt.Printf("Balance: %d\n", balance)
}
I get undefined ethclient and undefined common.
I don't understant why I get these errors? it used to work normally
Make sure to import the correct packages. Here's a working example:
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
ctx := context.Background()
fmt.Println(os.Getenv("GATEWAY"))
client, err := ethclient.Dial(os.Getenv("GATEWAY"))
if err != nil {
log.Fatalf("could not connect to Ethereum gateway: %v\n", err)
}
defer client.Close()
accountAddress := common.HexToAddress("786af135e476c3b6061482e90c6273b8ee78c159")
balance, _ := client.BalanceAt(ctx, accountAddress, nil)
fmt.Printf("Balance: %d\n", balance)
}
You're not importing packages your code is using.
Add import "github.com/ethereum/go-ethereum/ethclient"
I am not sure where common package should be, but you're missing import for it as well.

Publish message to SNS with AWS Go SDK

I'm having issue with sending a message to SNS with the AWS Go SDK. Documentation for the Publish function is a little bit obscure.
My piece of code is:
package main
import (
"encoding/json"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go/aws"
"log"
)
type Person struct {
Name string `json:"name"`
}
func main() {
cfg, _ := external.LoadDefaultAWSConfig()
snsClient := sns.New(cfg)
person := Person{
Name:"ok",
}
jsonStr, _ := json.Marshal(person)
req := snsClient.PublishRequest(&sns.PublishInput{
TopicArn: aws.String("arn:aws:sns:us-east-1:*****:ok"),
Message: aws.String(string(jsonStr)),
MessageStructure: aws.String("json"),
MessageAttributes: map[string]sns.MessageAttributeValue{
"default": {
DataType: aws.String("String"),
StringValue: aws.String(string(jsonStr)),
},
},
})
res, err := req.Send()
if err != nil {
log.Fatal(err)
}
log.Print(res)
}
When I launch this code I receive the following message :
2019/01/24 20:14:24 InvalidParameter: Invalid parameter: Message Structure - No default entry in JSON message body
status code: 400, request id: 55940de1-9645-5485-96c5-592586957ce8
exit status 1
Maybe someone can help me with that ?
Thanks
I've found a solution to my issue.
package main
import (
"encoding/json"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/aws"
"log"
)
type Message struct {
Default string `json:"default"`
}
type Person struct {
Name string `json:"name"`
}
func main() {
cfg, _ := external.LoadDefaultAWSConfig()
snsClient := sns.New(cfg)
person := Person{
Name: "Felix Kjellberg",
}
personStr, _ := json.Marshal(person)
message := Message{
Default: string(personStr),
}
messageBytes, _ := json.Marshal(message)
messageStr := string(messageBytes)
req := snsClient.PublishRequest(&sns.PublishInput{
TopicArn: aws.String("arn:aws:sns:us-east-1:*****:ok"),
Message: aws.String(messageStr),
MessageStructure: aws.String("json"),
})
res, err := req.Send()
if err != nil {log.Fatal(err)
}
log.Print(res)
}
Some encoding inception was needed
You have to add a "default" field to your json payload for subscribers that can't consume your message payload. Take a look at this (towards the bottom): https://docs.aws.amazon.com/sns/latest/dg/mobile-push-send-custommessage.html

storagegateway.ListGateways() returning "Invalid resource" with status code: 400

I am having trouble using the go sdk to list all of the storage gateways in a region using the following code:
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/storagegateway"
)
func newSess() (sess *session.Session) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-2"),
})
if err != nil {
fmt.Printf("Error creating session: %s\n", err)
return
}
return sess
}
func main() {
sgw := storagegateway.New(newSess())
input := &storagegateway.ListGatewaysInput{
Limit: aws.Int64(5),
Marker: aws.String("1"),
}
result, err := sgw.ListGateways(input)
if err != nil {
fmt.Printf("Error listing gateways %s\n", err)
return
}
fmt.Println(result)
}
My environment is the following:
Go version: 1.9.2 darwin/amd64
Operating system: macOS 10.13.2
aws-sdk-go: v1.12.63
The SDK is using the default profile specified in ~/.aws/config as it should and the credentials are correct as well. Furthermore, I am able to list the gateways using the awscli alone.
This could very well be something simple that I have overlooked as I am no expert. That said, any help would be greatly appreciated.
The example code seems to work as expected for me by simply removing the Marker: aws.String("1") line.