Top 5 ChatGPT use cases in AWS Solution Architecture role

Hey there, my friends! Today, I want to share with you the top 5 use cases of ChatGPT in my daily job. But before we dive in, let me tell you a little story. Recently, I made the switch from Obsidian to Logseq, for note-taking, and it got me into the habit of preferring shorter form in general. So, let's get straight to the point and explore how ChatGPT can help us save time and make our lives easier!

Tools Used in This Episode

Before we begin, let's quickly go over the tools we'll be using for these use cases:

  • AWS CLI: The command-line interface for interacting with AWS services
  • ChatGPT: Our trusty AI assistant
  • CloudFormation: Native AWS Infrastructure as a Code tool
  • ZSH: A powerful shell with cool features

Now, let's jump into the exciting world of ChatGPT!

Use Case 1: Getting Subnets

Imagine you're responsible for writing some Landing Zone handover documentation. You have to list numerous subnets across multiple AWS accounts. It can be quite tedious and time-consuming. But fear not! ChatGPT is here to save the day.

To generate a Markdown table listing all the subnets in a specific VPC, use the following command:

1List in the form of a Markdown table all the subnets in VPC ID x.
2Display the Subnet ID, CIDR Block, and the tag 'Name' in a
3column named 'Name'. Use the AWS CLI.
4Format the tags as a string, not an array.

Which generate:

1aws ec2 describe-subnets \
2	--filters "Name=vpc-id,Values=x" \
3	--query 'Subnets[*].{SubnetId: SubnetId, CidrBlock: CidrBlock, Tags: Tags[?Key==`Name`].Value | [0]}' \
4	--output text | awk 'BEGIN{print "| Subnet ID | CIDR Block | Name |"} {printf("| %-12s | %-11s | %-4s |\n", $1, $2, $3)}'

The result? Ta-da! A neatly formatted table with all the necessary details.

Subnet IDCIDR BlockName
subnet-0a703b1877c7d6b3a10.201.10.0/26
subnet-Application-1-primary-vpc
subnet-0e8f05f1b6c45ec5110.201.10.192/26
subnet-Data-2-primary-vpc
subnet-0fd362daf6757849010.201.10.128/26
subnet-Data-1-primary-vpc
subnet-0ef10e7b79ad98de510.201.10.64/26
subnet-Application-2-primary-vpc
subnet-0d931e05c320692eb10.201.8.0/24
subnet-Public-1-primary-vpc
subnet-06144454dbd44ed2110.201.9.0/24
subnet-Public-2-primary-vpc

Use Case 2: Connect to running Instances

Do you prefer using AWS Systems Manager (SSM) instead of SSH for accessing instances? If so, here's a handy alias for you:

1alias ssm="aws ssm start-session --target"

But to use this alias, you need a list of running EC2 instances with their IDs. ChatGPT has your back once again!

To generate a Markdown table listing all the running EC2 instances along with their instance IDs, status, and tags (env and project), use the following prompt:

1List all running EC2 instances, get instanceid,
2status, and tags: env, project.
3Based on that build markdown table, which will display
4following columns id, status, env, project and fill
5it with gathered data. Get tags as string, not array

Generated output was:

1aws ec2 describe-instances \
2	--filters "Name=instance-state-name,Values=running" \
3	--query 'Reservations[].Instances[].[InstanceId, State.Name, join(`,`,Tags[?Key==`env`].Value), join(`,`,Tags[?Key==`project`].Value)]' \
4	--output text | awk -F'\t' '{print "| " $1 " | " $2 " | " $3 " | " $4 " |"}'

Voila! You now have a table displaying the essential information about your running instances.

1| i-0f9c122fccddaf11f | running | dev | sectree |
2| i-0f9c1991ddde81fff | running | dev | sectree |
3| i-00dd122f3ade91dds | running | dev | sectree |

This solution as one minus, it's hard to build an zsh alias with it.. however we can use fzf here, and make it a bit easier.

Use Case 3: Documentation

Sometimes, we need to write documentation for internal purposes, especially when it comes to S3-like modules. ChatGPT can be a fantastic help in such situations!

To generate README-style documentation on the basic usage of the AWS CLI S3 command, simply ask ChatGPT:

1Generate readme style documentation
2about basic usage of aws cli s3 command. Print is as markdown

