MoreRSS

site iconThe Practical DeveloperModify

A constructive and inclusive social network for software developers.
Please copy the RSS to your reader, or quickly subscribe to:

Inoreader Feedly Follow Feedbin Local Reader

Rss preview of Blog of The Practical Developer

CinemaSins: Everything Wrong With KPop Demon Hunters In 16 Minutes Or Less

2025-11-22 06:00:55

Everything Wrong With KPop Demon Hunters In 16 Minutes Or Less

CinemaSins just dropped a cheeky, rapid-fire roast of the new KPop Demon Hunters movie, calling out every over-the-top moment and plot quirk—yet embracing the film’s sheer fun and spectacle as they tally up the sins.

Hungry for more? They’ve got a loaded Linktree, Patreon page and immortal polls to fill out, plus Discord and Reddit hangouts. Don’t forget to follow the sin-squad (Jeremy, Chris, Aaron, Jonathan, Deneé, Ian & Daniel) on Twitter and Instagram for your daily dose of movie nitpicks.

Watch on YouTube

Inside ChatGPT: Deconstructing "Attention Is All You Need" (Part 1)

2025-11-22 05:49:32

To understand how modern Large Language Models (LLMs) like ChatGPT work, we must first understand the architecture that changed everything: the Transformer. Before we dive into the complex layers, we need to establish why we moved away from previous methods and how the model initially processes language.

1. The Predecessor: Recurrent Neural Networks (RNNs) and Their Limitations

Before the "Attention Is All You Need" paper, the standard for processing sequential data (like text) was the Recurrent Neural Network (RNN).In an RNN, data is processed sequentially. We give the network an initial state (State 0) along with an input x1 to produce an output y1 and a hidden state. This hidden state is passed forward to the next step, allowing the network to "remember" previous inputs.

The Vanishing Gradient Problem

While intuitive, RNNs suffer from severe limitations, specifically slow computation for long sequences and the vanishing or exploding gradient problem.

To understand this, let's look at calculus, specifically the Chain Rule.

If we have a composite function

F(x)=(f∘g)(x)F(x) = (f \circ g)(x) F(x)=(fg)(x)
, the derivative is:
F′(x)=f′(g(x))⋅g′(x) F'(x) = f'(g(x)) \cdot g'(x) F(x)=f(g(x))g(x)

In a deep neural network, backpropagation involves multiplying gradients layer by layer (like the chain rule). If we have many layers, we are essentially multiplying many numbers together.

Imagine multiplying fractions like:

12×12×12×⋯ \frac{1}{2} \times \frac{1}{2} \times \frac{1}{2} \times \cdots 21×21×21×

As the number of layers (or time steps) increases, this number becomes infinitesimally small ("vanishes") or massively large ("explodes"). This makes it incredibly difficult for the model to access or learn from information that appeared early in a long sequence.

2. The Transformer Architecture

The Transformer abandons recurrence entirely, relying instead on an Encoder-Decoder architecture. It processes the entire sequence at once, which solves the speed and long-term dependency issues of RNNs.

The Input Matrix

Let's look at how data enters the model.

If we have an input sentence of length 6 (Sequence Length) and a model dimension (

dmodeld_{model}dmodel
) of 512, our input is a matrix of size
(6,512)(6, 512)(6,512)
.

Each row represents a word, and the columns (length 512) represent that word as a vector. You might ask: Why 512 dimensions?

We need high-dimensional space to capture:

  1. Semantic Meaning: What the word actually means.
  2. Syntactic Role: Is it a noun, verb, or adjective?
  3. Relationships: How it relates to other words (e.g., "King" vs "Queen").
  4. Context: Multiple contexts the word can appear in.

Input Embedding

Computers don't understand strings; they understand numbers. We take our original sentence:

"Your cat is a lovely cat"

First, we map these to Input IDs (their position in the vocabulary):
We then map these IDs into a vector of size 512. Note that these vectors are not fixed; they are learned parameters that change during training to better represent the word's meaning.

3. Positional Encoding

Since the Transformer processes words in parallel (not sequentially like an RNN), it has no inherent concept of "order." It doesn't know that "Your" comes before "cat." We must inject this information manually using Positional Encodings.

We want the model to treat words that appear close to each other as "close" mathematically. To do this, we use trigonometric functions because they naturally represent continuous patterns that the model can easily learn to extrapolate.

We add this positional vector to our embedding vector. The formula used in the paper is:

  • For even positions (
    2i2i2i
    ):
PE(pos,2i)=sin⁡(pos100002i/dmodel) PE(pos, 2i) = \sin\left(\frac{pos}{10000^{2i/d_{model}}}\right) PE(pos,2i)=sin(100002i/dmodelpos)
  • For odd positions (
    2i+12i+12i+1
    ):
PE(pos,2i+1)=cos⁡(pos100002i/dmodel) PE(pos, 2i+1) = \cos\left(\frac{pos}{10000^{2i/d_{model}}}\right) PE(pos,2i+1)=cos(100002i/dmodelpos)

This ensures that every position has a unique encoding that is consistent across training and inference.

4. Self-Attention: The Core Mechanism

This is the "magic" of the architecture. Self-attention allows the model to relate words to each other within the same sentence. It determines how much "focus" the word "lovely" should have on the word "cat."

The formula for Scaled Dot-Product Attention is:

Attention(Q,K,V)=softmax(QKTdk)V \text{Attention}(Q, K, V) = \text{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right)V Attention(Q,K,V)=softmax(dkQKT)V

Where:

  • Q (Query): What I am looking for.
  • K (Key): What I contain.
  • V (Value): The actual content I will pass along.

