Task 2: AWS Shell Scripting with Node.js on Windows
Task Details
Objective:
The goal of this task is to create a Node.js script that interacts with AWS services to list IAM users and groups, and then save the results to a file. This task will help you understand how to use the AWS SDK for JavaScript in a Node.js environment on a Windows machine. Additionally, the task will introduce you to basic AWS service integration using EC2, S3, and RDS.
Task Breakdown:
Create a Node.js Script:
The script will utilize the AWS SDK to perform the following actions:
List all IAM users.
List all IAM groups.
Output the Results to a File:
- The script will save the output to a text file on your local machine.
AWS Services to Use:
EC2: Optionally, the script can be run on an EC2 instance.
S3: You can upload the output file to an S3 bucket for storage.
RDS: While this script doesn't directly interact with RDS, understanding this service is essential for broader AWS knowledge.
Solution Document
1. Prerequisites
Before you begin, ensure you have the following:
Node.js Installed: Download and install Node.js from nodejs.org.
AWS CLI Installed: Install the AWS CLI for Windows by following the instructions here.
-
AWS SDK for JavaScript: This library allows interaction with AWS services from Node.js.
AWS Credentials Configured: Use
aws configure
to set up your AWS credentials, or configure them using environment variables.Steps to Configure AWS CLI Using
aws configure
Open Command Prompt or PowerShell on your Windows machine.
Run the
aws configure
Command:aws configure
Enter Your AWS Credentials:
AWS Access Key ID: Enter your AWS Access Key ID when prompted. This key is used to authenticate your requests to AWS. If you don’t have an access key, you can generate one in the AWS Management Console under the IAM section.
AWS Secret Access Key: Enter your AWS Secret Access Key. This key is used along with your Access Key ID to securely authenticate requests to AWS services.
Default Region Name: Enter the AWS region you want to use by default (e.g.,
us-east-1
,us-west-2
). This setting determines which AWS region your requests will be sent to by default.Default Output Format: Choose the output format for the CLI. The most common formats are
json
,text
, andtable
. For most use cases,json
is recommended.
The process will look like this:
AWS Access Key ID [None]: YOUR_ACCESS_KEY_ID
AWS Secret Access Key [None]: YOUR_SECRET_ACCESS_KEY
Default region name [None]: us-east-1
Default output format [None]: json
Verify Configuration:
After completing the configuration, you can verify it by running:
aws sts get-caller-identity
This command will return information about the IAM identity you configured. If it returns a JSON response with your account and user details, the configuration was successful.
Generating AWS Access Keys (If Needed)
If you don't have an access key yet, follow these steps to create one:
Log in to the AWS Management Console.
Navigate to the IAM Dashboard.
Select "Users" from the navigation pane.
Choose the User for which you want to create an access key (or create a new IAM user).
Go to the "Security credentials" tab.
Scroll down to the "Access keys" section and click on "Create access key".
Download the Access Key ID and Secret Access Key. Make sure to store the secret access key securely, as it will not be shown again.
Changing Configuration
You can always reconfigure your credentials by running aws configure
again or by manually editing the configuration files located in the .aws
directory in your user profile (typically found at C:\Users\<YourUsername>\.aws\
on Windows).
config: Contains the region and output format settings.
credentials: Contains your access key ID and secret access key.
By following these steps, you'll have your AWS CLI properly configured and ready to interact with AWS services.
2. Setting Up Your Node.js Project
Step 1: Initialize a Node.js Project
Open Command Prompt or PowerShell.
Create a new directory for your project and navigate into it:
mkdir aws-scripting cd aws-scripting
Initialize a new Node.js project:
npm init -y
Step 2: Install AWS SDK
Install the AWS SDK for JavaScript:
npm install aws-sdk
3. Writing the Node.js Script
Step 1: Create the Script
Create a new file named
list_iam.js
in your project directory.Add the following code to
list_iam.js
:const AWS = require('aws-sdk'); const fs = require('fs'); // Configure AWS SDK AWS.config.update({ region: 'us-east-1' }); // Update the region as needed // Create IAM service object const iam = new AWS.IAM(); async function listIAMUsersAndGroups() { try { // List IAM users const usersResponse = await iam.listUsers().promise(); const users = usersResponse.Users.map(user => user.UserName).join('\n'); // List IAM groups const groupsResponse = await iam.listGroups().promise(); const groups = groupsResponse.Groups.map(group => group.GroupName).join('\n'); // Write results to file const output = `Listing IAM Users:\n${users}\n\nListing IAM Groups:\n${groups}`; fs.writeFileSync('iam_list_output.txt', output); console.log('Output saved to iam_list_output.txt'); } catch (error) { console.error('Error listing IAM users or groups:', error); } } listIAMUsersAndGroups();
AWS SDK Configuration: Set the AWS region where your IAM service is located.
IAM Service Object:
const iam = new AWS.IAM();
creates an instance of the IAM service.Listing IAM Users and Groups: Uses
iam.listUsers().promise()
andiam.listGroups().promise()
to list users and groups.Writing Output: The results are written to
iam_list_output.txt
using Node.js'sfs
module.
Step 2: Run the Script
Open Command Prompt or PowerShell as Administrator.
Navigate to the directory where
list_iam.js
is saved.Run the script using Node.js:
node list_iam.js
Verify the output in
iam_list_output.txt
to ensure it contains the IAM users and groups.
6. Conclusion
This document has provided a comprehensive guide to completing Task 2 using Node.js on a Windows machine. By following these steps, you can list IAM users and groups, output the results to a file, and integrate this with other AWS services like EC2 and S3. This approach not only helps you accomplish the task but also deepens your understanding of how to interact programmatically with AWS.