How Does a Python Docstring Work?

Problem scenario
You know that Python has a feature for documentation strings that can print out when your program runs. How does it work as comments and as something that is displayed at the time the code is executed?

Solution
To best illustrate how one works, create a file called test.py with this code and run it:

def cool_function(n):
    '''This is an example of a docstring.  You can explain the functionality of the function here.'''
    return "cool"

print(cool_function.__doc__)

How Do You Find the amd/i386 Architecture Indication for your Linux OS (to know what type of Debian Package you will need)?

Problem scenario
You are trying to find out what .deb file you need (e.g., from pkgs.org). How do you find out if you need to decide between options like i386, arm, or amd64 on a Debian or Ubuntu server? What should you do?

Solution
Run this command:

sudo dpkg --print-architecture

How Do You Troubleshoot the Error “An error occurred (InvalidParameterException) when calling the CreateNodegroup operation: The provided nodeRole is invalid.”?

Problem scenario
From the AWS CLI you try to create a node group. But you get this error: "An error occurred (InvalidParameterException) when calling the CreateNodegroup operation: The provided nodeRole is invalid."

Solution
The above can happen when you use a role's name but not the ARN string (such as arn:aws:iam::123456:role/foobar). Use the ARN string of the role instead of what you were using.

You may want to try a command such as this:

aws iam list-roles | jq -r '.Roles[] | select(.AssumeRolePolicyDocument.Statement[].Principal.Service=="eks.amazonaws.com")'

How Do You Use Boto3 to Create EC-2 Instances?

Problem scenario
You want to create EC-2 instances using an SDK (a program that automatically generates AWS resources). How do you do this?

Solution
Prerequisites

You have Boto3 installed. If you need assistance, see this posting if you have CentOS/RHEL/Fedora or this posting for Debian/Ubuntu.

Procedures
Run this program to create a RHEL server (but you can replace the "ami-0a54aef4ef3b5f881" with the image ID of your choice):

# Replace AKIAabcdefghijk with your AWS access key ID
# Replace 1a2b3c4d5e6f7g8h9/i0j0k9l8m7n6o5p4q3r2s1tuvwxyz with your AWS Secret Access key
# Replace us-west-1 with the region of your choice
# Replace ami-0a54aef4ef3b5f881 with the image ID of your choice.
# Replace subnet-abcd1234 with the subnet ID of your choice
# Replace foobarkeyname with the AWS key pair name of your choice
# You can set the MaxCount to the number of EC-2 instances you want to create.  This example will create 2.

import boto3
ec2 = boto3.resource('ec2', region_name='us-west-1', aws_access_key_id='AKIAabcdefghijk', aws_secret_access_key='1a2b3c4d5e6f7g8h9/i0j0k9l8m7n6o5p4q3r2s1tuvwxyz')

# create a new EC2 instance
instances = ec2.create_instances(
     ImageId='ami-0a54aef4ef3b5f881',
     MinCount=1,
     MaxCount=2,
     SubnetId='subnet-abcd1234',
     InstanceType='t2.micro',
     KeyName='foobarkeyname'
 )

What Is The Difference between Iterable and Subscriptable in Python?

Question
You have seen messages when coding in Python about iterable and subscriptable. What is the difference between these two?

Answer
A subscriptable does not necessarily use the iter method, but an iterable always does.

A subscriptable object in Python leverages the getitem() method, but it may or may not use the iter method.

To learn more, see these links: StackOverflow, Programiz.com, and w3schools.com

How Do You Troubleshoot an apt Command That Has an Error “An error occurred during the signature verification”?

Problem scenario
You are trying to run an apt command. It is failing. You get an error like this:

W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://repo.mongodb.org/apt/ubuntu…/foobar/1.1 Release: The following signatures were invalid: KEYEXPIRED 1507497109

What should you do?

Possible solution #1
This is not recommended. If for some reason you are not concerned about security on the server, try one or both of these commands:

sudo apt-get update --allow-unauthenticated
sudo apt-get update --allow-insecure-repositories

You may want to read this posting.

Possible solution #2
Find sources.list: sudo find / -name sources.list

If the file has references to foobar (where "foobar" is the repository name in the error), back up sources.list, edit sources.list so there are no references too it. Run sudo apt -y update, and try your apt command again.

Possible solution #3
Go to /etc/apt/sources.list.d/
Find files that refer to the "foobar" in the error. Back them up to files like this:
bak.original.name.bak. Now run the sudo apt -y update command. Now try your apt command again.

Possible solution #4
If the error is like this "W: An error occurred during the signature verification. The repository is not updated and the previous index files will be used. GPG error: http://packages.cloud.google.com/apt cloud-sdk-buster InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ABC1234567DEF890 NO_PUBKEY DEC9876GFE999

Run these commands*:

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABC1234567DEF890
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DEC9876GFE999

*Replace "ABC1234567DEF890" or "DEC9876GFE999" with the hexadecimal string you see. You may only have to run the command one time.

How Do You Find out which VPC Your Lambda Function Is In?

Problem scenario
Some external requests that a Lambda function is making are not working. The Lambda logs indicate that the function is timing out. You want to see what VPC your Lambda function is in. What do you do?

Solution
Log into AWS via the web UI.
Go here (but change "us-west-1" in both places to your VPC's region: https://us-west-1.console.aws.amazon.com/lambda/home?region=us-west-1#/functions/ )
Search for "VPC"
It may be that your Lambda function is not attached to any VPC.

How Do You Troubleshoot dpkg Errors from apt Commands like “package post-installation script subprocess returned error exit status 1”?

Problem scenario
On a Debian/Ubuntu server, you are trying to run apt commands. But you get errors like this:

dpkg: error processing package oracle-java11-installer-local (--configure):
installed oracle-java11-installer-local package post-installation script subprocess returned error exit status 1
Setting up nmap-common (7.80+dfsg1-2build1) …
Setting up liblua5.3-0:amd64 (5.3.3-1.1ubuntu2) …
dpkg: dependency problems prevent configuration of default-jre-headless:
default-jre-headless depends on openjdk-11-jre-headless; however:
Package openjdk-11-jre-headless is not installed.
Package oracle-java11-installer-local which provides openjdk-11-jre-headless is not configured yet.

dpkg: error processing package default-jre-headless (--configure):
dependency problems - leaving unconfigured
No apport report written because the error message indicates its a followup error from a previous failure.
dpkg: dependency problems prevent configuration of default-jre:
default-jre depends on default-jre-headless (= 2:1.11-72); however:
Package default-jre-headless is not configured yet.
Version of default-jre-headless on system, provided by oracle-java11-installer-local:amd64, is .
default-jre depends on openjdk-11-jre; however:
Package openjdk-11-jre is not installed.
Package oracle-java11-installer-local which provides openjdk-11-jre is not configured yet.

What should you do?

Solution
Remove oracle-java11-installer-local. Run a command like this: sudo apt -y remove oracle-java11-installer-local