The Matrix Math

For a sequence length of 6 and dimension 512:

  1. We multiply Q (6 × 512) by K^T (512 × 6).
  2. This results in a (6 × 6) matrix.
  3. We apply the Softmax function. This turns the scores into probabilities (summing up to 1).

This (6 × 6) matrix captures the interaction between every word and every other word. When we multiply this by V, we get a weighted sum of the values, where the weights are determined by the compatibility of the Query and Key.

Key Benefits of Self-Attention:

  • Permutation Invariant: It treats the sequence as a set of relationships rather than a strict list.
  • Parameter Efficiency: Pure self-attention requires no learnable parameters (though the linear layers surrounding it do).
  • Long-range Dependencies: Words at the start of a sentence can attend to words at the end just as easily as adjacent words.

Key Benefits of Self-Attention:

  • Permutation Invariant: It treats the sequence as a set of relationships rather than a strict list.
  • Parameter Efficiency: Pure self-attention requires no learnable parameters (though the linear layers surrounding it do).
  • Long-range Dependencies: Words at the start of a sentence can attend to words at the end just as easily as adjacent words.

Summary & Looking Ahead

We have successfully moved away from the sequential limitations of RNNs and embraced the parallel nature of Transformers. We've learned how to convert text into meaningful vector spaces, inject order using positional encoding, and, most importantly, derive the mathematical foundation of how words "pay attention" to each other using Queries, Keys, and Values.

But there is a catch.

The mechanism we just described, a single pass of

softmax(QKTdk)V\text{softmax}(\frac{QK^T}{\sqrt{d_k}})Vsoftmax(dkQKT)V
, is only capable of focusing on one type of relationship at a time. For example, it might focus heavily on syntactic relationships (such as subject-verb agreement) but completely miss semantic nuances (like sarcasm or references).

Real-world language is too complex for a single "gaze." To build a model like ChatGPT, we need it to look at the sentence through multiple lenses simultaneously.

In Part 2, we will take the self-attention mechanism and clone it, creating Multi-Head Attention. We will then see how these attention scores are processed through Feed-Forward Networks to finally construct the complete Transformer block.

Stay tuned.

AWS community day Workshop: Building Your First DevOps Blue/Green Pipeline with ECS

2025-11-22 05:49:20

In this workshop, I will guide you step by step how to build a Blue-Green deployment pipeline with AWS pipeline and deploys on AWS ECS with EC2. This deployment strategy helps teams to first test an app while routing traffic to a non-prod route, when confirmed that the app functions as expected, then the traffic is routed to prod. In case of any failure, a rollback is initiated for the previously working version hence avoiding downtime.

Architecture

App Flow

Application Archichitecture

The Infrastructure consists of an Application Load Balancer which exposes the app on Port 80, routes traffic to the autoscaling group managed by ECS, a fleet of EC2 instances running in two AZs for High availability. Each of these instances have ECS agent installed and the necessary software for running our Docker containers. These interact with Aurora PostgreSQL database in RDS for data storage. Aurora PostgreSQL database is configured to replicate data in another availability zone for a resilient database configuration. When there is a spike in traffic, ECS will launch other containers in order to accommodate the load.

Deployment Flow

Provided the developers do changes and want to update the deployed app, our CI/CD pipeline is configured to automate the deployment. With code stored in GitLab, once there is a push, AWS CodePipeline is triggered via a connection app setup and authenticated to Gitlab, Codebuild builds, containerize and push the image to ECR, next Codedeploy deploys the new app, first will deploy it with port 8080 for tests, if the team is happy with their changes, they can approve for traffic to be switch to the newly deployed container and terminate the former. The former containers can be configured to still run for some time while the newly deployed is tested, if everything is fine, then the last version of the deployed app can be terminated.

Build it step by step

**Note**: _If you face issues putting it up and working, go to the optional stage and deploy with Cloudformation_

Ensure you have a code editor like VScode, you have a Gitlab account, an AWS account will appropriate permissions(full access to RDS, ECS, ECR, IAM), configure awscli on you terminal with access keys

Clone the Code

On your local computer terminal(i use wsl with ubuntu os) run

git clone https://gitlab.com/ndzenyuy/tripmgmt.git
cd tripmgmt
rm -rf .git
git init
code .

It will clone the repository that has the application code to your local machine, enter the folder and remove the .git file so as to remove the url for the repository from whence the code was pulled, therefore making it possible to use the code and push to your own repository.

On the browser, go to GitLab and create an account if you don't have one, in it, create a repository named "tripmgmt", you can make the project public and don't create it with a READme.md file. Now copy the repository http url and go back to your terminal and run the following:

git remote add origin <your repo url>
git add .
git commit -m "Initial commit"
git push --set-upstream origin main

These will push the code to the newly created repository.

Setup AWS Resources

Create required AWS IAM Roles

  • Task Execution Role: Grants the Amazon ECS container and Fargate agents permission to make AWS API calls on your behalf.

To create the ecsTaskExecutionRole IAM role

  1. Open the IAM console
  2. In the navigation pane, choose Roles, Create role.
  3. In the Trusted entity type, select AWS Service
  4. Under Use case, select Elastic Container Service and Elastic Container Service Task, then choose Next: Permissions.
  5. In the Attach permissions policy section, search for AmazonECSTaskExecutionRolePolicy, select the policy, and then choose Next: Review.
  6. For Role Name, type ecsTaskExecutionRole and choose Create role.
  7. Please save value of the Role ARN in the open text file to use in the later section of the workshop.
  • ECS Container Instance Role: This IAM role is required for the EC2 launch type. The Amazon ECS container agent makes calls to the Amazon ECS API on your behalf. Container instances that run the agent require an IAM policy and role for the service to know that the agent belongs to you.

