This is the second part of the series that is discussing the AWS tools for Windows PowerShell. We will discuss about how AWS tools interact with EC2 and S3 services.

VMware Training – Resources (Intense)

For EC2 service we will:

  • List the EC2 instances
  • Launch a new instance
  • Terminate an instance

For S3 service we will:

  • List the buckets and their contents
  • Create a new bucket
  • Copy objects to the bucket and from the bucket
  • Delete objects from the bucket
  • Delete buckets

So let’s start with EC2. Right now, I have five EC2 instances running:

So let’s use Powershell to retrieve the same information and confirm the output is matching. We are going to use this command to filter out the tags only for instances: Get-EC2Tag | Where-Object {$_.ResourceType -eq ‘instance’}

The command “Get-EC2Instance” will give you all the instances that you own. However, the output returned will be limited to the key-pair used, the owner ID and the reservation ID.

Let’s go ahead and launch a new EC2 instance. For this we will need to specify a key pair and a security group. We will use the ones that are already configured.

First let’s see what key pairs and security groups we have available. For key pairs, we will use this command “Get-EC2KeyPair”:

And to find out what security groups we have “Get-EC2SecurityGroup”:

We are going to use the security group “launch-wizard-10” which allows SSH.

This will be the command to launch a new Windows EC2 instance”

New-EC2Instance -ImageId ami-beca16d6 -MinCount 1 -MaxCount 1 -KeyName key-access-aws-instances -SecurityGroups launch-wizard-10 -InstanceType t1.micro

If I’m checking the EC2 dashboard, I will see that a new EC instance is being launched based on the details provided:

As you can see, the new EC2 instance doesn’t have a tag assigned. So let’s assign one. You would need to create a variable to specify what will be the name of the tag and what will be the value. Then you will use that variable to set the tag to the instance:

$tag = New-Object Amazon.EC2.Model.Tag
$tag.Key = "Name"
$tag.Value = "TEST_EC2_FROM_POWERSHELL"
New-EC2Tag -Resources "i-2823e4c6" -Tags $tag

Right away, it can be seen in EC2 dashboard that the tag has been added:

Let’s move on and terminate the instance whose tag is “TEST_EC2_AMAZON_LINUX”. This is how it’s done “Stop-EC2Instance -Instance i-afe35044 –Terminate”:

Checking the EC2 console, we are seeing that the instance is in the “Terminated” state:

It’s time to move on to some basic operations that you can do on S3 (buckets and objects).

I created it using the S3 console a bucket and added these three files:

This is the content of the S3 bucket:

So, let’s confirm that we have the same information using AWS tools for PowerShell. To get the list of buckets, we need to use “Get-S3Buckets” and to get the content of the buckets, we need to use this command “Get-S3Object -BucketName test_bucket_us_east_1_001”:

Let’s create a new S3 bucket and then delete it. To create a new bucket, we need to use the command “New-S3Bucket” and to delete one, we need to use “Remove-S3Bucket”. The below is showing how to do that:

Now let’s copy a file from the S3 bucket locally to the computer and from the S3 bucket to the local computer.

First let’s see what files we have on our local folder:

We will copy the file “file1.txt” from S3 and name it “file1_from_S3”. This is the command “Read-S3Object -BucketName test_bucket_us_east_1_001 -Key file1.txt -File file1_from_S3”:

If you remember, on S3 bucket, we had only three files. Let’s copy the file “file4.txt” from the local computer to the S3 bucket under the name “file4_from_hdd.txt”. This is the command: “Write-S3Object -BucketName test_bucket_us_east_1_001 -File .\file4.txt -key file4_from_hdd.txt”:

The last operation that we will perform on an S3 bucket will be to delete objects from it. Let’s delete “file2.txt”. This will be the command “Remove-S3Object -BucketName test_bucket_us_east_1_001 -Key file2.txt”

By now we have covered everything that we mentioned in the beginning of the article.

So, you should know how to launch a new EC2 instance, list all the EC2 instances that you own, list all the S3 buckets that you own and their contents. You should also know how to copy files from and to the S3 buckets and to create and delete S3 buckets.

Due to the nature of this tool, AWS tools for PowerShell, this is just a very tiny part of what you can accomplish with AWS tools. There is no way that an article can cover all the possible cmdlets combinations.

The purpose of this article was to show you some of the most common operations in AWS.

The Amazon AWS provides great resources on what all the cmdlets are that you can use and how you can use them. Detailed information about each one and examples are also provided.

The scope of the series was to make you aware of another CLI type access to the AWS services, how to get access to it and how to use it.

I hope you will find useful the introductory information from the first part of the series along with the examples provided in the second part.

Reference

  1. AWS Tools for Windows PowerShell
  2. Using the AWS Tools for Windows PowerShell