I Built a Web Application on AWS With Zero Inbound Ports — Then Tried to Break Its Security
Building CloudSecure with AWS, Cloudflare Tunnel, Terraform, Python, and evidence-driven security validation. By George Koufie | AWS Community Builder — Networking & Content Delivery The Challenge: Can a Website Be Pub
Building CloudSecure with AWS, Cloudflare Tunnel, Terraform, Python, and evidence-driven security validation.
By George Koufie | AWS Community Builder — Networking & Content Delivery
The Challenge: Can a Website Be Publicly Accessible Without Exposing Its Server?
Every organization faces a difficult security challenge: applications must be accessible to users, but the infrastructure hosting those applications must be protected from unauthorized access.
A traditional web application architecture often exposes a load balancer or web server through ports 80 and 443. Security groups, firewalls, network segmentation, and web application firewalls help manage that exposure.
But what if we could remove inbound access to the application server entirely?
That question became the foundation of my latest project: CloudSecure, a hands-on cloud security lab built using Amazon Web Services (AWS), Cloudflare, Terraform, and Python.
I wanted to do more than build infrastructure. I wanted to demonstrate a complete security engineering workflow:
Deploy an application with a reduced attack surface.
Assess its security configuration across two platforms.
Deliberately introduce a controlled security misconfiguration.
Detect the problem using automated security checks.
Remediate the configuration using Infrastructure as Code.
Collect evidence showing whether the remediation actually worked.
On November 26, 2026, I deployed the lab in AWS, performed the assessments and security tests, and then dismantled the environment to avoid unnecessary charges.
The results demonstrated why security needs to be tested rather than assumed.
- The Business Problem
For organizations operating internet-facing applications, cloud security is not simply about deploying firewalls.
Security teams must understand whether:
Servers are unnecessarily exposed to inbound connections.
Network security rules enforce the intended access restrictions.
Encryption configurations meet organizational requirements.
Administrative access is properly controlled and auditable.
Cloud security misconfigurations can be detected reliably.
Security remediation can be validated with evidence.
These concerns become especially important in regulated environments, including organizations that process payment information.
Misconfigurations can also arise gradually. A security group might be modified during troubleshooting, a TLS configuration might accept outdated protocols, or a web application firewall rule might fail to recognize alternate request formats.
A control that exists is not necessarily a control that works.
The goal of CloudSecure was to build a small, reproducible environment where these security questions could be answered through actual testing.
- Solution Architecture
CloudSecure combines AWS infrastructure security with Cloudflare edge protection.
The AWS environment includes:
Amazon VPC with separate application and isolated subnets.
Amazon EC2 running a Python demonstration application.
Security groups with no inbound access rules.
AWS IAM roles for workload permissions.
AWS Systems Manager for administrative access.
Amazon CloudWatch for session and network logging.
Encrypted Amazon EBS storage.
Terraform for infrastructure provisioning and changes.
Cloudflare provides:
DNS and proxy services.
Cloudflare Tunnel for application connectivity.
TLS configuration and HTTPS enforcement.
Custom web application firewall rules.
Security analytics.
A Python assessment tool uses the AWS and Cloudflare APIs to examine selected security configurations and generate JSON evidence.
[Insert architecture diagram from the GitHub README here.]
How requests reach the application
A visitor connects to Cloudflare over HTTPS.
Cloudflare evaluates the request against its configured edge security controls and forwards permitted traffic through Cloudflare Tunnel.
The tunnel connector, cloudflared, runs on the AWS instance and initiates outbound connectivity to Cloudflare.
The application itself listens only on:
127.0.0.1:8080
This means the application does not bind to the EC2 instance's external network interface.
The EC2 security group also has no inbound rules.
Consequently, visitors can use the application through the Cloudflare hostname, but cannot directly connect to the application's origin ports.
This design reduces exposure without requiring inbound SSH access or an internet-facing application load balancer.
- Engineering Decision: Cloudflare Tunnel Instead of an Internet-Facing Origin
One of the most important design decisions was to use Cloudflare Tunnel.
Traditional approaches often require an internet-facing entry point, with inbound traffic allowed on selected ports.
CloudSecure takes a different approach: the server establishes the connection outward, while Cloudflare handles incoming user requests.
The Terraform security group contains outbound rules for required services and tunnel connectivity, but no inbound rules.
resource "aws_security_group" "origin" {
name_prefix = "cloudsecure-origin-"
description = "No inbound access; outbound TLS and tunnel"
vpc_id = aws_vpc.lab.id
egress {
description = "HTTPS to SSM, package repos and Cloudflare API"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
The complete configuration also permits the required Cloudflare Tunnel connections on port 7844.
During testing, the public application responded successfully through Cloudflare using TLS 1.3.
However, an attempt to connect directly to the instance's public IP on the application's port received no response.
VPC Flow Logs also recorded more than 2,000 rejected connection attempts against the instance.
This demonstrated two things:
First, the application remained reachable through the intended route.
Second, the security group prevented inbound connections from reaching the origin directly.
There is an important architectural distinction here: the EC2 instance still used a public IPv4 address for outbound connectivity to avoid NAT Gateway expenses. The server was not deployed in a fully private subnet.
For a production deployment, I would favor private subnets, controlled egress, and multiple tunnel connectors to improve defense in depth and availability.
[Insert screenshot: EC2 security group with no inbound rules.]
[Insert screenshot: Cloudflare Tunnel healthy connector.]
- Eliminating SSH: AWS Systems Manager for Administration
A second design decision was to avoid traditional SSH access.
Instead of opening port 22 or distributing private SSH keys, I configured AWS Systems Manager Session Manager.
This allowed administration through authenticated AWS access without enabling inbound SSH connections.
The EC2 instance uses an IAM role with the AWS-managed AmazonSSMManagedInstanceCore policy.
CloudSecure also configures CloudWatch logging for administrative sessions.
This approach provides several operational advantages:
No inbound SSH port is required.
Access is controlled through IAM permissions.
Administrative access can be revoked centrally.
Session activity can be logged.
Engineers can perform maintenance and troubleshooting without managing SSH keys.
For organizations managing multiple cloud servers, centralized access management and operational evidence are important parts of reducing administrative risk.
- Building a Security Assessment Engine With Python
Protecting infrastructure is only one part of cloud security.
I also wanted a way to verify that the expected controls remained in place.
For this, I developed a Python security assessment engine using boto3 and the Cloudflare API.
The scanner examines selected AWS and Cloudflare configurations, including:
Platform
Security checks
AWS
IMDSv2 enforcement
AWS
Security group exposure
AWS
EBS encryption
Cloudflare
Proxied tunnel DNS configuration
Cloudflare
Minimum TLS version
Cloudflare
HTTPS enforcement
The assessment results are written to a timestamped JSON report.
One of the most important design principles was how the scanner handles uncertainty.
The tool supports four statuses:
PASS: The selected control meets the expected configuration.
FAIL: The configuration does not meet the expected requirement.
ERROR: The scanner could not complete the check.
NOT_TESTED: The control was not evaluated.
Why is that distinction important?
Because a security tool should never report a passing result simply because it was unable to access the information needed to perform an assessment.
A real permission failure
During the initial combined assessment, the Cloudflare API token lacked the permission needed to read zone security settings.
The Cloudflare API returned 403 Forbidden.
Instead of crashing or treating the unavailable information as secure, the scanner recorded an ERROR and continued evaluating the remaining checks.
After correcting the token's permissions, the affected checks completed successfully.
This reinforced a principle that matters in security engineering:
An assessment result is only useful when it accurately reflects what was actually verified.
- Testing the Web Application Firewall — and Finding Bypasses
Another valuable part of the project involved Cloudflare's web application firewall.
I configured a custom rule to block requests targeting a test path:
/blocked-test
Initially, the rule matched the exact path.
A normal request to /blocked-test returned HTTP 403, indicating that Cloudflare blocked it.
However, I did not stop with the first successful test.
I submitted variations of the same request to determine whether the rule could be bypassed.
Two variations passed through the first rule:
/blocked-test/
/BLOCKED-TEST
The application returned 404 for these requests, showing that they had reached the application rather than being blocked at the edge.
This was not the intended security behavior.
I revised the firewall expression to normalize the case and match the beginning of the request path:
(starts_with(lower(http.request.uri.path), "/blocked-test"))
After the change, the documented retest blocked all seven bypass attempts.
Legitimate requests to /health and /api/transactions continued to return HTTP 200.
This was an important lesson.
A firewall rule can appear effective while still containing gaps that only become visible through negative testing.
[Insert screenshots: initial WAF rule, blocked request, and Cloudflare security events.]
- The Most Important Experiment: Breaking and Restoring IMDSv2
The strongest security validation in CloudSecure was a controlled break-and-fix exercise involving the EC2 Instance Metadata Service.
AWS uses the Instance Metadata Service to make instance information and temporary IAM role credentials available to workloads.
IMDSv2 improves protection by requiring callers to obtain a session token before accessing metadata.
For the lab, I deployed the EC2 instance with IMDSv2 required.
The baseline assessment showed:
7 PASS · 0 FAIL · 0 ERROR
Then I deliberately changed the Terraform configuration:
http_tokens = "optional"
This allowed requests using the older, tokenless metadata interface.
Before applying the change, I reviewed the Terraform plan.
It showed one in-place change, with no new resources and no destroyed resources.
After applying the change, I ran the security scanner again.
The IMDSv2 check correctly changed from PASS to FAIL.
But I wanted more than a failed configuration check.
Demonstrating the actual security impact
Using AWS Systems Manager, I tested whether the instance metadata endpoint would respond without an IMDSv2 token.
During the misconfigured state:
Tokenless IMDS request: HTTP 200
Tokenless credentials-path request: HTTP 200
The results demonstrated that the token requirement was no longer protecting the metadata endpoint.
I then restored the secure Terraform configuration:
http_tokens = "required"
After applying the remediation, I repeated the tests.
Tokenless IMDS request: HTTP 401
Tokenless credentials-path request: HTTP 401
Valid IMDSv2 request: HTTP 200
This provided evidence that:
The scanner detected the deliberately introduced misconfiguration.
The insecure metadata access path became accessible during the drill.
Terraform successfully restored the intended configuration.
Tokenless requests were rejected after remediation.
Legitimate IMDSv2 requests continued working.
Finally, Terraform reported no outstanding changes, confirming that the deployed configuration matched the code.
For me, this was the most valuable part of the project.
It demonstrated not just security configuration, but detection, controlled change management, remediation, and post-change validation.
[Insert screenshot: IMDSv2 required in AWS.]
[Insert screenshot or formatted excerpt: before-and-after evidence from the drill.]
- Security Assessment Results
CloudSecure's documented deployment produced the following results:
Validation
Result
Combined AWS and Cloudflare assessment
7 of 7 checks passed
Direct access to application origin
Blocked
Cloudflare application availability
HTTP 200
TLS 1.0 and 1.1 after remediation
Rejected
TLS 1.2 and 1.3
Accepted
Revised WAF bypass tests
7 of 7 blocked
IMDSv2 misconfiguration
Detected
IMDSv2 remediation
Validated
Rejected inbound network attempts
More than 2,000 recorded
Automated scanner unit tests
14
These results describe the tested controls in this particular lab. They do not establish that the environment is fully secure or compliant with every applicable standard.
Nevertheless, they provide a useful example of how security teams can move from configuration assumptions to verifiable evidence.
- Mapping Security Evidence to PCI DSS
I also created a starter control mapping aligned with selected requirements of PCI DSS v4.0.
The mapping connects implemented and tested controls to security objectives such as network protection, secure system configuration, encryption, access management, logging, and security testing.
Examples include:
PCI DSS requirement area
CloudSecure evidence
Requirement 1 — Network security controls
Security group restrictions and tunnel configuration
Requirement 2 — Secure configurations
IMDSv2 enforcement
Requirement 3 — Protection of stored account data
Encrypted EBS storage demonstration
Requirement 4 — Encryption in transit
TLS configuration checks
Requirement 6 — Secure systems and software
WAF test evidence
Requirement 10 — Logging and monitoring
VPC Flow Logs and SSM session logs
It is important to distinguish control mapping from compliance validation.
CloudSecure does not process real payment card data, does not represent a complete cardholder data environment, and does not constitute a PCI DSS certification or formal assessment.
In addition, some lab configurations are intentionally below production-oriented requirements. For example, the lab's default CloudWatch log retention is 90 days rather than the 12 months generally expected for relevant PCI DSS audit logs.
These limitations are recorded in the repository rather than hidden.
In a professional security assessment, documenting gaps and accepted risks is just as important as documenting successful controls.
- Infrastructure as Code, Automated Testing, and CI/CD
Terraform provisions the AWS environment, including networking, IAM, EC2, security groups, storage encryption, and logging resources.
The project also includes a GitHub Actions pipeline that performs:
Terraform formatting and validation.
Checkov infrastructure security scanning.
Python scanner unit tests.
ShellCheck validation of the instance bootstrap script.
The pipeline uses commit-pinned GitHub Actions and hash-locked Python dependencies to improve build reproducibility and reduce software supply-chain risk.
Another important design choice was to document deliberate Checkov exceptions directly in the Terraform code.
For example, the lab permits a public IPv4 address for outbound connectivity because the instance's inbound security group rules remain closed.
Rather than silently suppressing the finding, the code records the reason for the exception.
This is the difference between ignoring a security finding and documenting an intentional architectural trade-off.
- What I Learned
Several lessons stood out from this project.
Secure defaults still need validation
The new Cloudflare zone initially accepted outdated TLS versions until I explicitly configured the minimum version.
Test the behavior, not only the configuration
The first WAF rule appeared successful until request variations revealed bypasses.
Failures need distinct reporting
An API permission error should never be treated as a successful compliance check.
Infrastructure changes should be predictable
Reviewing Terraform plans before the IMDSv2 drill helped confirm that the change would affect the intended configuration without rebuilding the instance.
Cost optimization introduces trade-offs
Using a public IPv4 address for outbound connectivity avoided NAT Gateway charges but reduced defense in depth compared with a private-subnet architecture.
Security evidence strengthens engineering decisions
The most useful results were not screenshots showing that a feature was enabled. They were tests demonstrating how the system behaved before and after a security change.
- What I Would Improve Next
CloudSecure is a working laboratory implementation, not a finished enterprise security platform.
My next improvements would focus on expanding the architecture and assessment coverage.
These include:
Deploying the application in private subnets.
Adding multiple Cloudflare Tunnel connectors for high availability.
Introducing customer-managed KMS keys.
Expanding log retention and security monitoring.
Integrating AWS Config and Security Hub findings.
Adding CloudTrail assessment checks.
Automating additional WAF validation tests.
Implementing Cloudflare Access for identity-based application protection.
Expanding the PCI DSS control mapping with more comprehensive evidence.
These additions would help move the project closer to the architecture and operational capabilities expected in larger enterprise environments.
Final Thoughts: Security Is Stronger When It Can Be Demonstrated
CloudSecure reinforced something I consider fundamental to cloud engineering:
Deploying a security control is not the same as proving that it works.
An effective security engineering process should include architecture decisions, infrastructure automation, configuration assessment, negative testing, controlled remediation, and documented validation.
Through this project, I gained practical experience integrating AWS infrastructure with Cloudflare edge security and developing a Python assessment engine that produces evidence rather than assumptions.
More importantly, I learned how to investigate whether a security control is effective, reproduce an issue, make a controlled change, and verify the outcome.
That is the engineering discipline I intend to keep developing as I continue working in cloud infrastructure, security, automation, and platform reliability.
Explore the Project
GitHub: https://github.com/gkoufie1/cloudsecure
The repository includes Terraform configurations, Python assessment code, automated tests, screenshots, sanitized security findings, and the complete IMDSv2 remediation evidence.
If you are learning AWS networking, cloud security, Terraform, or DevSecOps, I hope this project provides a useful foundation for building and testing your own environment.
George Koufie is an AWS Community Builder in the Networking & Content Delivery category, with a focus on AWS networking, cloud infrastructure, automation, and operational reliability.
Medium tags: AWS, Cloud Security, Cloudflare, Terraform, DevSecOps
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.