To create the ecsInstanceRole IAM role

  1. In the navigation pane, choose Roles, Create role.
  2. In the Trusted entity type, select AWS Service
  3. Under Use case, select EC2, then choose Next: Permissions.
  4. In the Attach permissions policy section, search for AmazonEC2ContainerServiceforEC2Role, select the policy, and then choose Next: Review.
  5. For Role Name, type ecsInstanceRole and choose Create role.
  • ECS CodeDeploy Role: Before you can use the CodeDeploy blue/green deployment type with Amazon ECS, the CodeDeploy service needs permissions to update your Amazon ECS service on your behalf. These permissions are provided by the CodeDeploy IAM role.

To create the ecsCodeDeployRole IAM role

  1. In the navigation pane, choose Roles, Create role.
  2. In the Trusted entity type, select AWS Service
  3. Under Use case, select CodeDeploy and CodeDeploy - ECS, then choose Next: Permissions.
  4. In the Attach permissions policy section, AWSCodeDeployRoleForECS policy should come as selected, choose Next: Review.
  5. For Role Name, type ecsCodeDeployRole and choose Create role

Create Container Repository

  1. Open the Amazon ECR console.
  2. In the navigation pane, under Private Registry choose Repositories.
  3. On the Repositories page, choose Create repository.
  4. For Repository name, enter a name for your repository like devops/tripmgmtdemo
  5. For Tag immutability, keep it Mutable for this workshop. Repositories configured with immutable tags will prevent image tags from being overwritten. For more information, see Image Tag Mutability.
  6. For Encryption settings, select AWS Key Management Service (KMS). Use an AWS managed key. To read more about encrypting at rest, see ECR Encryption at Rest.
  7. For Scan on push, keep it Disabled for this workshop. Repositories configured to scan on push will start an image scan whenever an image is pushed, otherwise image scans need to be started manually. For more information, see Image Scanning.
  8. Choose Create repository.
  9. Please save value of the Repository URL in the open text file to use in the later section of the workshop.

Setup Aurora PostgreSQL RDS Database

In this section, we will setup Aurora PostgreSQL RDS with Multi-AZ configuration.

  1. Go to the AWS Management Console and open the Amazon RDS console.
  2. In the navigation pane, choose Databases.
  3. Choose Create database.
  4. In Choose a database creation method, choose Standard Create.
  5. In Engine options, choose Aurora (PostgreSQL Compatible).
  6. In Templates, choose Dev/Test template.
    1. Clear Auto generate a password check box.
    2. Enter Master password value as SuperSecretPGSqlPwd##2006 and enter the same password in Confirm password.
  7. In the DB cluster identifier field, give Aurora DB cluster name, tripmgmtdb-cluster
  8. To enter your master password, do the following in Credential Settings
  9. For Instance configuration, choose db.t4g.medium under Burstable classes (includes t classes)
  10. For Availability & durability, choose Create an Aurora Replica/Reader node in a different AZ (recommended for scaled availability)
  11. Keep defaults for Connectivity, Babelfish settings, Database authentication, Monitoring sections.
  12. For Additional configuration, enter tripmgmt under Initial database name.
  13. Choose Create database.
  14. For Databases, choose the name of the new Aurora DB cluster. On the RDS console, the details for new DB cluster appear. The DB cluster and its DB instance have a status of creating until the DB cluster is ready to use. When the state changes to available for both, you can connect to the DB cluster. Depending on the DB instance class and the amount of storage, it can take up to 20 minutes before the new DB cluster is available.
  15. On the Connectivity & security tab, note the port and the endpoint of the writer DB instance. Please save value of the endpoint and port of the cluster in the open text file to use in the later section of the workshop. Endpoint URL should be of format tripmgmtdb-cluster.cluster-UNIQUEID.AWSREGION.rds.amazonaws.com
  16. Click on Writer Node and on the Connectivity & security tab, please save value of the Security Group Id under Security section (i.e. sg-xxxxxxxx) in the open text file to use in the later section of the workshop.
  • Create Application Load Balancer and Target Groups

We will create an Amazon EC2 application load balancer. This will be the public endpoint to access Trip Management Monolith Application. The load balancer must use a VPC with two public subnets in different Availability Zones.
First create Target groups for your load balancer

  1. Sign in to the AWS Management Console and open the Amazon EC2 console
  2. In the navigation pane, choose Target groups and select Create target group.
  3. Under Basic configuration, select IP addresses target type.
  4. In Name, enter a target group name alb-tg-tripmgmtdemo-1
  5. In Protocol choose HTTP. In Port, enter 80.
  6. Keep rest of the settings as defaults.
  7. Select Next: Register Targets.
  8. Select Create target group.
  9. Repeat above steps to create another target group for Protocol HTTP and Port 8080, name it alb-tg-tripmgmtdemo-2

