Definition:
What is AWS CodeDeploy?
AWS CodeDeploy is a continuous deployment service. This means CodeDeploy...
Automates deployments: Eliminates error-prone manual steps - no more manually copying files and running commands to deploy the application.
Enables consistent rollouts: The application deploys the same way every time.
Minimizes downtime: Can deploy in ways that keep the application available.
Handles failures: Can automatically roll back if something goes wrong.
Go to CloudFormation console in AWS
What is AWS CloudFormation?
Think of CloudFormation is AWS' infrastructure as code tool. Instead of clicking around the AWS console to set up resources (which gets tedious fast!), a single template file is written that describes everything needed - like EC2 instances, security groups, databases, and more. Then, CloudFormation reads this file and builds the entire environment that is needed, exactly the same way every time.
Click on Create Stack → With new resources (from drop down)
What is a CloudFormation stack?
When a CloudFormation template is deployed , a stack is created - think of it as a project folder that holds all the connected resources. The cool thing is that CloudFormation treats this stack as a single unit, so we can create, update, or delete all those resources together with one command.
Select Choose an existing template → Under Specify Template select “Upload a template” and upload the YAML file (gamerhub-cicd-cloudformation-template) as cloud formation template → Click next
The template’s structure:
Resource Type | Status | Comments |
---|---|---|
VPC, Subnet, IGW, Route Table | ✅ | Creates public networking for EC2 |
Security Group | ✅ | Allows HTTP access from your given IP |
EC2 Instance | ✅ | Tagged, has instance profile and public IP |
IAM Role for EC2 | ✅ | SSM & S3 read — suitable for connecting + pulling artifact |
Output (URL) | ✅ | Prints public URL to check Flask app later |
Why are we deploying networking resources too?
By defining these networking resources in the template, we're not just launching an EC2 instance, but creating a complete, secure, and configurable infrastructure that can be easily replicated or modified. This is an especially good idea for EC2 instances that are hosting web apps, because they have more complex needs like connecting with multiple databases and controlling both public and private network traffic.
Give a stack name like : GamerHubCodeDeployEC2Stack
Paste the IP adress in MyIP field . Check IP using : https://checkip.amazonaws.com/
Paste the IP like : 123.45.67.89/32 Note: /32 is important → Click Next
Why do we add /32 to the IP address?
Adding /32 to your IP address is like telling AWS "I only want this exact address to have access, not any others." The /32 is CIDR notation that specifies exactly how many IP addresses are included in your rule.
With /32, it's just one - your specific IP. If we used /24 instead, we'd be allowing 256 different IP addresses! For security, we're being as specific as possible to minimize who can access our EC2 instance.
Under Stack Failure options choose Roll back all stack resources and Delete all newly created resources .
What are Stack failure options?
Stack failure options are your safety net when things don't go as planned. They determine what CloudFormation should do if it runs into an error while creating your resources:
- Roll back all stack resources: This is like having an "undo" button for your entire deployment. If anything fails, CloudFormation will automatically revert everything back to how it was before you started. This prevents you from ending up with a half-built environment that might not work or cost you money unnecessarily.
- Delete all newly created resources: This makes sure CloudFormation cleans up after itself during a rollback. No resources left behind to surprise you on your bill next month!
Scroll down and check this option under Capabilities:
Why would CloudFormation create an IAM role?
IAM roles are like special visitor passes that AWS services can "wear" to temporarily access other services. In our case, our deployment EC2 instance will use a role to access files from S3.
Why do you think it'll need access to S3 (have you created an S3 bucket anywhere)? Because the deployment environment will need to use the build artifacts stored in your S3 bucket
Click Next → Review details and Click Submit
Go to resources tab to see the list of resources that are being created
Go to Events tab:
What is a CloudFormation stack event?
Every time CloudFormation creates, updates, or deletes something, it records an event - "Starting to create EC2 instance," "Security group created successfully," "There isn't enough capacity to create a new VPC." These events give you visibility into exactly what's happening behind the scenes, which is super helpful for troubleshooting if something goes wrong.
Wait for the stack's status to become CREATE_COMPLETE.
If it shows : ROLLBACK_IN_PROGRESS or ROLLBACK_COMPLETE it means CloudFormation encountered an error while creating your resources. This is a common issue.
To fix this:
- Check the detailed error messages in the Events tab
- Make any needed corrections to template parameters or IAM permissions
- Try creating the stack again
NOTE: REFER GITHUB FOR THE SCRIPTS