# Amazon Q Developer CLI Demo This readme demonstrates how to use Amazon Q Developer CLI to create infrastructure as code (IaC) for deploying a secure, highly available web service using AWS Fargate. Amazon Q Developer CLI extends far beyond container services. By completing this demo, you'll discover its versatility in automating various development tasks, making it a valuable tool for streamlining your software development lifecycle. ## What is Amazon Q Developer? Amazon Q Developer is an AI-powered assistant designed specifically for software development and DevOps tasks. It helps developers write, understand, and transform code, create infrastructure, troubleshoot issues, and answer questions about AWS services and best practices. This demo focuses on the new feature Amazon Q for command line. [Installing Amazon Q for command line](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line-installing.html) Additionally you can install the IDE extension. [Installing the Amazon Q Developer extension or plugin in your IDE](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/q-in-IDE-setup.html) ## What is Amazon Q Developer CLI? The Amazon Q Developer CLI is a command-line interface that brings the power of Amazon Q directly to your terminal. It allows you to: - Generate and modify code and infrastructure templates - Get answers to technical questions - Troubleshoot errors - Understand code and configurations - Automate repetitive tasks ## Demo Objective In this demo, we'll interact with Amazon Q Developer CLI through natural language at a terminal prompt. I set myself a task to build repeatable code for deploying a container on AWS from scratch. I'll use the `q chat` feature to help us build the repo structure, IaC and deploy a highly available container image on ECS Fargate. > We do this in 3 prompts and 97 words!! 1. Create a CloudFormation template that deploys a highly available NGINX web server using AWS Fargate 2. Secure the service by restricting access to only our IP address 3. Implement auto-scaling based on CPU utilization 4. Ask Q to update the stack to include additional tags 5. Replicate the same infrastructure using AWS CDK with TypeScript ## Demo Walkthrough ### Step 1: Setting Up the CloudFormation Template First, we used Amazon Q to create a CloudFormation template that provisions: - A VPC with public subnets across multiple availability zones - Security groups that restrict access to our IP address - An Application Load Balancer to distribute traffic - An ECS cluster with Fargate tasks running NGINX - Auto-scaling configuration based on CPU utilization - CloudWatch logging integration **Example Prompt** Command used to create the CloudFormation template <pre style="white-space: pre-wrap;"> q chat </pre> <pre style="white-space: pre-wrap;"> I want to create a IaC with a CFN folder. I want to create a yaml CFN file that deploys ECS Fargate service with autoscaling defaults. The docker image can be from a public repository that will show a basic web page that is accessible from the internet. Restrict the load balancer service to only being available via my current public IP address. The Fargate instance only needs standard IAM permissions for cloudwatch. </pre> Amazon Q generated a complete `fargate-service.yaml` file in the `CFN` directory with all necessary resources and configurations. ### Step 2: Deploying the CloudFormation Stack Q cli then deployed the CloudFormation stack, similiar to this using AWS CLI: ```bash= # Command used to deploy the stack aws cloudformation create-stack \ --stack-name fargate-web-service \ --template-body file://CFN/fargate-service.yaml \ --capabilities CAPABILITY_IAM ``` Amazon Q prompted us if we would like help to monitor the deployment status and verify that all resources were created successfully. ### Step 3: Adding Resource Tags We then wanted to modify the stack with new inputs. We updated the CloudFormation template to include tags for better resource management: **Example Prompt** Command used to update the stack with tags <pre style="white-space: pre-wrap;"> Update the CFN to include tags for environment=dev, customer=amazon, expiry=15-05-2025 </pre>pre> Amazon Q modified the template to add these tags to all resources and then updated the deployed stack. ### Step 4: Creating a CDK Version Finally, we used Amazon Q to create an equivalent infrastructure using AWS CDK with TypeScript: **Example Prompt** Command used to create the CDK project <pre style="white-space: pre-wrap;"> Create a CDK stack written in TypeScript that replicates the same infrastructure </pre> Amazon Q: 1. Set up a complete CDK project structure 2. Check for Typescript local library dependencies (and install) 3. Check for CDK installation (and install) 4. Created the necessary TypeScript files 5. Implemented the same infrastructure components as in the CloudFormation template 6. Added proper documentation 7. Synthesize the stack to make sure it will run ## Project Structure After the above 3 prompts, the blank repository now has the following directories and files. All from Amazon Q Developer reading and writing to the file system. ``` . ├── CFN/ │ └── fargate-service.yaml # CloudFormation template ├── CDK/ │ ├── bin/ # CDK app entry point │ ├── lib/ # CDK stack definition │ ├── cdk.json # CDK configuration │ ├── package.json # Node.js package configuration │ ├── tsconfig.json # TypeScript configuration │ └── README.md # CDK-specific documentation └── README.md # This file ``` ## Infrastructure Architecture ``` ┌───────────────────────────────────────────────────┐ │ AWS Cloud │ │ │ │ ┌─────────────────────────────────────────────┐ │ │ │ VPC │ │ ┌───────────────┐ │ │ │ │ │ │ │ │ ┌─────────────┐ ┌──────────────┐ │ │ │ User │ │ │ │ │ │ │ │ │ │ (Your IP: │ ┌─────────────────┐ │ │ │ Public │ │ Public │ │ │ │ xxx.xx.xx.xx)├────────►│ Application ├────────────────────┼──┼─►│ Subnet 1 ├───────►│ Subnet 2 │ │ │ │ │ │ Load Balancer │ │ │ │ │ │ │ │ │ └───────────────┘ │ (Internet- │ │ │ └──────┬──────┘ └──────┬───────┘ │ │ │ facing) │ │ │ │ │ │ │ └────────┬────────┘ │ │ │ │ │ │ │ │ │ ▼ ▼ │ │ │ │ │ ┌──────────────────────────────────────┐ │ │ │ │ │ │ │ │ │ └─────────────────────────────┼──┼─►│ ECS Cluster │ │ │ │ │ │ │ │ │ │ │ │ ┌─────────────┐ ┌─────────────┐ │ │ │ │ │ │ │ Fargate │ │ Fargate │ │ │ │ │ │ │ │ Service │ │ Service │ │ │ │ │ │ │ │ (NGINX) │ │ (NGINX) │ │ │ │ │ │ │ │ Container │ │ Container │ │ │ │ │ │ │ └─────────────┘ └─────────────┘ │ │ │ │ │ │ │ │ │ │ │ │ Auto-scaling Group │ │ │ │ │ └──────────────────┬───────────────────┘ │ │ │ │ │ │ │ │ └─────────────────────┼───────────────────────┘ │ │ │ │ │ ▼ │ │ ┌───────────────┐ │ │ │ CloudWatch │ │ │ │ Logs │ │ │ └───────────────┘ │ │ │ └───────────────────────────────────────────────────┘ ``` ## Key Benefits of Using Amazon Q Developer CLI 1. **Rapid Infrastructure Development**: Generated complete, working CloudFormation and CDK templates in minutes 2. **Best Practices Built-in**: Implemented security, high availability, and proper resource configurations 3. **Contextual Understanding**: Maintained consistency between different IaC approaches (CloudFormation and CDK) 4. **Iterative Refinement**: Easily updated resources with new requirements (like adding tags) 5. **Documentation Generation**: Created comprehensive documentation explaining the architecture ## Conclusion This demo showcases how Amazon Q Developer CLI can significantly accelerate infrastructure development by generating, explaining, and modifying infrastructure as code. It demonstrates the ability to work with multiple IaC frameworks (CloudFormation and CDK) while maintaining consistent architecture and best practices. By using Amazon Q Developer CLI, we were able to quickly create a secure, highly available web service with proper access controls, auto-scaling, and monitoring - all while following AWS best practices. Amazon Q Developer CLI enabled us to create, modify and run code. The prompt retains the context of the chat history enabling you to iterate on your work until you are satisfied with the output. By having the chat available in the terminal window you can have the assistant help with a diverse range of tasks you'd like to perform in your IDE or AWS account. ## Getting Started To try this demo yourself, follow the documentation [Using Amazon Q Developer on the command line](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line.html) Experience the power of Amazon Q Developer CLI on your own projects. In minutes, I was able to generate **380 lines** of **CloudFormation** code, deploy a complete AWS service, and create a full synthesized CDK project - tasks that would typically take hours to complete manually. You may remember that I started with a blank directory. Here is all the files created by Amazon Q Developer CLI at the completion of the demo. ![image](https://hackmd.io/_uploads/S1y5WzT0ke.png)