To create an Amazon EC2 application load balancer

  1. Sign in to the AWS Management Console and open the Amazon EC2 console
  2. In the navigation pane, choose Load Balancers, choose Create Load Balancer.
  3. Choose Application Load Balancer, and then choose Create.
  4. In Name, enter the name of your load balancer tripmgmtdemo-alb
  5. In Scheme, choose internet-facing.
  6. In IP address type, choose ipv4.
  7. Under Network mapping, in VPC, choose the default VPC, and then choose any two availability zones under Mappings. Please save value of the Availability Zone / Subnet ids in the open text file to use in the later section of the workshop.
  8. Under Security groups, choose Create new security group
    1. Give it the name ALBSecurityGroup in the Security group name field.
    2. Give it the description in the Description field.
    3. Add Inbound Rule, Allow 80 port (HTTP) inbound traffic from My IP
    4. Add Inbound Rule, Allow 8080 port (CustomTCPPort) inbound traffic from My IP
    5. Select Create security group
    6. Please save value of the newly created Security Group Id (i.e. sg-xxxxxxxx..) in the open text file to use in the later section of the workshop.
    7. Come back to Create Application Load Balancer tab, refresh Security groups and select newly created Security group, removing any existing pre-selected Security groups.
  9. Under Listeners and routing, configure two listener ports for your load balancer:
    1. Under Protocol, choose HTTP, and Under Port, enter 80.
    2. Under Default action, select alb-tg-tripmgmtdemo-1 Target Group.
    3. Choose Add listener.
    4. Under Protocol, choose HTTP, and Under Port, enter 8080.
    5. Under Default action, select alb-tg-tripmgmtdemo-2 Target Group.
  10. Choose Create load balancer.
  11. Go to Load Balancers and click on the newly created load balancer. From the Description tab, please save value of the DNS name of the Load Balancer in the open text file to use in the later section of the workshop.

Setup ECS Cluster

Create ECS Cluster

Amazon ECS creates an Amazon EC2 Auto Scaling launch template and Auto Scaling group on your behalf as part of the AWS CloudFormation stack.

  1. Open the Amazon ECS console.
  2. In the navigation pane, choose Clusters.
  3. On the Clusters page, choose Create Cluster.
  4. Under Cluster configuration, for Cluster name, enter ecs-cluster-tripmgmtdemo
  5. Under Infrastructure, keep AWS Fargate (serverless) selected and also select Amazon EC2 instances. Next, configure the Auto Scaling group which acts as the capacity provider.

Select Create new group, and then provide the following details about the group:

  • For Operating system/Architecture, choose Amazon Linux 2.
  • For EC2 instance type, choose t3.large. Here, t3.large selected as required for this workshop to complete in given time.
  • For Capacity, enter 2 for the minimum number and 4 for the maximum number of instances to launch in the Auto Scaling group.

  • Under Network settings for Amazon EC2 instances, choose default VPC and same two subnets, which were selected at that time of creating Application Load Balancer in previous section.

  • Choose Create

Create Security Group for EC2 Container Instance

  1. Open the Amazon EC2 console.
  2. On the navigation pane, under Network & Security, choose Security Groups
  3. On the next page, choose Create security group.
  4. For Security group name, enter ECS-ALB-SecurityGroup for the security group name and give meaningful description under Description.
  5. Choose default VPC
  6. Add Inbound Rule, Allow 80 port (HTTP) inbound traffic from Application Loadbalancer Security Group you have created in earlier section (i.e. sg-xxxxx).
  7. Add Inbound Rule, Allow 8080 port (CustomTCPPort) inbound traffic from Application Loadbalancer Security Group you have created in earlier section (i.e. sg-xxxxx).
  8. Select Create security group
  9. Please save value of the newly created Security Group Id (i.e. sg-xxxxxxxx..) in the open text file to use in the later section of the workshop.

Create ECS Task

  1. First create Amazon CloudWatch Log Group, where taskdef is configured to send all logs.

    1. Go to CloudWatch Console
    2. Select Log Groups,
    3. Click Create log group button,
    4. Log Group Name: enter tripmgmt-demo-ecstask-loggrp and Retention setting select 5 days
    5. Choose Create
  2. In order to create an ECS Task, we will need to create a ECS Task Definition file. Go to you cli, make sure it is pointing to the folder tripmgmt and delete the file taskdef.json by running the command

rm taskdef.json
  1. Copy below given environment variables in plain text-file and change respective environment variable values as per the workshop resources created by you in the previous sections. After modifying values of respective environment variables, execute these export commands on the commandline.
export LOG_GROUP=tripmgmt-demo-ecstask-loggrp
export DB_USERNAME=postgres
export DB_PASSWORD=SuperSecretPGSqlPwd##2006
export AWS_DEFAULT_REGION=<<AWS-REGION>>

export TASK_EXECUTION_ROLE_ARN="arn:aws:iam::<<AccountID>>:role/ecsTaskExecutionRole"
export AURORA_PGSQL_RDS_URL="tripmgmtdb-cluster.cluster-<<UNIQUEID>>.<<AWS-REGION>>.rds.amazonaws.com"
export ECR_LATEST_IMAGE_URL="<<AccountID>>.dkr.ecr.<<AWS-REGION>>.amazonaws.com/devops/tripmgmtdemo:latest"
  • LOG_GROUP : Verify name as you have created in Step No 1 in this section, only change if you have used different log group name.
  • DB_USERNAME: Database username set while creating Aurora PostgreSQL DB Cluster.
  • DB_PASSWORD: Database password set while creating Aurora PostgreSQL DB Cluster.
  • TASK_EXECUTION_ROLE_ARN: Task Execution Role created in Create IAM Roles section.
  • AURORA_PGSQL_RDS_URL: Aurora PostgreSQL RDS Cluster URL as created in Create Aurora PostgreSQL DB section.
  • ECR_LATEST_IMAGE_URL: Elastic Container Registry Image URL from Create Container Repository section.
  • Create the task definition from the command line by running
envsubst < "taskdef_src.json" > "taskdef.json"
aws ecs register-task-definition --cli-input-json file://taskdef.json
  1. Please save the value of the TaskDefinition ARN from output in the open text file to use in the later section of the workshop. It is at the very top of the output and should look something like arn:aws:ecs:<>:<>:task-definition/task-tripmgmt:1.

  2. Commit taskdef.json in repository.

