
Configuring ECS Resources using CDK and Amazon Q
Amazon Q Developer accelerates creation of ECS Resources, explains issues, and helps to resolve issues quickly.
David Scott
Amazon Employee
Published Mar 5, 2025
Welcome to another installment of Q-Bits, our regular series showcasing cool ways Amazon employees are leveraging Amazon Q Developer. Today, we're diving into how Q Developer can assist with creating and editing Docker Compose files.
I started by creating a new CDK project in a new folder using the CDK CLI. I have a preference toward Python so that is the language I chose but you can choose whichever supported language you prefer.
This will give you the basic structure you need to quickly get started with your project. From there I opened the Q Chat window in VS Code and used /dev to ask Q to generate the CDK for my deployment. The sample prompt I used is below:
Update the CDK to deploy a fully configured ECS Stack with CDK. Use CDK Context to choose between deploying using a Fargate or EC2 Launch type. Configure auto-scaling based on load balancer traffic. Use CodePipeline to orchestrate the deployment updates. Build a sample httpd container based on the "redhat/ubi9-minimal" image and deploy it to a new ECR repo for use by the app
In my experience it's important to be as detailed and specific as possible with giving Q instructions especially for CDK generation. The great thing about using the Q chat feature is I walked away to grab my lunch delivery while it was processing. I came back to the following response from the Q - Dev chat:

Since I was working from scratch I didn't review the generated Diffs but if you were making modifications to an existing project you will want to carefully review the generated Diffs. I did review the generated CDK and once I was happy enough with what was there I tested with deployment (and if you've done any work with LLM generated CDK in the past you're probably aware that it most likely did not work on the first try)
Shockingly we had some issues. First you see a deprecation warning which can be expected. I'm going to ignore that for now since it's not a critical need to fix, however, I will need to go back and fix it before I'm done to ensure it doesn't break the next time I update. Reviewing the stack trace the error manifests around Line 74 of the ecs_q_stack.py file.
Based on the error it may be easy to figure out what went wrong but in this case I didn't really know so I decided to have Q help me by highlighting the code to include the if-block, right click, go to Amazon Q, and choose Fix.

Q generated the following code segment and change summary. It made adjustments to use the more specific TaskDefinitions for both segments of the if-block. However, there is still an issue when we run another cdk deploy...

This one is pretty straightforward. The FargateTaskDefinition doesn't have a logging argument unlike the other ones. I could just remove that attribute and worry about it later...or I could ask Q if it has any ideas what to do. This time I'm going to use Q Chat which you can open with the + in the Q panel.
How can I specify ECS Fargate logging with the Python CDK since it does not have a logging attribute? As an example you can review my ecs_q_stack.py at line 73


I brought in the suggestion to remove the logging attribute from the FargateTaskDefinition and verified I had it as part of my container definition and did another cdk deploy

and we have now succeeded with the synthesis of our CDK and it is deploying. Depending on what was generated for you this may or may not successfully deploy due to the Docker Image. Mine did not immediately succeed so keep reading for how I resolved the issue with Q's help.
Originally my deployment failed because I had no image in my ECR Repository. This is primarily a Python issue due to not having support for cdk-docker-image-deployment to allow CDK to push to a custom-defined ECR repository. Q did, however, generate the following Dockerfile for me to stand up a basic httpd welcome page.
So I asked Q Chat how I can proceed
How can I build my Dockerfile from the CDK and deploy it to my ECS cluster without having to write a custom script

I inserted these changes and triggered another CDK Deploy and within a few minutes had a fully deployed and routable cluster.

Amazon Q Developer has significantly streamlined the ability to generate and solve problems with writing CDK even for somewhat complex stacks. It is intelligent enough to leverage L3 constructs where it makes sense (see the use of ApplicationLoadBalancedFargateService) and the various methods of troubleshooting will help you progress without needing to dig into the documentation as much as you would have had to do in the past.
As we continue to explore the capabilities of Amazon Q Developer, it's clear that it's becoming an indispensable tool for developers, whether working on personal projects or professional tasks. Stay tuned for more Q-Bits, where we'll continue to showcase how Amazon Q Developer is revolutionizing our development processes.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.