2

I am new to AWS and working on Relational Database Service(RDS) for the first time.

I have created a database(MySQL) on RDS from the AWS console. Now I am trying to create some tables in the database and to simply insert and retrieve data from it using Golang SDK. I have created the below code in Golang to run a SQL query, but I am getting an error as below.

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/rdsdataservice"
)

func main() {

    // Initialize a session in us-east-2 
    sess, _ := session.NewSession(&aws.Config{
        Region: aws.String("us-east-2")},
    )


    // Sending a request using the ExecuteStatementRequest method.

    SQLStatement := `CREATE TABLE Persons (
        PersonID int,
        LastName varchar(255),
        FirstName varchar(255),
        Address varchar(255),
        City varchar(255)
    );`

    // Create RDS service client
    rdsdataservice_client := rdsdataservice.New(sess)

    req, resp := rdsdataservice_client.ExecuteStatementRequest(&rdsdataservice.ExecuteStatementInput{
        Database:    aws.String("database-1"),
        ResourceArn: aws.String("arn:aws:rds:us-east-2:9xxxxxxxx9:db:database-1"),
        SecretArn:   aws.String("arn:aws:secretsmanager:us-east-2:9xxxxxxxx9:secret:RDS_Credentials-IZOXv0"),
        Sql:         aws.String(SQLStatement),
    })

    err1 := req.Send()
    if err1 == nil { // resp is now filled
        fmt.Println("Response:", resp)
    } else {
        fmt.Println("error:", err1)
    }

}

I am getting below error -

BadRequestException: Invalid cluster arn: arn:aws:rds:us-east-2:9xxxxxxxx9:db:database-1
        status code: 400, request id: a7744d7c-4f19-232f-8bf1-c25662968d55

I will appreciate if anyone can help me to find the issue.

I am using the following API reference documentation - link. I doubt that these API's are only for Aurora database and not for others. Please correct me if I am wrong and suggest the correct API's references.

Rahul Satal
  • 2,107
  • 3
  • 32
  • 53

1 Answers1

7

Finally, the issue is resolved.

Actually there were multiple issues in my case.

  1. At first, I was getting below error -

BadRequestException: Invalid cluster arn: arn:aws:rds:us-east-2:9xxxxxxxx9:db:database-1 status code: 400, request id: a7744d7c-4f19-232f-8bf1-c25662968d55

this is because I was using wrong ResourceArn. Follow this to find the ResourceArn -

RDS --> databases

enter image description here

and make sure the ARN has cluster in it like below -

arn:aws:rds:us-east-2:9xxxxxxxx9:cluster:database-1

This is what I was missing.

  1. Then I got below error -

BadRequestException: HttpEndpoint is not enabled for cluster database-2. Please refer to https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/data-api.html#data-api.troubleshooting status code: 400, request id: f6ceef3e-c43c-43a3-a5f2-aeafdedd9b71

This error is solved by enabling data API. Details are in the above link.

EDIT

How to get the SecretARN

For getting the SecretARN you have to create a secret using [Secret Manager] 2. AWS Secrets Manager is an AWS service that makes it easier for you to manage secrets. Secrets can be database credentials, passwords, third-party API keys, and even arbitrary text. You can store and control access to these secrets centrally by using the Secrets Manager console, the Secrets Manager command-line interface (CLI), or the Secrets Manager API and SDKs.

Follow this link to create a secret for RDS. For getting started you can disable the automatic rotation of the secret.

Rahul Satal
  • 2,107
  • 3
  • 32
  • 53