git add taskdef.json
git commit -m "Updated Taskdef"
  1. You can also verify the Task Definition in the Amazon ECS console , left navigation bar under Amazon ECS, Task Definitions.

Prepare AppSpec File
AWS CodeDeploy requires AppSpec YAML-formatted file to manage deployment. In the project folder on the terminal, run the code below and edit the value of the taskdefinition arn to reflect what you had in step 4 above(similar to: arn:aws:ecs:<>:<>:task-definition/task-tripmgmt:1.)

nano appspec.yaml
  1. Commit appspec.yaml in repository
git add appspec.yaml
git commit -m "appspec file with Taskdef url"
git push

Optional: Deploy the infrastructure with Cloudformation (Recommended)

  1. Login to the console and search for cloudformation
  2. Create a new stack
  3. Prerequisite - Prepare template: Choose existing stack
  4. Specify template: Upload a template file
  5. Upload a template file: Choose file -> search on your computer, in the tripmgmt/cloudformation-infra folder/subfolder and choose tripmgmt.yml

On specify Stack details

Stack name: tripmgmt
KeyPairName: <select a keypair in you account or create on if absent>
PublicSubnet1Id: <select a public subnet>
PublicSubnet2Id: <select a public subnet>
VpcId: <select the default vpc id>
click next

Scroll down and acknowledge,

and click next scroll and click submit

This will create the infrastructure needed to run our Pipeline.

DevOps Pipeline with Blue/Green Deployment

The Engineering team commit changes to code base and then push those changes to a git repository. It triggers AWS CodePipeline action. AWS CodeBuild will compile source code and build & store container image in an Elastic Container Registry. AWS CodeDeploy will initiate Blue/Green deployment of ECS Task(s) in an ECS Cluster through ECS Service.

Setup CodeBuild with GitLab Integration

To setup AWS CodeBuild, first we need to create/modify buildspec.yml file to describe build steps. After that, we need to configure the CodeBuild Project linking with the GitLab Repository.
On the cli, run the code below and edit the values of the following parameters to suite those you have:

  • AWS_ACCOUNT_ID
  • AWS_DEFAULT_REGION
  • REPOSITORY_URI
nano buildspec.yml

After editing, run ctrl + x, when prompted to save changes press Y then press enter to confirm.

Setup CodeBuild Project

  1. Open the CodeBuild console.
  2. On the Build projects page, choose Create build project.

  1. In Project configuration: Enter a name for this build project tripmgmt-demo-build. Build project names must be unique across each AWS account. You can also include an optional description of the build project to help other users understand what this project is used for.

  1. If required, Select Build badge to make your project's build status visible and embeddable.
  2. In Source: For Source provider, choose GitLab.
  3. You will then get an error stating that you have not connected to GitLab.
  4. Press the Manage account credentials link.
  5. Select GitLab as your credential type.
  6. In Connection, press create a new GitLab connection.
  7. Give the connection the name tripmgmt-connection.
  8. Press the Connect to GitLab button and authenticate.
  9. Authorize AWS Connector for GitLab.
  10. After being redirected back to the AWS Console, press Connect.
  11. You should now be able to save and add your new GitLab connection.
  12. From Repository, choose the repository you have created for this project demo.

  1. For Source version, type main.
  2. In Environment: choose Managed Image with Operating system as Ubuntu,
  3. For Runtime(s) choose Standard and Image choose the one with Standard:5, having Image Version as Always use the latest image for this runtime version

  1. For Service role choose New service role and give it a meaningful name codebuild-tripmgmt-demo-build-service-role
  2. Open Aditional configuration in the same section. Check mark Privileged action.
  3. For Buildspec, select that you want to Use a buildspec file, add the buildspec.yml file location and name - buildspec.yml

  1. For Logs, check mark CloudWatch logs and give Group name as codebuild-logs, Stream name as tripmgmtdemo-build.
  2. Click Create build project.

  1. Now edit newly created service role codebuild-tripmgmt-demo-build-service-role to allow accessing ECR repository through AmazonEC2ContainerRegistryPowerUser managed policy.

    1. Open codebuild-tripmgmt-demo-build-service-role role from IAM console .
    2. Choose Attach policies.
    3. To narrow the available policies to attach, for Filter, type AmazonEC2ContainerRegistryPowerUser
    4. Check the box to the left of the AWS managed policy and choose Attach policy and Update.
  2. Select the build project and Start build to test GitLab and CodeBuild integration.

  3. Post successful build, you can verify new docker image in Amazon ECR console.

Deploy on Amazon ECS with EC2

As per the architectural diagram, Engineering team commit changes to code base and then push those changes to a Git repository. It triggers AWS CodePipeline action, AWS CodeBuild will compile source code and build & store container image in an Elastic Container Registry. AWS CodeDeploy will initiate Blue/Green deployment of ECS Task(s) in an ECS Cluster through ECS Service. Upon successful deployment of ECS Task(s), users can access Web Portal through DNS URL pointing to ALB endpoint. ALB endpoint serves client requests from ECS Cluster after automatically adjusting capacity to maintain steady state.

Create CodeDeploy Application

To create a CodeDeploy application

  1. Open the CodeDeploy console and choose Create application.
  2. In Application name, enter the name you want to use Deploy-Tripmgmt-Demo-App
  3. In Compute platform, choose Amazon ECS.
  4. Choose Create application.

