Part 2

Welcome to the second compilation I'm putting together of quick tips on AWS services. To see my other tips, click on Parts 1 & 3 below.

As always I'm assuming you have all the required credentials and privileges set up to perform the actions in these tips.

Try the following clickable links for tips on:

Creating pre-signed URLs for S3 objects

Scheduling an AWS Lambda on a recurring basis (like a cron job)

Various useful AWS CLI commands

Using Python to download an S3 object to your local laptop/PC

Creating pre-signed URL's for S3 object

By default, all Amazon S3 objects are private, and only the object owner has permission to access them. Sometimes it's useful to be able to allow others to download your S3 object without changing the overall permissions on S3. Pre-signed URL's do this and you don't even need to have an AWS account to use them once you receive it. Only the object owner can create a pre-signed URL for it. You can create pre-signed url's in many ways. I'm going to show how to do it via the AWS CLI, so if you don't already have that installed, do so now. To give access to an object simply type something like the following in.

aws s3 presign s3://your-bucket/your-object-key --expires-in 604800

The expires-in parameter is the number of seconds that the object will be made available for download. So in this example, it's set to 7 days. After you type in this command you'll receive a large string something like this in return.

https://s3-path-to-your-object?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=ASIA36TOLVNMWOEY65VN%2F20230728%2Feu-west-2%2Fs3%2Faws4_request&X-Amz-Date=20230728T141125Z&X-Amz-Expires=604800&X-Amz-SignedHeaders=host&X-Amz-Security-Token=FwoGZXIvYXdzENj%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaDFJ6gIf7WR5r4qSGHSLnAewJNuwN7%2BA7AjFzyZ%2F7dBCj%2F0cp7ZEjuRT6mJBZT7V14JmX7fWZS03VXOZyid%2F1Ta2bNR7NGMX16Jceomw1SOhUluW36ZaxQPlEGcfsZfH1E1Q4exzLnwrteL7uCjuUhf%2FffpmKKncrejHztL7K2al%2F8%2BrUv5W0m4NMwJcoFnzGyDh1b1IIU8kFnLTVnprZtMt17RWA3iFypbUgh2gtT9Cw4AP%2BnVTBVORz03wUHr46DF5qEyiasJXaxJTjYSXH3yNIU5ljbL5LJ3PaQSTfzWXafCupc5sh5S8DOzIZNtILuXiwKltxcSiFhoqmBjIr6oAUOPJxosapvu5%2B79q9qsTpplwoo8VlM2oWPvcRMl5UnSNZsSBPR70UPw%3D%3D&X-Amz-Signature=447e688ced28b07414b12b0d2d0e88ab0ba4fd5e7ee9147c87be7589c7be60d9

The one caveat to the above is that if you create a pre-signed URL while signed in to AWS using temporary tokens or credentials, the URL will expire immediately after the token or credentials expire. This is the case even if the URL was created with a later expiration time.

Scheduling an AWS Lambda on a recurring basis

Want to schedule an AWS Lambda to run on a recurring schedule, say for example, 10 AM on the first Thursday of each month?

Easy, just trigger it from Eventbridge using a CRON expression. This will do it:

00 10 ? * 5#1 *

Various useful AWS CLI commands

Anything you can do via the AWS console, you can also do via the AWS command line interpreter ( the CLI). So it's useful to get to know this utility and some of the cool things it does. If you don't already have the CLI installed on your system go to:-

You'll find instructions there for installation on Linux, Mac and Windows.

Here are a few useful things you can do, (assuming you have all the required privileges).

List the contents of an S3 bucket

aws s3 ls your_bucket_name

Leave out your_bucket_nameif you want to list ALL your buckets.

Modify an RDS instance type

aws rds modify-db-instance — db-instance-identifier <your_db_instance> — db-instance-class db.m4.large — apply-immediately

The --apply-immediately option causes the change to take effect immediately. If you don't include this option, the change will take effect during the next maintenance window.

Restore an RDS DB via a snapshot

aws rds restore-db-instance-from-db-snapshot — db-instance-identifier your_db_instance — db-snapshot-identifier your_dbsnapshot_name

Create an S3 bucket

aws s3 mb s3://your_bucket_name

Copy a file to and from your S3 bucket

To S3: aws s3 cp your-file.txt s3://your_bucket_name

From S3: aws s3 cp s3://your_bucket_name/your_file.txt your_local_file.txt

Using Python to download an S3 object to your local laptop/PC

import boto3

def download_file():
    s3 = boto3.client('s3')

    bucket_name = 'my-bucket'
    file_key = 'my-key'
    local_file_name = 'local-file.txt'

    s3.download_file(bucket_name, file_key, local_file_name)

download_file()

Done!

That's all for me for now. Hopefully, you found this article useful. If you did, please check out my profile page at this link. From there, you can see my other published stories and subscribe to get notified when I post new content.

If you liked this article, you'll probably find these of interest too.