How to set up the AWS SDK using configs, access key ID, and secret access keys. (2023)

Introduction

As an AWS beginner, I struggled to use the access key ID and secret key to access the products provided by Amazon web services.

I wanted to use the Polly (text-to-speech) cognitive service provided by Amazon under AI/ML category. I created the credentials but didn’t get how to use those in the project to access the APIs. In this article, I’ll show step-by-step.

Create the IAM user

If you don’t know what IAM is, watch this quick video below, otherwise skip further. Basically, you need to create a sub-user, where you grant access to the only resources you want this user to use. It gives you the “Access key ID” and “Secret access key” that we’ll use to access the API via the AWS-SDK.

Install the AWS-SDK

Head over to NPM or any other package manager relevant to yours (pip for python, yarn, etc). And install their SDK.

npm i aws-sdk

Once you’re done with installing the SDK, read the docs for your use case and the service you want to use. As all the services are available to be used via the SDK.

Use the environment variables

The next step is to make sure nobody gets unauthorized access to your cloud account. Firstly, add .env (environment variable) to your .gitignore file so it does not gets pushed to your GitHub repository.

Secondly, Install the dotenv package if you haven’t yet.

Initialize an AWS instance.

const AWS = require("aws-sdk"); // require the SDK.

The second step, provide the keys.

AWS.config.credentials = new AWS.Credentials(
  process.ENV.access_key, // Your access key ID
  process.ENV.secret_key, // Your secret access key
);

// Define your service region.
AWS.config.region = "ap-south-1"; // Can also be saved in ENV vars.

Now you’re all set. You can access any service, like in my case I wanted to use Polly, the speech synthesis product. I’ll publish a new post on how to use it. Check out more development articles 🙂