To create a CodeDeploy deployment group

  1. On your application page's Deployment groups tab, choose Create deployment group.
  2. In Deployment group name, enter a name that describes the deployment group deploygrp-tripmgmt-demo
  3. In Service role, choose a service role ecsCodeDeployRole, which was created in earlier section Create required AWS IAM Roles and grants CodeDeploy access to Amazon ECS.
  4. In Environment configuration, choose your Amazon ECS cluster name and service name.
  5. From Load balancers, choose the name of the load balancer that serves traffic to your Amazon ECS service.
  6. From Production listener port, choose the port and protocol for the listener that serves production traffic to your Amazon ECS service. (Production listener port : 80 and Test listener port : 8080)
  7. From Target group 1 name and Target group 2 name, choose the target groups used to route traffic during your deployment. Make sure that these are the target groups you created for your load balancer.
  8. Under Deployment settings,

    • Choose Specify when to reroute traffic and choose 0 days, 0 hours, and 5 minutes. This will reroute the traffic to successfully deployed updated tasks after 5 Minutes. This is the time required by you to verify the successfully deployed updated tasks and decide whether to rollback or continue reroute the traffic.
    • Under Deployment configuration, choose CodeDeployDefault:ECSAllAtOnce. It will configure CodeDeploy to shift all traffic to the updated Amazon ECS container at once.
    • Under Original revision termination, and choose 0 days, 0 hours, and 0 minutes. It will configure CodeDeploy to terminate the original tasks immediately after rerouting the traffic. You can choose to keep the original tasks for a desired duration as required. After termination of original tasks, you cannot rollback manually or automatically.
  9. Choose Create deployment group.

Create CodePipeline

In this section, we will create a DevOps pipeline with the following actions:

  • A source stage with a GitLab action
  • A build stage with a CodeBuild action
  • A deployment stage with an Amazon ECS deploy action with Blue/Green Deployment.
  1. Sign in to the AWS Management Console and open the CodePipeline console
  2. On the Welcome page, Getting started page, or the Pipelines page, choose Create pipeline.
  3. In Step 1: Choose creation settings, pick Build custom pipeline
  4. In Step 2: Choose pipeline settings, in Pipeline name, enter codepipeline-tripmgmt-demo.
  5. In Service role, Choose New service role to allow CodePipeline to create a new service role in IAM. Enter name as codepipeline-tripmgmt-demo-service-role

  1. In Artifact store, Choose Default location to use the default artifact store, such as the Amazon S3 artifact bucket designated as the default, for your pipeline in the region you have selected for your pipeline.
  2. Choose Next.
  3. In Step 2: Add source stage, in Source provider, choose GitLab. In Connection select the connection you created earlier. Following this, select the repository and default branch you chose earlier. The rest can be kept at default.
  4. Choose Next.
  5. In Step 3: Add build stage, in Build provider, choose Other build providers and then AWS CodeBuild. In Project name choose the name of the build project. The rest can be kept default.
  6. Choose Next.
  7. Choose Skip test stage at the bottom of the page.
  8. In Step 4: Add deploy stage, in Deploy provider choose Amazon ECS (Blue/Green)

  1. In AWS CodeDeploy application name, choose CodeDeploy Application name. In AWS CodeDeploy deployment group, choose CodeDeploy Application's Deployment Group.
  2. In Amazon ECS task definition, choose BuildArtifact and enter taskdef.json.
  3. In AWS CodeDeploy AppSpec file, choose BuildArtifact and enter appspec.yaml.
  4. Choose Next.

  1. Review the information, and then choose Create pipeline. Immediately, stop the Pipeline Execution with Stop and Abandon pop-up action.
    Modify Deploy Stage

    1. Open (or remain in) the CodePipeline console & select the Pipeline name, for example codepipeline-tripmgmt-demo.
    2. Click Edit, and scroll down at bottom and "Edit Stage" , click on Edit icon of Deploy Action Group.
    3. In Input artifacts, choose SourceArtifact and remove BuildArtifact
    4. Modify Amazon ECS task definition, choose SourceArtifact and keep taskdef.json.
    5. Modify AWS CodeDeploy AppSpec file, choose SourceArtifact and keep appspec.yaml.
    6. Choose Done
    7. Scroll up and press Save.

DevOps Pipeline in Action

  1. Go to Trip Management Monolith Application code base, preferrably home page and make a visible modification which you can identify after releasing a change. Look at the below screenshot, having (Tripment - New Change) at the end at line#7. File path: src/main/webapp/app/home/home.component.html

  1. Commit applied changes and push changes to the GitLab repository. This will trigger the pipeline:

On the browser where you pasted the DNS of the load balancer, check the app that is deployed, it should still be the one without the changes

After the deployment is complete, before traffic is rerouted, check the browser with the 8080 port, you should see the web page with the changes:

Code deploy still deploying the new container on ECS, traffic is still flowing to the initial deployment. Once the deployment stage is complete, it will forward test traffic to the same alb link but on a different port:8080

The pipeline will wait 5 minutes for the newly deployed app to be tested(as configured in code pipeline). Once the time elapses, the pipeline will automatically route traffic

After tests, the engineer can now reroute traffic by clicking on reroute traffic, after they are sure their changes are as expected:

The traffic will instantly be rerouted to the new tasks, and will keep the former tasks should incase there is need for rollback after live traffic is routed to it, now both links for test and prod will display the same app, while waiting for a termination of the former:

Traffic activity:

During the deployment phase, out of expected 2 task, ECS runs 4 tasks, this will ensure that the newly deployed app is stable before the termination of these can be authorized.

Clean-up

On the console, go to Cloudformation stacks and delete tripmgmt stack, or if you created the resources on the console, then delete them in the order they were created. Go to Developer tools and delete the pipeline, in codebuild, delete the build job.

