Amazon web services — 101

Javeeth Basha
TestVagrant
Published in
7 min readJun 8, 2020

Setting up applications on cloud infrastructure has become a necessity, more than ever before. Software organisations are building immensely distributed systems serving multiple geographies today. Availability & speed of such systems are factors that prove to be differentiators amongst such applications and organisations are bringing in the best of cloud setup to cope up with these. When organisations are looking for robust cloud infrastructure, the hunt lands quite comfortably on AWS — one of the leaders in providing SaaS applications across all regions of the globe.

In this blog, let’s look at what AWS is and how are teams setting up AWS CLI in local and access AWS services using programming SDKs.

AWS is a Cloud Service provider by Amazon which provides a variety of different services that helps to create and deploy and maintain different types of application.

There are multiple reasons for preferring AWS over other cloud providers because of its reliability, security, and cost-effectiveness that is gonna bring. AWS provides a variety of different services across different domains that can’t be listed in a single picture. AWS is one package for all of your needs.

Getting Started with AWS

AWS provides a web application AWS console to manage its services. In simple words, it’s a GUI that gives a holistic view of all the services and allows them to manage those services provided by AWS.

It has a list of services provided by AWS that can be created, deployed, and maintained using a simple intuitive web-based user interface.

Accessing AWS services

An AWS Identity and Access Management (IAM) is a service that is used to create a new user who consists of a name and credentials (password, Access Keys). Every user is an entity that you create in AWS to represent the person or application that uses it to interact with AWS.

AWS services are accessed in different ways,

  1. AWS Console can be accessed using Username and Password.
  2. Access keys - a combination of access key ID and a secret access key can be used to make programmatic calls to AWS.

Setting up AWS CLI

Download AWS CLI and configure it with our local devices to control and manage AWS services from the command line. After Installing the CLI verify the version using

$ aws --version
aws-cli/2.0.6 Python/3.7.4 Darwin/18.7.0 botocore/2.0.0

Once Installation is done, set AWS credentials that you have created using IAM to the local AWS CLI.

To Set credentials in the AWS credentials profile file on your local system, located at:

  • ~/.aws/credentials on Linux, OS X, or Unix
  • C:\Users\USERNAME\.aws\credentials on Windows

This file contains lines in the following format:

[default]
AWS_ACCESS_KEY_ID = your_access_key_id
AWS_SECRET_ACCESS_KEY = your_secret_access_key

Replace your Access keys and that’s all you need, the CLI setup is done.

To Set region in the AWS config profile file on your local system, located at:

  • ~/.aws/config on Linux, OS X, or Unix
  • C:\Users\USERNAME\.aws\config on Windows

This file contains lines in the following format:

[default]
region = Your_Region
Ex :
[default]
region = ap-south-1

Different named profiles can be added and appended to the same file.

Once profile and credentials are configured then use CLI to manage all AWS services using AWS-CLI-Commands.

Setting up programmatic access — AWS SDK implementation

Now that you have accessed AWS console and AWS CLI what’s next, if you are a coder then you will be searching for an SDK to connect to the AWS services. Given that AWS has a large number of services it will be hard to have different libraries to connect and access it.

What if we have one reliable and secure library that can manage all of our AWS services, are you looking for it?. Then all you need to do is add the below dependency to your project. Magic happens in a single line

implementation ‘com.amazonaws:aws-java-sdk:1.11.792’

AWS provides the above JAVA SDK to access its services. It provides a Java API for Amazon Web Services. Using the SDK, you can easily build Java applications that work with Amazon S3, Amazon EC2, Amazon DynamoDB, and more. After adding the dependency we can start building our service class and access the service using java code. Let’s see a sample

Code Snippet to Create SQSBuilder and connect to it.

The above code will create an SQS Builder object and with the help of which you can create an SQS Queue with the specified name.

In the above example we haven’t provided any credentials and region, SDK will extract those details from the .aws/credentials and .aws/config files which we configured.

So wondering how to access services in different regions other than we set in the .aws/config file. Then pass System property with name AWS_REGION and provide the required region you want to access as a value to it, this will override the default value.

In the same way, SDK picks default profile and if you wish to select a different profile you have configured in .aws/config file, then pass your profile name as a value to the AWS_PROFILE environment variable or aws.profile system property.

For Example, I have added a profile with name USA_West

[default]
region = ap-south-1
[USA_West]
region = us-west-1

So if you want the USA_West to be selected, then pass the profile name like below

Select a specific profile using aws.profile system property.

Now that we have discussed how to access different regions and different profiles to connect to services across other regions. This leaves us to think about whether this workflow is convenient for a CI environment. Let’s see other options then, shall we?

Accessing AWS from a CI Environment

When we work in a CI Environment the Credentials and Regions should be available to the code without depending on the .aws/credentials and .aws/config files.

Let’s see how AWS SDK helps us to overcome this scenario.

Step 1: Create a CredentialsProvider Object.

BasicAWSCredentials class provided by the package ‘com.amazonaws.auth’ takes 2 parameters, the first one ‘AccessKey’ and the second one ‘SecretKey’. The credentials which we set manually can be passed to this class which will provide us with a BasicAWSCredentials, which then is passed to AWSStaticCredentialsProvider to create a CredentialsProvider object.

Code Snippet to Create AWS CredentialsProvider.

Step 2: Creating a Client with CredentialsProvider and Region.

AWS SDK provides us a way to pass Regions with the help of an Enum “Regions” provided by the package ‘com.amazonaws.regions’.

The CredentialsProvider we created in step 1 and Region Enum is passed to the Client when we build it. In the below example, awsCredentialsProvider and region are passed to the AmazonDynamoDBClientBuilder to create a service object.

Code Snippet to Create AWS DynamoDBClient with CredentialsProvider and Region.

Step 3: Accessing the DynamoDB Service

Amazon DynamoDB is a fully managed proprietary NoSQL database, once the DynamoDB Client Object is built then we can pass it to our service to connect to it and access it.

In the below example, the client is passed to DynamoDB service using which we can access its tables and perform operations over it.

Code Snippet to connect to DynamoDB.

I hope you will be interested to see how to build some other service. Let’s see some sample code on how to build S3 service and SNS.

Building S3 Service :

Amazon S3 is a service offered by AWS that provides object storage through a web service interface.

Code Snippet to build an S3 service client.

Building SNS Service :

Amazon SNS is a web service that enables applications, end-users, and devices to instantly send and receive notifications from the cloud.

Code Snippet to build an SNS service client and Create a Topic.

As we see in the above steps, with the help of CredentialsProvider and Regions we can create pipeline jobs to connect to AWS Services and do operations without depending on any local setup.

Conclusion

Being an early leader in cloud computing AWS lets us know how big it is when we Deep Diving into it. With that said imagine how difficult it would have been without an SDK or CLI to access and manage its services. Having the knowledge of AWS CLI and Java SDK helps us to reduce the effort to connect to any AWS service via command line and code.

Happy Coding !!!!

--

--