#Avoid hardcoding AWS credentials

1 messages · Page 1 of 1 (latest)

blissful moth
#

I'm trying out Ballerina for a sample tool I'm writing. I want to periodically inject test data into a DynamoDB table. Started looking into aws.dynamodb module, however I can't find a way to avoid hard coding the credentials.

So far I've tried specifying an empty struct awsCredentials to see if standard environment variables are being looked up.

import ballerina/io;
import ballerinax/aws.dynamodb;

public function main() {
    io:println("testing dynamodb connection...");
   
    dynamodb:ConnectionConfig ddbConfig = {
      awsCredentials: {},
      region: "ap-southeast-2",
    };

    dynamodb:Client dbClient = check new(ddbConfig);
}

But I get the following error, which indicates that there is no such lookup?

ERROR [main.bal:(14:23,14:25)] incompatible mapping constructor expression for type '(ballerinax/aws.dynamodb:2.0.0:AwsCredentials|ballerinax/aws.dynamodb:2.0.0:AwsTemporaryCredentials)'
ERROR [main.bal:(16:1,16:1)] missing identifier
error: compilation contains errors

I've tried looking up lib.ballerina.io and just plain old googling, but I can't find a proper example on this scenario. Appreciate if some pointers can be provided.

gritty rose
blissful moth
#

No, this seems like a usable pattern, although I would expect an AWS library to be looking up standard AWS environment variables.

versed heath
#

In Ballerina, environment variables can be utilized through the ballerina/os module. The value of an environment variable can be retrieved using the os:getenv function

import ballerina/io;
import ballerinax/aws.dynamodb;
import ballerina/os;

configurable string accessKeyId = os:getEnv("ACCESS_KEY_ID");
configurable string secretAccessKey = os:getEnv("SECRET_ACCESS_KEY");
configurable string region = os:getEnv("REGION");


public function main() {
    io:println("testing dynamodb connection...");
    dynamodb:ConnectionConfig config = {
    awsCredentials: {accessKeyId: accessKeyId, secretAccessKey: secretAccessKey},
    region: region
    };
    dynamodb:Client|error dbClient = new(config);
}
royal spruce