Untitled

2025-11-22 05:45:26

Check out this Pen I made!

The 2025 Advantage: Multi-Stack + UX Thinking + AI Automation

2025-11-22 05:34:52

The dev ecosystem is shifting fast.
Engineers who combine design intuition, multi-stack skills, and AI-driven workflows are leading the next wave of digital innovation.

This blend is helping teams ship better products with fewer bottlenecks - and developers who embrace it are becoming the new industry leaders.

Introducing Nano Banana Pro: Complete Developer Tutorial

2025-11-22 05:30:41

You loved Nano-Banana? Created figurine images of all your friends and ghost face behind all your foes? Here now comes the not-so-nano "Gemini 3 Pro Image" model, that you will all prefer calling Nano Banana Pro!

While the Flash model (Nano Banana) brought speed and affordability, the Pro version introduces "thinking" capabilities, search grounding, and high-fidelity 4K output. It's time to go bananas with complex creative tasks!

This guide will walk you through the advanced features of Nano Banana Pro using the Gemini Developer API.

This guide will cover:

  1. Using Nano Banana Pro in Google AI Studio
  2. Project setup
  3. Initialize the Client
  4. Basic Generation (The Classics)
  5. The "Thinking" Process
  6. Search Grounding
  7. High-Resolution 4K Generation
  8. Multilingual Capabilities
  9. Advanced Image Mixing
  10. Pro-Exclusive Demos

Note: for an interactive version of this post, checkout the python cookbook or the AI Studio's Javascript Notebook.

1) Using Nano Banana Pro in Google AI Studio

While end-users can access Nano Banana Pro in the Gemini app, the best environment for developers to prototype and test prompts is Google AI Studio. AI Studio is a playground to experiment with all available AI models before writing any code, and it's also the entry point for building with the Gemini API.

You can use Nano Banana Pro within AI Studio. To get started, go to aistudio.google.com, sign in with your Google account, and select Nano Banana Pro (Gemini 3 Pro Image) from the model picker.

Contrary to Nano-Banana, the pro version doesn't have a free tier, which means you need to select an API key with billing enabled (see "project setup" section below).

Get started with Nano Banana Pro on AI Studio

Tip: You can also vibe code Nano Banana web apps directly in AI Studio at ai.studio/apps, or explore the code and remix one of the existing apps.

2) Project setup

To follow this guide, you will need the following:

If you already are a hardcore Gemini API user with all of that, great! just skip this section and move to the next one. Otherwise, here's how to get started:

Step A: Get your API Key

When you first log in on AI Studio, a Google Cloud project and an API key should be automatically created.

Open the API key management screen and click on the "copy" icon to copy your API key.

Copy your API key

Step B: Enable Billing

Since Nano Banana Pro doesn't have a free tier. You must enable billing on your Google Cloud project.

In the API key management screen, click Set up billing next to your project and follow the on-screen instructions.

Set up billing

How much does Nano Banana Pro cost?

Image generation with Nano Banana Pro is more expensive than the Flash version, especially for 4K images. At the time this post is published, a 1K or 2K image costs $0.134, while a 4K one costs $0.24 (plus the token cost of the input and the text output).

Check the pricing in the documentation for the latest details.

Step C: Install the SDK

Choose the SDK for your preferred language.

Python:

pip install -U google-genai
# Install the Pillow library for image manipulation
pip install Pillow

JavaScript / TypeScript:

npm install @google/genai

The following examples use the Python SDK for demonstration. Equivalent code snippets to use Nano Banana in JavaScript are provided in this JS Notebook.

3) Initialize the Client

To use the Pro model, you'll need to use the gemini-3-pro-image-preview model ID.

from google import genai
from google.genai import types

# Initialize the client
client = genai.Client(api_key="YOUR_API_KEY")

# Set the model ID
PRO_MODEL_ID = "gemini-3-pro-image-preview"

4) Basic Generation (The Classics)

Before we get into the fancy stuff, let's look at a standard generation. You can control the output using response_modalities (to get text and images or only images) and aspect_ratio.

prompt = "Create a photorealistic image of a siamese cat with a green left eye and a blue right one"
aspect_ratio = "16:9" # "1:1","2:3","3:2","3:4","4:3","4:5","5:4","9:16","16:9" or "21:9"

response = client.models.generate_content(
    model=PRO_MODEL_ID,
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=['Text', 'Image'], # Or just ['Image']
        image_config=types.ImageConfig(
            aspect_ratio=aspect_ratio,
        )
    )
)

# Display the image
for part in response.parts:
    if image:= part.as_image():
        image.save("cat.png")

Siamese cat

Chat mode is also an option (it's actually what I would recommend for multi-turn editing). Check the 8th example, "Polyglot Banana", for an example.

5) The "Thinking" Process (It's alive!)

Nano Banana Pro isn't just drawing; it's thinking. This means it can reason through your most complex, twisted prompts before generating an image. And the best part? You can peek into its brain!

To enable this, set include_thoughts=True in the thinking_config.

prompt = "Create an unusual but realistic image that might go viral"
aspect_ratio = "16:9"

response = client.models.generate_content(
    model=PRO_MODEL_ID,
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=['Text', 'Image'],
        image_config=types.ImageConfig(
            aspect_ratio=aspect_ratio,
        ),
        thinking_config=types.ThinkingConfig(
            include_thoughts=True # Enable thoughts
        )
    )
)

# Display the image and thoughts
for part in response.parts:
  if part.thought:
    print(f"Thought: {part.text}")
  elif image:= part.as_image():
    image.save("viral.png")

Viral image

