How Do You Run an AWS CLI Command to Allow Access to an EC-2 Instance in Security Group?

Problem Scenario
You want to modify a security group to allow SSH connections from an IP address. How do you do this with the AWS CLI?

Solution
You have to know the security group’s ID. Replace “sg-abcd1234” with the group ID, and replace 1.2.3.0/24 with the IP address and subnet mask you want to allow to connect from in this command and then run it:

aws ec2 authorize-security-group-ingress \
–group-id sg-abcd1234 \
–protocol tcp \
–port 22 \
–cidr 1.2.3.0/24

How Do You List the ELBs in Your AWS Account when You Know One Exists But the Command You Run Shows None?

Problem scenario
You run an AWS CLI command to list load balancers. It returns none. You know there are load balancers. What should you do?

Solution
Run these commands:

aws elb describe-load-balancers
aws elbv2 describe-load-balancers

(It could be that you have a newer load balancer or an older style load balancer. If you run both of the above commands,

How Do You Troubleshoot the Boto3 Error “AttributeError: ‘ec2.ServiceResource’ object has no attribute ‘terminate_instances'”?

Problem scenario
You are trying to terminate some EC-2 instances. But you see a message like this:

“AttributeError: ‘ec2.ServiceResource’ object has no attribute ‘terminate_instances'”
“AttributeError: ‘ec2.ServiceResource’ object has no attribute ‘destroy_instances'”
“AttributeError: ‘ec2.ServiceResource’ object has no attribute ‘delete_instances'”

What should you do?

Solution
Rewrite the terminate portion to look like this:

ec2.instances.filter(InstanceIds=[’i-0abcd1234′, ‘i-0wxyz9999’]).terminate() …

How Do You Use Boto and CloudFormation to Delete a Stack?

Problem scenario
You want to delete a CloudFormation Stack with Boto. What do you do?

Solution
Run a program like this (but replace “continualstack” with the name of the stack you deployed):

import boto3
cloudf = boto3.client(‘cloudformation’, region_name=’us-west-1′, aws_access_key_id=’AKIAMNOPQRST’, aws_secret_access_key=’FOOBAR1234/ZYXWV987654′)
json_stack=open(‘lampstack.json’, ‘r’).read()
cloudf.delete_stack(StackName=’continualstack’) …

How Do You Pass Two or More Subnets to an “aws eks” Command?

Problem scenario
You want to pass more than one subnet to an “aws eks” command.

You tried to delimit the list with commas (or separate two subnet IDs with commas). You received this error message:

An error occurred (InvalidParameterException) when calling the CreateNodegroup operation: The subnet ID ‘subnet-0abcd1234,subnet-zyxw9876’ does not exist (Service: AmazonEC2; Status Code: 400; Error Code: InvalidSubnetID.NotFound; Request ID: Proxy: null)

What should you do?

How Do You Deploy a Windows 2019 Server in AWS?

Problem scenario
You want to create a Windows 2016 server in AWS.  How do you do this?

Solution
1.  In the AWS Console go to the EC2 Dashbaord.
2.  Go to Launch Instance -Find the AMI for Microsoft Windows Server 2016 Base and click “Select.”
3.  Click “Next: Configure Instance Details”
4.  Click “Next: Add Storage”
5.  Click “Next: Add Tags”
6.