And just like magic, you'll have a comprehensive guide at your fingertips, covering installation, configuration, and basic usage of the AWS CLI S3 command. Which looks like that:

 1# AWS CLI S3 Command - README
 2
 3This document provides an overview of the basic usage of the AWS Command Line Interface (CLI) S3 command. The AWS CLI allows you to interact with Amazon Simple Storage Service (S3), a highly scalable cloud storage service provided by Amazon Web Services (AWS).
 4
 5## Installation
 6
 7To use the AWS CLI, you need to have it installed on your local machine. Follow these steps to install the AWS CLI:
 8
 91. Ensure that you have Python 3 installed on your machine. You can check this by running the command `python3 --version` in your terminal.
102. Install the AWS CLI by running the following command in your terminal:
11
12    `pip3 install awscli`
13
143. Once the installation is complete, you can verify it by running the command `aws --version`. It should display the version of the AWS CLI installed.
15
16## Configuration
17
18Before using the AWS CLI S3 command, you need to configure it with your AWS credentials. Follow these steps to configure the AWS CLI:
19
201. Obtain your AWS access key ID and secret access key from the AWS Management Console.
212. Run the following command in your terminal:
22
23    `aws configure`
24
253. When prompted, enter your AWS access key ID, secret access key, default region name, and default output format. The default region name is the AWS region where you want to perform your S3 operations (e.g., `us-east-1`).
264. Once the configuration is complete, the AWS CLI is ready to use.
27
28## Basic Usage
29
30The AWS CLI S3 command provides a set of subcommands to interact with S3 buckets and objects. Here are some common examples of how to use the AWS CLI S3 command:
31
32### 1. List Buckets
33
34To list all the S3 buckets in your AWS account, use the following command:
35
36`aws s3 ls`
37
38### 2. List Objects in a Bucket
39
40To list all the objects in a specific S3 bucket, use the following command:
41
42
43`aws s3 ls s3://bucket-name`
44
45Replace `bucket-name` with the name of the bucket you want to list objects from.
46
47### 3. Upload a File to a Bucket
48
49To upload a file to an S3 bucket, use the following command:
50
51`aws s3 cp /path/to/local/file s3://bucket-name/key`
52
53Replace `/path/to/local/file` with the local file path you want to upload, `bucket-name` with the name of the target bucket, and `key` with the desired key/name for the uploaded file in S3.
54
55### 4. Download a File from a Bucket
56
57To download a file from an S3 bucket to your local machine, use the following command:
58
59
60`aws s3 cp s3://bucket-name/key /path/to/local/file`
61
62Replace `bucket-name` with the name of the source bucket, `key` with the key/name of the file in S3, and `/path/to/local/file` with the local file path where you want to save the downloaded file.
63
64### 5. Delete a File from a Bucket
65
66To delete a file from an S3 bucket, use the following command:
67
68`aws s3 rm s3://bucket-name/key`
69
70Replace `bucket-name` with the name of the bucket containing the file to delete, and `key` with the key/name of the file in S3.
71
72## Additional Resources
73
74For more information on the AWS CLI S3 command and its options, you can refer to the official AWS CLI documentation:
75
76- [AWS CLI S3 Command Reference](https://docs.aws.amazon.com/cli/latest/reference/s3/index.html)

Use Case 4: Improving Security

Are you looking to improve the security and quality of your AWS CloudFormation templates? Look no further! ChatGPT can provide you with some insightful suggestions.

Let's say you have a CloudFormation template for an EC2 security group that could use some hardening. Ask ChatGPT to improve its security and quality, providing the template as input. You'll receive an enhanced version that priorities security best practices. With prompt like this:

 1Improve security and quality of fallowing AWS CloudFormation template:
 2Resources:
 3	MySecurityGroup:
 4	Type: AWS::EC2::SecurityGroup
 5	Properties:
 6		GroupDescription: My Security Group
 7		SecurityGroupIngress:
 8			- CidrIp: 0.0.0.0/0
 9			- IpProtocol: -1
10		SecurityGroupEgress:
11			- CidrIp: 0.0.0.0/0
12			- IpProtocol: -1

Our result will be:

 1Resources:
 2	WebServerSecurityGroup:
 3	Type: AWS::EC2::SecurityGroup
 4	Properties:
 5		GroupDescription: Security Group for Web Server
 6		SecurityGroupIngress:
 7			- CidrIp: 10.0.0.0/24
 8			- IpProtocol: tcp
 9			- FromPort: 22
10			- ToPort: 22
11		SecurityGroupEgress:
12			- CidrIp: 0.0.0.0/0
13			- IpProtocol: tcp
14			- FromPort: 80
15			- ToPort: 443

Use Case 5: Landing Zone Testing

Have you ever found yourself in need of testing a specific environment? It's a crucial step in post-implementation checks. But fear not, my fellow adventurers, for I bring you the power of ChatGPT, the friendly AI assistant who's always here to help!

Imagine this scenario: you want to spin up an AWS CloudFormation template that creates a t2.micro instance in a public subnet, another instance in a private subnet, an ALB (Application Load Balancer), and connects the ALB to the instance in the private subnet. Sounds complex, right? But with ChatGPT by your side, it's a breeze!

Here's the magical CloudFormation template that ChatGPT conjured up for us with usage of prompt:

1Generate AWS Cloudformation template that will spin one t2.micro
2instance in public subnet, second in private subnet,
3add ALB and attach it to instance in private subnet.
4Additionally: Treat subnets ids as parameters. Assume
5that networking part is done, already Install nginx on
6instance in private subnet Get ALB DNS as output variable,
7and based on it generate curl command

ChatGPT response:

 1AWSTemplateFormatVersion: "2010-09-09"
 2Parameters:
 3  PublicSubnetId:
 4    Type: String
 5    Description: ID of the public subnet
 6  PrivateSubnetId:
 7    Type: String
 8    Description: ID of the private subnet
 9
10Resources:
11  PublicInstance:
12    Type: "AWS::EC2::Instance"
13    Properties:
14      InstanceType: t2.micro
15      ImageId: ami-xxxxxxxx  # Replace with your desired AMI ID
16      SubnetId: !Ref PublicSubnetId
17
18  PrivateInstance:
19    Type: "AWS::EC2::Instance"
20    Properties:
21      InstanceType: t2.micro
22      ImageId: ami-xxxxxxxx  # Replace with your desired AMI ID
23      SubnetId: !Ref PrivateSubnetId
24      UserData:
25        Fn::Base64: !Sub |
26          #!/bin/bash
27          yum update -y
28          yum install -y nginx
29          service nginx start
30          chkconfig nginx on
31
32  LoadBalancer:
33    Type: "AWS::ElasticLoadBalancingV2::LoadBalancer"
34    Properties:
35      Name: MyLoadBalancer
36      Subnets:
37        - !Ref PublicSubnetId
38      SecurityGroups:
39        - !Ref LoadBalancerSecurityGroup
40      Type: application
41
42  TargetGroup:
43    Type: "AWS::ElasticLoadBalancingV2::TargetGroup"
44    Properties:
45      Name: MyTargetGroup
46      Port: 80
47      Protocol: HTTP
48      TargetType: instance
49      Targets:
50        - Id: !Ref PrivateInstance
51          Port: 80
52
53  Listener:
54    Type: "AWS::ElasticLoadBalancingV2::Listener"
55    Properties:
56      LoadBalancerArn: !Ref LoadBalancer
57      Port: 80
58      Protocol: HTTP
59      DefaultActions:
60        - Type: forward
61          TargetGroupArn: !Ref TargetGroup
62
63Outputs:
64  ALBDNS:
65    Value: !GetAtt LoadBalancer.DNSName
66    Description: DNS name of the Application Load Balancer

Isn't it amazing? This template will set up the instances, install Nginx on the private instance, create the ALB, and configure everything for you. Just provide the subnet IDs as parameters, and you're good to go!

But wait, there's more! ChatGPT even generated a handy curl command for you to test the ALB. Simply deploy the CloudFormation stack, and then use the ALB DNS name in the following command:

1$ ALB_DNS=$(aws cloudformation describe-stacks --stack-name <stack-name> --query 'Stacks[0].Outputs[?OutputKey==`ALBDNS`].OutputValue' --output text)
2$ curl http://${ALB_DNS}

Summary

In this blog post, we explore the question of whether we should fear AI, particularly Generative AI. In my opinion, there is no need for fear—at least not yet. Instead, I see AI, specifically ChatGPT, as a friend in need rather than an enemy. The post challenges the notion that AI, like an Articician Intelligent system, can replace Solution Architects, developers, and designers entirely. Through personal experiences, the I would like to highlights the importance of learning how to effectively communicate with AI prompts and utilize them for the right purposes. Ultimately, the I believes that AI serves as an invaluable assistant, capable of tackling tedious tasks that were previously too expensive or time-consuming for humans.