This transparency helps you understand how the model interpreted your request. It's like having a conversation with your artist!

6) Search Grounding (Real-time magic)

One of the most game-changing features is Search Grounding. Nano Banana Pro isn't stuck in the past; it can access real-time data from Google Search to generate accurate, up-to-date images. Want the weather? You got it.

For example, you can ask it to visualize the current weather forecast:

prompt = "Visualize the current weather forecast for the next 5 days in Tokyo as a clean, modern weather chart. add a visual on what i should wear each day"

response = client.models.generate_content(
    model=PRO_MODEL_ID,
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=['Text', 'Image'],
        image_config=types.ImageConfig(
            aspect_ratio="16:9",
        ),
        tools=[{"google_search": {}}] # Enable Google Search
    )
)

# Save the image
for part in response.parts:
    if image:= part.as_image():
        image.save("weather.png")

# Display sources (you must always do that)
print(response.candidates[0].grounding_metadata.search_entry_point.rendered_content)

Weather in Tokyo

7) Go Big or Go Home: 4K Generation

Need print-quality images? Nano Banana Pro supports 4K resolution. Because sometimes, bigger is better.

prompt = "A photo of an oak tree experiencing every season"
resolution = "4K" # Options: "1K", "2K", "4K", be careful lower case do not work.

response = client.models.generate_content(
    model=PRO_MODEL_ID,
    contents=prompt,
    config=types.GenerateContentConfig(
        response_modalities=['Text', 'Image'],
        image_config=types.ImageConfig(
            aspect_ratio="1:1",
            image_size=resolution
        )
    )
)

Oak tree experiencing all seasons

Note: 4K generation comes at a higher cost, so use it wisely!

8) Polyglot Banana (Multilingual Capabilities)

The model can generate and even translate text within images across over a dozen languages. It's basically a universal translator for your eyes.

# Generate an infographic in Spanish
message = "Make an infographic explaining Einstein's theory of General Relativity suitable for a 6th grader in Spanish"

response = chat.send_message(message,
    config=types.GenerateContentConfig(
        image_config=types.ImageConfig(aspect_ratio="16:9")
    )
)

# Save the image
for part in response.parts:
    if image:= part.as_image():
        image.save("relativity.png")

General Relativity in Spanish

# Translate it to Japanese
message = "Translate this infographic in Japanese, keeping everything else the same"
response = chat.send_message(message)

# Save the image
for part in response.parts:
    if image:= part.as_image():
        image.save("relativity_JP.png")

General Relativity in Japanese

9) Mix it up! (Advanced Image Mixing)

While the Flash model can mix up to 3 images, the Pro model can handle up to 14 images! That's a whole party in one prompt. Perfect for creating complex collages or showing off your entire product line.

# Mix multiple images
response = client.models.generate_content(
    model=PRO_MODEL_ID,
    contents=[
        "An office group photo of these people, they are making funny faces.",
        PIL.Image.open('John.png'),
        PIL.Image.open('Jane.png'),
        # ... add up to 14 images
    ],
)

# Save the image
for part in response.parts:
    if image:= part.as_image():
        image.save("group_picture.png")

Note: If you want very high fidelity for your characters, limit yourself to 5, which is already more than enough for a party night!

10) Show off time! (Pro-Exclusive Demos)

Here are some examples of what's possible only with Nano Banana Pro. Prepare to be amazed:

Personalized Pixel Art (Search Grounding)

Prompt: "Search the web then generate an image of isometric perspective, detailed pixel art that shows the career of Guillaume Vernade"

This uses search grounding to find specific information about a person and visualizes it in a specific style.

Guillaume Vernade career

Complex Text Integration

Prompt: "Show me an infographic about how sonnets work, using a sonnet about bananas written in it, along with a lengthy literary analysis of the poem. Good vintage aesthetics"

The model can generate coherent, lengthy text and integrate it perfectly into a complex layout.

High-Fidelity Mockups

Prompt: "A photo of a program for the Broadway show about TCG players on a nice theater seat, it's professional and well made, glossy, we can see the cover and a page showing a photo of the stage."

Create photorealistic mockups of print materials with accurate lighting and texture.

11) Best Practices and prompting tips for Nano Banana and Nano Banana Pro

To achieve the best results with the Nano Banana models, follow these prompting guidelines:

Be Hyper-Specific: The more detail you provide about subjects, colors, lighting, and composition, the more control you have over the output.
Provide Context and Intent: Explain the purpose or desired mood of the image. The model's understanding of context will influence its creative choices.
Iterate and Refine: Don't expect perfection on the first try. Use the model's conversational ability to make incremental changes and refine your image.
Use Step-by-Step Instructions: For complex scenes, break your prompt into a series of clear, sequential instructions.
Use Positive Framing: Instead of negative prompts like "no cars," describe the desired scene positively: "an empty, deserted street with no signs of traffic."
Control the Camera: Use photographic and cinematic terms to direct the composition, such as "wide-angle shot", "macro shot", or "low-angle perspective".
Use search grounding to your advantage: When you know that you want the model to use real-time or real-world data, be very precise about it. "Search the web about the last Olympic Lyonnais's game and make an infographics" will work better than just "an infographics of the OL last games" (which should still work, but don't take chances).

For a deeper dive into best practices, check the prompting guide in the documentation and the prompting best practices for Nano Banana publish on the official blog.

Wrap up

Nano Banana Pro (Gemini 3 Pro Image) opens up a new frontier for AI image generation. With its ability to think, search, and render in 4K, it's a tool for serious creators (and serious fun).

Ready to try it out? Head over to Google AI Studio, try or customize our Apps or check out the cookbook.